Как создать графический интерфейс для программы Rock Paper Scissors Lizard Spock - PullRequest
0 голосов
/ 03 июля 2019

Я сделал Камень, Бумагу, Ножницы, Ящерицу, Спока. Я нахожусь в точке, я хотел бы начать создавать графический интерфейс, но я не знаю, с чего начать. На данный момент у меня есть всплывающие окна, используемые для всей грязной работы. Но я хотел бы иметь возможность получить все в одном графическом интерфейсе, а также отобразить «историю» в графическом интерфейсе.

Я прошу прощения, если мой вопрос расплывчат, неправильно сформулирован или не в том месте. Кроме того, я благодарю всех, кто потратил время, чтобы высказать некоторые соображения, предложения или исправления.

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. Я действительно интересуюсь кодированием, и мне стало скучно идти в темпе моего класса. Итак, я взял одно из наших заданий и решил просто продолжить работу над ним. Теперь я должен начать с того, что если что-то выглядит странно, извините! Я просто делаю вещи так, как меня "учили".

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...