Browse Source

added Tic Tac Toe game loop and finished attribute to Game

feature-game
Dion Aliu 3 years ago
parent
commit
657953f190
  1. 9
      src/main/java/Game/Game.java
  2. 6
      src/main/java/Game/TicTacToe/Board.java
  3. 50
      src/main/java/Game/Tictactoe.java
  4. 11
      src/main/java/Main.java

9
src/main/java/Game/Game.java

@ -5,6 +5,7 @@ import java.util.ArrayList;
public abstract class Game {
protected ArrayList<String> 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<String> outputBuffer) {
this.outputBuffer = outputBuffer;
}

6
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;

50
src/main/java/Game/Tictactoe.java

@ -15,18 +15,62 @@ 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());
}
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:");
}

11
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();
}
}
}
Loading…
Cancel
Save