-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTicTacToe.java
More file actions
458 lines (408 loc) · 17.2 KB
/
TicTacToe.java
File metadata and controls
458 lines (408 loc) · 17.2 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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
package com.tictactoe;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TicTacToe implements Runnable {
private Socket socket;
private DataOutputStream dos;
private DataInputStream dis;
private Painter painter_user;
private String ip;
private int port;
private Scanner scanner = new Scanner(System.in);
private JFrame frame;
private final int WIDTH = 506;
private final int HEIGHT = 527;
private Thread thread;
private ServerSocket serverSocket;
private BufferedImage board;
private BufferedImage redX;
private BufferedImage blueX;
private BufferedImage redCircle;
private BufferedImage blueCircle;
private void Carregar_Imagens() {
try {
board = ImageIO.read(getClass().getResourceAsStream("/board.png"));
blueX = ImageIO.read(getClass().getResourceAsStream("/blueX.png"));
blueCircle = ImageIO.read(getClass().getResourceAsStream("/blueCircle.png"));
redX = ImageIO.read(getClass().getResourceAsStream("/redX.png"));
redCircle = ImageIO.read(getClass().getResourceAsStream("/redCircle.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
private String[] spaces = new String[9];
private int MaxNumeroPortas = 65535;
private int MinNumeroDePortas = 0;
private boolean turno = false;
private boolean adversary = true;
private boolean accepted = false;
private boolean Impossivel_Conexao_Adversario = false;
private boolean VITORIA = false;
private boolean InimigoGanhou = false;
private boolean EMPATE = false;
private void Frame_Generator_Method_JFrame(){
frame = new JFrame();
frame.setTitle("Tic-Tac-Toe");
frame.setContentPane(painter_user);
frame.setSize(WIDTH, HEIGHT);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.setVisible(true);
}
private void Draw_Text_Function(Graphics valor, Font valor_fonte,String String_Mensagem){
valor.setColor(Color.RED);
valor.setFont(valor_fonte);
Graphics2D Graficos_2d = (Graphics2D) valor;
Graficos_2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int TamanoDaString = Graficos_2d.getFontMetrics().stringWidth(String_Mensagem);
valor.drawString(String_Mensagem, WIDTH / 2 - TamanoDaString / 2, HEIGHT / 2);
return;
}
private void DesenharLinha(int PosicaoX,int PosicaoY,int PosicaoX2,int PosicaoY2,Graphics Grafico){
if(PosicaoX < 0 || PosicaoX > 500){
Scanner variavel = new Scanner(System.in);
int posicao;
System.out.println("Valor da Primeira Posicao De X Fora da Tela, Inserir Novamente");
posicao = scanner.nextInt();
DesenharLinha(posicao, PosicaoY, PosicaoX2, PosicaoY2,Grafico);
}
else if(PosicaoY < 0 || PosicaoY > 500){
Scanner variavel = new Scanner(System.in);
int posicao;
System.out.println("A Altura do Valor de Y esta fora da tela, por favor re-digitar ");
posicao = scanner.nextInt();
DesenharLinha(PosicaoX, posicao, PosicaoX2, PosicaoY2,Grafico);
}
else if(PosicaoX2 < 0 || PosicaoX2 > 500){
Scanner variavel = new Scanner(System.in);
int posicao;
System.out.println("Valor da Segunda Posicao de X Fora da Tela, Inserir Novamente");
posicao = scanner.nextInt();
DesenharLinha(PosicaoX, PosicaoY, posicao, PosicaoY2,Grafico);
}
else if(PosicaoY2 < 0 || PosicaoY2 > 500){
Scanner variavel = new Scanner(System.in);
int posicao;
System.out.println("A Altura do Valor de Y2 esta fora da tela, por favor re-digitar ");
posicao = scanner.nextInt();
DesenharLinha(PosicaoX, PosicaoY, PosicaoX2, posicao,Grafico);
}
Graphics2D Grafico_reta = (Graphics2D) Grafico;
Grafico_reta.setStroke(new BasicStroke(15));
Grafico.setColor(Color.BLACK);
Grafico.drawLine(Calculo_Pontos_Da_RetaXX, Calculo_Pontos_Da_RetaXY, Calculo_Pontos_Da_RetaX2X, Calculo_Pontos_Da_RetaX2Y);
}
private int tamanhoDosEspacos = 160;
private int errors = 0;
private int primeiro_lugar = -1;
private int segundo_lugar = -1;
private Font font = new Font("Times New Roman", Font.BOLD, 32);
private Font smallerFont = new Font("Times New Roman", Font.BOLD, 20);
private Font largerFont = new Font("Times New Roman", Font.BOLD, 50);
private String StringDeEspera = "Esperando Jogador";
private String unableToCommunicateWithOpponentString = "Incp de se comunicar com adversario.";
private String StringDeVitoria = "Voce Ganhou!";
private String InimigoGanhouString = "Oponente Ganhou!";
private String StringDeEmpate = "Jogo termina em empate.";
private int Calculo_Pontos_Da_RetaXX = (primeiro_lugar % 3 * tamanhoDosEspacos + 10 * primeiro_lugar % 3 + tamanhoDosEspacos / 2);
private int Calculo_Pontos_Da_RetaXY = ((int) (primeiro_lugar / 3) * tamanhoDosEspacos + 10 * (int) (primeiro_lugar / 3) + tamanhoDosEspacos / 2);
private int Calculo_Pontos_Da_RetaX2X = (segundo_lugar % 3 * tamanhoDosEspacos + 10 * segundo_lugar % 3 + tamanhoDosEspacos / 2);
private int Calculo_Pontos_Da_RetaX2Y = ((int) (segundo_lugar / 3) * tamanhoDosEspacos + 10 * (int) (segundo_lugar / 3) + tamanhoDosEspacos / 2);
private int[][] Combinacoes_Vitoria = new int[][] { { 0, 1, 2 }, { 3, 4, 5 }, { 6, 7, 8 }, { 0, 3, 6 }, { 1, 4, 7 }, { 2, 5, 8 }, { 0, 4, 8 }, { 2, 4, 6 } };
private void Iniciar_Servidor() {
try {
serverSocket = new ServerSocket(port, 8, InetAddress.getByName(ip));
} catch (Exception e) {
e.printStackTrace();
}
turno = true;
adversary = false;
}
public TicTacToe() {
System.out.println("Por Favor Insira o IP: ");
ip = scanner.nextLine();
System.out.println("Por Favor Insira a Porta: ");
port = scanner.nextInt();
boolean Resposta = ((port < MinNumeroDePortas || port > MaxNumeroPortas) == true) ? true : false;
while (Resposta) {
System.out.println("A porta selecionada e invalida");
System.out.println("Por favor Digite um numero de Portas Validas, valores entre 1 e 65535: ");
port = scanner.nextInt();
Resposta = ((port < MinNumeroDePortas || port > MaxNumeroPortas) == true) ? true : false;
}
Carregar_Imagens();
painter_user = new Painter();
painter_user.setPreferredSize(new Dimension(WIDTH, HEIGHT));
if (!Connection()){
Iniciar_Servidor();
}
Frame_Generator_Method_JFrame();
thread = new Thread(this, "TicTacToe");
thread.start();
}
@SuppressWarnings("unused")
public static void main(String[] args) {
TicTacToe TicTac = new TicTacToe();
}
public void run() {
while (true) {
ciclick();
painter_user.repaint();
if (!adversary && !accepted) {
ListenToServer();
}
}
}
private void Renderizador_Quadros(Graphics g) {
g.drawImage(board, 0, 0, null);
if (Impossivel_Conexao_Adversario) {
Draw_Text_Function(g,new Font("Times New Roman", Font.BOLD, 20), unableToCommunicateWithOpponentString);
}
if (accepted) {
for (int i = 0; i < spaces.length; i++) {
int calculo_valor_x = (i % 3) * (160) + 10 * (i % 3);
int calculo_valor_y = (int) (i / 3) * 160 + 10 * (int) (i / 3);
if (spaces[i] != null) {
if (spaces[i].equals("X")) {
if (adversary) {
g.drawImage(redX, calculo_valor_x,calculo_valor_y, null);
} else {
g.drawImage(blueX,calculo_valor_x,calculo_valor_y, null);
}
} else if (spaces[i].equals("O")) {
if (adversary) {
g.drawImage(blueCircle,calculo_valor_x,calculo_valor_y, null);
} else {
g.drawImage(redCircle,calculo_valor_x,calculo_valor_y, null);
}
}
}
}
if (VITORIA || InimigoGanhou) {
Graphics2D g2 = (Graphics2D) g;
g2.setStroke(new BasicStroke(10));
g.setColor(Color.BLACK);
g.drawLine(primeiro_lugar % 3 * tamanhoDosEspacos + 10 * primeiro_lugar % 3 + tamanhoDosEspacos / 2, (int) (primeiro_lugar / 3) * tamanhoDosEspacos + 10 * (int) (primeiro_lugar / 3) + tamanhoDosEspacos / 2, segundo_lugar % 3 * tamanhoDosEspacos + 10 * segundo_lugar % 3 + tamanhoDosEspacos / 2, (int) (segundo_lugar / 3) * tamanhoDosEspacos + 10 * (int) (segundo_lugar / 3) + tamanhoDosEspacos / 2);
g.setColor(Color.RED);
g.setFont(largerFont);
if (VITORIA) {
int stringWidth = g2.getFontMetrics().stringWidth(StringDeVitoria);
g.drawString(StringDeVitoria, WIDTH / 2 - stringWidth / 2, HEIGHT / 2);
} else if (InimigoGanhou) {
int stringWidth = g2.getFontMetrics().stringWidth(InimigoGanhouString);
g.drawString(InimigoGanhouString, WIDTH / 2 - stringWidth / 2, HEIGHT / 2);
}
}
if (EMPATE) {
Graphics2D g2 = (Graphics2D) g;
g.setColor(Color.BLACK);
g.setFont(largerFont);
int stringWidth = g2.getFontMetrics().stringWidth(StringDeEmpate);
g.drawString(StringDeEmpate, WIDTH / 2 - stringWidth / 2, HEIGHT / 2);
}
} else {
g.setColor(Color.RED);
g.setFont(new Font("Times New Roman", Font.BOLD, 32));
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
int stringWidth = g2.getFontMetrics().stringWidth("Esperando Jogador");
g.drawString("Esperando Jogador", WIDTH / 2 - stringWidth / 2, HEIGHT / 2);
}
}
private void ciclick() {
if (!turno && !Impossivel_Conexao_Adversario) {
try {
int space = dis.readInt();
if (adversary){
spaces[space] = "X";
}
else{
spaces[space] = "O";
}
CheckVitoriaAdversario();
ChecarEmpate();
turno = true;
} catch (IOException e) {
e.printStackTrace();
errors++;
}
}
if (errors >= 10){
Impossivel_Conexao_Adversario = true;
}
}
private void ChecarVitoria() {
for (int i = 0; i < Combinacoes_Vitoria.length; i++) {
if (adversary) {
if (spaces[Combinacoes_Vitoria[i][0]] == "O" && spaces[Combinacoes_Vitoria[i][1]] == "O" && spaces[Combinacoes_Vitoria[i][2]] == "O") {
primeiro_lugar = Combinacoes_Vitoria[i][0];
segundo_lugar = Combinacoes_Vitoria[i][2];
VITORIA = true;
}
} else {
if (spaces[Combinacoes_Vitoria[i][0]] == "X" && spaces[Combinacoes_Vitoria[i][1]] == "X" && spaces[Combinacoes_Vitoria[i][2]] == "X") {
primeiro_lugar = Combinacoes_Vitoria[i][0];
segundo_lugar = Combinacoes_Vitoria[i][2];
VITORIA = true;
}
}
}
}
private void CheckVitoriaAdversario() {
for (int i = 0; i < Combinacoes_Vitoria.length; i++) {
if (adversary) {
if (spaces[Combinacoes_Vitoria[i][0]] == "X" && spaces[Combinacoes_Vitoria[i][1]] == "X" && spaces[Combinacoes_Vitoria[i][2]] == "X") {
primeiro_lugar = Combinacoes_Vitoria[i][0];
segundo_lugar = Combinacoes_Vitoria[i][2];
InimigoGanhou = true;
}
} else {
if (spaces[Combinacoes_Vitoria[i][0]] == "O" && spaces[Combinacoes_Vitoria[i][1]] == "O" && spaces[Combinacoes_Vitoria[i][2]] == "O") {
primeiro_lugar = Combinacoes_Vitoria[i][0];
segundo_lugar = Combinacoes_Vitoria[i][2];
InimigoGanhou = true;
}
}
}
}
private void ChecarEmpate() {
for (int i = 0; i < spaces.length; i++) {
if (spaces[i] == null) {
return;
}
}
EMPATE = true;
}
private void ListenToServer() {
Socket socket = null;
try {
socket = serverSocket.accept();
try{
dos = new DataOutputStream(socket.getOutputStream());
}
catch (IOException e){
System.out.println("Erro com o OutPut do LitenToServer");
System.out.println(e.toString());
}
try{
dis = new DataInputStream(socket.getInputStream());
}
catch (IOException e){
System.out.println("Erro com o InPut do LitenToServer");
System.out.println(e.toString());
}
accepted = true;
System.out.println("CLIENTE FEZ SOLICITACAO PARA SE JUNTAR, SOLICITACAO ACEITA");
} catch (IOException e) {
e.printStackTrace();
}
}
private boolean Connection() {
try {
socket = new Socket(ip, port);
try{
dos = new DataOutputStream(socket.getOutputStream());
}
catch(IOException e){
System.out.println("Erro Com o OutPut do Connection");
System.out.println(e.toString());
}
try{
dis = new DataInputStream(socket.getInputStream());
}
catch (IOException e){
System.out.println("Erro Com o Input do Connection");
System.out.println(e.toString());
}
accepted = true;
} catch (IOException e) {
System.out.println("Incapas de se conectar ao servidor no endereco: " + ip + ":" + port + " | Iniciando o Servidor");
return false;
}
System.out.println("Conectado Ao Server Com Sucesso.");
return true;
}
private class Painter extends JPanel implements MouseListener {
private static final long serialVersionUID = 1L;
public Painter() {
setFocusable(true);
requestFocus();
setBackground(Color.WHITE);
addMouseListener(this);
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Renderizador_Quadros(g);
}
@Override
public void mouseClicked(MouseEvent e) {
if (accepted) {
if (turno) {
boolean variavel = !Impossivel_Conexao_Adversario;
boolean Conclusao = (!VITORIA && !InimigoGanhou);
if (Conclusao == true) {
int new_Variable = (variavel && Conclusao) == true ? 1 : 0;
if (variavel == true && new_Variable == 1) {
int x = e.getX() / tamanhoDosEspacos;
int y = e.getY() / tamanhoDosEspacos;
y *= 3;
int position = x + y;
if (spaces[position] == null) {
if (!adversary) {
spaces[position] = "X";
} else {
spaces[position] = "O";
}
turno = false;
repaint();
Toolkit.getDefaultToolkit().sync();
try {
dos.writeInt(position);
dos.flush();
} catch (IOException e1) {
errors++;
e1.printStackTrace();
}
ChecarVitoria();
ChecarEmpate();
}
}
}
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
}
}