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

  1. package Game;
  2. import Game.TicTacToe.Board;
  3. public class Tictactoe extends Game {
  4. private String input;
  5. private Board currentBoard;
  6. private boolean crossTurn;
  7. public Tictactoe() {
  8. init();
  9. }
  10. private void init() {
  11. crossTurn = true;
  12. currentBoard = new Board();
  13. outputBuffer.add("Welcome to Tic Tac Toe. \nCross start the game");
  14. outputBuffer.addAll(currentBoard.getOutputBoard());
  15. outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
  16. }
  17. @Override
  18. public void update(String input) {
  19. outputBuffer.clear();
  20. if (isFinished()) {
  21. resetBoard();
  22. return;
  23. }
  24. boolean validTurn = false;
  25. try {
  26. validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn);
  27. } catch (NumberFormatException e) {
  28. }
  29. outputBuffer.addAll(currentBoard.getOutputBoard());
  30. if (validTurn)
  31. switchTurn();
  32. else
  33. outputBuffer.add("Invalid Turn!");
  34. switch (currentBoard.getCurrentState()) {
  35. case CIRCLEWIN:
  36. outputBuffer.add("Circle won the game gg");
  37. setFinished(true);
  38. break;
  39. case CROSSWIN:
  40. outputBuffer.add("Cross won the game gg");
  41. setFinished(true);
  42. break;
  43. case DRAW:
  44. outputBuffer.add("l2p");
  45. setFinished(true);
  46. break;
  47. case NOTFINISHED:
  48. outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
  49. break;
  50. default:
  51. throw new IllegalStateException("Unexpected value: " + currentBoard.getCurrentState());
  52. }
  53. if (isFinished()) {
  54. outputBuffer.add("Please enter any key to start the game!");
  55. }
  56. }
  57. public void resetBoard() {
  58. setFinished(false);
  59. currentBoard = new Board();
  60. crossTurn = true;
  61. outputBuffer.add("Starting a new Game... Prepare for the fight");
  62. outputBuffer.addAll(currentBoard.getOutputBoard());
  63. outputBuffer.add((crossTurn ? "Cross" : "Circle") + " it´s your Turn, please choose a Cell:");
  64. }
  65. public void switchTurn() {
  66. crossTurn = !crossTurn;
  67. }
  68. }