在猜拳游戏的代码里,做四个类分别定义人怎样出拳出拳以及电脑怎样出拳,以及游戏如何玩和一个测试类函数,分别在person类及在computer类中写入计算每局比分的函数
person类:
package com.game;
import java.util.Scanner;
class Person {
String name="666";
int score;
String fist;
public void Fist(){
System.out.print("666出");
Scanner scanner=new Scanner(System.in);
this.fist=scanner.next();
}
public int setScore(){
return score;
}
public void getScore(){
score++;
}
computer类
package com.game;
import java.util.Random;
class Computer {
String name="电脑";
String fist=null;
int score;
public void Fist(){
Random random=new Random();
int n=random.nextInt(3)+1;
switch(n){
case 1:
this.fist="石头";
System.out.println("电脑出石头");
break;
case 2:
this.fist="剪刀";
System.out.println("电脑出剪刀");
break;
case 3:
this.fist="布";
System.out.println("电脑出布");
break;
}
}
public int setscore(){
return score;
}
public void getScore(){
score++;
}
}
Game类
package com.game;
import java.awt.*;
import java.util.Scanner;
class Game {
public void playTreeTime(){
Person person=new Person();
Computer computer=new Computer();
int i=0;
int m=0,n=0;
for(i=1;i<=3;++i){
System.out.println("第"+i+"局");
person.Fist();
computer.Fist();
if ((person.fist).equals("石头")) {
if((computer.fist).equals("石头")){
System.out.println("平局");
}
else if((computer.fist).equals("剪刀")){
System.out.println("666赢!");
person.getScore();
}
else if((computer.fist).equals("布")){
System.out.println("电脑赢!");
computer.getScore();
}
}
if((person.fist).equals("剪刀")){
if((computer.fist).equals("石头")){
System.out.println("电脑赢!");
computer.getScore();
}
else if((computer.fist).equals("剪刀")){
System.out.println("平局!");
}
else if((computer.fist).equals("布")){
System.out.println("666赢!");
person.getScore();
}
}
if((person.fist).equals("布")){
if((computer.fist).equals("石头")){
System.out.println("666赢!");
person.getScore();
}
else if((computer.fist).equals("剪刀")){
System.out.println("电脑!");
}
else if((computer.fist).equals("布")){
System.out.println("电脑赢!");
computer.getScore();
}
}
System.out.println("666:电脑="+person.score+":"+computer.score);
}
if(person.score<computer.score)
System.out.println("电脑最后赢了!");
else if(person.score>computer.score)
System.out.println("666最后赢了");
else
System.out.println("平局!");
}
public void getResult(){
}
public void start(){
while(true){
Game game=new Game();
game.playTreeTime();
System.out.println("是否继续?");
Scanner scanner=new Scanner(System.in);
String str=scanner.next();
if(str.equals("是")){
continue;
}else{
break;
}
}
}
}
测试函数
package com.game;
import java.awt.*;
public class TestMain {
public static void main(String[] args) {
Game game=new Game();
System.out.println("===========欢迎来到猜拳游戏!============");
System.out.println("游戏规则:三局两胜,每轮统计分数");
System.out.println("========================================");
game.start();
}
}