**任务
完成人机猜拳互动游戏的开发
**主要功能
选取对战角色
猜拳
记录分数
package com.fingerguessinggame;
import java.util.Scanner;
/*
* 用户类
* */
public class User {
//用户姓名
String userName;
//用户积分
int score;
public int showFist(){
System.out.print("请出拳:1.剪刀 2.石头 3.布(输入相应数字):");
Scanner input = new Scanner(System.in);
int mynumber = input.nextInt();
switch (mynumber){
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石头");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("输入错误");
break;
}
return mynumber;
}
}
package com.fingerguessinggame;
import java.util.Random;
/**
* 计算机类:实现计算机出拳
*
*/
public class Computer {
//电脑名字
String nameComputer;
//电脑的分数
int score;
//设置一个1——3的随机整数
Random random = new Random();
int a = random.nextInt(3);
//实现一个方法:自动出拳
public int computerPunches(){
switch (a+1){
case 1:
System.out.println(nameComputer+"出拳:剪刀");
break;
case 2:
System.out.println(nameComputer+"出拳:石头");
break;
case 3:
System.out.println(nameComputer+"出拳:布");
break;
}
return a+1;
}
}
package com.fingerguessinggame;
import java.time.Year;
import java.util.Objects;
import java.util.Scanner;
/**
* 需求说明
* 创建游戏类Game
* 编写游戏类的初始化方法initial()
* 编写游戏类的开始游戏方法startGame()
*/
public class Game {
Scanner input = new Scanner(System.in);
User User;//用户玩家
Computer Computer;//电脑玩家
int count;//对战次数
int score;//分数
//游戏类的初始化方法
public void initial() {
//初始化用户类
User = new User();
//初始化游戏类
Computer = new Computer();
//初始化对战次数
count = 0;
}
/**
* 开始游戏页面
*/
public void startGame() {
System.out.println("------------------------------------欢迎进入游戏世界---------------------------------------");
System.out.println();
System.out.println();
System.out.println();
System.out.println(" ****************************************** ");
System.out.println(" **************** 猜拳,开始 ************* ");
System.out.println(" ****************************************** ");
System.out.println();
System.out.println();
System.out.println();
System.out.println("出拳规则:1.石头 2.剪刀 3.布");
System.out.print("请选着对方角色(1:刘备 2:孙权 3:曹操)(按序号选择角色):");
int role = input.nextInt();
switch (role) {
case 1:
Computer.nameComputer = "刘备";
System.out.println("你选择了" + Computer.nameComputer + "对战");
break;
case 2:
Computer.nameComputer = "孙权";
System.out.println("你选择了" + Computer.nameComputer + "对战");
break;
case 3:
Computer.nameComputer = "曹操";
System.out.println("你选择了" + Computer.nameComputer + "对战");
break;
default:
System.out.println("输入序号错误");
break;
}
System.out.print("请输入你的姓名:");
String own = input.next();
User.userName = own;
System.out.println(User.userName + "VS" + Computer.nameComputer + "对战!");
System.out.println();
System.out.print("要开始了吗?(y/n) ");
String ifstart = input.next();
if (ifstart.equals("n")) {
System.out.println("系统退出");
}
//用户得分
User.score = 0;
//电脑得分
Computer.score = 0;
while (ifstart.equals("y")) {
//对战次数
count++;
int mynumber = User.showFist();//自己出拳
int computernumber = Computer.computerPunches(); //电脑出拳
/*System.out.println(mynumber);
System.out.println(computernumber);*/
if (mynumber == computernumber) {
System.out.println("结果:和局,真衰!嘿嘿,等着瞧吧!");
} else if (mynumber == 1 && computernumber == 2) {
Computer.score++;
System.out.println("结果:你输了");
} else if (mynumber == 1 && computernumber == 3) {
User.score++;
System.out.println("结果:你赢了");
} else if (mynumber == 2 && computernumber == 1) {
User.score++;
System.out.println("结果:你赢了");
} else if (mynumber == 2 && computernumber == 3) {
Computer.score++;
System.out.println("结果:你输了");
} else if (mynumber == 3 && computernumber == 1) {
Computer.score++;
System.out.println("结果:你输了");
} else if (mynumber == 3 && computernumber == 2) {
User.score++;
System.out.println("结果:你赢了");
}
System.out.print("是否开始下一轮(y/n) ");
ifstart = input.next();
if (ifstart.equals("n")){
System.out.println("------------------------------------");
System.out.println(User.userName+" VS "+Computer.nameComputer);
System.out.println("对战次数:"+count);
System.out.println(User.score);
System.out.println(Computer.score);
if (User.score==Computer.score){
System.out.println("结果:打成平手,下次再和你一分高下");
}else if (User.score>Computer.score){
System.out.println("结果:呵呵,我赢了");
}else {
System.out.println("结果:惜败");
}
}
}
}
}
package com.fingerguessinggame;
public class Main {
public static void main(String[] args) {
Game game = new Game();
game.initial();
game.startGame();
}
}