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