分享一个有趣的猜拳小游戏代码,游戏功能:
1.实现人机对战,对战双方:Computer 和 People
puter通过获取随机数而出拳,0代表石头,1代表剪刀,2代表布
3.People通过键盘输入石头剪刀布与计算机进行游戏
4.游戏共三局算一把,三局结束后输出最终结果并打印双方比分
5.判断是否继续游戏
代码如下:
package com.dao1;
public class main {
public static void main(String[] args) {
Game game = new Game();
game.start();
}
}
package com.dao1;
import java.util.Scanner;
public class Game {
private Computer computer;
private People people;
public Game(){
people = new People("Jamie");
computer= new Computer("lisi");
}
public void judge(String fistPeople,String fistComputer){
if(fistPeople.equals("石头") &&fistComputer.equals("剪刀") ||
fistPeople.equals("剪刀")&&fistComputer.equals("布") ||
fistPeople.equals("布")&&fistComputer.equals("石头") ){
System.out.println(people.getName() + "胜利");
people.addScore(1);
}else if(fistPeople.equals(fistComputer)){
System.out.println("平局!不得分");
}else{
System.out.println(computer.getName() + "胜利!");
computer.addScore(1);
}
}
private void judgeThree(){
if(people.getScore() == computer.getScore()){
System.out.println("平局啦");
System.out.println(people.getName()+":"+computer.getName()+
"="+people.getScore()+":"+computer.getScore());
}else if(people.getScore() > computer.getScore()){
System.out.println(people.getName() + "胜利!");
System.out.println(people.getName()+":"+computer.getName()+
"="+people.getScore()+":"+computer.getScore());
}else{
System.out.println(computer.getName() + "胜利!");
System.out.println(people.getName()+":"+computer.getName()+
"="+people.getScore()+":"+computer.getScore());
}
}
public void start(){
boolean flag = true;
while(flag){
System.out.println("游戏开始!");
Scanner scanner = new Scanner(System.in);
int count = 0;
while(count<3){
System.out.println("请出拳!");
String fistPeople = people.fistPeople();
String fistComputer = computer.fistComputer();
System.out.println("对方出:"+fistComputer);
judge(fistPeople,fistComputer);
count++;
}
judgeThree();
System.out.println("是否继续游戏:是,否");
String string = scanner.next();
if(string.equals("否")){
flag = false;
}
}
}
}
package com.dao1;
import java.util.Random;
public class Computer {
private String name;
private int score;
public Computer(String name){
this.name = name;
score = 0;
}
public String fistComputer(){
Random random = new Random();
int num = random.nextInt(3);
String fist;
if(num == 0){
fist = "石头";
}else if(num == 1){
fist ="剪刀";
}else{
fist = "布";
}
return fist;
}
public void addScore(int score){
this.score += score;
}
public String getName(){
return name;
}
public int getScore(){
return score;
}
}
package com.dao1;
import java.util.Scanner;
public class People {
private String name;
private int score;
public People(String name){
this.name = name;
score = 0;
}
public String fistPeople(){
System.out.println("石头 剪刀 布");
System.out.println("Jamie出:");
Scanner scanner = new Scanner(System.in);
String fist = scanner.next();
return fist;
}
public void addScore(int score){
this.score += score;
}
public String getName(){
return name;
}
public int getScore(){
return score;
}
}
如果觉得《猜拳java 猜拳小游戏(Java代码实现)》对你有帮助,请点赞、收藏,并留下你的观点哦!