java项目实现猜拳小游戏
本文实例为大家分享了java实现猜拳小游戏的具体代码,供大家参考,具体内容如下
项目名称
猜拳小游戏
项目描述
玩家与电脑进行猜拳游戏,玩家行为采用输入方式,电脑行为采用随机形式。
代码实现
测试类
publicclassTest{
publicstaticvoidmain(String[]args){
Gamegame=newGame();
game.start();
}
}
主类:实现主方法
publicclassGame{
privatePeoplepeople;
privateComputercomputer;
publicGame(){
people=newPeople("zs");
computer=newComputer("computer");
}
publicvoidstart(){
booleanflag=true;
while(flag){
System.out.println("开始游戏:");
intcount=0;
while(count<3){
StringpeopleFist=people.doFist();
StringcomFist=computer.doFist();
//people赢
if(peopleFist.equals("石头")&&comFist.equals("剪刀")||
peopleFist.equals("剪刀")&&comFist.equals("布")||
peopleFist.equals("布")&&comFist.equals("石头")){
System.out.println(people.getName()+"赢了");
people.addScore(1);
}elseif(peopleFist.equals("石头")&&comFist.equals("石头")||
peopleFist.equals("剪刀")&&comFist.equals("剪刀")||
peopleFist.equals("布")&&comFist.equals("布")){
System.out.println("平局");
}elseif(peopleFist.equals("石头")&&comFist.equals("布")||
peopleFist.equals("剪刀")&&comFist.equals("石头")||
peopleFist.equals("布")&&comFist.equals("剪刀")){
System.out.println(computer.getName()+"赢了");
computer.addScore(1);
}
count++;
}
if(people.getScore()>computer.getScore()){
System.out.println(people.getName()+"赢了"+people.getScore()+":"+computer.getScore());
}elseif(people.getScore()==computer.getScore()){
System.out.println("平局");
}elseif(people.getScore()
玩家
publicclassPeople{
privateStringname;
privateintscore;
publicPeople(Stringname){
this.name=name;
score=0;
}
publicStringgetName(){
returnname;
}
publicvoidaddScore(intscore){
this.score+=score;
}
publicintgetScore(){
returnscore;
}
publicintsetScore(){
this.score=0;
returnscore;
}
publicStringdoFist(){
System.out.println("请出拳:");
Scannerscanner=newScanner(System.in);
Stringfist=scanner.next();
returnfist;
}
}
电脑
publicclassComputer{
privateStringname;
privateintscore;
publicComputer(Stringname){
this.name=name;
score=0;
}
publicStringgetName(){
returnname;
}
publicvoidaddScore(intscore){
this.score+=score;
}
publicintgetScore(){
returnscore;
}
publicintsetScore(){
this.score=0;
returnscore;
}
publicStringdoFist(){
Randomrandom=newRandom();
intn=random.nextInt(3);
Stringfist;
if(n==0){
fist="石头";
}elseif(n==1){
fist="剪刀";
}else{
fist="布";
}
System.out.println("对方出的是:"+fist);
returnfist;
}
}
更多有趣的经典小游戏实现专题,分享给大家:
C++经典小游戏汇总
python经典小游戏汇总
python俄罗斯方块游戏集合
JavaScript经典游戏玩不停
javascript经典小游戏汇总
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。