永发信息网

java程序编写小游戏 要求:程序随机产生20—50根火柴

答案:1  悬赏:10  手机版
解决时间 2021-12-04 04:08
java程序编写小游戏 要求:程序随机产生20—50根火柴
最佳答案
为什么用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都不怎么会
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
上图中涂色部分的面积占正方形面积的8/17
为什么那么多玩家,解说都喜欢Uzi
C语言对于顺序表的存储结构定义如何理解,为什
ss1卡雷拉轨道赛车馆地址有知道的么?有点事
做AB梁的剪力和弯矩图
从灰瑶村到长宁多少公里
阳历1982年1月2日上升星座
2017年辽宁理科考生军检合格分数629分能上国
单选题印度获得独立的时间是A.1945年B.1946年
宝莲灯孙悟空说他还不知道沉香究竟是他的师侄
为什么仓鼠以前让我摸,现在他不让了
为什么要选择青沫茶颜来投资 高颜值茶饮总不
拳霸泰拳馆(杨浦馆)地址好找么,我有些事要过
老黄历是按阳历还是农历算的
恶战中那个唱歌的舞女叫什么啊?
推荐资讯
想和京东淘宝开个快递代收点代收一个怎么收费
从铁西区荣富饭店到大东区好妈王饭店坐公交车
骑电动车超载加醉驾派出所说要行政拘留请问合
过滤型饮水机从装水桶往溢水咋回事
午睡的时候,有时睡到一半突然要醒了,快醒来
(1)如图1所示,体温表温的示数为______℃.
一个数的十位、十分位和千分位上都是8,其它
谁知道“杀父娶母”的故事啊
我恨你,女生说的啥意思
得胜派出所地址在什么地方?想过去办事
环境影响评价最早是那个国家提出的?
Itwasnotuntil1999()theirsovereignrightsove
正方形一边上任一点到这个正方形两条对角线的
阴历怎么看 ?