java程序编写小游戏 要求:程序随机产生20—50根火柴
答案:1 悬赏:10 手机版
解决时间 2021-12-04 04:08
- 提问者网友:捧腹剧
- 2021-12-03 22:14
java程序编写小游戏 要求:程序随机产生20—50根火柴
最佳答案
- 五星知识达人网友:摆渡翁
- 2021-12-03 22:20
为什么用AWT不用 swing?
算法思想很简单
是取胜原理
用反推法:欲拿最后一根,必须留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();
}
}追问我原理知道,并且你点出了我最大的弱点,就是不会造个界面,AWT或swing都不怎么会
算法思想很简单
是取胜原理
用反推法:欲拿最后一根,必须留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();
}
}追问我原理知道,并且你点出了我最大的弱点,就是不会造个界面,AWT或swing都不怎么会
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