如何获得等于7的六个骰子的所有组合?

时间:2017-03-22 20:30:42

标签: java arrays loops dice

这是一个家庭作业,我应该掷6个骰子,然后选择任意组合制作7并摒弃它们。 EX。 1,1,1,1,1,2。我的分数是0,因为我必须丢弃所有六个骰子。

起初我认为这是2个骰子的任意组合,但现在已被告知。我的代码现在反映了2个骰子,我不知道如何在不使用蛮力和写入每个可能的组合并检查它的情况下完成所有六个骰子。

        private void calculateScore(){
            //checks for 7's, calculates score if none, updates values  
            discardDieMessage = "Discard Dice. Combinations of die equals 7";

            boolean noSevens = true;
            //diceRoll is an array with 6 values
            for(int i = 0; i<diceRoll.length;i++){
                int temp = diceRoll[i];
                for(int j = 0; j<diceRoll.length;j++){
                    if(temp + diceRoll[j] == 7){noSevens = false;}
                }
            }

            if(noSevens){
                playerScore[playerNumber] = 0;
                for (int i = 0; i < diceRoll.length;i++) {
                    playerScore[playerNumber] += diceRoll[i];
                }
                playerScoreString = new String ("Your score is: " + 
                                + playerScore[playerNumber] +
                                "\nYou rolled: " + currentPlayerRolls + " time(s)");    
                update();
                outputTextArea.setText(playerScoreString);
                calculateButton.setDisable(true);
                messageLabel.setText("");
            }
            else {
                messageLabel.setText(discardDieMessage);    
                messageLabel.setTextFill(Paint.valueOf("red"));
            }
        }

UPDATE_1:这是我到目前为止的完整代码。这是一个7人的游戏,玩家掷6个骰子,他们必须丢弃等于7 EX。{3,2,1,1}的骰子而其他两个骰子是{6,6},所以其余的骰子不再等于7.

