如何为石头剪刀布蜥蜴Spock程序创建GUI

时间:2019-07-03 15:54:33

标签: java

我制作了石头,纸,剪刀,蜥蜴,史波克。我现在想开始构建GUI,但是我不知道从哪里开始。此刻,我已经使用弹出窗口来完成所有肮脏的工作。但是我希望能够将所有内容集成到单个GUI上,并且在GUI中也显示“历史记录”。

如果我的问题含糊,措辞错误或在错误的地方,我深表歉意。同时,我还要感谢所有花时间提出一些见解,建议或更正的人。

import java.util.Scanner;
import javax.swing.JOptionPane;

public class RockPaperscissors2 {

    static final int HISTORY_LIMIT = 5;
    static final int RAND_LIMIT = 5;
    static final int NUM_ERROR = 0;
    static final int NUM_ROCK = 1;
    static final int NUM_PAPER = 2;
    static final int NUM_SCISSORS = 3;
    static final int NUM_LIZARD = 4;
    static final int NUM_SPOCK = 5;

    static final String STR_ERROR = "ERROR";
    static final String STR_ROCK = "Rock";
    static final String STR_PAPER = "Paper";
    static final String STR_SCISSORS = "Scissors";
    static final String STR_LIZARD = "Lizard";
    static final String STR_SPOCK = "Spock";

    static final String CUTS = " cuts ";
    static final String COVERS = " covers ";
    static final String CRUSHES= " crushes ";
    static final String POISONS = " poisons ";
    static final String SMASHES = " smashes ";
    static final String DECAPITATES = " decapitates ";
    static final String EATS = " eats ";
    static final String DISPROVES = " disproves ";
    static final String VAPORIZES = " vaporizes ";
    static final String SMASHES2 = " crushes ";

    static final int PLAYER_TIE = 0;
    static final int PLAYER_1 = 1;
    static final int PLAYER_2 = 2;

    static final String PC_NAME = "Computer";

    public static void main(String[] args) {

        Scanner input = new Scanner(System.in);

        boolean continuing = false;
        int player1Choice = 0; 
        int player2Choice = 0;
        int winner = 0;
        int phrase = 0;
        int inputJOP = 0;
        int PC_Count = 0;
        int Player_count = 0;
        int tie = 0;
        int i = -1;

        String[] history = new String[HISTORY_LIMIT];
        String playerName = "";

        do
        {
            Object[] start = {"Yes", "No", "History"};
            inputJOP = start(start);
            if(inputJOP == 2) {
                Object[] insideStart = {"Yes", "No"};
                history(history);
                inputJOP = start(insideStart);
            }//end if
            if(inputJOP != 0)
                continuing = false;
            else
            {
                continuing = true;
                if(playerName == "")
                    playerName = getName();
                player1Choice = getComputerChoice();
                player2Choice = getChoice(input);
                if(player2Choice == 6)
                    break;
                winner = determineWinner(player1Choice, player2Choice);
                phrase = determinePhrase(player1Choice, player2Choice);
                if (winner == PLAYER_1) {
                  PC_Count += 1;
                  JOptionPane.showMessageDialog(null, "      " + choiceNumToString(player1Choice) + getPhrase(phrase) + choiceNumToString(player2Choice) 
                          + " \n          Computer wins!");
                  i++;
                  setHistory(history, i, PC_NAME, choiceNumToString(player1Choice), getPhrase(phrase), choiceNumToString(player2Choice));
                }//end if
                else if (winner == PLAYER_2) {
                    Player_count += 1;
                    JOptionPane.showMessageDialog(null, "      " + choiceNumToString(player2Choice) + getPhrase(phrase) + choiceNumToString(player1Choice)
                            + "\n              You win!");
                    i++;
                    setHistory(history, i, playerName, choiceNumToString(player2Choice), getPhrase(phrase), choiceNumToString(player1Choice));
                }//end if
                else {
                    JOptionPane.showMessageDialog(null, "    Computer chose: " +
                            choiceNumToString(player1Choice) +
                            "\n         You chose: " + choiceNumToString(player2Choice) + "\n         Tie game, bummer!");
                    tie += 1;
                    i++;
                    setHistory(history, i, choiceNumToString(player1Choice));
                }//end else
            }//end else
        }//end do loop 
        while(continuing == true);
            stats(playerName, Player_count, PC_Count, tie);
    }//end main

    public static int getChoice(Scanner input)
    {
        int choice = NUM_ERROR;
        Object[] possible = {STR_ROCK, STR_PAPER, STR_SCISSORS, STR_LIZARD, STR_SPOCK, "Quit"};
        Object selection = JOptionPane.showOptionDialog(null, "                                                      OK, let's play! "
                + "\n                                                    Please pick one.", STR_ROCK + " " + STR_PAPER + " " + 
                STR_SCISSORS + " " + STR_LIZARD + " " + STR_SPOCK, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, possible, possible[0]);
        choice = ((int)selection + 1);
        return choice;
        //return choice;
    }//end getChoice

    public static int getComputerChoice()
    {
       int choice = 0;
       choice = (int)(Math.floor(Math.random() * RAND_LIMIT + 1));
       return choice;
    }//end getComputerChoice

    public static String choiceNumToString(int choiceNum)
    {
       String choice = "";
       switch(choiceNum)
       {
          case 1:
             choice =  STR_ROCK;
             break;
          case 2:
             choice =  STR_PAPER;
             break;
          case 3:
             choice =  STR_SCISSORS;
             break;
          case 4:
              choice = STR_LIZARD;
              break;
          case 5:
              choice = STR_SPOCK;
              break;
          default:
             choice =  STR_ERROR;
             break;
       }//end switch
       return choice;
    }//end ChoiceNumToString

