實(shí)現(xiàn)人機(jī)猜拳需要有機(jī)器類和用戶類分別設(shè)置其屬性撵枢,并在一個(gè)管理員類中實(shí)現(xiàn)循環(huán)對(duì)戰(zhàn)和為贏家加分古毛,最后用一個(gè)Main類調(diào)用管理員類實(shí)現(xiàn)人機(jī)猜拳的目標(biāo)。
首先創(chuàng)建用戶類和機(jī)器類
創(chuàng)建用戶類(User)
public class User {
String userName;//用戶名
int score; //得分
public int userAttribute() {
System.out.print("請(qǐng)出拳:1.剪刀2.石頭3.布(輸入相應(yīng)的數(shù)字):");
Scanner scanner = new Scanner(System.in);
int choice = scanner.nextInt(); //選擇出拳
switch(choice) {
case 1:
System.out.println("你出拳:剪刀");
break;
case 2:
System.out.println("你出拳:石頭");
break;
case 3:
System.out.println("你出拳:布");
break;
default:
System.out.println("輸入錯(cuò)誤!");
break;
}
return choice; //返回選擇
}
}
創(chuàng)建機(jī)器人類(Robot)
public class Robot {
String robotName; //機(jī)器名
int score;//機(jī)器得分
public int chuQuan() {
int random =(int)(Math.random()*3)+1; //生成3個(gè)隨機(jī)數(shù)
switch(random) {
case 1:
System.out.println(computerName+"出拳:石頭");
break;
case 2:
System.out.println(computerName+"出拳:剪刀");
break;
case 3:
System.out.println(computerName+"出拳:布");
break;
}
return random;
}
}
創(chuàng)建管理類(Administrator)
public class Administrator {
Scanner scanner = new Scanner(System.in);
User User;//用戶玩家
Robot Robot;//電腦玩家
int count;//對(duì)戰(zhàn)次數(shù)
//游戲類的初始化方法
public void initial(){
//創(chuàng)建用戶對(duì)象
User= new User();
//創(chuàng)建電腦對(duì)象
Robot = new Robot();
//初始化對(duì)戰(zhàn)次數(shù)為0
count = 0;
}
//開始游戲的方法
public void startGame(){
System.out.println("*******************");
System.out.println("** 猜拳材诽,開始 **");
System.out.println("*******************");
System.out.println("出拳規(guī)則:1.石頭 2.剪刀 3.布");
System.out.println("請(qǐng)輸入你的姓名:");
User.userName = scanner.next();
System.out.print("請(qǐng)選著對(duì)方角色(1:劉備 2:孫權(quán) 3:曹操)");
int choose = scanner.nextInt();//接收用戶選擇的角色
switch(choose){
case 1:
Robot.robotName = "劉備";//把值賦給電腦類的name
System.out.println("你選擇了"+Robot.robotName+"對(duì)戰(zhàn)");
break;
case 2:
Robot.robotName = "孫權(quán)";
System.out.println("你選擇了"+Robot.robotName+"對(duì)戰(zhàn)");
break;
case 3:
Robot.robotName = "曹操";
System.out.println("你選擇了"+Robot.robotName+"對(duì)戰(zhàn)");
break;
default:
System.out.println("輸入有誤渤昌!");
break;
}
System.out.println("要開始嗎(y/n)");
String con = scanner.next();
if(con.equals("n")){//如果不開始輸入"n"的話 就提示退出
System.out.println("系統(tǒng)退出!");
}
int perFist;//用戶出的拳
int comFist;//計(jì)算機(jī)出的拳
while(con.equals("y")){
//出拳
perFist = User.userAttribute();//調(diào)用用戶類的方法
comFist = Robot.chuQuan();//調(diào)用電腦類的方法
//裁決
if((perFist == 1 && comFist == 1) || (perFist == 2 && comFist == 2) || (perFist == 3 && comFist == 3)){
System.out.println("結(jié)果:你們平局了");//平局
}else if((perFist == 1 && comFist == 3) || (perFist == 2 && comFist == 1) || (perFist == 3 && comFist == 2)){
System.out.println("結(jié)果:你失敗了");
Robot.score++;//記錄電腦贏的分?jǐn)?shù)
}else{
System.out.println("結(jié)果:恭喜澈驼,你贏了辛燥!");
User.score++;//記錄用戶贏的分?jǐn)?shù)
}
count++;//對(duì)戰(zhàn)次數(shù)依次加一
System.out.println("是否進(jìn)入下一輪(y/n)");
con = scanner.next();
}
//如果結(jié)束 則顯示最后的結(jié)果
showResult();
}
//顯示最后的輸出結(jié)果
public void showResult(){
System.out.println("****************************************");
System.out.println(Robot.robotName+"\tVS\t"+User.userName);
System.out.println("對(duì)戰(zhàn)次數(shù):"+count);
System.out.println("姓名"+"\t"+"積分");
System.out.println(User.userName+"\t"+User.score);
System.out.println(Robot.robotName+"\t"+Robot.score);
if(User.score>Robot.score){//如果用戶的分?jǐn)?shù)大于電腦的分?jǐn)?shù) 則用戶贏
System.out.println("恭喜"+User.userName+"贏了!");
}else{
System.out.println("恭喜"+Robot.robotName+"贏了!");
}
System.out.println("****************************************");
}
}
最后創(chuàng)建一個(gè)Mian類
public class Mian {
public static void main(String[] args) {
Administrator administrator = new Administrator();
administrator.initial();//調(diào)用初始化方法
administrator.startGame();//調(diào)用開始游戲的方法
}
}