From bac753cd78ea7672df7b141cdff4a55d3c5adf9b Mon Sep 17 00:00:00 2001 From: Keerthana Srinivasan Date: Fri, 5 Nov 2021 01:15:49 -0400 Subject: [PATCH] Completed lab --- .../zipcodewilmington/tictactoe/Board.java | 66 +++++++++++++++++-- 1 file changed, 62 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..ccb974f 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -4,23 +4,81 @@ * @author leon on 6/22/18. */ public class Board { + private Character[][] matrix; public Board(Character[][] matrix) { + this.matrix = matrix; } public Boolean isInFavorOfX() { - return null; + + return isDiagonalOf('X') || isHorizontalOf('X') || isVerticalOf('X'); } public Boolean isInFavorOfO() { - return null; + + return isDiagonalOf('O') || isHorizontalOf('O') || isVerticalOf('O'); } public Boolean isTie() { - return null; + return (!(isInFavorOfO() || isInFavorOfX())); } public String getWinner() { - return null; + String winner = ""; + if(isInFavorOfX()){ + winner = "X"; + }else if(isInFavorOfO()){ + winner = "O"; + } + return winner; + } + + public Boolean isHorizontalOf(Character player) { + + boolean isHorizontalFound = false; + for (int i = 0; i < matrix.length; i++) { + if (matrix[i][0] == player && matrix[i][1] == player && matrix[i][2] == player) { + isHorizontalFound = true; + break; + } + } + + return isHorizontalFound; + } + + public Boolean isVerticalOf(Character player) { + + boolean isVerticalFound = false; + for (int i = 0; i < matrix.length; i++) { + if (matrix[0][i] == player && matrix[1][i] == player && matrix[2][i] == player) { + isVerticalFound = true; + break; + } + } + + return isVerticalFound; + } + + public Boolean isDiagonalOf(Character player) { + + boolean isLeftDiagonalFound = true; + for (int i = 0; i < matrix.length; i++) { + if (matrix[i][i] != player) { + isLeftDiagonalFound = false; + break; + } + } + + boolean isRightDiagonalFound = true; + int maxIndex = matrix.length - 1; + for (int i = 0; i < matrix.length; i++) { + if (matrix[i][maxIndex - i] != player) { + isRightDiagonalFound = false; + break; + } + } + + return isLeftDiagonalFound || isRightDiagonalFound; } }