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
| package org.k.developpez.forum;
import java.util.Arrays;
import java.util.Random;
public class Verificateur {
enum Direction {
NORD, SUD, EST, OUEST, NORD_EST, NORD_OUEST, SUD_EST, SUD_OUEST
};
enum Type {
X, O
};
Type tab[][] = new Type[7][7];
public boolean verification(int posNS, int posEO) {
int total = 0;
int LIMIT = 4;
// Protection
if (posNS < 0 || posNS >= tab.length) {
return false;
}
if (posEO < 0 || posEO >= tab[posNS].length) {
return false;
}
Type typeVoulu = tab[posNS][posEO];
// Vérification verticale :
total = nombrePion(Direction.NORD, typeVoulu, posNS, posEO) + 1 + nombrePion(Direction.SUD, typeVoulu, posNS, posEO);
System.out.println("nombre de pion vers le NORD "+nombrePion(Direction.NORD, typeVoulu, posNS, posEO));
System.out.println("nombre de pion vers le SUD "+nombrePion(Direction.SUD, typeVoulu, posNS, posEO));
System.out.println("Total en N-S est " +total);
if (total >= LIMIT) {
return true;
}
// Vérification horizontale :
total = nombrePion(Direction.OUEST, typeVoulu, posNS, posEO) + 1 + nombrePion(Direction.EST, typeVoulu, posNS, posEO);
System.out.println("nombre de pion vers le OUEST "+nombrePion(Direction.OUEST, typeVoulu, posNS, posEO));
System.out.println("nombre de pion vers le EST "+nombrePion(Direction.EST, typeVoulu, posNS, posEO));
System.out.println("Total en O-E est " +total);
if (total >= LIMIT) {
return true;
}
// Vérification diagonale 1 :
total = nombrePion(Direction.NORD_OUEST, typeVoulu, posNS, posEO) + 1 + nombrePion(Direction.SUD_EST, typeVoulu, posNS, posEO);
System.out.println("nombre de pion vers le NORD_OUEST "+nombrePion(Direction.NORD_OUEST, typeVoulu, posNS, posEO));
System.out.println("nombre de pion vers le SUD_EST "+nombrePion(Direction.SUD_EST, typeVoulu, posNS, posEO));
System.out.println("Total en NO-SE est " +total);
if (total >= LIMIT) {
return true;
}
// Vérification diagonale 2 :
total = nombrePion(Direction.SUD_OUEST, typeVoulu, posNS, posEO) + 1 + nombrePion(Direction.NORD_EST, typeVoulu, posNS, posEO);
System.out.println("nombre de pion vers le SUD_OUEST "+nombrePion(Direction.SUD_OUEST, typeVoulu, posNS, posEO));
System.out.println("nombre de pion vers le NORD_EST "+nombrePion(Direction.NORD_EST, typeVoulu, posNS, posEO));
System.out.println("Total en SO-NE est " +total);
if (total >= LIMIT) {
return true;
}
return false;
}
public int nombrePion(Direction direction, Type typeVoulu, int posNSOrigin, int posEOOrigin) {
// Reprise des coordonées.
int posNS = deplacementNS(direction, posNSOrigin);
int posEO = deplacementEO(direction, posEOOrigin);
// Protection
if (posNS < 0 || posNS >= tab.length) {
return 0;
}
if (posEO < 0 || posEO >= tab[posNS].length) {
return 0;
}
if (tab[posNS][posEO] == typeVoulu) {
return 1 + nombrePion(direction, typeVoulu, posNS, posEO);
}
return 0;
}
private int deplacementNS(Direction direction, int posNSOrigin) {
int posNS = posNSOrigin;
// Déplacement en NS
switch (direction) {
case NORD:
case NORD_EST:
case NORD_OUEST:
posNS--;
break;
case SUD:
case SUD_EST:
case SUD_OUEST:
posNS++;
break;
default:
break;
}
return posNS;
}
private int deplacementEO(Direction direction, int postEOOrigin) {
int posEO = postEOOrigin;
// Déplacement en EO
switch (direction) {
case OUEST:
case SUD_OUEST:
case NORD_OUEST:
posEO--;
break;
case EST:
case SUD_EST:
case NORD_EST:
posEO--;
break;
default:
break;
}
return posEO;
}
/**
* Code pas beau fait à l'arrache pour avoir une entrée pour la fonction verification
* @param args
*/
public static void main(String[] args) {
Random rn = new Random();
Verificateur verif = new Verificateur();
for (int i = 0; i < verif.tab.length; i++) {
for (int j = 0; j < verif.tab[i].length; j++) {
switch (rn.nextInt(3)) {
case 0:
// on laisse à null
break;
case 1:
verif.tab[i][j] = Type.X;
break;
case 2:
verif.tab[i][j] = Type.O;
break;
default:
break;
}
}
}
System.out.println(Arrays.deepToString(verif.tab));
System.out.println("A-t-on gagné en 0,0 => "+verif.verification(0, 0));
System.out.println("A-t-on gagné en 3,2 => "+verif.verification(3, 2));
System.out.println("A-t-on gagné en 9,9 => "+verif.verification(9, 9));
}
} |
Partager