-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTicTacToe.java
More file actions
174 lines (150 loc) · 4.16 KB
/
Copy pathTicTacToe.java
File metadata and controls
174 lines (150 loc) · 4.16 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import java.util.Scanner;
/**
* A simple Tic-Tac-Toe game.
*
* @author marco.mangan@pucrs.br
* @version 2022-06-08
*/
class TicTacToe {
/**
* Initialize a new board.
*
* @return a 3x3 board
*/
public static String[][] init() {
String[][] b = new String[3][3];
for (int i = 0; i < b.length; i++) {
for (int j = 0; j < b[i].length; j++) {
b[i][j] = " ";
}
}
assert b != null : "O board nao pode ser null!";
return b;
}
/**
* Returns a string with a board representation.
*
* @param board a 3x3 board
*/
public static String display(String[][] board) {
assert board != null : "O board nao pode ser null!";
//if (board == null) {
// throw new IllegalArgumentException("O board nao pode ser null!");
//}
String r = " 012\n";
for (int i = 0; i < board.length; i++) {
r += i + "|";
for (int j = 0; j < board[i].length; j++) {
r += board[i][j];
}
r += "|\n";
}
return r;
}
/**
* Set a
*
* @param
* @param
* @param
* @param
*/
public static void set(String[][] b, int i, int j, String p) {
if (b[i][j].equals(" ")) {
b[i][j] = p;
}
}
/**
*
*/
public static boolean isVictory(String[][] board, String player) {
for (int i = 0; i < board.length; i++) {
if (board[i][0].equals(player) &&
board[i][1].equals(player) &&
board[i][2].equals(player)
) {
return true;
}
}
for (int j = 0; j < board.length; j++) {
if (board[0][j].equals(player) &&
board[1][j].equals(player) &&
board[2][j].equals(player)
) {
return true;
}
}
if (board[0][0].equals(player) &&
board[1][1].equals(player) &&
board[2][2].equals(player)
) {
return true;
}
if (board[0][2].equals(player) &&
board[1][1].equals(player) &&
board[2][0].equals(player)
) {
return true;
}
return false;
}
/**
*
*/
public static boolean hasFreeCell(String[][] board) {
for (int i = 0; i < board.length; i++) {
for (int j = 0; j < board.length; j++) {
if (board[i][j].equals(" ")) {
return true; // encerra ao encontrar a primeira celula livre
}
}
}
return false;
}
/**
* Run
*
* @param
*/
public static void main(String[] args) {
Scanner sc;
String[][] board;
String player = "x";
int i, j;
System.out.println("Jogo da Velha!");
board = init();
sc = new Scanner(System.in);
while (true) {
System.out.println(display(board));
if (!hasFreeCell(board)) {
System.out.printf("%nEMPATE!%n");
break;
}
System.out.printf("%nJogador: %s%n", player);
System.out.printf("%nLinha (0, 1, 2 ou -1 para sair):");
i = sc.nextInt();
if (i < 0) {
System.out.printf("%nJogador %s desistiu! Vitoria do oponente!%n", player);
break;
}
System.out.printf("%nColuna (0, 1, 2 ou -1 para sair):");
j = sc.nextInt();
if (j < 0) {
System.out.printf("%nJogador %s desistiu! Vitoria do oponente!%n", player);
break;
}
set(board, i, j, player);
if (isVictory(board, player)) {
System.out.printf("%nO jogador %s venceu!%n", player);
break;
}
if (player.equals("x")) {
player = "o";
} else {
player = "x";
}
}
System.out.println(display(board));
sc.close();
}
}