1. 程式人生 > >猜拳遊戲專案

猜拳遊戲專案

猜拳遊戲專案

專案功能點:

1猜拳遊戲,每一把遊戲進行三局,可以玩多把,每一局贏者積一分,輸者不扣分,平局不計分,三局決勝負。
2兩人進行猜拳遊戲。可以隨機選取要比賽的對手。
考察知識點:
Java類和物件
設計思路:
1有兩方,甲方:玩家(Player) 乙方:電腦(Computer)模擬 進行猜拳遊戲
2 猜拳開始:贏者積一分,平局不計分,輸者不扣分。每次三局決勝負
3 每一把遊戲決定最終的勝負,繼續下一輪遊戲的開始。

程式碼展示:

package com.finger;

import java.util.Scanner;

/**
 * 描述:開始遊戲
 *
 * @Author administrator{GINO ZHANG}
 * @Date2018/12/13
 */
public class Game {
    private Computer computer;
    private Player player;
    Game(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("zs lisi ww");
        String name = scanner.next();
        computer = new Computer(name);
        player = new Player("lll");
    }

    public static void main(String[] args) {
        
    }{
        System.out.println(player.getName() +" VS "+computer.getName());
        Scanner scanner = new Scanner(System.in);

        int fistPlayer;
        int fistComputer;
        while(true){
            int count = 0;
            while(count<3) {
                System.out.println("第"+(count+1)+"次出拳");
                fistPlayer = player.showFist();
                fistComputer = computer.showFist();
                count++;
                if (fistComputer == Constant.STONE && fistPlayer == Constant.CLOTH || fistComputer == Constant.SCISSOR && fistPlayer == Constant.STONE || fistComputer == Constant.CLOTH && fistPlayer == Constant.SCISSOR) {//player贏
                    player.addScore(1);
                }
                if (fistComputer == Constant.CLOTH && fistPlayer == Constant.STONE|| fistComputer == Constant.STONE && fistPlayer == Constant.SCISSOR|| fistComputer == Constant.SCISSOR && fistPlayer == Constant.CLOTH) {//贏
                    computer.addScore(1);
                }
            }
            int playerScore = player.getScore();
            int computerScore = computer.getScore();
            System.out.println("最終結果:"+player.getName()+playerScore+" : "+computer.getName()+computerScore);
            if(playerScore>computerScore){
                System.out.println(player.getName() + "獲勝");
            }else if(playerScore == computerScore){
                System.out.println("平局");
            }else{
                System.out.println(computer.getName() + "獲勝");
            }

            System.out.println("是否繼續下一輪猜拳 是or否");

            if(scanner.next().equals("是")){
                continue;
            }else{
                break;
            }
        }
    }
}
package com.finger;

import java.util.Scanner;

/**
 * 描述:玩家類
 *
 * @Author administrator{GINO ZHANG}
 * @Date2018/12/13
 */
public class Player {
    private String name;
    private int score;
    public Player(String name){
        score = 0;
        this.name = name;
    }
    public int showFist(){//玩家進行出拳操作
        System.out.println("請出拳:1.石頭,2.剪刀,3.布");
        Scanner scanner = new Scanner(System.in);
        int f = scanner.nextInt();
        return f;
    }
    public String getName(){
        return name;
    }
    public void addScore(int n){//贏者積一分
        score += n;
    }
    public int getScore(){
        return score;
    }
}
package com.finger;

import java.util.Random;

/**
 * 描述:電腦類
 *
 * @Author administrator{GINO ZHANG}
 * @Date2018/12/13
 */
public class Computer {
    private String name;
    private int score;
    public Computer(String name){
        this.score = 0;
        this.name = name;
    }
    public int showFist(){//隨機出拳
 		Random random = new Random();
        int fist = random.nextInt(3)+1;
        return fist;
    }
    public String getName(){
        return name;
    }

    /**
     * 記錄成績
     * @param n
     */
    public void addScore(int n){
        score += n;
    }
    public int getScore(){
        return score;
    }
}

package com.finger;

/**
 * 描述:常量類
 *
 * @Author administrator{GINO ZHANG}
 * @Date2018/12/13
 */
public interface Constant {
    public static final int STONE = 1;
    public static final int SCISSOR = 2;
    public static final int CLOTH = 3;
}