该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
//stone1.setEditable(false);
//stone2.setEditable(false);
label1.setText(numToStr(gStone1 - a));
label2.setText(numToStr(gStone2 - b));
setTitle("计算机正在计算...");
if(m.calculate(gStone1 - a, gStone2 - b)){//判断是不是已经到0了
//delay(20000);
try{
Thread.sleep(1000);
}
catch(InterruptedException ie){
}
gStone1 = m.getgS1();
gStone2 = m.getgS2();
if(gStone1 == 0 && gStone2 == 0)
JOptionPane.showConfirmDialog(this, "YOU LOSE!!");
else{
setTitle("轮到你拉!");
}
label1.setText(numToStr(gStone1));
label2.setText(numToStr(gStone2));
//stone1.setEditable(true);
//stone2.setEditable(true);
}
else{
JOptionPane.showConfirmDialog(this, "YOU WIN!!");
}
}
else {
JOptionPane.showConfirmDialog(this, "Not Right!!");
}
}
repaint();
}
}
class Algorithm{
int [][]ans = new int [26][2];
int gS1, gS2;
public int getgS1(){
return gS1;
}
public int getgS2()
{
return gS2;
}
int getSmall(int n, boolean arr[]){
for (int i = 1; i <= 50; i++){
if(!arr[i])
return i;
}
return 50;
}
public Algorithm(){
ans[0][0] = 0;
ans[0][1] = 0;
ans[1][0] = 1;
ans[1][1] = 2;
boolean[] arr = new boolean[52];
for(int i = 1; i <= 50; i++)
arr[i] = false;
arr[1] = arr[2] = true;
for (int i = 2; i
ans[i][0] = getSmall(i, arr);
ans[i][1] = ans[i][0] + i;
arr[ans[i][0]] = true;
arr[ans[i][1]] = true;
}
/*for (int i = 1; i
System.out.print(ans[i][0]);
System.out.print(' ');
System.out.println(ans[i][1]);
}*/
}
boolean isFail(int a, int b){
for (int i = 0; i
if(a == ans[i][0] && b == ans[i][1])
return true;
else if(a == ans[i][1] && b == ans[i][0])
return true;
}
return false;
}
void changStone(int a, int b)
{
for (int i = 0; i
if((a - ans[i][0]) == (b - ans[i][1])){
gS1 = ans[i][0];
gS2 = ans[i][1];
return;
}
else if((a - ans[i][1]) == (b - ans[i][0])){
gS1 = ans[i][1];
gS2 = ans[i][0];
return;
}
else if(a == ans[i][0]){
gS1 = ans[i][0];
gS2 = ans[i][1];
return;
}
else if(a == ans[i][1]){
gS1 = ans[i][1];
gS2 = ans[i][0];
return;
}
else if(b == ans[i][1]){
gS1 = ans[i][0];
gS2 = ans[i][1];
return;
}
else if(b == ans[i][0]){
gS1 = ans[i][1];
gS2 = ans[i][0];
return;
}
}
}
public boolean calculate(int a, int b){//从a, b状态取得最佳状态。
if(isFail(a, b)){//在必败状态
if(a == 0 && b == 0)
return false;
if(a > 0)
gS1 = a - 1;
else if (b > 0)
gS2 = b - 1;
}
else{//在必胜状态,将gS1,gS2变成必败状态
changStone(a, b);
}
return true;
}
}