Commit 15255b8d authored by Ciliste's avatar Ciliste
Browse files

Début création de game contre IA

parent 67c0547e
Loading
Loading
Loading
Loading
+4945 −0

File added.

Preview size limit exceeded, changes collapsed.

+2 −0
Changes for jeuAssemblage/src/main/App.java: 2 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -9,6 +9,8 @@ import view.MainFrame;

public class App {

	public static final String RESSOURCES_PATH = "/assets/";

    private static JFrame frame = null;

    public static void main(String[] args) throws Exception {
+32 −0
Changes for jeuAssemblage/src/model/ProfileUtils.java: 32 added lines, 0 removed lines.
Original line number Diff line number Diff line
package model;

import java.io.InputStream;

import main.App;

public final class ProfileUtils {
	
	private static final String DEFAULT_PROFILE_NAME_FILE = "default_names.txt";

	private ProfileUtils() {
		
	}

	public static String generateRandomProfileName() {

		try (InputStream fos = ProfileUtils.class.getResourceAsStream(App.RESSOURCES_PATH + DEFAULT_PROFILE_NAME_FILE)) {

			byte[] buffer = new byte[fos.available()];

			fos.read(buffer);

			String[] names = new String(buffer).split("\n");

			return names[(int) (Math.random() * names.length)];
		} 
		catch (Exception e) {

			return DEFAULT_PROFILE_NAME_FILE + " not found";
		}
	}
}
+2 −1
Changes for jeuAssemblage/src/view/MainFrame.java: 2 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -9,6 +9,7 @@ import javax.swing.JFrame;

import com.formdev.flatlaf.FlatDarkLaf;

import main.App;
import view.screen.MainScreen;

public class MainFrame extends JFrame {
@@ -30,7 +31,7 @@ public class MainFrame extends JFrame {

		try {

			InputStream stream = MainFrame.class.getResourceAsStream("/assets/icon.png");
			InputStream stream = MainFrame.class.getResourceAsStream(App.RESSOURCES_PATH + "icon.png");

			Image image = ImageIO.read(stream).getScaledInstance(25, 25, Image.SCALE_SMOOTH);

+113 −0
Changes for jeuAssemblage/src/view/screen/AIGameCreation.java: 113 added lines, 0 removed lines.
Original line number Diff line number Diff line
package view.screen;

import static view.utils.SwingUtils.getHeightTimesPourcent;
import static view.utils.SwingUtils.getWidthTimesPourcent;

import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JTable;

import model.ProfileUtils;
import view.MainFrame;
import view.utils.SwingUtils;

public class AIGameCreation extends SoloGameCreation {
	
	private final JList<BotDescriptor> bots = new JList<>();

	private final JButton btnAddBot = new JButton("Ajouter un robot");

	private final JScrollPane scrollPane = new JScrollPane(bots);

	public AIGameCreation(MainFrame mainFrame) {
		
		super(mainFrame);

		add(scrollPane);
		add(btnAddBot);

		btnAddBot.addActionListener(e -> {

			final String botName = ProfileUtils.generateRandomProfileName();

			BotDescriptor bot = new BotDescriptor(botName, 1);

			BotDescriptor[] botDescriptors = new BotDescriptor[bots.getModel().getSize() + 1];
			for (int i = 0; i < bots.getModel().getSize(); i++) {

				botDescriptors[i] = bots.getModel().getElementAt(i);
			}

			botDescriptors[bots.getModel().getSize()] = bot;

			bots.setListData(botDescriptors);
		});
	}

	@Override
	public void doLayout() {
		
		super.doLayout();

		final int PADDING_LEFT = getWidthTimesPourcent(this, .05f);
		final int PADDING_TOP = getHeightTimesPourcent(this, .05f);
		
		final int SCROLLPANE_WIDTH = getWidthTimesPourcent(this, .15f);
		final int SCROLLPANE_HEIGHT = getHeightTimesPourcent(this, .15f);

		scrollPane.setBounds(
			getWidth() - SCROLLPANE_WIDTH - PADDING_LEFT,
			PADDING_TOP,
			SCROLLPANE_WIDTH,
			SCROLLPANE_HEIGHT
		);

		btnAddBot.setBounds(
			scrollPane.getX(),
			scrollPane.getY() + scrollPane.getHeight() + SwingUtils.getHeightTimesPourcent(this, .01f),
			scrollPane.getWidth(),
			SwingUtils.getHeightTimesPourcent(this, .05f)
		);
	}

	private static class BotDescriptor {

		private String name;
		private int difficulty;

		public BotDescriptor(String name, int difficulty) {

			this.name = name;
			this.difficulty = difficulty;
		}

		public String getName() {

			return name;
		}

		public int getDifficulty() {

			return difficulty;
		}

		public void setName(String name) {

			this.name = name;
		}

		public void setDifficulty(int difficulty) {

			this.difficulty = difficulty;
		}

		@Override
		public String toString() {

			return name + " (" + difficulty + ")";
		}
	}
}
Loading