From 657953f1902ae23210b899360605e7cdeb83f6e0 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Wed, 26 Jan 2022 11:25:53 +0100 Subject: [PATCH] added Tic Tac Toe game loop and finished attribute to Game --- src/main/java/Game/Game.java | 9 +++++ src/main/java/Game/TicTacToe/Board.java | 6 +-- src/main/java/Game/Tictactoe.java | 52 +++++++++++++++++++++++-- src/main/java/Main.java | 11 ++++++ 4 files changed, 71 insertions(+), 7 deletions(-) diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java index f927c82..39bc4f8 100644 --- a/src/main/java/Game/Game.java +++ b/src/main/java/Game/Game.java @@ -5,6 +5,7 @@ import java.util.ArrayList; public abstract class Game { protected ArrayList outputBuffer = new ArrayList<>(); + private boolean isFinished = false; public abstract void update(String input); @@ -14,6 +15,14 @@ public abstract class Game { } } + public boolean isFinished() { + return isFinished; + } + + protected void setFinished(boolean isFinished) { + this.isFinished = isFinished; + } + protected void setOutputBuffer(ArrayList outputBuffer) { this.outputBuffer = outputBuffer; } diff --git a/src/main/java/Game/TicTacToe/Board.java b/src/main/java/Game/TicTacToe/Board.java index c3a9e87..daaea57 100644 --- a/src/main/java/Game/TicTacToe/Board.java +++ b/src/main/java/Game/TicTacToe/Board.java @@ -11,7 +11,7 @@ public class Board { EMPTY } - enum CurrentState { + public enum CurrentState { DRAW, CIRCLEWIN, CROSSWIN, @@ -119,8 +119,8 @@ public class Board { public CurrentState getCurrentState() { - int counterCross = 0; - int counterCircle = 0; + int counterCross; + int counterCircle; for (int[] pattern : winPatterns) { counterCircle = 0; diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java index 035b7b7..22ef89a 100644 --- a/src/main/java/Game/Tictactoe.java +++ b/src/main/java/Game/Tictactoe.java @@ -15,19 +15,63 @@ public class Tictactoe extends Game { 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(); - boolean validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn); + if (isFinished()) { + resetBoard(); + return; + } + boolean validTurn = false; + try { + validTurn = currentBoard.setCellState(Integer.parseInt(input), crossTurn); + + } catch (NumberFormatException e) { + + } + outputBuffer.addAll(currentBoard.getOutputBoard()); - if(validTurn) { + if (validTurn) switchTurn(); - } else { + 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()); } - outputBuffer.add((crossTurn?"Cross":"Circle") + " it´s your Turn, please choose a Cell:"); + 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() { diff --git a/src/main/java/Main.java b/src/main/java/Main.java index af6d878..89b9c35 100644 --- a/src/main/java/Main.java +++ b/src/main/java/Main.java @@ -1,6 +1,17 @@ +import Game.Tictactoe; + +import java.util.Scanner; + public class Main { public static void main(String[] args){ System.out.println("Hello world!"); + Tictactoe ttt = new Tictactoe(); + Scanner scan = new Scanner(System.in); + ttt.print(); + while (scan.hasNext()) { + ttt.update(scan.next()); + ttt.print(); + } } }