You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
81 lines
2.3 KiB
81 lines
2.3 KiB
package Game;
|
|
|
|
import Game.TicTacToe.Board;
|
|
|
|
public class Tictactoe extends Game {
|
|
|
|
private String input;
|
|
private Board currentBoard;
|
|
private boolean crossTurn;
|
|
|
|
public Tictactoe() {
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
crossTurn = true;
|
|
currentBoard = new Board();
|
|
outputBuffer.add("Welcome to Tic Tac Toe. \nCross start the game");
|
|
outputBuffer.addAll(currentBoard.getOutputBoard());
|
|
outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
|
|
}
|
|
|
|
@Override
|
|
public void update(String input) {
|
|
outputBuffer.clear();
|
|
if (isFinished()) {
|
|
resetBoard();
|
|
return;
|
|
}
|
|
boolean validTurn = false;
|
|
try {
|
|
validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn);
|
|
|
|
} catch (NumberFormatException e) {
|
|
|
|
}
|
|
|
|
outputBuffer.addAll(currentBoard.getOutputBoard());
|
|
if (validTurn)
|
|
switchTurn();
|
|
else
|
|
outputBuffer.add("Invalid Turn!");
|
|
|
|
switch (currentBoard.getCurrentState()) {
|
|
case CIRCLEWIN:
|
|
outputBuffer.add("Circle won the game gg");
|
|
setFinished(true);
|
|
break;
|
|
case CROSSWIN:
|
|
outputBuffer.add("Cross won the game gg");
|
|
setFinished(true);
|
|
break;
|
|
case DRAW:
|
|
outputBuffer.add("l2p");
|
|
setFinished(true);
|
|
break;
|
|
case NOTFINISHED:
|
|
outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
|
|
break;
|
|
default:
|
|
throw new IllegalStateException("Unexpected value: " + currentBoard.getCurrentState());
|
|
}
|
|
if (isFinished()) {
|
|
outputBuffer.add("Please enter any key to start the game!");
|
|
}
|
|
}
|
|
|
|
public void resetBoard() {
|
|
setFinished(false);
|
|
currentBoard = new Board();
|
|
crossTurn = true;
|
|
outputBuffer.add("Starting a new Game... Prepare for the fight");
|
|
outputBuffer.addAll(currentBoard.getOutputBoard());
|
|
outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
|
|
}
|
|
|
|
public void switchTurn() {
|
|
crossTurn = !crossTurn;
|
|
}
|
|
|
|
}
|