From a0b3290987ac7ca2518ecb076c4955919889fc84 Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Fri, 20 Oct 2017 22:58:07 +0200 Subject: [PATCH 1/2] Finished implementation of Human, improving IO --- Makefile | 5 ++++- src/game/Main.java | 32 +++++++++++++++++++++++++++++--- src/player/Human.java | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 4 deletions(-) create mode 100644 src/player/Human.java diff --git a/Makefile b/Makefile index 251c2f2..8015138 100644 --- a/Makefile +++ b/Makefile @@ -17,6 +17,7 @@ src/game/GameHistory.java \ src/game/GameEntry.java \ src/game/PlayerObject.java \ src/player/Player.java \ +src/player/Human.java \ src/player/malte/MalteAI.java \ src/player/malte/Pattern.java \ src/player/malte/Item.java \ @@ -30,13 +31,15 @@ BUILDFILES=$(OBJECTS) $(FXMLS_BUILD) MAIN=game.Main N=100 +P1= +P2= all: $(CLASSES) $(FXMLS_BUILD) $(JAVAC) $(JAVAC_OPTIONS) -cp $(CLASSPATH) $(CLASSES) -d $(BUILDS) run: $(CLASSES) - $(JAVA) -cp $(BUILDS) $(MAIN) + $(JAVA) -cp $(BUILDS) $(MAIN) $(P1) $(P2) simulate: $(CLASSES) $(JAVA) -cp $(BUILDS) $(MAIN) $(N) diff --git a/src/game/Main.java b/src/game/Main.java index 69c5bb6..fa21344 100644 --- a/src/game/Main.java +++ b/src/game/Main.java @@ -1,17 +1,43 @@ package game; import player.Player; +import player.Human; import player.maurizio.MaurizioAI; import player.malte.MalteAI; public class Main { public static void main(String[] args) { - if (args.length == 1) { + Player p1, p2; + if (args.length == 2) { + switch (args[0].toLowerCase()) { + case "maurizio": + p1 = new MaurizioAI("Maurizio"); + break; + case "human": + p1 = new Human(); + break; + case "malte": + default: + p1 = new MalteAI("Malte"); + } + switch (args[1].toLowerCase()) { + case "malte": + p2 = new MalteAI("Malte"); + break; + case "human": + p2 = new Human(); + break; + case "maurizio": + default: + p2 = new MaurizioAI("Maurizio"); + } + new Game(p1, p2).start(true, true); //1. param = output mode, 2. param = random beginner + } else if (args.length == 1) { Game.simulate(Integer.parseInt(args[0])); } else { - Player p1 = new MalteAI("Malte"); - Player p2 = new MaurizioAI("Maurizio"); + p1 = new MalteAI("Malte"); + p2 = new MaurizioAI("Maurizio"); new Game(p1, p2).start(true, true); //1. param = output mode, 2. param = random beginner } } diff --git a/src/player/Human.java b/src/player/Human.java new file mode 100644 index 0000000..9263c36 --- /dev/null +++ b/src/player/Human.java @@ -0,0 +1,38 @@ +package player; + +import java.util.Scanner; + +public class Human implements Player { + + private Scanner sc; + private int id; + private int enemyID; + private String name; + + public Human() { + sc = new Scanner(System.in); + System.out.print("Bitte gib deinen Namen ein: "); + this.name = sc.nextLine(); + } + + @Override + public int move(int[][] board) { + System.out.print("Choose a move: "); + return sc.nextInt(); + } + + @Override + public void setPlayerID(int id) { + this.id = id; + } + + @Override + public void setEnemyID(int enemyID) { + this.enemyID = enemyID; + } + + @Override + public String getName() { + return this.name; + } +} From b8512a169325780aeb5c29688c37b0616dad083f Mon Sep 17 00:00:00 2001 From: Malte Tammena Date: Fri, 20 Oct 2017 23:05:15 +0200 Subject: [PATCH 2/2] IO good enough --- src/player/Human.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/player/Human.java b/src/player/Human.java index 9263c36..84d792e 100644 --- a/src/player/Human.java +++ b/src/player/Human.java @@ -1,6 +1,9 @@ package player; import java.util.Scanner; +import java.util.HashSet; +import java.util.Set; +import java.util.Arrays; public class Human implements Player { @@ -17,8 +20,18 @@ public class Human implements Player { @Override public int move(int[][] board) { - System.out.print("Choose a move: "); - return sc.nextInt(); + Set options = new HashSet<>(Arrays.asList(0,1,2,3,4,5,6)); + for (Integer i: new HashSet(options)) { + if (board[i][0] != 0) { + options.remove(i); + } + } + int choice = -1; + while (!options.contains(new Integer(choice))) { + System.out.print("Choose a move from " + options + ": "); + choice = sc.nextInt(); + } + return choice; } @Override