From 1fb9393f64dc70f0d303307c4d551463d1076ef3 Mon Sep 17 00:00:00 2001 From: Brandon Chambers Date: Wed, 30 Oct 2019 19:21:24 -0400 Subject: [PATCH] complete --- .../zipcodewilmington/tictactoe/Board.java | 75 ++++++++++++++++++- 1 file changed, 71 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..0bf6a42 100644 --- a/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java +++ b/src/main/java/rocks/zipcodewilmington/tictactoe/Board.java @@ -4,23 +4,90 @@ * @author leon on 6/22/18. */ public class Board { + + Character[][] xO; + + public Board(Character[][] matrix) { + this.xO = matrix; } + public Boolean isInFavorOfX() { - return null; + + return (checkHorizontal().equals("X") || checkVertical().equals("X") || checkDiagonal().equals("X")); } public Boolean isInFavorOfO() { - return null; + return (checkHorizontal().equals("O") || checkVertical().equals("O") || checkDiagonal().equals("O")); } + public Boolean isTie() { - return null; + + if (checkHorizontal().equals(' ') && checkVertical().equals(' ') && checkDiagonal().equals(' ')){ + return true; + } + else if(!isInFavorOfO() && !isInFavorOfX()){ + return true; + } + return false; } public String getWinner() { - return null; + if (isInFavorOfX()) { + return "X"; + } + + if (isInFavorOfO()) { + return "O"; + } else { + return ""; + } } + + public String checkHorizontal() { + String symbol = ""; + + + for (int row = 0; row < xO.length; row++) { + + if (xO[row][0].equals(xO[row][1]) && xO[row][0].equals(xO[row][2]) && (!xO[row][0].equals(' '))) { + symbol = xO[row][0].toString(); + break; + } + } + return symbol; + } + + + public String checkVertical() { + String symbol = ""; + + + for (int col = 0; col < xO.length; col++) { + + if (xO[0][col].equals(xO[1][col]) && xO[0][col].equals(xO[2][col]) && (!xO[0][col].equals(' '))) { + symbol = xO[0][col].toString(); + break; + } + } + return symbol; + } + + + public String checkDiagonal() { + String symbol = ""; + + if (xO[0][2].equals(xO[1][1]) && xO[0][2].equals(xO[2][0]) && !xO[2][0].equals(' ')) { + symbol = xO[1][1].toString(); + + } + else if (xO[0][0].equals(xO[1][1]) && xO[0][0].equals(xO[2][2]) && !xO[0][0].equals(' ')) { + symbol = xO[1][1].toString(); + + } + return symbol; + } }