使用面向对象编程(Java)制作猪游戏

时间:2015-09-17 00:42:07

标签: java

嘿伙计们,所以我为我的CS项目用Java制作了猪游戏。我没有太多麻烦,因为我只用过一堂课。但是,我的教授现在正在让我们实施OOP,而且我遇到了很多麻烦。这是我的工作猪游戏:

package edu.bsu.cs121.zmbarnes;

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

public class PigGame {

public static void main(String[] args) {
    int player1TurnScore = 0;
    int player1TotalScore = 0;
    int player2TurnScore = 0;
    int player2TotalScore = 0;
    int dice;
    int dice2;
    String input = "r";
    char repeat;

    Scanner keyboard = new Scanner(System.in);
    Random diceRoll = new Random();

    System.out.println("Welcome to the game of Pig!\n");

    while(player1TotalScore < 100 || player2TotalScore < 100){
        // human's turn
        System.out.println();
        System.out.println("It is Player 1's turn.");
        do{
            dice = diceRoll.nextInt(6) + 1;
            System.out.println("You rolled a " + dice);

            if(dice == 1){
                player1TurnScore = 0;
                System.out.println("You lose your turn!");
                System.out.println("Your total score is " + player1TotalScore);
                break;
            }else{
                player1TurnScore += dice;
                System.out.println("Your turn score is " + player1TurnScore);
                System.out.println("And your total score is " + player1TotalScore);
                System.out.println("If you hold, " + player1TurnScore + " points will be added to your total score.");
                System.out.println("Enter 'r' to roll again, or 'h' to hold.");
                input = keyboard.nextLine();
                repeat = input.charAt(0);

                if(repeat == 'h'){
                    break;
                }
            }
        }while(input.equalsIgnoreCase("r") || dice != 1);

        player1TotalScore += player1TurnScore;
        System.out.println("Your score is " + player1TotalScore);
        player1TurnScore = 0;

        if(player1TotalScore >= 100){
            System.out.println("Your total score is " + player1TotalScore);
            System.out.println("PLAYER 1 WINS!");
            break;
        }

        // Player 2's turn
        System.out.println();
        System.out.println("It is Player 2's turn.");
        do{
            dice2 = diceRoll.nextInt(6) + 1;
            System.out.println("You rolled a " + dice2);

            if(dice2 == 1){
                player2TurnScore = 0;
                System.out.println("You lose your turn!");
                System.out.println("Your total score is " + player2TotalScore);
                break;
            }else{
                player2TurnScore += dice2;
                System.out.println("Your turn score is " + player2TurnScore);
                System.out.println("And your total score is " + player2TotalScore);
                System.out.println("If you hold, " + player2TurnScore + " points will be added to your total score.");
                System.out.println("Enter 'r' to roll again, or 'h' to hold.");
                input = keyboard.nextLine();
                repeat = input.charAt(0);

                if(repeat == 'h'){
                    break;
                }
            }
        }while(input.equalsIgnoreCase("r") || dice2 != 1);

        player2TotalScore += player2TurnScore;
        System.out.println("Your score is " + player2TotalScore);
        player2TurnScore = 0;

        if(player2TotalScore >= 100){
            System.out.println("Your total score is " + player2TotalScore);
            System.out.println("PLAYER 2 WINS!");
            break;
        }
    }
    keyboard.close();
}
}

我必须使用带有roll()方法的Dice类,带有void takeTurn()的void类,void bankPoints(),void makeChoice()和boolean hasWon()方法,带有GameOfPig类和Project类使用void play()方法,以及带有main()的主要Project类。

我只是在概念化方面遇到问题,我如何将代码移动到这些方法中,以便游戏以完全相同的方式工作。任何帮助将不胜感激!

0 个答案:

没有答案