Nathan Hall 5 年之前
父節點
當前提交
e13cc7c30c
共有 1 個檔案被更改,包括 35 行新增4 行删除
  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 查看文件

9
     private Integer rowIndex;
9
     private Integer rowIndex;
10
     private Integer columnIndex;
10
     private Integer columnIndex;
11
 
11
 
12
-    public TicTacToe(){}
12
+    public TicTacToe() {
13
+    }
13
 
14
 
14
     public TicTacToe(String[][] board) {
15
     public TicTacToe(String[][] board) {
15
         this.board = board;
16
         this.board = board;
48
     }
49
     }
49
 
50
 
50
     public String[] getRow(Integer value) {
51
     public String[] getRow(Integer value) {
51
-        return null;
52
+        return board[value];
52
     }
53
     }
53
 
54
 
54
     public String[] getColumn(Integer value) {
55
     public String[] getColumn(Integer value) {
56
+        String[] str = new String[3];
57
+
55
         return null;
58
         return null;
56
     }
59
     }
57
 
60
 
58
     public Boolean isRowHomogenous(Integer rowIndex) {
61
     public Boolean isRowHomogenous(Integer rowIndex) {
59
 
62
 
60
         for (int i = 1; i < 3; i++) {
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
                 return false;
65
                 return false;
63
             }
66
             }
64
         }
67
         }
67
     }
70
     }
68
 
71
 
69
     public Boolean isColumnHomogeneous(Integer columnIndex) {
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
     public String getWinner() {
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
         return null;
105
         return null;
75
     }
106
     }
76
 }
107
 }