package day141018;
/**
*
* 21根火柴,每次选择1-4根火柴(人先取,最后保证电脑胜利)
* @author XWJ
*/
import java.util.Scanner;
public class Match21 {
public static void main(String[] args) {
Scanner N = new Scanner(System.in);
int total = 21, total2 = 21;
while (total > 1) {
System.out.print("请输入您要抽取的火柴数量:");
int hum = N.nextInt();
int com;
if (1 <= hum || hum <= 4) {
com = 5 - hum;
System.out.printf("电脑抽取的火柴数量为:%d\n", com);
total = total - com - hum;
total2 = total - hum;
System.out.printf("剩余火柴数量为:%d\n", total);
if (total2 == 1) {
System.out.printf("玩家胜利!");
} else if (total == 1) {
System.out.printf("庄家胜利!");
}
}
}
N.close();
}
}
/**
*
* 21根火柴,每次选择1-4根火柴(人先取,最后保证电脑胜利)
* @author XWJ
*/
import java.util.Scanner;
public class Match21 {
public static void main(String[] args) {
Scanner N = new Scanner(System.in);
int total = 21, total2 = 21;
while (total > 1) {
System.out.print("请输入您要抽取的火柴数量:");
int hum = N.nextInt();
int com;
if (1 <= hum || hum <= 4) {
com = 5 - hum;
System.out.printf("电脑抽取的火柴数量为:%d\n", com);
total = total - com - hum;
total2 = total - hum;
System.out.printf("剩余火柴数量为:%d\n", total);
if (total2 == 1) {
System.out.printf("玩家胜利!");
} else if (total == 1) {
System.out.printf("庄家胜利!");
}
}
}
N.close();
}
}
本文探讨了在限定火柴数量(21根)的情况下,通过编程实现的人机交互游戏,玩家与AI如何根据策略抽取火柴以确保AI最终获胜。通过控制火柴抽取范围在1-4根之间,实现智能决策过程。
669

被折叠的 条评论
为什么被折叠?



