Yesoda Sanka 5 年之前
父節點
當前提交
86c8d90d19
共有 1 個檔案被更改,包括 58 行新增5 行删除
  1. 58
    5
      src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java

+ 58
- 5
src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java 查看文件

@@ -1,5 +1,7 @@
1 1
 package rocks.zipcode.io.quiz3.arrays;
2 2
 
3
+import java.util.Arrays;
4
+
3 5
 /**
4 6
  * @author leon on 09/12/2018.
5 7
  */
@@ -7,9 +9,8 @@ public class TicTacToe {
7 9
     String[][] board;
8 10
 
9 11
 
10
-
11 12
     public TicTacToe(String[][] board) {
12
-        this.board=board;
13
+        this.board = board;
13 14
     }
14 15
 
15 16
     public String[] getRow(Integer value) {
@@ -23,16 +24,68 @@ public class TicTacToe {
23 24
     }
24 25
 
25 26
     public Boolean isRowHomogenous(Integer rowIndex) {
26
-        return null;
27
+        for (int i = 0; i < rowIndex; i++) {
28
+            return true;
29
+
30
+        }
31
+        return false;
27 32
     }
28 33
 
29 34
     public Boolean isColumnHomogeneous(Integer columnIndex) {
35
+      for(int i=0;i<columnIndex;i++ ) {
36
+          return true;
37
+      }
30 38
 
31
-        return null;
39
+          return false;
32 40
     }
33 41
 
34 42
     public String getWinner() {
35 43
 
36
-        return null;
44
+
45
+        String[] board = new String[9];
46
+        String turn = "X";
47
+        for (int a = 0; a < 8; a++) {
48
+            String line = null;
49
+            switch (a) {
50
+                case 0:
51
+                    line = board[0] + board[1] + board[2];
52
+                    break;
53
+                case 1:
54
+                    line = board[3] + board[4] + board[5];
55
+                    break;
56
+                case 2:
57
+                    line = board[6] + board[7] + board[8];
58
+                    break;
59
+                case 3:
60
+                    line = board[0] + board[3] + board[6];
61
+                    break;
62
+                case 4:
63
+                    line = board[1] + board[4] + board[7];
64
+                    break;
65
+                case 5:
66
+                    line = board[2] + board[5] + board[8];
67
+                    break;
68
+                case 6:
69
+                    line = board[0] + board[4] + board[8];
70
+                    break;
71
+                case 7:
72
+                    line = board[2] + board[4] + board[6];
73
+                    break;
74
+            }
75
+            if (line.equals("XXX")) {
76
+                return "X";
77
+            } else if (line.equals("OOO")) {
78
+                return "O";
79
+            }
80
+        }
81
+        for (int a = 0; a < 9; a++) {
82
+            if (Arrays.asList(board).contains(String.valueOf(a+1))) {
83
+                break;
84
+            }
85
+            else if (a == 8)
86
+                return turn;
87
+        }
88
+        return turn;
89
+
37 90
     }
38 91
 }