From 711f610c542b1f58c4462fd27b2d2019c5439524 Mon Sep 17 00:00:00 2001 From: Dion Aliu Date: Sat, 22 Jan 2022 17:40:28 +0100 Subject: [PATCH] added write board function for TicTacToe --- src/main/java/Game/Game.java | 2 ++ src/main/java/Game/Tictactoe.java | 29 +++++++++++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/main/java/Game/Game.java b/src/main/java/Game/Game.java index 540a3d3..fd0e0ff 100644 --- a/src/main/java/Game/Game.java +++ b/src/main/java/Game/Game.java @@ -6,6 +6,8 @@ public abstract class Game { protected ArrayList outputBuffer = new ArrayList<>(); + public abstract void update(String string); + public void print() { for (String s : outputBuffer) { System.out.println(s); diff --git a/src/main/java/Game/Tictactoe.java b/src/main/java/Game/Tictactoe.java index 371c54a..c936bb2 100644 --- a/src/main/java/Game/Tictactoe.java +++ b/src/main/java/Game/Tictactoe.java @@ -4,7 +4,6 @@ import java.util.ArrayList; public class Tictactoe extends Game { - enum State { CIRCLE, CROSS, @@ -21,7 +20,10 @@ public class Tictactoe extends Game { currentBoard = newBoard(); } - + @Override + public void update(String string) { + writeBoard(currentBoard); + } public char getStatedChar(State state) { switch (state) { @@ -47,4 +49,27 @@ public class Tictactoe extends Game { public State[] getCurrentBoard() { return currentBoard; } + /* + 1 ║2 ║3 + o ║ x ║ o + ═════╬═════╬═════ + 4 ║5 ║6 + o ║ x ║ o + ═════╬═════╬═════ + 7 ║8 ║9 + o ║ x ║ o + */ + private void writeBoard(State[] state) { + this.outputBuffer.clear(); + outputBuffer.add("1 ║2 ║3"); + outputBuffer.add(" " + getStatedChar(state[0]) + " ║ " + getStatedChar(state[1]) + " ║ " + getStatedChar(state[2]) +" "); + outputBuffer.add("═════╬═════╬═════"); + + outputBuffer.add("4 ║5 ║6"); + outputBuffer.add(" " + getStatedChar(state[3]) + " ║ " + getStatedChar(state[4]) + " ║ " + getStatedChar(state[5]) +" "); + outputBuffer.add("═════╬═════╬═════"); + + outputBuffer.add("7 ║8 ║9"); + outputBuffer.add(" " + getStatedChar(state[6]) + " ║ " + getStatedChar(state[7]) + " ║ " + getStatedChar(state[8]) +" "); + } }