-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathJogoDaVelhaVer2.java
More file actions
232 lines (194 loc) · 9.68 KB
/
JogoDaVelhaVer2.java
File metadata and controls
232 lines (194 loc) · 9.68 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package com.leandro.aula20.labs;
import java.util.Scanner;
public class JogoDaVelhaTeste {
int contJogador1 = 0;
int contJogador2 = 0;
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String nomeJogador1 = "";
String nomeJogador2 = "";
int jogador1 = 0;
int jogador2 = 0;
System.out.println("Digite o nome do Jogador 1: ");
nomeJogador1 = scan.next();
System.out.println("Digite o nome do Jogador 2: ");
nomeJogador2 = scan.next();
int linhaJogador1 = 0;
int colunaJogador1 = 0;
int linhaJogador2 = 0;
int colunaJogador2 = 0;
boolean resposta = false;
int[][] matrizJogo = new int[3][3];
System.out.println(
"\n\tCOLUNA 1 COLUNA 2 COLUNA 3\n"
+ "\n\t\t|\t|\n"
+ "LINHA 1 \t|\t|\n"
+ "\t\t|\t|\n"
+ "\t_ _ _ __|_ _ _ _|__ _ _ _\n"
+ "\t\t|\t|\n"
+ "LINHA 2\t\t|\t|\n"
+ "\t\t|\t|\n"
+ "\t_ _ _ __|_ _ _ _|__ _ _ _\n"
+ "\t\t|\t|\n"
+ "\t\t|\t|\n"
+ "LINHA 3\t\t|\t|\n"
+ "\t\t|\t|\n");
for (int i = 0; i < 9; i++) {
if (i % 2 == 0) {
iniciaJogador1(matrizJogo, nomeJogador1);
} else {
iniciaJogador2(matrizJogo, nomeJogador2);
}
}
}
public static boolean verificaGanhador1(int[][] matrizJogo) {
boolean resposta = false;
for (int i = 0; i < matrizJogo.length; i++) {
for (int j = 0; j < matrizJogo[i].length; j++) {
if (matrizJogo[0][0] + matrizJogo[0][1] + matrizJogo[0][2] == -3) {
resposta = true;
} else if (matrizJogo[1][0] + matrizJogo[1][1] + matrizJogo[1][2] == -3) {
resposta = true;
} else if (matrizJogo[2][0] + matrizJogo[2][1] + matrizJogo[2][2] == -3) {
resposta = true;
} else if (matrizJogo[0][0] + matrizJogo[1][1] + matrizJogo[2][2] == -3) {
resposta = true;
} else if (matrizJogo[0][2] + matrizJogo[1][1] + matrizJogo[2][0] == -3) {
resposta = true;
} else if (matrizJogo[0][0] + matrizJogo[1][0] + matrizJogo[2][0] == -3) {
resposta = true;
} else if (matrizJogo[0][1] + matrizJogo[1][1] + matrizJogo[2][1] == -3) {
resposta = true;
} else if (matrizJogo[0][2] + matrizJogo[1][2] + matrizJogo[2][2] == -3) {
resposta = true;
}
}
}
return resposta;
}
public static boolean verificaGanhador2(int[][] matrizJogo) {
boolean resposta = false;
for (int i = 0; i < matrizJogo.length; i++) {
for (int j = 0; j < matrizJogo[i].length; j++) {
if (matrizJogo[0][0] + matrizJogo[0][1] + matrizJogo[0][2] == 3) {
resposta = true;
} else if (matrizJogo[1][0] + matrizJogo[1][1] + matrizJogo[1][2] == 3) {
resposta = true;
} else if (matrizJogo[2][0] + matrizJogo[2][1] + matrizJogo[2][2] == 3) {
resposta = true;
} else if (matrizJogo[0][0] + matrizJogo[1][1] + matrizJogo[2][2] == 3) {
resposta = true;
} else if (matrizJogo[0][2] + matrizJogo[1][1] + matrizJogo[2][0] == 3) {
resposta = true;
} else if (matrizJogo[0][0] + matrizJogo[1][0] + matrizJogo[2][0] == 3) {
resposta = true;
} else if (matrizJogo[0][1] + matrizJogo[1][1] + matrizJogo[2][1] == 3) {
resposta = true;
} else if (matrizJogo[0][2] + matrizJogo[1][2] + matrizJogo[2][2] == 3) {
resposta = true;
}
}
}
return resposta;
}
public static void iniciaJogador1(int[][] matrizJogo, String nomeJogador1) {
Scanner scan = new Scanner(System.in);
int linhaJogador1 = 0;
int colunaJogador1 = 0;
boolean resposta = false;
System.out.println("\t" + nomeJogador1 + ", em que posição "
+ "você irá marcar a bolinha(O)?: ");
System.out.println("Linha: ");
linhaJogador1 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador1 = scan.nextInt();
if ((linhaJogador1 < 1 || linhaJogador1 > 3) || (colunaJogador1 < 1 || colunaJogador1 > 3)) {
do {
System.out.println("Opção(ões) inválida(s). Tente novamente.");
System.out.println("\t" + nomeJogador1 + ", em que posição "
+ "você irá marcar a bolinha(O)?: ");
System.out.println("Linha: ");
linhaJogador1 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador1 = scan.nextInt();
} while ((linhaJogador1 < 1 || linhaJogador1 > 3) || (colunaJogador1 < 1 || colunaJogador1 > 3));
}
if (matrizJogo[linhaJogador1 - 1][colunaJogador1 - 1] == 0) {
matrizJogo[linhaJogador1 - 1][colunaJogador1 - 1] = -1;
resposta = verificaGanhador1(matrizJogo);
if (resposta) {
System.out.println(nomeJogador1 + ", você é o grande ganhador deste Jogo.");
System.exit(0);
}
} else { // EVITANDO PREENCHIMENTO EM POSIÇÃO JÁ PREENCHIDA
do {
System.out.println("Essa posição já está marcada. Tente outra posição.");
do {
System.out.println("Opção(ões) inválida(s). Tente novamente.");
System.out.println("\t" + nomeJogador1 + ", em que posição "
+ "você irá marcar a bolinha(O)?: ");
System.out.println("Linha: ");
linhaJogador1 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador1 = scan.nextInt();
} while ((linhaJogador1 < 1 || linhaJogador1 > 3) || (colunaJogador1 < 1 || colunaJogador1 > 3));
} while (matrizJogo[linhaJogador1 - 1][colunaJogador1 - 1] != 0);
matrizJogo[linhaJogador1 - 1][colunaJogador1 - 1] = -1;
resposta = verificaGanhador1(matrizJogo);
if (resposta) {
System.out.println(nomeJogador1 + ", você é o grande ganhador deste Jogo.");
System.exit(0);
}
}
}
public static void iniciaJogador2(int matrizJogo[][], String nomeJogador2) {
Scanner scan = new Scanner(System.in);
int linhaJogador2 = 0;
int colunaJogador2 = 0;
boolean resposta = false;
System.out.println("\t" + nomeJogador2 + ", em que posição "
+ "você irá marcar o \"X\")?: ");
System.out.println("Linha: ");
linhaJogador2 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador2 = scan.nextInt();
if ((linhaJogador2 < 1 || linhaJogador2 > 3) || (colunaJogador2 < 1 || colunaJogador2 > 3)) {
do {
System.out.println("Opção(ões) inválida(s). Tente novamente.");
System.out.println("\t" + nomeJogador2 + ", em que posição "
+ "você irá marcar o \"X\"?: ");
System.out.println("Linha: ");
linhaJogador2 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador2 = scan.nextInt();
} while ((linhaJogador2 < 1 || linhaJogador2 > 3) || (colunaJogador2 < 1 || colunaJogador2 > 3));
}
if (matrizJogo[linhaJogador2 - 1][colunaJogador2 - 1] == 0) {
matrizJogo[linhaJogador2 - 1][colunaJogador2 - 1] = 1;
resposta = verificaGanhador2(matrizJogo);
if (resposta) {
System.out.println(nomeJogador2 + ", você é o grande ganhador deste Jogo.");
System.exit(0);
}
} else { // EVITANDO PREENCHIMENTO EM POSIÇÃO JÁ PREENCHIDA
do {
System.out.println("Essa posição já está marcada. Tente outra posição.");
do {
System.out.println("Opção(ões) inválida(s). Tente novamente.");
System.out.println("\t" + nomeJogador2 + ", em que posição "
+ "você irá marcar o \"X\"?: ");
System.out.println("Linha: ");
linhaJogador2 = scan.nextInt();
System.out.println("Coluna: ");
colunaJogador2 = scan.nextInt();
} while ((linhaJogador2 < 1 || linhaJogador2 > 3) || (colunaJogador2 < 1 || colunaJogador2 > 3));
} while (matrizJogo[linhaJogador2 - 1][colunaJogador2 - 1] != 0);
matrizJogo[linhaJogador2 - 1][colunaJogador2 - 1] = 1;
resposta = verificaGanhador2(matrizJogo);
if (resposta) {
System.out.println(nomeJogador2 + ", você é o grande ganhador deste Jogo.");
System.exit(0);
}
}
}
}