-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReglas.java
More file actions
138 lines (117 loc) · 4.66 KB
/
Reglas.java
File metadata and controls
138 lines (117 loc) · 4.66 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package TresEnRayaB;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Vicky
*/
public class Reglas implements Constantes {
private char[] tableroLetras;
private int[][] tableroPos;
private int estado;
private int contCasillas;
private char ganador;
private int turno;
private boolean toca;
//private Datos datos;
public Reglas() {
this.estado = ESPERANDO;
this.tableroPos = new int[][]{{0, 1, 2}, {3, 4, 5}, {6, 7, 8}};
this.tableroLetras = new char[]{' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
this.turno = 1;
this.toca = true;
}
public int getEstado() {
return estado;
}
public void setEstado(int estado) {
this.estado = estado;
}
public int getTurno() {
return turno;
}
public void setTurno(int turno) {
this.turno = turno;
}
public char getGanador() {
return ganador;
}
public void setGanador(char ganador) {
this.ganador = ganador;
}
public char[] getTableroLetras() {
return tableroLetras;
}
public void setTableroLetras(char[] tableroLetras) {
this.tableroLetras = tableroLetras;
}
public void setToca(boolean toca) {
this.toca = toca;
}
public synchronized void compruebaJugadaSynchronized(int id, int posicion) {
while (id != turno) {
try {
wait();
} catch (InterruptedException ex) {
Logger.getLogger(Reglas.class.getName()).log(Level.SEVERE, null, ex);
}
}
while (toca) {
if (id == 1 && tableroLetras[posicion] == ' ') {
tableroLetras[posicion] = 'X';
contCasillas++;
} else if (id == 2 && tableroLetras[posicion] == ' ') {
tableroLetras[posicion] = 'O';
contCasillas++;
}
turno++;
if (turno > 2) {
turno = 1;
}
toca = false;
notify();
}
}
public boolean compruebaGanador() {
boolean gana = false;
if (turno == 1) {
if ((tableroLetras[0] == 'X' && tableroLetras[1] == 'X' && tableroLetras[2] == 'X')
|| (tableroLetras[3] == 'X' && tableroLetras[4] == 'X' && tableroLetras[5] == 'X') || (tableroLetras[6] == 'X' && tableroLetras[7] == 'X' && tableroLetras[8] == 'X')
|| (tableroLetras[0] == 'X' && tableroLetras[3] == 'X' && tableroLetras[6] == 'X') || (tableroLetras[1] == 'X' && tableroLetras[4] == 'X' && tableroLetras[7] == 'X')
|| (tableroLetras[2] == 'X' && tableroLetras[5] == 'X' && tableroLetras[8] == 'X') || (tableroLetras[0] == 'X' && tableroLetras[4] == 'X' && tableroLetras[8] == 'X')
|| (tableroLetras[6] == 'X' && tableroLetras[6] == 'X' && tableroLetras[2] == 'X')) {
gana = true;
ganador = JUGADORX;
estado = FINALIZADO;
}
} else if (turno == 2) {
if ((tableroLetras[0] == 'O' && tableroLetras[1] == 'O' && tableroLetras[2] == 'O')
|| (tableroLetras[3] == 'O' && tableroLetras[4] == 'O' && tableroLetras[5] == 'O') || (tableroLetras[6] == 'O' && tableroLetras[7] == 'O' && tableroLetras[8] == 'O')
|| (tableroLetras[0] == 'O' && tableroLetras[3] == 'O' && tableroLetras[6] == 'O') || (tableroLetras[1] == 'O' && tableroLetras[4] == 'O' && tableroLetras[7] == 'O')
|| (tableroLetras[2] == 'O' && tableroLetras[5] == 'O' && tableroLetras[8] == 'O') || (tableroLetras[0] == 'O' && tableroLetras[4] == 'O' && tableroLetras[8] == 'O')
|| (tableroLetras[6] == 'O' && tableroLetras[6] == 'O' && tableroLetras[2] == 'O')) {
gana = true;
ganador = JUGADORO;
estado = FINALIZADO;
}
}
return gana;
}
public boolean compruebaEmpate() {
boolean empate = false;
if (/*turno == 1 && */!compruebaGanador() && contCasillas == 9) {
empate = true;
estado = FINALIZADO;
/*} else if (turno == 2 && !compruebaGanador() && contCasillas == 9) {
empate = true;
estado = FINALIZADO;
}*/
}
return empate;
}
}