24h購物| | PChome| 登入
2012-01-20 08:04:35| 人氣672| 回應0 | 上一篇 | 下一篇

[JAVA][作業練習] Lab2

推薦 0 收藏 0 轉貼0 訂閱站台


TheGameOfPig.java


/**

 *
 * @author Shiang-Yun Yang
The game of Pig is a sample two player dice game in which the first player to
reach 100 or more points wins. Players take turns. On each turn a player rolls a
six-sided die. After each roll:

    . If the player rolls a 1, then the player gets no new points and it becomes the
      other player's turn.
    . If the player rolls 2 through 6, then he can either:
    
        . ROLL AGAIN or
        . HOLD. At this point the sum of all rolls is added to the player's score
          and it becomes other player's turn.

Write a program that plays the gmae of Pig, where one player is a human and the
other is the computer. When it is the human's turn, the program should show the
score of both players and the previous roll. Allow the human to input "r" to roll
again or "h" to hold.

The computer program should play according to following rule:

    . Keep rolling when it is the computer's turn until it has accumulated 20 or
      more points, then hold. If the computer wins or rolls a 1, then the turn ends
      immediately.

Allow the human to roll first. The following line of code can be used to generate
a randon number between 1 and 6 that can correspond to the dice roll:

int diceroll = (int) (Math.random() * 6) + 1;
 */
public class TheGameOfPig {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Player human = new Player("Human");
        Player computer = new Player("Computer");
        int index = 0;
        do {
            System.out.println("Your turn !");
            System.out.println("Your score is " + human.getScore() + ".");
            System.out.println("Computer's score is " + computer.getScore() + ",");
            human.Turn();
            if(human.getScore() >= 100) {
                System.out.println("You win ! Your score is " + human.getScore() + " .");
                break;
            }
            computer.Turn();
            if(computer.getScore() >= 100) {
                System.out.println("You lose ! Computer's score is " + computer.getScore() + " .");
                break;
            }
            System.out.println("==================================================");
        } while(true);
    }
}

Player.java


/**
 *
 * @author Shiang-Yun Yang
 */
import java.util.Scanner;

public class Player {
    private static int Point;
    private int Score;
    private String Name;
    private Scanner read = new Scanner(System.in);
   
    public Player() {
        Score = 0;
        Name = "Computer";
    }
    public Player(String PlayerName) {
        Score = 0;
        Name = PlayerName;
    }
    public int getScore() {
        return Score;
    }
    private int DiceRoll() {
        return (int)(Math.random()*6) + 1;
    }
    public void Turn() {
        int Dice;
        Point = 0;
        if(Name.equals("Computer")) {
            while(true) {
                Dice = DiceRoll();
                if(Point == 0 && Dice != 1) {
                    System.out.println("Computer roll " + Dice + " at first.");
                } else if(Point >= 20 || Dice == 1) {
                    if(Point >= 20)
                        System.out.println("Computer selects HOLD and gets " + Point + " .");
                    else
                        System.out.println("Computer roll " + Dice + " .");
                    break;
                } else {
                    System.out.println("Computer ROLL AGAIN and roll " + Dice + " .");
                }
                Point += Dice;
            }
            Score += Point;
        } else {
            String Select;
            Dice = DiceRoll();
            if(Point == 0) {
                System.out.println("You roll " + Dice + " at first.");
                Point += Dice;
            }
            while(true) {               
                System.out.println("The new points is " + Point + " .");
                System.out.println("Please input your action : (h = HOLD, r = ROLL AGAIN)");
                Select = read.next();
                if(Select.equals("h")) { /*hold*/
                    break;
                }
                if(Select.equals("r")) { /*roll again*/
                    Dice = DiceRoll();
                    if(Dice == 1) {
                        System.out.println("You roll " + Dice + " .");
                        break;
                    } else {
                        System.out.println("You ROLL AGAIN and roll " + Dice + " .");
                    }
                    Point += Dice;
                }
            }
            Score += Point;
        }
    }
}

下載 : 專案
我是用 NetBeans 編譯

台長: Morris
人氣(672) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: [學習]Java |
此分類下一篇:[JAVA][作業練習] Lab4
此分類上一篇:[JAVA][作業練習] Lab1

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文