עמודים לתרגול למבחן
עמודים 125,135,146,157,166,186,199
פתרון דומינו
public static int getNumOfDomino(){
int num;
System.out.println("Please insert number of domino parts");
num = reader.nextInt();
return num;
}
public static int getDominoRandNumber(){
return (int)(Math.random()*7);
}
public static boolean isDominoChain(int d1,int d2,int d3,int d4){
if (d1 == d3 || d1 == d4)
return true;
if (d2 == d3 || d2 == d4)
return true;
return false;
}
public static int getLeftDominoChain(int d1,int d2,int d3,int d4){
if (d2 == d3){
return d1;
}
else{
if (d2 == d4)
return d1;
return d2;
}
}
public static int getRightDominoChain(int d1,int d2,int d3,int d4){
if (d2 == d3){
return d4;
}
else{
if (d1 == d3)
return d4;
return d3;
}
}
public static void main(String[] args) {
int domino1,domino2,domino3,domino4,count=0,num,d1,d2;
num = getNumOfDomino();
domino1 = getDominoRandNumber();
domino2 = getDominoRandNumber();
System.out.println("Domino #1: ("+domino1+","+domino2+")");
count++;
for (int i=1;i<num;i++){
domino3 = getDominoRandNumber();
domino4 = getDominoRandNumber();
System.out.print("Domino #"+(i+1)+": ("+domino3+","+domino4+")");
if (isDominoChain(domino1,domino2,domino3,domino4)){
count++;
d1 = getLeftDominoChain(domino1,domino2,domino3,domino4);
d2 = getRightDominoChain(domino1,domino2,domino3,domino4);
domino1 = d1;
domino2 = d2;
System.out.println(" In chain");
}
else{
System.out.println(" Not in chain");
}
}
System.out.println("Total count is : "+count);
}