//all-gui imports
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.stage.Stage;
    //GUI specific controls
    import javafx.scene.control.Button;
    import javafx.scene.control.Label;
    import javafx.scene.control.TextField;
    import javafx.scene.control.TextArea;
    import javafx.scene.paint.Color;
    //GUI specific containers
    import javafx.scene.layout.GridPane;
    import javafx.scene.layout.HBox;
    import javafx.scene.layout.BorderPane;
    import javafx.geometry.Pos;
    import javafx.geometry.HPos;
    //EventHandler Interface
    import javafx.event.EventHandler;
    import javafx.event.ActionEvent;

    import java.util.Random;
    import javafx.scene.paint.*;
    import java.util.Arrays;
    import java.util.stream.*;
    import java.io.*;
    /***************************************************************************/
    public class SevensGameGUI extends Application {

        //declare the scene and main stage
        private Stage primaryStage;
        private Scene scene;
        //declare the handler
        private SevensGameHandler handler;
        //declare panes going to be used
        private BorderPane mainPane;
        private GridPane topPane;
        private GridPane centerPane;
        private GridPane outputPane;
        private HBox bottomPane;

        //declare up labels, boxes, buttons, and textAreas
        private Button rollButton;
        private Button calculateButton;
        private Button endButton;
        private Button[] discardButton;

        private Label playerNumberLabel;
        private Label totalPlayersLabel;
        private Label rollNumberLabel;
        private Label maxRollLabel;

        private TextField[] diceTextField;//Dice TextFields
        private TextField playerNumberTextField; //What player is currently playing
        private TextField totalPlayersTextField;
        private TextField rollNumberTextField; //What roll the player is on
        private TextField maxRollTextField; //The total rolls a player has availiable
        private Label messageLabel; //Display error
        private TextArea outputTextArea;

        //setting up program-specfic variables
        private int MAX_THROWS = 3;
        private int[] playerScore;
        private int[] diceRoll;
        private int[] discardDie;
        private int DICE_COUNT;
        private int playerNumber;
        private int firstPlayerRolls;
        private int currentPlayerRolls;
        private int winningPlayer;
        Random generator = new Random();
        private String cannotRollMessage = "";
        private String playerScoreString = "";
        private String discardDieMessage = "";
    /***************************************************************************/   
        public void start(Stage primaryStage) {
            startUP();
            sculptGUI();
            prepareGame();
        }
    /***************************************************************************/
        private class SevensGameHandler implements EventHandler<ActionEvent>{
            public void handle(ActionEvent ae){
                if(ae.getSource() == rollButton){
                    rollDice();
                }else if(ae.getSource() == calculateButton){
                    calculateScore();
                }else if(ae.getSource() == endButton){
                    nextPlayer();
                }
                for (int i = 0; i < discardButton.length; i++){
                    if(ae.getSource() == discardButton[i]){
                        diceRoll[i] = 0;
                        update();
                        discardButton[i].setDisable(true);  
                    }   
                }
            }
        }//end of SevensGameHandler
    /***************************************************************************/
        private void rollDice(){    
            calculateButton.setDisable(false);
            //rolls dice, shows updated values  
            for (int i = 0; i < diceRoll.length; i++) {
            //this rolls the dice 6 times and checks if the button is disabled
                if(!discardButton[i].isDisable()){diceRoll[i] = (generator.nextInt(6)+ 1);}
            }
            //increments how many times the first player rolls
            if (playerNumber == 0) {
                firstPlayerRolls++;
            }
            currentPlayerRolls++;
            //checking updated values and sends error message if so
            if (currentPlayerRolls == MAX_THROWS) {
                cannotRollMessage = ("Error: Player " + 
                            (playerNumber + 1) + " cannot roll anymore!");
                messageLabel.setText(cannotRollMessage);
                messageLabel.setTextFill(Paint.valueOf("red"));
                rollButton.setDisable(true);
            }
            update();
        }
    /***************************************************************************/
        private void calculateScore(){

            //checks for 7's, calculates score if none, updates values  
            discardDieMessage = "Discard Dice. Combinations of die equals 7";

            boolean noSevens = true;
            //diceRoll is an array with 6 values
            for(int i = 0; i<diceRoll.length;i++){
                int temp = diceRoll[i];
                for(int j = 0; j<diceRoll.length;j++){
                    if(temp + diceRoll[j] == 7){noSevens = false;}
                }
            }

            if(noSevens){
                playerScore[playerNumber] = 0;
                for (int i = 0; i < diceRoll.length;i++) {
                    playerScore[playerNumber] += diceRoll[i];
                }
                playerScoreString = new String ("Your score is: " + 
                                + playerScore[playerNumber] +
                                "\nYou rolled: " + currentPlayerRolls + " time(s)");    
                update();
                outputTextArea.setText(playerScoreString);
                calculateButton.setDisable(true);
                messageLabel.setText("");
            }
            else {
                messageLabel.setText(discardDieMessage);    
                messageLabel.setTextFill(Paint.valueOf("red"));
            }
        }
    /***************************************************************************/
        private void nextPlayer(){
            //increments player, resets rolls and such, updates values
            if (playerNumber == 0) {
                MAX_THROWS = firstPlayerRolls;  
            }
            playerNumber++;
            clearBoard();
            update();
            calculateButton.setDisable(false);
            rollButton.setDisable(false);
            for (int i = 0; i < DICE_COUNT; i++){
            discardButton[i].setDisable(false);
            }
            outputTextArea.clear();

            for(int i = 0;i<playerScore.length;i++){
                if(playerScore[i] > playerScore[winningPlayer]){
                    winningPlayer = i;
                }
            }
            outputTextArea.appendText("\n\tPlayer " + Integer.toString(winningPlayer+1)+
                " is Winning with " + Integer.toString(playerScore[winningPlayer])+" Points");

            if ( playerNumber == playerScore.length){
                endGame();  
            }
        }//end of nextPlayer()
    /***************************************************************************/
        private void endGame(){
            //sets up a seperate pane to display the winning player along with player scores
            TextArea scoreArea = new TextArea();
            Button closeButton = new Button("Close");
            GridPane endPane = new GridPane();
            scoreArea.setPrefSize(300,350);
            endPane.setHgap(15);
            endPane.setVgap(5);
            endPane.setAlignment(Pos.CENTER);
            endPane.add(scoreArea,0,0);
            endPane.add(closeButton,0,1);
            endPane.setHalignment(closeButton ,HPos.CENTER);
            endPane.setHalignment(scoreArea ,HPos.CENTER);
            Scene endScene = new Scene(endPane,500,500);
            primaryStage.setScene(endScene);
            primaryStage.show();
            closeButton.setOnAction(e -> {
                System.exit(0);
            });

            //output scores and player info
            scoreArea.appendText("Player \t Score\n\n");

            for(int i = 0;i<playerScore.length;i++){
                scoreArea.appendText("Player " + Integer.toString(i+1) +"\t " + 
                Integer.toString(playerScore[i]) + "\n");
            }
            scoreArea.appendText("\n\tCongratulations Player " +
            Integer.toString(winningPlayer+1) + "\n\tYOU WIN!");
        }
    /***************************************************************************/   
        private void prepareGame(){
            //Prepares the game for the player to use by initializing variables 
            //and handlers
            playerScore = new int[] {0,0,0,0,0};
            diceRoll = new int[] {0,0,0,0,0,0};
            discardDie = new int[] {0,0,0,0,0,0};
            DICE_COUNT = 6;
            playerNumber = 0;
            firstPlayerRolls = 0;
            currentPlayerRolls = 0;
            winningPlayer =0;

            update();

            handler = new SevensGameHandler();
            rollButton.setOnAction(handler);
            calculateButton.setOnAction(handler);
            endButton.setOnAction(handler);
            for(int i = 0;i < discardButton.length; i++) {
            discardButton[i].setOnAction(handler);
            }

        }
    /***************************************************************************/
        private void clearBoard(){
            //Method used to reset and update the GUI
            for (int i = 0; i < diceRoll.length; i++) {
            diceRoll[i] = 0;
            discardDie[i] = 0;
            }
            currentPlayerRolls = 0;
            rollButton.setDisable(false);
            messageLabel.setText("");
            outputTextArea.setText("");
            playerNumberTextField.setText(Integer.toString(playerNumber + 1));
            totalPlayersTextField.setText(Integer.toString(playerScore.length));
            rollNumberTextField.setText(Integer.toString(currentPlayerRolls));
            maxRollTextField.setText(Integer.toString(MAX_THROWS));
        }
    /***************************************************************************/
        private void update() {
            //Method used to update the GUI
            for (int i = 0; i < diceTextField.length; i++) {
            diceTextField[i].setText(Integer.toString(diceRoll[i]));
            }
            outputTextArea.setText("");
            calculateButton.setDisable(false);
            playerNumberTextField.setText(Integer.toString(playerNumber + 1));
            totalPlayersTextField.setText(Integer.toString(playerScore.length));
            rollNumberTextField.setText(Integer.toString(currentPlayerRolls));
            maxRollTextField.setText(Integer.toString(MAX_THROWS - currentPlayerRolls));
            }
    /***************************************************************************/
        private void startUP() {
        //Buttons
            rollButton = new Button ("ROLL");
            calculateButton = new Button ("CALCULATE");
            endButton= new Button ("END TURN");

            discardButton = new Button[6];
            for (int i = 0; i < discardButton.length; i++){
            discardButton[i] = new Button("Discard " + (i+1));
            }
        //Labels
            playerNumberLabel = new Label("Player #");
            totalPlayersLabel = new Label("Total Players");
            rollNumberLabel = new Label("Roll #");
            maxRollLabel = new Label("Remaining Rolls");
        //TextFields
            diceTextField = new TextField[6];
            for (int k = 0; k < diceTextField.length; k++){
            diceTextField[k] = new TextField("");
            }
            for (int j = 0; j < diceTextField.length; j++) {
            diceTextField[j].setDisable(true);
            }   
            playerNumberTextField = new TextField ();
            playerNumberTextField.setDisable(true);
            totalPlayersTextField = new TextField ();
            totalPlayersTextField.setDisable(true);
            rollNumberTextField = new TextField ();
            rollNumberTextField.setDisable(true);
            maxRollTextField = new TextField ();
            maxRollTextField.setDisable(true);
            messageLabel = new Label ();
            outputTextArea = new TextArea();

            topPane = new GridPane();
            centerPane = new GridPane();
            outputPane = new GridPane();
            bottomPane = new HBox();
            mainPane = new BorderPane();
        }
    /***************************************************************************/
        private void sculptGUI() {
            //setting textFields parameters
            playerNumberLabel.setPrefWidth(85);
            totalPlayersLabel.setPrefWidth(85);
            rollNumberLabel.setPrefWidth(85);
            maxRollLabel.setPrefWidth(85);

            for (int i = 0; i < diceTextField.length; i++) {
            diceTextField[i].setPrefWidth(50);
            }
            playerNumberTextField.setPrefWidth(85);
            totalPlayersTextField.setPrefWidth(85);
            maxRollTextField.setPrefWidth(85);
            rollNumberTextField.setPrefWidth(85);
            outputTextArea.setPrefSize(300,350);
            //sculpting topPane with GUI elements
            topPane.setHgap(15);
            topPane.setVgap(5);
            topPane.setPrefHeight(100);
            topPane.setAlignment(Pos.CENTER);
                //placement of top pane
            topPane.add(playerNumberLabel, 0, 0);
            topPane.add(playerNumberTextField, 0, 1);
            topPane.add(totalPlayersLabel, 1, 0);
            topPane.add(totalPlayersTextField, 1, 1);
            topPane.add(maxRollLabel, 2, 0);
            topPane.add(maxRollTextField, 2, 1);
            topPane.add(rollNumberLabel, 3, 0);
            topPane.add(rollNumberTextField, 3, 1);
            //sculpting centerPane with GUI elements
            centerPane.setHgap(20);
            centerPane.setVgap(5);
            centerPane.setAlignment(Pos.CENTER);
            centerPane.setHalignment(messageLabel ,HPos.CENTER);
                //placements in top line 
            centerPane.add(diceTextField[0], 0, 0);
            centerPane.add(discardButton[0], 0, 1);
            centerPane.add(diceTextField[1], 0, 2);
            centerPane.add(discardButton[1], 0, 3);
                //placement in 2nd line 
            centerPane.add(diceTextField[2], 1, 0);
            centerPane.add(discardButton[2], 1, 1);
            centerPane.add(diceTextField[3], 1, 2);
            centerPane.add(discardButton[3], 1, 3);
                //placement in 3rd line
            centerPane.add(diceTextField[4], 2, 0);
            centerPane.add(discardButton[4], 2, 1);
            centerPane.add(diceTextField[5], 2, 2);
            centerPane.add(discardButton[5], 2, 3);
            centerPane.add(messageLabel, 0, 4, 3, 1);
            //added seperate pane to house the output in one cell,
            //and the dice controls in another (for proper centering)
            outputPane.add(centerPane,0,0);
            outputPane.add(outputTextArea, 0, 1);
            outputPane.setHgap(20);
            outputPane.setVgap(5);
            outputPane.setAlignment(Pos.CENTER);
            //sculpting bottomPane with GUI elements
            bottomPane.setSpacing(25);
            bottomPane.getChildren().addAll(rollButton, calculateButton, endButton);
            bottomPane.setAlignment(Pos.CENTER);
            bottomPane.setPrefHeight(100);
            //setting the main display with seperate panes
            mainPane.setTop(topPane);
            mainPane.setCenter(outputPane);
            mainPane.setBottom(bottomPane);
            //setting up the scene and stage
            scene = new Scene(mainPane, 500, 500);
            primaryStage = new Stage();
            primaryStage.setTitle ("Sevens Game (Craps) GUI");
            primaryStage.setScene(scene);
            primaryStage.show();
        }//end of sculptGUI
    /***************************************************************************/
    }//end of SevensGameGUI

0 个答案:

没有答案