-
Notifications
You must be signed in to change notification settings - Fork 0
/
OXgame.java
159 lines (128 loc) · 3.05 KB
/
OXgame.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.InputMismatchException;
import java.util.Scanner;
public class OXgame {
static Scanner kb = new Scanner(System.in);
static char[] table = { '-', '-', '-', '-', '-', '-', '-', '-', '-' };
static char player = 'o';
static int round = 0;
static int row;
static int col;
static int index = 0;
static void printTable() {
for (int i = 0; i < table.length; i++) {
System.out.print(table[i] + " ");
if (i == 2 || i == 5 || i == 8) {
System.out.println();
}
}
}
static void printWelcome() {
System.out.println("Welcome to OX Game <3");
}
static void printTurn() {
if (player == 'o')
player = 'x';
else
player = 'o';
round++;
System.out.println("Turn "+ player);
}
static void inputPosition() {
while (true) {
try {
System.out.println("Please input Row, Col : ");
row = kb.nextInt();
col = kb.nextInt();
if ((row < 1 || row > 3) || (col < 1 || col > 3)) {
System.out.println("Min value of Row and Col is 1 \n" + "Max value of Row and Col is 3");
continue;
}
if (checkBlank(getRealPosition())) {
System.out.println("Please change position!!");
continue;
}
table[getRealPosition()] = player;
break;
} catch (InputMismatchException e) {
System.out.println("Position Incorrect");
if (kb.hasNext()) {
kb.nextLine();
}
}
}
}
static boolean checkBlank(int index) {
if (table[index] == '-')
return false;
return true;
}
static int getRealPosition() {
return (row - 1) * 3 + col - 1;
}
static boolean checkResult( int index, int diff) {
if (table[index] == table[index + diff] && table[index + diff] == table[index + (diff * 2)])
return true;
return false;
}
static boolean checkResultSlant() {
if (checkBlank(0) && checkResult(0, 4))
return true;
if (checkBlank(2) && checkResult(2, 2))
return true;
return false;
}
static boolean checkResultVertical() {
if (checkBlank(0) && checkResult(0, 1))
return true;
if (checkBlank(3) && checkResult(3, 1))
return true;
if (checkBlank(6) && checkResult(6, 1))
return true;
return false;
}
static boolean checkResultHorizontal() {
if (checkBlank(0) && checkResult(0, 3))
return true;
if (checkBlank(1) && checkResult(1, 3))
return true;
if (checkBlank(2) && checkResult(2, 3))
return true;
return false;
}
static boolean checkWin() {
if (checkResultVertical())
return true;
if (checkResultHorizontal())
return true;
if (checkResultSlant())
return true;
return false;
}
static boolean checkDraw() {
if (round == 9) {
player = 'd';
return true;
}
return false;
}
static void printWinner() {
printTable();
if (player == 'd')
System.out.println("Draw!!");
else
System.out.println("player " + Character.toString(player) + " Win!!");
}
public static void main(String[] args) {
printWelcome();
while (true) {
printTable();
printTurn();
inputPosition();
if (checkWin())
break;
if (checkDraw())
break;
}
printWinner();
}
}