package auto;
public class Point24Compare
{
public static int Max = 13;
public static int Min = 1;
public static void main(String[] args)
{
int[] pokers = {0,0,0,0};
for(int i=0; i<4; i++)
{
pokers[i] = (int)Math.round(Math.random()*(Max-Min)+Min);
}
System.out.println("Select any 4 cards: "+pokers[0]+','+pokers[1]+','+pokers[2]+','+pokers[3]);
Calculate24Point(pokers);
}
public static boolean Calculate24Point(int [] pokers)
{
float tmpF = 0;
int[] operator = {0,0,0};
int[] index = {0,1,2,3};
int indexMax = 3;
int indexMin = 0;
int tmpI = 0;
boolean canSet = false;
boolean OK = false;
for(int count=0; count<10000000; count++)
{
for(int i=0; i<4; i++)
{
if(0 == i)
{
index[0] = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
}
else
{
while(true)
{
tmpI = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
for(int j=0; j<i; j++)
{
if(tmpI != index[j])
{
canSet = true;
}
else
{
canSet = false;
break;
}
}
if(canSet)
{
index[i] = tmpI;
break;
}
}
}
}
for(int i=0; i<3; i++)
{
operator[i] = (int)Math.round(Math.random()*(indexMax-indexMin)+indexMin);
}
OK = Calc24Point(pokers, index, operator);
if(OK)
{
break;
}
}
if(!OK)
{
System.out.println("These cards can not calculate the 24");
}
return OK;
}
public static boolean Calc24Point(int[] pokers, int[] index, int[] operator)
{
float tmpF = 0;
switch(operator[0])
{
case 0:
tmpF = ((float)pokers[index[0]]) + pokers[index[1]];
break;
case 1:
tmpF = ((float)pokers[index[0]]) - pokers[index[1]];
break;
case 2:
tmpF = ((float)pokers[index[0]]) * pokers[index[1]];
break;
case 3:
tmpF = ((float)pokers[index[0]]) / pokers[index[1]];
break;
default:
return false;
}
for(int i=1; i<operator.length; i++)
{
switch(operator[i])
{
case 0:
tmpF = tmpF + pokers[index[i+1]];
break;
case 1:
tmpF = tmpF - pokers[index[i+1]];
break;
case 2:
tmpF = tmpF * pokers[index[i+1]];
break;
case 3:
tmpF = tmpF / pokers[index[i+1]];
break;
default:
return false;
}
}
if(tmpF > 23.9999 && tmpF <24.0001)
{
char []oper = new char[operator.length];
for(int i=0; i<operator.length; i++)
{
switch(operator[i])
{
case 0:
oper[i] = '+';
break;
case 1:
oper[i] = '-';
break;
case 2:
oper[i] = '*';
break;
case 3:
oper[i] = '/';
break;
default:
return false;
}
}
System.out.println("These cards can calculate the 24: (("+String.valueOf(pokers[index[0]])+oper[0]+String.valueOf(pokers[index[1]])+')'+oper[1]+String.valueOf(pokers[index[2]])+')'+oper[2]+String.valueOf(pokers[index[3]]));
return true;
}
return false;
}
}