From b81a45f120447a053afbee30288fe6c1aa7aec36 Mon Sep 17 00:00:00 2001 From: Greg Donnelly Date: Wed, 3 Mar 2021 23:37:12 -0500 Subject: [PATCH 1/3] Alright, so I made the isInFavorOfX, isInFavorOfO, isTie, and getWinner all hopefully have the parts they need. Now should just be setting all of the conditionals to the different ways X and O can win --- .../zipcodewilmington/tictactoe/Board.java | 41 +++++++++++++++++-- 1 file changed, 37 insertions(+), 4 deletions(-) diff --git a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java index f56452f..fcf4b40 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -1,26 +1,59 @@ package rocks.zipcodewilmington.tictactoe; +import com.sun.org.apache.xpath.internal.operations.Bool; + +import java.util.Arrays; + /** * @author leon on 6/22/18. */ public class Board { + char x = 'X'; + char o = 'O'; + String winner = ""; public Board(Character[][] matrix) { + //X wins first + if (matrix[0][0] == x && matrix[0][1] == x && matrix[0][2] == x) { + winner = winner.concat("X"); + } else if (matrix[1][0] == x && matrix[1][1] == x && matrix[1][2] == x) { + winner = winner.concat("X"); + } else if (matrix[2][0] == x && matrix[2][1] == x && matrix[2][2] == x) { + winner = winner.concat("X"); + } else if (matrix[0][0] == x && matrix[1][0] == x && matrix[2][0] == x) { + winner = winner.concat("X"); + } else { + winner = ""; + } } public Boolean isInFavorOfX() { - return null; + boolean xWins = false; + if (winner.equals("X")) { + xWins = true; + } + return xWins; } public Boolean isInFavorOfO() { - return null; + Boolean oWins = false; + if (winner.equals("O")) { + oWins = true; + } + return oWins; } public Boolean isTie() { - return null; + Boolean tieGame = false; + if (winner.equals("")) { + tieGame = true; + } + return tieGame; } public String getWinner() { - return null; + String whoWins = winner; + System.out.println(whoWins); + return whoWins; } } From 558e88e770d168db631ab930fdc4728145cc8805 Mon Sep 17 00:00:00 2001 From: Greg Donnelly Date: Thu, 4 Mar 2021 15:26:20 -0500 Subject: [PATCH 2/3] not done. but all X wins are passing. Wanna check on the other version --- .../java/rocks/zipcodewilmington/tictactoe/Board.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java index fcf4b40..5bd7b51 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -12,14 +12,20 @@ public class Board { char o = 'O'; String winner = ""; public Board(Character[][] matrix) { - //X wins first + //X wins rows if (matrix[0][0] == x && matrix[0][1] == x && matrix[0][2] == x) { winner = winner.concat("X"); } else if (matrix[1][0] == x && matrix[1][1] == x && matrix[1][2] == x) { winner = winner.concat("X"); } else if (matrix[2][0] == x && matrix[2][1] == x && matrix[2][2] == x) { winner = winner.concat("X"); - } else if (matrix[0][0] == x && matrix[1][0] == x && matrix[2][0] == x) { + } + //X wins columns + else if (matrix[0][0] == x && matrix[1][0] == x && matrix[2][0] == x) { + winner = winner.concat("X"); + }else if (matrix[0][1] == x && matrix[1][1] == x && matrix[2][1] == x) { + winner = winner.concat("X"); + }else if (matrix[0][2] == x && matrix[1][2] == x && matrix[2][2] == x) { winner = winner.concat("X"); } else { winner = ""; @@ -56,4 +62,5 @@ public String getWinner() { return whoWins; } + } From 4c48aef936267864701902f6e7ddaed815c9b9b5 Mon Sep 17 00:00:00 2001 From: Greg Donnelly Date: Thu, 4 Mar 2021 15:35:04 -0500 Subject: [PATCH 3/3] TicTacToe passing all tests --- .../zipcodewilmington/tictactoe/Board.java | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java index 5bd7b51..5299ce3 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -27,6 +27,34 @@ else if (matrix[0][0] == x && matrix[1][0] == x && matrix[2][0] == x) { winner = winner.concat("X"); }else if (matrix[0][2] == x && matrix[1][2] == x && matrix[2][2] == x) { winner = winner.concat("X"); + } + //X wins diagonal + else if (matrix[0][0] == x && matrix[1][1] == x && matrix[2][2] == x) { + winner = winner.concat("X"); + }else if (matrix[0][2] == x && matrix[1][1] == x && matrix[2][0] == x) { + winner = winner.concat("X"); + } + // O wins rows + else if (matrix[0][0] == o && matrix[0][1] == o && matrix[0][2] == o) { + winner = winner.concat("O"); + } else if (matrix[1][0] == o && matrix[1][1] == o && matrix[1][2] == o) { + winner = winner.concat("O"); + } else if (matrix[2][0] == o && matrix[2][1] == o && matrix[2][2] == o) { + winner = winner.concat("O"); + } + //O wins columns + else if (matrix[0][0] == o && matrix[1][0] == o && matrix[2][0] == o) { + winner = winner.concat("O"); + }else if (matrix[0][1] == o && matrix[1][1] == o && matrix[2][1] == o) { + winner = winner.concat("O"); + }else if (matrix[0][2] == o && matrix[1][2] == o && matrix[2][2] == o) { + winner = winner.concat("O"); + } + //O wins diagonal + else if (matrix[0][0] == o && matrix[1][1] == o && matrix[2][2] == o) { + winner = winner.concat("O"); + }else if (matrix[0][2] == o && matrix[1][1] == o && matrix[2][0] == o) { + winner = winner.concat("O"); } else { winner = ""; }