算法思想很簡單
是取勝原理
用反推法:欲拿最後壹根,必須留2根在那裏,欲留2根,必須上壹輪留2+3+1=6給對方,(它拿壹,妳拿三,它拿二,妳拿二,它拿三,妳拿壹。都是留2根)。再向上壹輪,就是6+4=10。
取勝原理:把隨機產生的火柴數,分解成:2+4的n次方+m,(m≤3),當m=0的時候,後取者勝,當m=1、2、3的時候,先取者勝。先取者取完m,留2+4的n次方給對方,對方不管取多少,妳取的數和對方相加等於4,壹直到最後,留2根給對方,根據規則,對方只能取壹根,留壹根給妳取勝。
另:取完者勝(含最後壹根):最後留4根給對方,不管對方取多少,妳都可以壹次取完。上壹輪同樣加4。
取勝原理:把隨機產生的火柴數,分解成:4的n次方+m,(m≤3),當m=0的時候,後取者勝,當m=1、2、3的時候,先取者勝。先取者取完m,留4的n次方給對方,對方不管取多少,妳取的數和對方相加等於4,壹直到最後,留4根給對方。
代碼調試可用
自己用GUI搭個界面 二十分鐘的事
import java.util.Scanner;
public class MatchGame {
private static Scanner scanner = new Scanner(System.in);;
private int total;
private Computer com;
private static int exit = 1;
public MatchGame(int from, int to, String level) {
if (from >= to) {
throw new IllegalArgumentException();
}
total = (int)( Math.random() * (to - from)) + from;
com = new Computer(level);
}
public void start() {
System.out.println("0 means endGame, 4 means restartGame!");
System.out.println("The number of matches is " + total);
System.out.println("~Start~");
System.out.println("----------------------------------------");
while (true) {
int u = scanner.nextInt();
if (u > 0 && u < 4) {
System.out.println("You entered " + u);
if (total - u <= 0) {
exit = 2;
endGame();
}
total = total - u;
System.out.println("Total : " + total);
int c = com.play(u, total);
System.out.println("Computer selected " + c + " matches~");
if (total - c <= 0) {
exit = 0;
endGame();
}
total = total - c;
System.out.println("Total : " + total);
}else if (u == 0) {
endGame();
}else if (u > 4 || u < 0) {
System.out.println("You entered Wrong number~");
} else {
restart();
}
}
}
public static void restart() {
MatchGame game;
System.out
.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");
int level = scanner.nextInt();
if (level == 1) {
game = new MatchGame(20, 50, Computer.HARD);
} else if (level == 2) {
game = new MatchGame(20, 50, Computer.NORMAL);
} else {
game = new MatchGame(20, 50, Computer.EASY);
}
game.start();
}
public static void endGame() {
if (exit == 0) {
System.out.println("YOU WIN!!!");
} else if (exit == 2) {
System.out.println("YOU LOSE!!!");
}
System.exit(exit);
}
public static class Computer {
private static String EASY = "EASY";
private static String NORMAL = "NORMAL";
private static String HARD = "HARD";
private static String LEVEL;
private int com;
public Computer(String level) {
LEVEL = level;
}
public int play(int user, int total) {
if (LEVEL.equals(EASY)) {
com = 1;
} else if (LEVEL.equals(NORMAL)) {
com = (int) (Math.random() * 2) + 1;
} else {
int i;
if (total % 4 == 0) {
i = total / 4 - 1;
} else {
i = total / 4;
}
com = total - 4 * i - 1;
if (com == 0) {
com = 4 - user;
}
}
return com;
}
}
public static void main(String[] args) {
MatchGame game;
System.out
.println("Please select Computer Level: 1:HARD 2:NORMAL 3:EASY");
int level = scanner.nextInt();
if (level == 1) {
game = new MatchGame(20, 50, Computer.HARD);
} else if (level == 2) {
game = new MatchGame(20, 50, Computer.NORMAL);
} else {
game = new MatchGame(20, 50, Computer.EASY);
}
game.start();
}
}