From bba25f53d27f301cb6b2bef6af590a9a01fbdac3 Mon Sep 17 00:00:00 2001 From: johnS93 Date: Sat, 3 Jul 2021 23:23:00 -0400 Subject: [PATCH] I don't want to play tictactoe anymore --- pom.xml | 12 ++++ .../zipcodewilmington/tictactoe/Board.java | 57 ++++++++++++++++--- .../tictactoe/PlayerTies2Test.java | 1 + .../tictactoe/PlayerWinsRow2Test.java | 3 + 4 files changed, 64 insertions(+), 9 deletions(-) diff --git a/pom.xml b/pom.xml index 3b8d043..09a1b58 100644 --- a/pom.xml +++ b/pom.xml @@ -7,6 +7,18 @@ rocks.zipcodewilmington tic-tac-toe 1.0 + + + + org.apache.maven.plugins + maven-compiler-plugin + + 14 + 14 + + + + junit diff --git a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java index f56452f..14f5b89 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -1,26 +1,65 @@ package rocks.zipcodewilmington.tictactoe; +import java.util.Arrays; + /** * @author leon on 6/22/18. */ public class Board { - public Board(Character[][] matrix) { - } + private Character[][] matrix; + private String winner = ""; - public Boolean isInFavorOfX() { - return null; + public Board(Character[][] matrix) { this.matrix = matrix;} + public Character[][] getBoard() { return this.matrix; } + + public boolean isInFavorOfX() { + return getWinner() == "X"; } - public Boolean isInFavorOfO() { - return null; + public boolean isInFavorOfO() { + return getWinner() == "O"; } - public Boolean isTie() { - return null; + public boolean isTie() { + return getWinner() == ""; } public String getWinner() { - return null; + + + if ((this.matrix[0][0] == this.matrix[0][1]) && (this.matrix[0][0] == this.matrix[0][2])) { + getCharAndSetInFavor(this.matrix[0][0]); + } else if ((this.matrix[1][0] == this.matrix[1][1]) && (this.matrix[1][0] == this.matrix[1][2])) { + getCharAndSetInFavor(this.matrix[1][0]); + } else if ((this.matrix[2][0] == this.matrix[2][1]) && (this.matrix[2][0] == this.matrix[2][2])) { + getCharAndSetInFavor(this.matrix[2][0]); + } else if ((this.matrix[0][0] == this.matrix[1][0]) && (this.matrix[0][0] == this.matrix[2][0])) { + getCharAndSetInFavor(this.matrix[0][0]); + } else if ((this.matrix[0][1] == this.matrix[1][1]) && (this.matrix[0][1] == this.matrix[2][1])) { + getCharAndSetInFavor(this.matrix[0][1]); + } else if ((this.matrix[0][2] == this.matrix[1][2]) && (this.matrix[0][2] == this.matrix[2][2])) { + getCharAndSetInFavor(this.matrix[0][2]); + } else if ((this.matrix[0][0] == this.matrix[1][1]) && (this.matrix[0][0] == this.matrix[2][2])) { + getCharAndSetInFavor(this.matrix[0][0]); + } else if ((this.matrix[0][2] == this.matrix[1][1]) && (this.matrix[0][2] == this.matrix[2][0])) { + getCharAndSetInFavor(this.matrix[0][2]); + } + System.out.println(this.winner); + return this.winner; + } + + public void getCharAndSetInFavor(Character c) { + switch(c) { + case 'X': + this.winner = "X"; + break; + case 'O': + this.winner = "O"; + break; + default: + System.out.println(this.winner); + return; + } } } diff --git a/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerTies2Test.java b/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerTies2Test.java index 9193c60..047843e 100644 --- a/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerTies2Test.java +++ b/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerTies2Test.java @@ -28,6 +28,7 @@ public void getWinnerTest() { // When String actualWinner = board.getWinner(); + System.out.println(this.board); // Then Assert.assertEquals(expectedWinner, actualWinner); diff --git a/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerWinsRow2Test.java b/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerWinsRow2Test.java index c04abfc..92b810c 100644 --- a/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerWinsRow2Test.java +++ b/src/test/java/rocks/zipcodewilmington/tictactoe/PlayerWinsRow2Test.java @@ -72,5 +72,8 @@ public void isTieTest() { Assert.assertEquals(expected, actual); } + } + +