人机猜拳

package cn.c;
/*计算机类*/
public class Computer {

public int showFist(){
int comfist=(int)(Math.random()*3+1);
switch (comfist) {
case 1:
System.out.println("电脑出拳:剪刀");
break;

case 2:
System.out.println("电脑出拳:石头");
break;
case 3:
System.out.println("电脑出拳:布");
break;
}
return comfist;
}
}

 


 

package cn.c;

/*游戏类*/
import java.util.Scanner;

public class Game {
String name; //名字
int count; //对战的次数
int comSum; //计算得分 赢+1
int PerSum; //人得分 赢+1
Scanner input=new Scanner(System.in);
public void initial(){
System.out.println("-------------------------欢迎进入游戏世界-------------------------\n");
System.out.println("\t\t********************");
System.out.println("\t\t*******开始,猜拳******");
System.out.println("\t\t********************\n\n");

System.out.println("出拳规则:1.剪刀2.石头.3.布");
System.out.println("请选择对方角色(1:刘备2:孙权3:刘备 ):");
int chooice1=input.nextInt();
switch(chooice1){
case 1:
name="刘备";
System.out.println("你选择了刘备对战\n");
break;
case 2:
name="孙权";
System.out.println("你选择了孙权对战\n");
break;
case 3:
name="曹操";
System.out.println("你选择了曹操对战\n");
break;
}


}

public void startGame(){
/*开始游戏*/
Person person=new Person();
Computer computer=new Computer();

System.out.print("要开始吗?(y/n)");
String con=input.next();
if (con.equals("y")) {
count++;
/*出拳*/
int perFist=person.showFist();
int comFist=computer.showFist();

/*裁决*/
if ((perFist==1&&comFist==1)|| //平均
(perFist==2&&comFist==2)||
(perFist==3&&comFist==3)) {
System.out.println("结果:和局,真衰!\n");
startGame();
}else if((perFist==1&&comFist==3)|| //用户赢
(perFist==2&&comFist==1)||
(perFist==3&&comFist==2)){
System.out.println("结果:恭喜,你赢了!\n");
PerSum++;
startGame();

}else{
System.out.println("结果说:你输了,你真笨!\n");
comSum++;
startGame();
}


}else {
showResult();
}

}

public void showResult(){
/*显示最后的结果*/
System.out.println("-------------------------------------------------------");
System.out.println("你 VS "+name);
System.out.println("\n"+"对战的次数:"+count+"\t你得分:"+PerSum+"\t"+name+"得分:"+comSum);
if (PerSum>PerSum) {
System.out.println("哈哈,你真聪明,棒棒的!!");
}else{
System.out.println("呵呵,笨笨的,下次加油啊!");
}

}

}

 


package cn.c;

import java.util.Scanner;

public class Person {
String name; //名字
int score; //积分
Scanner input=new Scanner(System.in);
public int showFist(){
System.out.print("请出拳:1.剪刀2.石头3.布(请输入相应的数字):");
int chooice=input.nextInt();
switch(chooice){
case 1:
System.out.print("你出拳:"+"剪刀\n");
break;
case 2:
System.out.print("你出拳:"+"石头\n");
break;
case 3:
System.out.print("你出拳:"+"布\n");
break;
}

return chooice;
}
}


 

 

package cn.c;

public class TestGame {

/**
* @param args
*/
public static void main(String[] args) {
Game game=new Game();
game.initial(); //选择对方角色
game.startGame(); //开始游戏

}

}

转载于:https://www.cnblogs.com/BingBing-Deng/p/9295295.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值