Simran Bhutani 5 年之前
父節點
當前提交
c8ea4bb6f3
共有 1 個檔案被更改,包括 46 行新增2 行删除
  1. 46
    2
      src/main/java/rocks/zipcode/io/quiz3/arrays/TicTacToe.java

+ 46
- 2
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
  */
@@ -43,8 +45,50 @@ public class TicTacToe {
43 45
 
44 46
     public String getWinner() {
45 47
 
48
+        String[] board = new String[9];
49
+        String turn = "X";
50
+        for (int a = 0; a < 8; a++) {
51
+            String line = null;
52
+            switch (a) {
53
+                case 0:
54
+                    line = board[0] + board[1] + board[2];
55
+                    break;
56
+                case 1:
57
+                    line = board[3] + board[4] + board[5];
58
+                    break;
59
+                case 2:
60
+                    line = board[6] + board[7] + board[8];
61
+                    break;
62
+                case 3:
63
+                    line = board[0] + board[3] + board[6];
64
+                    break;
65
+                case 4:
66
+                    line = board[1] + board[4] + board[7];
67
+                    break;
68
+                case 5:
69
+                    line = board[2] + board[5] + board[8];
70
+                    break;
71
+                case 6:
72
+                    line = board[0] + board[4] + board[8];
73
+                    break;
74
+                case 7:
75
+                    line = board[2] + board[4] + board[6];
76
+                    break;
77
+            }
78
+            if (line.equals("XXX")) {
79
+                return "X";
80
+            } else if (line.equals("OOO")) {
81
+                return "O";
82
+            }
83
+        }
46 84
 
47
-
48
-        return null;
85
+        for (int a = 0; a < 9; a++) {
86
+            if (Arrays.asList(board).contains(String.valueOf(a+1))) {
87
+                break;
88
+            }
89
+            else if (a == 8)
90
+                return turn;
91
+        }
92
+        return turn;
49 93
     }
50 94
 }