猜拳游戏,利用面向对象

猜拳游戏项目

项目功能点:
    1)猜拳游戏,每一把游戏进行三局,可以玩多把,每一局赢者积一分,输者不扣分,平局不计分,三局决胜负。
    2)两人进行猜拳游戏。可以随机选取要比赛的对手。
考察知识点:
   Java类和对象
设计思路:
    1) 有两方,甲方:玩家(Player) 乙方:电脑(Computer)模拟 进行猜拳游戏
    2) 猜拳开始:赢者积一分,平局不计分,输者不扣分。每次三局决胜负
    3) 每一把游戏决定最终的胜负,继续下一轮游戏的开始。
package FingerGuess;

/**
 * Description:常量类
 *
 * @Author Fann
 * @Data 2018/12/8
 */
public class Constant {
    public static final int ROCK = 0;//A代表石头
    public static final int SCISSORS = 1;//B代表剪刀
    public static final int PAPER = 2;//C代表布
}
package FingerGuess;

import java.util.Random;
import java.util.Scanner;

/**
 * Description:
 *
 * @Author Fann
 * @Data 2018/12/8
 */
public class GuessFingerGame {
    public int score_Player; //玩家分数
    public int score_Computer;//电脑分数
    Scanner in = new Scanner( System.in );  //用来让玩家输入
    Random random = new Random(  );  //随机产生当作电脑出的拳


    //游戏开始,进行三次.
    public void gameStart() throws Exception {
        System.out.println("游戏开始:-----------------");
        for (int i = 0; i < 3; i++) {
            System.out.println("玩家第"+i+"次出拳(输入0代表石头,输入1代表剪刀,输入2代表布):");
            int player = in.nextInt();

            int computer = random.nextInt(3);  //0-2
            if(computer == Constant.ROCK){
                System.out.println("电脑出拳为:0(石头)");
            }else if(computer == Constant.SCISSORS){
                System.out.println("电脑出拳为:1(剪刀)");
            }else{
                System.out.println("电脑出拳为:2(布)");
            }


            int result = compare( player,computer );
            //判断比较的结果是什么,进行加分.
            if(result == 1){ //玩家赢了,玩家加分
                score_Player++;
            }else if(result == -1){ //电脑赢了,电脑加分
                score_Computer++;
            }
        }
        //三次循环后,游戏结束.
        gameEnd();
    }


    public void showScore(){
        System.out.print("player的分数:"+score_Player+"玩家的分数:"+score_Computer);
    }

    /**
     *  用来比较电脑和玩家的猜拳
     *  返回1表示玩家赢  返回0表示平局  返回-1表示电脑赢
     */
    public int compare(int player,int computer) throws Exception {
        //参数校验
        if(player < 0 || player > 2 || computer < 0 || computer > 2){
            throw new Exception( "参数错误" );
        }

        if(player == Constant.ROCK){ //玩家为石头
            if(computer == Constant.ROCK){
                return 0;  //电脑也为石头 返回平局.
            }else if(computer == Constant.PAPER){
                return -1;  //电脑为布,返回电脑赢
            }else{
                return 1;
            }
        }

        if(player == Constant.PAPER){ //玩家为布
            if(computer == Constant.ROCK){
                return 1;   //电脑为石头,返回玩家赢
            }else if(computer == Constant.PAPER){
                return 0;  //电脑也为布,返回平局.
            }else{
                return -1;
            }
        }

        if(player == Constant.SCISSORS){ //玩家为剪刀
            if(computer == Constant.SCISSORS){
                return 0;  //电脑也为剪刀,返回平局.
            }else if(computer == Constant.ROCK){
                return -1;  //电脑为石头,返回-1.
            }else{
                return 1;
            }
        }
        return 2;
    }


    /**
     *游戏结束时,打印出结果.
     */
    public void gameEnd(){
        System.out.println("游戏结束:-------------");
        if(score_Computer > score_Player){
            System.out.println("电脑"+score_Computer+"分,玩家"+score_Player+"分,     电脑获胜!!");
        }else if(score_Player == score_Computer){
            System.out.println("电脑"+score_Computer+"分,玩家"+score_Player+"分,     两人平局!!");
        }else{
            System.out.println("电脑"+score_Computer+"分,玩家"+score_Player+"分,     玩家获胜!!");
        }
    }
}
package FingerGuess;

import java.util.Scanner;

/**
 * Description:
 *
 * @Author Fann
 * @Data 2018/12/8
 */
public class Main {
    public static void main(String[] args) throws Exception {
        //判断玩家是否要继续
        Scanner in = new Scanner( System.in );

        while (true){
            GuessFingerGame game = new GuessFingerGame();
            game.gameStart();
            System.out.println("是否继续游戏  y/n?");
            if(in.nextLine().equalsIgnoreCase( "n" )){
                break;
            }
        }
        System.out.println("程序退出");
    }
}

你可能感兴趣的:(java程序)