-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
148 lines (132 loc) · 3.36 KB
/
TicTacToe.java
File metadata and controls
148 lines (132 loc) · 3.36 KB
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
/**
* Implements the Tic Tac Toe game.
*/
package games;
import java.util.Random;
class TicTacToe {
// The game field.
char[][] field = new char[3][3];
// Size of the line to become a winner
int winSize = 3;
// If true, it's x turn to move. False - o's one.
boolean xTurn = true;
// Number of free cells left on the field.
public int freeCells;
// Random generator for random turns.
Random generator = new Random();
/**
* Game constructor, intiliazes the field.
*/
public TicTacToe() {
freeCells = 0;
for (int i = 0; i < field.length; i++) {
for (int j = 0; j < field[i].length; j++) {
freeCells++;
field[i][j] = ' ';
}
}
}
/**
* Determines if there is a winner in the game.
* @return space - no winner, x - x won, o - o won.
*/
public char getWinner() {
for (int i = 0; i < field.length; i++) {
for (int j = 0; j < field[i].length; j++) {
if (field[i][j] == ' ') {
continue;
}
String[] directions = {"hor", "ver", "dir", "dil"};
for (String dir : directions) {
if (isWinThere(j, i, dir)) {
return field[i][j];
}
}
}
}
return ' ';
}
/**
* Checks the direction from a specific point for a winning
* combination.
* @param x X coordinate.
* @param y Y coordinate.
* @param dir Direction to check: hor, ver, dir, dil.
* @return True if the combination is found (there is a winner).
*/
boolean isWinThere(int x, int y, String dir) {
char symbol = field[y][x];
int result = 0;
while (y >= 0 && x >= 0 && y < field.length && x < field.length &&
field[y][x] == symbol) {
if (dir == "hor" || dir == "dir") {
x++;
}
if (dir == "dil") {
x--;
}
if (dir == "ver" || dir == "dir" || dir == "dil") {
y++;
}
result++;
}
return result == winSize;
}
/**
* Makes a random turn, switches current player.
*/
public void makeRandomTurn() {
int a = 1 + generator.nextInt(freeCells);
System.out.println(a + " out of " + freeCells);
int counter = 0;
for (int i = 0; i < field.length; i++) {
for (int j = 0; j < field[i].length; j++) {
if (field[i][j] != ' ') {
continue;
}
counter++;
if (counter == a) {
if (xTurn) {
field[i][j] = 'x';
freeCells--;
} else {
field[i][j] = 'o';
freeCells--;
}
xTurn = !xTurn;
return;
}
}
}
}
/**
* Initialisis a game of TicTacToe, does random turns until
* a winner is found or out of free space, prints the field.
* @param args Array of arguments for launching the program.
*/
public static void main(String[] args) {
TicTacToe game = new TicTacToe();
game.printField();
while (game.freeCells > 0) {
game.makeRandomTurn();
game.printField();
char winner = game.getWinner();
if (winner != ' ') {
System.out.println(winner + " has won!");
return;
}
System.out.println();
}
}
/**
* Prints the field of Tic Tac Toe.
*/
public void printField() {
for (char[] row : field) {
for (char value : row) {
System.out.print(value + " ");
}
System.out.println();
}
}
}