-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTabuleiro.java
More file actions
77 lines (69 loc) · 1.98 KB
/
Copy pathTabuleiro.java
File metadata and controls
77 lines (69 loc) · 1.98 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
public class Tabuleiro {
public char[][] t = new char[3][3];
private int moveCount = 0;
public Tabuleiro() {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
t[i][j] = '-';
}
}
}
public Tabuleiro copy() {
Tabuleiro copia = new Tabuleiro();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
copia.t[i][j] = this.t[i][j];
}
}
copia.moveCount = moveCount;
return copia;
}
public boolean validPlay(int i, int j) {
if (i < 0 || j < 0 || i > 2 || j > 2 || t[i][j] != '-')
return false;
return true;
}
public void play(char player, int i, int j) {
t[i][j] = player;
moveCount++;
}
public String getLinha(int linha) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 3; i++) {
sb.append(t[linha][i]);
}
return sb.toString();
}
@Override
public String toString() {
StringBuilder str = new StringBuilder(); // ganhar o habito de usar esta classe para ser mais eficiente no uso de
// memória
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
str.append(t[i][j]);
}
str.append("\n"); // %n so funciona dentro do sout
}
return str.toString();
}
public char checkEnd() {
if (moveCount == 0)
return '-'; // ainda nao houve nenhum movimento feito
if ((t[0][0] != '-' && t[0][0] == t[1][1] && t[1][1] == t[2][2]) || // diagonal principal
(t[0][2] != '-' && t[0][2] == t[1][1] && t[1][1] == t[2][0]) // diagonal secundaria
)
return t[1][1];
for (int i = 0; i < 3; i++) {
if (t[i][0] != '-' && t[i][0] == t[i][1] && t[i][1] == t[i][2] // linhas
)
return t[i][0];
if (t[0][i] != '-' && t[0][i] == t[1][i] && t[1][i] == t[2][i] // colunas
)
return t[0][i];
}
if (moveCount == 9) {
return 'D';
}
return '-'; // nao existe linha/coluna/diagonal com o msm simbolo
}
}