人机猜拳
Posted bingbing-deng
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了人机猜拳相关的知识,希望对你有一定的参考价值。
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("-------------------------欢迎进入游戏世界-------------------------
");
System.out.println(" ********************");
System.out.println(" *******开始,猜拳******");
System.out.println(" ********************
");
System.out.println("出拳规则:1.剪刀2.石头.3.布");
System.out.println("请选择对方角色(1:刘备2:孙权3:刘备 ):");
int chooice1=input.nextInt();
switch(chooice1){
case 1:
name="刘备";
System.out.println("你选择了刘备对战
");
break;
case 2:
name="孙权";
System.out.println("你选择了孙权对战
");
break;
case 3:
name="曹操";
System.out.println("你选择了曹操对战
");
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("结果:和局,真衰!
");
startGame();
}else if((perFist==1&&comFist==3)|| //用户赢
(perFist==2&&comFist==1)||
(perFist==3&&comFist==2)){
System.out.println("结果:恭喜,你赢了!
");
PerSum++;
startGame();
}else{
System.out.println("结果说:你输了,你真笨!
");
comSum++;
startGame();
}
}else {
showResult();
}
}
public void showResult(){
/*显示最后的结果*/
System.out.println("-------------------------------------------------------");
System.out.println("你 VS "+name);
System.out.println("
"+"对战的次数:"+count+" 你得分:"+PerSum+" "+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("你出拳:"+"剪刀
");
break;
case 2:
System.out.print("你出拳:"+"石头
");
break;
case 3:
System.out.print("你出拳:"+"布
");
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(); //开始游戏
}
}
以上是关于人机猜拳的主要内容,如果未能解决你的问题,请参考以下文章