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
| import java.util.Random;
public class Code {
private long id =-1;
private byte[] n = {-1, -1, -1, -1, -1,
-1, -1, -1, -1, -1};
public byte[]getN(){
return n;
}
public static void main(String[] args) {
Code myCode= new Code(15);
Code myCode2= new Code(15);
Code myCode3= new Code(16);
System.out.println(myCode.equals(myCode2));
System.out.println(myCode.equals(myCode3));
System.out.println(myCode+" -- "+myCode2+" -- "+myCode3);
//Génération de l'intégralité des Code
// Algo de génération Non optimisé et nombre de code à généré énorme.
// Prends plusieurs minutes pour 125560
Code[] mesCodes = new Code[3628800];
int index =0;
int id=-1;
while(index<3628800){
id++;
Code newOne =new Code(id);
if(!isIn(newOne,mesCodes,index)){
mesCodes[index]=newOne;
index++;
}
System.out.println("code found : "+index+" current id : "+id);
}
}
private static boolean isIn(Code newOne, Code[] mesCodes, int index) {
if(index>0){
for (int i = 0; i < index; i++) {
if(mesCodes[i].equals(newOne)){
return true;
}
}
}
return false;
}
public Code(long id) {
this.id=id;
int value;
Random key = new Random();
key.setSeed(id);
for (int i = 0; i < n.length; i++) {
while(this.n[i]==-1){
value =key.nextInt(10);
if(!this.isTaken(value)){
this.n[i]=(byte)value;
}
}
}
}
public boolean isTaken(int value){
if( n[0]==value || n[1]==value || n[2]==value || n[3]==value || n[4]==value
|| n[5]==value || n[6]==value || n[7]==value || n[8]==value || n[9]==value){
return true;
}
return false;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append(n[0]);
sb.append(n[1]);
sb.append(n[2]);
sb.append(n[3]);
sb.append(n[4]);
sb.append(n[5]);
sb.append(n[6]);
sb.append(n[7]);
sb.append(n[8]);
sb.append(n[9]);
return sb.toString();
}
@Override
public boolean equals(Object other) {
if(other instanceof Code){
Code otherCode =(Code) other;
for (int i = 0; i < this.n.length; i++) {
if(this.n[i]!=otherCode.getN()[i]){
return false;
}
}
return true;
}
return false;
}
} |
Partager