用oop 写 单机剪刀石头布游戏(内含有测试类)

class Ipad2//平板电脑

import java.util.Random;                                          

import java.util.Scanner;
public class Ipad2 {//平板电脑
private String name;
private int score;

public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setScore(int score){
this.score=score;
}
public int getScore(){
return this.score;
}
//出拳
public int showFist(){
int result=0;
Random rd=new Random();
result=rd.nextInt(3)+1;
switch(result){
case 1:
System.out.println("平板出的是拳头");
break;
case 2:
System.out.println("平板出的是布");
break;
case 3:
System.out.println("平板出的是剪刀");
break;

}
return result;
}

}

class Person2 //人
import java.util.Scanner;
public class Person2 {//人
private String name;
private int score;

public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setScore(int score){
this.score=score;
}
public int getScore(){
return this.score;
}

//出拳
public int showFist(){
int result=0;
Scanner sc=new Scanner(System.in);
System.out.println("请出拳:");
result=sc.nextInt();
switch(result){
case 1:
System.out.println("您出的是拳头");
break;
case 2:
System.out.println("您出的是布");
break;
case 3:
System.out.println("您出的是剪刀");
break;
}
return result;
}
}

class Referee2 //裁判
import java.util.Scanner;
public class Referee2 {//裁判
private Person2 person;//甲方
private Ipad2 ipad;//乙方
private int count;//比赛次数

//初始化
public void init(){
person=new Person2();
ipad=new Ipad2();
count=0;
}

//开始游戏
public void startGame(){
Scanner sc=new Scanner(System.in);
System.out.println("*欢迎进入猜拳区域*");
System.out.println("*请您输入您的昵称*");
String name=sc.nextLine();
person.setName(name);
System.out.println("请选择对手 1,喜羊羊  2.灰太狼  3.奥特曼");
int num=sc.nextInt();
switch(num){
case 1:
ipad.setName("喜羊羊");
break;
case 2:
ipad.setName("灰太狼 ");
break;
case 3:
ipad.setName("奥特曼 ");
break;
}
System.out.println("现在是否开始游戏(y/n)");
sc.nextLine();
String str=sc.nextLine();
while("y".equalsIgnoreCase(str)){
int perScore=person.getScore();
int ipadScore=ipad.getScore();
int pfist=person.showFist();
int ipadfist=ipad.showFist();
//1,拳头    2,布     3,剪刀
if(pfist==1&&ipadfist==2){
System.out.println("哈哈,你太菜了,输给了"+ipad.getName());
count++;
ipad.setScore(perScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==1&&ipadfist==3){
System.out.println("哈哈,你太棒了,赢了"+ipad.getName());
count++;
person.setScore(perScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==2&&ipadfist==3){
System.out.println("哈哈,你太菜了,输给了"+ipad.getName());
count++;
ipad.setScore(ipadScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==2&&ipadfist==1){
System.out.println("哈哈,你太棒了,赢了"+ipad.getName());
count++;
ipad.setScore(ipadScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==3&&ipadfist==1){
System.out.println("哈哈,你太菜了,输给了"+ipad.getName());
count++;
ipad.setScore(ipadScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==3&&ipadfist==2){
System.out.println("哈哈,你太棒了,赢了"+ipad.getName());
count++;
person.setScore(perScore+1);

}
//1,拳头    2,布     3,剪刀
if(pfist==ipadfist){
System.out.println("哈哈, 平局,平局");
count++;

}
System.out.println("继续比赛吗(y/n)?"); 
str=sc.nextLine();
}
// 显示成绩
showResult();


}
//计算结果
public int culResult(){
//人赢
if(person.getScore()>ipad.getScore()){
return 1;
}else if(person.getScore()==ipad.getScore()){
return 2;
}else{
return 3;
}
}
 
//显示成绩
public void showResult(){
System.out.println("********围观结果*********");
int result=culResult();
if(1==result){
System.out.println(person.getName()+"vs"+ipad.getName()+"完胜,比赛次数"+count+"分数"+person.getScore()+":"+ipad.getScore());
}
if(2==result){
System.out.println(person.getName()+"vs"+ipad.getName()+"平局,比赛次数"+count+"分数"+person.getScore()+":"+ipad.getScore());
}
if(3==result){
System.out.println(person.getName()+"vs"+ipad.getName()+"惨败,比赛次数"+count+"分数"+person.getScore()+":"+ipad.getScore());
}
}
}

class Test2 //测试类

public class Test2 {
public static void main (String [] args){
Referee2 referee=new Referee2();
referee.init();
referee.startGame();
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值