项目要求
通过控制台方式实现一个人机对战的猜拳游戏,用户通过输入(1,.剪刀 2.石头 3.布),
机器随机生成(1,.剪刀 2.石头 3.布),胜者积分,n局以后通过积分的多少判定胜负。
一、我创了两个类Game和GameRule
Game是测试类,GameRule是放了需要用到的一些方法。
二、思考步骤
1.思考怎么完成这个项目
首先我们要获取用户的输入,这就要用到Scanner获取用户的输入:Scanner input = new Scanner(System.in);
这个方法还需要导入一个包,导包操作用到import,import java.util.Scanner;
再接着我们就要接收用户的输入,用 int user = input.nextInt();
然后判断用户输入的数代表了什么,这里用到了switch语句,通过case区分了用户选择的操作。
2.机器随机出拳的实现
我们用Math.Random()函数,该方法是取大于等于0到小于1之间的随机数。
我们这边只需要机器随机出1、2、3三个数,所以我们要(int)(Math.random()*10)%3+1;
3.比较用户与机器出拳的结果
我们这里用到了if(){}else if(){}else{}的条件语句来比较结果
我们将用户输入的1-3与机器的1-3比较,要怎么才能实现呢,我们将用户的出拳减去机器的出拳
能得出所有结果列出如下:
1-1=0,平局;2-2=0,平局;3-3=0,平局
1-2=-1,机器胜;2-3=-1,机器胜;3-1=2,机器胜
1-3=-2,用户胜;2-1=1,用户胜,3-2=1,用户胜
那么便是当得到结果为0时,是平局的;结果为-1和2时机器胜,结果为-2和1时用户胜。
这就有了这代码:
if(result == 0) {
System.out.println("平局,不加分");
}else if(result == -2 || result == 1) {
userScore ++;
System.out.println("你赢了,加一分");
}else if(result == -1 || result == 2) {
machineScore ++;
System.out.println("机器人赢了,机器人加一分");
}
4.余下其他一些考虑
现在呢还需要有计分数的:
于是就有了userScore、machineScore来记录用户跟机器的得分情况。
考虑到提示下比较的轮数,较为直观友好,就又有了count来表示第几轮,用到以下代码
int count = 1;
System.out.println("第" + count + "轮:");
count ++;
我们要让用户与机器持续较量,这就用到了while循环语句,控制终止的条件是round,
同时接收用户想要玩几轮,用到以下代码
int round = input.nextInt();
while(round > 0){
round --;
}
三、源代码
Game类
import java.util.Scanner;
public class Game {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入要玩的局数:");
int round = input.nextInt(); // 控制游戏轮数的数据
int count = 1; //显示轮数的数据
while(round > 0) {
System.out.println("猜拳游戏开始(1-剪刀,2-石头,3-布,4-查看比分,5-不玩了,退出游戏):");
int user = input.nextInt();
System.out.println("第" + count + "轮:");
GameRule rule = new GameRule();
rule.userSelect(user);
int machine = rule.machineSelect();
rule.result(user, machine);
if(user == 4 || user == 5) {}
else {
round --;
count ++;
}
System.out.println("================");
if(round == 0) {
rule.score();
rule.totalResult();
}
}
}
}
GameRule类
public class GameRule {
private static int userScore = 0; //用户分数
private static int machineScore = 0; //机器人分数
private static boolean option = false; //判断用户有没有做除了猜拳以外的选项豆腐坊
public static int getUserScore() {
return userScore;
}
public static void setUserScore(int userScore) {
GameRule.userScore = userScore;
}
public static int getMachineScore() {
return machineScore;
}
public static void setMachineScore(int machineScore) {
GameRule.machineScore = machineScore;
}
/**
* 获取用户出的拳
* @param i
*/
public void userSelect(int i) {
switch(i){
case 1: System.out.println("你出了剪刀");
break;
case 2: System.out.println("你出了石头");
break;
case 3: System.out.println("你出了布");
break;
case 4:
case 5: option = true;
break;
default:System.out.println("请输入1-5选择要出什么");
break;
}
}
/**
* 用随机数获取机器人出的拳
* @return
*/
public int machineSelect() {
if(option) { //用户要是选择了4,5操作,没有出拳,机器人就不出拳
return 0;
}else {
int machine = (int)(Math.random()*10)%3+1; //产生1-3的随机数,表示机器出的
switch(machine) {
case 1: System.out.println("电脑出了剪刀");
return machine;
case 2: System.out.println("电脑出了石头");
return machine;
case 3: System.out.println("电脑出了布");
}
return machine;
}
}
/**
* 获取用户与机器人出的,显示用户与机器人的每轮结果
* @param user
* @param machine
*/
public void result(int user,int machine) {
if(user == 4) { //调用显示当前分数的方法
score();
}
else if(user == 5) { //调用显示当前分数与最终结果,退出程序
score();
totalResult();
System.exit(0);
}else {
int result = user - machine;
if(result == 0) {
System.out.println("平局,不加分");
}else if(result == -2 || result == 1) {
userScore ++;
System.out.println("你赢了,加一分");
}else if(result == -1 || result == 2) {
machineScore ++;
System.out.println("机器人赢了,机器人加一分");
}
}
}
/**
* 显示当前分数
*/
public void score() {
System.out.println("现在你有" + userScore + "分");
System.out.println("机器人有" + machineScore + "分");
}
/**
* 显示最终结果
*/
public void totalResult() {
if(userScore > machineScore) {
System.out.println("你获得了最后的胜利!");
}else if(userScore == machineScore){
System.out.println("最后平局!挑战者不错啊!");
}else {
System.out.println("你输了,机器人获胜!请再接再厉~~");
}
}
}
总结
第一次写博客不是很好,大家见谅,做的也就是勉强能用,很多需要完善,大佬勿怪,
有什么可以改善的,更好的建议,希望也能帮小弟指出,我一定虚心接受。