Nathan Hall 5 years ago
parent
commit
e13cc7c30c
1 changed files with 35 additions and 4 deletions
  1. 35
    4
      src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java

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

@@ -9,7 +9,8 @@ public class TicTacToe {
9 9
     private Integer rowIndex;
10 10
     private Integer columnIndex;
11 11
 
12
-    public TicTacToe(){}
12
+    public TicTacToe() {
13
+    }
13 14
 
14 15
     public TicTacToe(String[][] board) {
15 16
         this.board = board;
@@ -48,17 +49,19 @@ public class TicTacToe {
48 49
     }
49 50
 
50 51
     public String[] getRow(Integer value) {
51
-        return null;
52
+        return board[value];
52 53
     }
53 54
 
54 55
     public String[] getColumn(Integer value) {
56
+        String[] str = new String[3];
57
+
55 58
         return null;
56 59
     }
57 60
 
58 61
     public Boolean isRowHomogenous(Integer rowIndex) {
59 62
 
60 63
         for (int i = 1; i < 3; i++) {
61
-            if (this.board[rowIndex][i] != this.board[rowIndex][i - 1]){
64
+            if (this.board[rowIndex][i] != this.board[rowIndex][i - 1]) {
62 65
                 return false;
63 66
             }
64 67
         }
@@ -67,10 +70,38 @@ public class TicTacToe {
67 70
     }
68 71
 
69 72
     public Boolean isColumnHomogeneous(Integer columnIndex) {
70
-        return null;
73
+        for (int i = 1; i < 3; i++) {
74
+            if (this.board[i][columnIndex] != this.board[i - 1][columnIndex]) {
75
+                return false;
76
+            }
77
+        }
78
+
79
+        return true;
71 80
     }
72 81
 
73 82
     public String getWinner() {
83
+        Integer count = 0;
84
+        //rows
85
+        for (int i = 0; i < 3; i++) {
86
+            if (board[i][0] == board[i][1] && board[i][0] == board[i][2]) {
87
+                return board[i][0];
88
+            }
89
+        }
90
+        //columns
91
+        for (int i = 0; i < 3; i++) {
92
+            if (board[0][i] == board[1][i] && board[0][i] == board[2][i]) {
93
+                return board[0][i];
94
+            }
95
+            //diagonal1
96
+            if (i == 2 && board[i][i] == board[i - 1][i - 1] && board[i][i] == board[i - 2][i - 2]) {
97
+                return board[i][i];
98
+            }
99
+        }
100
+        //diagonal2
101
+            if (board[0][2] == board[1][1] && board[0][2] == board[2][0]){
102
+            return board[0][2];
103
+        }
104
+
74 105
         return null;
75 106
     }
76 107
 }