Browse Source

tic tac toe

Lauren Green 6 years ago
parent
commit
42b5121ba7
1 changed files with 54 additions and 4 deletions
  1. 54
    4
      src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java

+ 54
- 4
src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java View File

@@ -5,26 +5,76 @@ package rocks.zipcode.io.quiz3.arrays;
5 5
  */
6 6
 public class TicTacToe {
7 7
 
8
+    String[][] board = new String[3][3];
9
+
8 10
     public TicTacToe(String[][] board) {
11
+        this.board = board;
9 12
     }
10 13
 
11 14
     public String[] getRow(Integer value) {
12
-        return null;
15
+
16
+        return this.board[value];
13 17
     }
14 18
 
15 19
     public String[] getColumn(Integer value) {
16
-        return null;
20
+        String[] result = new String[3];
21
+        for (int i = 0; i < 3; i++) {
22
+            result[i] = this.board[i][value];
23
+        }
24
+        return result;
17 25
     }
18 26
 
19 27
     public Boolean isRowHomogenous(Integer rowIndex) {
20
-        return null;
28
+
29
+        String[] row = getRow(rowIndex);
30
+        if(row[0].equals(row[1]) && row[0].equals(row[2]))
31
+            return true;
32
+
33
+        return false;
21 34
     }
22 35
 
23 36
     public Boolean isColumnHomogeneous(Integer columnIndex) {
24
-        return null;
37
+
38
+        String[] col = getColumn(columnIndex);
39
+        if(col[0].equals(col[1]) && col[0].equals(col[2]))
40
+            return true;
41
+
42
+        return false;
43
+    }
44
+
45
+    public Boolean isDiagonalLeftHomogeneous() {
46
+
47
+        String[] diagonal = {this.board[0][0], this.board[1][1], this.board[2][2]};
48
+        if (diagonal[0].equals(diagonal[1]) && diagonal[0].equals(diagonal[2]))
49
+            return true;
50
+        return false;
51
+    }
52
+
53
+    public Boolean isDiagonalRightHomogeneous() {
54
+
55
+        String[] diagonal = {this.board[0][2], this.board[1][1], this.board[2][0]};
56
+        if (diagonal[0].equals(diagonal[1]) && diagonal[0].equals(diagonal[2]))
57
+            return true;
58
+        return false;
25 59
     }
26 60
 
61
+
62
+
27 63
     public String getWinner() {
64
+
65
+        if(isDiagonalLeftHomogeneous())
66
+            return getRow(0)[0];
67
+
68
+        if(isDiagonalRightHomogeneous())
69
+            return getRow(0)[2];
70
+
71
+        for (int i = 0; i < 3; i++) {
72
+            if(isRowHomogenous(i))
73
+                return getRow(i)[0];
74
+            if(isColumnHomogeneous(i))
75
+                return getColumn(i)[0];
76
+        }
77
+
28 78
         return null;
29 79
     }
30 80
 }