Expanded game functionality and refactoring
parent
6afafbd635
commit
132e142e72
|
|
@ -4,8 +4,8 @@ import java.util.Random;
|
||||||
|
|
||||||
public class ComputerPlayer extends Player {
|
public class ComputerPlayer extends Player {
|
||||||
|
|
||||||
public ComputerPlayer(String playerSign) {
|
public ComputerPlayer(String symbol) {
|
||||||
super(playerSign);
|
super(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -16,7 +16,7 @@ public class ComputerPlayer extends Player {
|
||||||
randomNumber = generateNumber();
|
randomNumber = generateNumber();
|
||||||
} while (!board.isFieldChoosable(randomNumber));
|
} while (!board.isFieldChoosable(randomNumber));
|
||||||
|
|
||||||
board.setBoardMove(randomNumber, playerSign);
|
board.setBoardMove(randomNumber, symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
private int generateNumber() {
|
private int generateNumber() {
|
||||||
|
|
|
||||||
|
|
@ -3,29 +3,53 @@ package Uebung_04_Andres;
|
||||||
public class Game {
|
public class Game {
|
||||||
|
|
||||||
private static Board board = new Board(); //just to avoid adding more parameters to game-won logic functions
|
private static Board board = new Board(); //just to avoid adding more parameters to game-won logic functions
|
||||||
private static ComputerPlayer computer;
|
private static Player player1;
|
||||||
private static HumanPlayer human;
|
private static Player player2;
|
||||||
|
|
||||||
public void startGame(boolean isHumanFirstToPlay) {
|
public void gameSetupAndStart(boolean isHumanFirstToPlay, boolean areTwoHumansPlaying) {
|
||||||
computer = new ComputerPlayer(isHumanFirstToPlay ? "O" : "X");
|
this.playerSetup(isHumanFirstToPlay, areTwoHumansPlaying);
|
||||||
human = new HumanPlayer(isHumanFirstToPlay ? "X" : "O");
|
this.play();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playerSetup(boolean isHumanFirstToPlay, boolean areTwoHumansPlaying) {
|
||||||
|
//Human vs human
|
||||||
|
if (areTwoHumansPlaying) {
|
||||||
|
player1 = new HumanPlayer("X");
|
||||||
|
player2 = new HumanPlayer("O");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
while (!board.isFull()) {
|
//Robot vs human
|
||||||
this.playerMove(isHumanFirstToPlay);
|
if (isHumanFirstToPlay) {
|
||||||
board.printBoard();
|
player1 = new HumanPlayer("X");
|
||||||
if (gameWasWon()) {
|
player2 = new ComputerPlayer("O");
|
||||||
System.out.println(isHumanFirstToPlay ? "\nDu hast gewonnen!" : "Der Roboter hat gewonnen!");
|
} else {
|
||||||
break;
|
player1 = new ComputerPlayer("X");
|
||||||
}
|
player2 = new HumanPlayer("O");
|
||||||
isHumanFirstToPlay = !isHumanFirstToPlay; //Invert to have the other player play
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void playerMove(boolean isHumanFirstToPlay) {
|
private void play() {
|
||||||
if (isHumanFirstToPlay) {
|
//If a human does the first move, print the board first b/c otherwise it's missing
|
||||||
human.move(board);
|
if (player1 instanceof HumanPlayer) board.printBoard();
|
||||||
|
|
||||||
|
boolean isTurnOfPlayer1 = true;
|
||||||
|
while (!board.isFull()) {
|
||||||
|
this.playerMove(isTurnOfPlayer1);
|
||||||
|
board.printBoard();
|
||||||
|
if (gameWasWon()) {
|
||||||
|
System.out.println(isTurnOfPlayer1 ? "X hat gewonnen!" : "O hat gewonnen!");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
isTurnOfPlayer1 = !isTurnOfPlayer1; //Invert to switch the player that makes the next move
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void playerMove(boolean isTurnOfPlayer1) {
|
||||||
|
if (isTurnOfPlayer1) {
|
||||||
|
player1.move(board);
|
||||||
} else {
|
} else {
|
||||||
computer.move(board);
|
player2.move(board);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -2,8 +2,8 @@ package Uebung_04_Andres;
|
||||||
|
|
||||||
public class HumanPlayer extends Player {
|
public class HumanPlayer extends Player {
|
||||||
|
|
||||||
public HumanPlayer(String playerSign) {
|
public HumanPlayer(String symbol) {
|
||||||
super(playerSign);
|
super(symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -12,10 +12,10 @@ public class HumanPlayer extends Player {
|
||||||
int choice;
|
int choice;
|
||||||
|
|
||||||
do {
|
do {
|
||||||
choice = TicTacToe.scanner.nextInt(); //-1 to transform from position to index in array
|
choice = TicTacToe.scanner.nextInt() - 1;
|
||||||
} while (!board.isFieldChoosable(choice-1));
|
} while (!board.isFieldChoosable(choice));
|
||||||
|
|
||||||
board.setBoardMove(choice-1, playerSign);
|
board.setBoardMove(choice, symbol);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,14 +2,14 @@ package Uebung_04_Andres;
|
||||||
|
|
||||||
public abstract class Player {
|
public abstract class Player {
|
||||||
|
|
||||||
protected String playerSign;
|
protected String symbol;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor to force inheriting classes to initialise the playerSign
|
* Constructor to force inheriting classes to initialise the playerSign
|
||||||
* @param playerSign: "X" or "O"
|
* @param playerSign: "X" or "O"
|
||||||
*/
|
*/
|
||||||
public Player(String playerSign) {
|
public Player(String symbol) {
|
||||||
this.playerSign = playerSign;
|
this.symbol = symbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
public abstract void move(Board board);
|
public abstract void move(Board board);
|
||||||
|
|
|
||||||
|
|
@ -10,13 +10,28 @@ public class TicTacToe {
|
||||||
scanner = new Scanner(System.in);
|
scanner = new Scanner(System.in);
|
||||||
Game game = new Game();
|
Game game = new Game();
|
||||||
|
|
||||||
System.out.println("Der Mensch wird den ersten Zug machen.\n"
|
System.out.println("Auswahlmenü-\n"
|
||||||
+ "Drücken Sie die Eingabetaste, um\n"
|
+ "[1] Roboter vs Spieler, Roboter beginnt\n"
|
||||||
+ "fortzufahren, oder geben Sie 1 ein,\n"
|
+ "[2] Roboter vs Spieler, Spieler beginnt\n"
|
||||||
+ "damit der Computer den ersten Zug macht:");
|
+ "[3] Spieler vs Spieler\n"
|
||||||
String choice = scanner.nextLine();
|
+ "Deine Wahl: ");
|
||||||
|
|
||||||
|
String choice = scanner.nextLine().trim();
|
||||||
System.out.println("######################################################################");
|
System.out.println("######################################################################");
|
||||||
game.startGame(!choice.trim().equals("1")); //If anything but a 1 is entered, human starts the game
|
switch (choice) {
|
||||||
|
case "1":
|
||||||
|
game.gameSetupAndStart(false, false);
|
||||||
|
break;
|
||||||
|
case "2":
|
||||||
|
game.gameSetupAndStart(true, false);
|
||||||
|
break;
|
||||||
|
case "3":
|
||||||
|
game.gameSetupAndStart(true, true);
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
System.out.println("Falsche Eingabe... also kein Spiel :)");
|
||||||
|
}
|
||||||
|
|
||||||
scanner.close();
|
scanner.close();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue