Newer
Older
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
package puissance4.controleur;
import java.util.Arrays;
/**
* Détection d'un alignement de booléens dans un plateau de type Puissance4
*
*/
public class FullRowDetection {
/**
* Détecte 4 'true' ou 4 'false' alignés dans une des 4 directions |, _, /, \.
*
* Si un élément a la valeur null, tout ce qui est au-dessus est sensé être null.
* @param board : un tableau de valeurs de type Boolean ou null
* @return les coordonnées des deux extrémités du premier alignement trouvé.
*/
public int[] detect( Boolean[][] board ) {
int l = 4;
int ll = l - 1;
int width = board.length;
int height = board[0].length;
// South - North
int h = height - ll;
for ( int col = 0; col < width; col++ )
for ( int row = 0; row < l; row++ ) {
Boolean b = board[col][row];
if ( b == null)
break;
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col][row + i] == b );
if ( success )
return new int[] { col, row, col, row + ll };
}
// West - East
int w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = 0; row < height; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col + i][row] == b );
if ( success )
return new int[] { col, row, col + ll, row };
}
}
// SouthWest - NorthEast
h = height - ll;
w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = 0; row < h; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= ( board[col + i][row + i] == b );
if ( success )
return new int[] { col, row, col + ll, row + ll };
}
}
// NorthWest - SouthEast
w = width - ll;
for ( int col = 0; col < w; col++ )
for ( int row = ll; row < height; row++ ) {
Boolean b = board[col][row];
if ( b != null ) {
boolean success = true;
for ( int i = 1; i < l && success; i++ )
success &= (board[col + i][row - i ] == b);
if ( success )
return new int[] { col, row, col + ll, row - ll };
}
}
// No connection detected
return null;
}
// Test
public static void main( String[] args ) {
int width = 6, height = 5;
boolean r = true; // red
boolean y = false; // yellow
// *Représentation* du plateau de jeu
Boolean[][] tab = {
{null, null, null, null, null, null},
{ r, null, r, y, null, null},
{ r, null, y, r, r, y},
{ r, y, y, r, r, y},
{ r, y, y, r, y, r}
};
// On intervertit lignes et colonnes
Boolean[][] board = new Boolean[width][height];
for( int i = 0; i < height; i++ )
for( int j = 0; j < width; j++ )
board[j][i] = tab[height-i-1][j] ;
System.out.println( Arrays.toString(
new FullRowDetection().detect( board )
) );
}
}