Java作业 第十三章 人机猜拳

人机猜拳:
人机类Computer代码:

import java.util.Scanner;
public class Computer {
	String name = "电脑";// 名字
	int score = 0;// 积分
	public int showFist() {
        //产生随机数
        int show = (int) ((Math.random()*10)%3+1); // 产生随机数,表示电脑出拳
        //输出出拳结果并返回
        switch (show) {
        case 1:
            System.out.println(name+"出拳:剪刀");
            break;
        case 2:
            System.out.println(name+"出拳:石头");
            break;
        case 3:
            System.out.println(name+"出拳:布");
            break;
        }

        return show;
    }
}

用户类Person:

import java.util.*;
public class Person {
	String name = "匿名"; //名字
    int score = 0;      //积分    

    public int showFist(){
        //接收用户的选择
        Scanner input = new Scanner(System.in);
        System.out.print("\n请出拳(1.剪刀2.石头3.布):");
        int show = input.nextInt();
        //输出出拳结果,并且返回
        switch (show) {
        case 1:
            System.out.println("你出拳:剪刀");
            break;
        case 2:
            System.out.println("你出拳:石头");
            break;
        case 3:
            System.out.println("你出拳:布");
            break;
        }
        return show;
    }

}

游戏类核心Game:

import java.util.*;
public class Game {
	Person person ; //用户

    Computer computer;//人机
    int count;// 对战次数

    public void inital(){
        person = new Person();
        computer = new Computer();
        count = 0;
    }
    //开始游戏
    public void startGame(){
        System.out.println("-------------欢迎进入猜拳游戏---------------");
        System.out.println();
        System.out.println("******************************************");
        System.out.println("          **  猜拳,开始  **");
        System.out.println("******************************************");
        System.out.println();
        System.out.println("出拳规则:1.剪刀2.石头3.布");
        Scanner input = new Scanner(System.in);
        String exit="n";//退出系统
        do {
            inital();//初始化
            //选择对手
            System.out.print("选择你的对手(1.刘备2.孙权3.曹操):");
            int role = input.nextInt();
            switch (role) {
			case 1:
				 computer.name="刘备";
				 System.out.println("你选择了"+ computer.name+"对战");
				break;
			case 2:
				 computer.name="孙权";
				 System.out.println("你选择了"+computer.name+"对战");
				 break;
			case 3:
				System.out.println("你选择了"+computer.name+"对战");
				break;
			default:
				System.out.println("输入错误!");
				break;
			}

            //游戏玩家输入姓名
            System.out.print("请输入你的姓名:");
            person.name=input.next();
            System.out.println(person.name+"    VS    "+computer.name+"对战\n");
            //开始游戏
            System.out.print("要开始吗?(y/n)");
            //开始一局游戏
            String start = input.next();
            int perFist;//用户出拳
            int comFist;//电脑出拳
            while (start.equals("y")) {
                //出拳
                perFist = person.showFist();
                comFist = computer.showFist();
                //判断胜负
                if ((perFist==1&&comFist==1)||(perFist==2&&comFist==2)||(perFist==3&&comFist==3)) {
                    System.out.println("结果:和局,真衰!\n");//平局
                }else if ((perFist==2&&comFist==1)||(perFist==3&&comFist==2)||(perFist==1&&comFist==3)) {
                    System.out.println("结果说:^_^,你输了,真笨!\n");//计算机赢
                    computer.score++;
                }else {
                    System.out.println("结果:恭喜,你赢了!\n");//胜利  
                   person.score++;
                }
                count++;
                System.out.print("是否进行下一轮(y/n)");
                start = input.next();
            }
            //显示结果
            showResult();
            System.out.print("\n要开始下一局吗?(y/n):");
            exit = input.next();
            System.out.println();

        } while (!exit.equals("n"));

        System.out.println("系统退出");
    }
    private void showResult() {
         //显示对战次数
        System.out.println("------------------------------");
        System.out.println(computer.name+"    VS    "+person.name);
        System.out.println("对战次数:"+count);
        //显示最终得分
        System.out.println("\n姓名\t得分");
        System.out.println(person.name+"\t"+person.score);
        System.out.println(computer.name+"\t"+computer.score+"\n");

        //显示对战结果
        int result = calcResult();
        if (result==1) {
            System.out.println("结果:打成平手,真衰!");
        }else if (result==2) {
            System.out.println("结果:恭喜,你赢了!");
        }else
        {
            System.out.println("结果:呵呵,笨笨,下次加油啊!");//计算机赢
        }
        System.out.println("--------------------------");
    }
    //计算比赛结果
    private int calcResult() {
        // TODO Auto-generated method stub
        if (person.score == computer.score) {
            return 1;//战平
        }else if (person.score > computer.score) {
            return 2;//游戏玩家赢
        }else {
            return 3;//惜败
        }
    }

}

测试类Test:

package class3;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		        Game game = new Game();

		        game.inital();

		        game.startGame();
	}

}

输出结果:
Java作业 第十三章 人机猜拳_第1张图片

你可能感兴趣的:(Java作业 第十三章 人机猜拳)