    public static String getPhrase(int choiceNum)
    {
       String choice = "";
       switch(choiceNum)
       {
          case 1:
             choice = CUTS;
             break;
          case 2:
             choice = COVERS;
             break;
          case 3:
             choice = CRUSHES;
             break;
          case 4:
              choice = POISONS;
              break;
          case 5:
              choice = SMASHES;
              break;
          case 6:
              choice = DECAPITATES;
              break;
          case 7:
              choice = EATS;
              break;
          case 8:
              choice = DISPROVES;
              break;
          case 9: 
              choice = VAPORIZES;
              break;
          case 10:
              choice = SMASHES2;
              break;
          default:
             choice =  STR_ERROR;
             break;
       }//end switch
       return choice;
    }//end getPhrase

    public static int determinePhrase(int choice1, int choice2) {
        if((choice1 == 2 && choice2 == 3) || (choice1 == 3 && choice2 == 2))
            return 1;//cuts
        else if((choice1 == 1 && choice2 == 2) || (choice1 == 2 && choice2 == 1))
            return 2;//covers
        else if((choice1 == 1 && choice2 == 4) || (choice1 == 4 && choice2 == 1))
            return 3;//crushes
        else if((choice1 == 4 && choice2 == 5) || (choice1 == 5 && choice2 == 4))
            return 4;//poisons
        else if((choice1 == 1 && choice2 == 3) || (choice1 == 3 && choice2 == 1) 
                || (choice1 == 3 && choice2 == 5) || (choice1 == 5 && choice2 == 3))
            return 5;//shashes
        else if((choice1 == 3 && choice2 == 4) || (choice1 == 4 && choice2 == 3))
            return 6;//decapitates
        else if((choice1 == 2 && choice2 == 4) || (choice1 == 4 && choice2 == 2))
            return 7;//eat      
        else if((choice1 == 2 && choice2 == 5) || (choice1 == 5 && choice2 == 2))
            return 8;//disproves    
        else if((choice1 == 1 && choice2 == 5) || (choice1 == 5 && choice2 == 1))
            return 9;//vaporizes
        else
            return 0;//error
    }//end determinePhrase

    public static int determineWinner(int choice1, int choice2)
    {
        if((choice1 == 1 && choice2 == 3) || (choice1 == 1 && choice2 == 4) || (choice1 == 2 && choice2 == 1) || (choice1 == 2 && choice2 == 5)
                || (choice1 == 3 && choice2 == 2) || (choice1 == 3 && choice2 == 4) || (choice1 == 4 && choice2 == 2) || (choice1 == 4 && choice2 == 5)
                || (choice1 == 5 && choice2 == 1) || (choice1 == 5 && choice2 == 3))
            return 1;//PC win
         else if((choice1 == 1 && choice2 == 2) || (choice1 == 1 && choice2 == 5) || (choice1 == 2 && choice2 == 3) || (choice1 == 2 && choice2 == 4)
                 || (choice1 == 3 && choice2 == 1) || (choice1 == 3 && choice2 == 5) || (choice1 == 4 && choice2 == 1) || (choice1 == 4 && choice2 == 3)
                 || (choice1 == 5 && choice2 == 2) || (choice1 == 5 && choice2 == 4))
            return 2;//player win
         else //((choice1 == 3 && choice2 == 3) || (choice1 == 1 && choice2 == 1) || (choice1 == 2 && choice2 == 2) 
             //|| (choice1 == 4 && choice2 == 4) || (choice1 == 5 && choice2 == 5))
            return 0;
    }//enddetermineWinner

    public static void history(String[] history) {
        for(int j = 0; j < HISTORY_LIMIT; j++)
            System.out.println(history[j]);
    }//end history()

    public static void setHistory(String[] history, int i, String winner, String choice1, String phrase, String choice2) {
        history[i] = (winner + " wins! " + choice1 + " " + phrase + " " + choice2 + ".");
    }//end setHistory

    public static void setHistory(String[] history, int i, String choice) {
        history[i] = ("TIE! You both chose " + choice + ".");
    }//end setHistory

    public static String getName() {
        String name = "";
        name = JOptionPane.showInputDialog("First let's get your name. \nPlease enter your name below.");
        return name;
    }//end getName

    public static void stats(String playerName, int Player_count, int PC_Count, int tie) {
        JOptionPane.showMessageDialog(null, "                STATS:" +
                "\n            " + playerName + "'s wins: " + Player_count + 
                "\n         " + "Computer wins: " + PC_Count +
                "\n                 " + "Ties: " + tie +
                "\n\n             Good-Bye! \n    Thank you for playing!");
    }//end stats()

    public static int start(Object[] start) {
        int choice = JOptionPane.showOptionDialog(null, "       " + STR_ROCK + ", " + STR_PAPER + ", " + STR_SCISSORS + ", " + 
                STR_LIZARD + ", " + STR_SPOCK + "\n" + "                            Wanna play?", STR_ROCK + ", " + STR_PAPER + ", " + STR_SCISSORS + ", " + 
                STR_LIZARD + ", " + STR_SPOCK, JOptionPane.YES_NO_OPTION, JOptionPane.PLAIN_MESSAGE, null, start, start[0]);
        return choice;
    }//end start()


}//end class RockPaperScissors2

我是新来的,而且是编程的新手。我正在当地的大学上课,目前正在上Java课。我对编码真的很感兴趣,并且因为我的课程进度而感到无聊。因此,我接受了我们的一项任务,并决定继续努力。 现在,我首先要说的是,如果有些东西看起来很奇怪,对不起!我只是按照我被“教”的方式做事。

0 个答案:

没有答案