将控制台代码转换为JOptionPane

时间:2016-12-28 15:55:16

标签: java swing user-interface joptionpane

我已经被要求创建一个我选择的星球大战主题游戏,必须使用JOptionPane而不是控制台来玩。我设置主菜单和基本的东西很好,但其余的我不知道该怎么做。

我的游戏是战舰游戏,到目前为止它在控制台中运行,但是我不知道该怎么做才是网格工作。游戏必须以电影中的BB-8角色为特色。我的代码在这里:

import java.util.*;
import javax.swing.*;
public class BattleShips {

    public static void main(String[] args) {

        Scanner a = new Scanner (System.in);
        System.out.print("Welcome To Battleships!\n\nSelect One Of The Following Options:\n\n1. Play\n2. How To Play\n3. Exit\n\n");
        String choice = a.nextLine();
        if(choice.equals("2")){
            System.out.print("\nThe rules of battleships are simple."
                    + "\n\n- Your aim is to destroy all enemy ships, the first to destoy all opposing ships first wins."
                    + "\n- You will have 4 ships all of which are 3 spaces long."
                    + "\n- You will have the choice of placing your ships anywhere you wish, "
                    + "simply by deciding if you want it vertical or horizontal and entering the centre coordinate."
                    + "\n- You will then have to guess where the enemy ships are and enter the coordinates, "
                    + "if you hit the enemy ships you will recieve points."
                    + "\n- If your ships are hit, you will lose a point.");
        }
        String[][] table = new String[7][7];
        String[] letter = {"A","B","C","D","E","F"};
        String[] numbers = {"0","1","2","3","4","5"};
        table[0][0] = " ";
        for(int i = 1 ;i < 7 ;i++){
            table[i][0] = (letter[i-1]);
            for(int j = 1 ;j < 7 ;j++){
                table[0][j] = (numbers[j-1]);
            }
        }
        for(int i = 1 ;i < 7 ;i++){
            for(int j = 1 ;j < 7 ;j++){
                table[i][j] = "~";
            }
        }
        String[][] hmtable = new String[7][7];
        String[] hmletter = {"A","B","C","D","E","F"};
        String[] hmnumbers = {"0","1","2","3","4","5"};
        hmtable[0][0] = " ";
        for(int i = 1 ;i < 7 ;i++){
            hmtable[i][0] = (hmletter[i-1]);
            for(int j = 1 ;j < 7 ;j++){
                hmtable[0][j] = (hmnumbers[j-1]);
            }
        }
        for(int i = 1 ;i < 7 ;i++){
            for(int j = 1 ;j < 7 ;j++){
                hmtable[i][j] = "~";
            }
        }
        String[][] table2 = new String[7][7];
        String[] letter2 = {"A","B","C","D","E","F"};
        String[] numbers2 = {"0","1","2","3","4","5"};
        table2[0][0] = " ";
        for(int i = 1 ;i < 7 ;i++){
            table2[i][0] = (letter2[i-1]);
            for(int j = 1 ;j < 7 ;j++){
                table2[0][j] = (numbers2[j-1]);
            }
        }
        for(int i = 1 ;i < 7 ;i++){
            for(int j = 1 ;j < 7 ;j++){
                table2[i][j] = (letter2[i-1])+(numbers2[j-1]);
            }
        }
        String[][] AItable = new String[7][7];
        String[] AIletter = {"A","B","C","D","E","F"};
        String[] AInumbers = {"0","1","2","3","4","5"};
        table[0][0] = " ";
        for(int i = 1 ;i < 7 ;i++){
            AItable[i][0] = (AIletter[i-1]);
            for(int j = 1 ;j < 7 ;j++){
                AItable[0][j] = (AInumbers[j-1]);
            }
        }
        for(int i = 1 ;i < 7 ;i++){
            for(int j = 1 ;j < 7 ;j++){
                AItable[i][j] = "~";
            }
        }
        boolean done = true;
        int counter = 0;
        while(done){
                int posnum = 1 + (int)(Math.random() * 6);
                int posletter = 1 + (int)(Math.random() * 6);
                int vorh = 1 + (int)(Math.random() * 2);
                if(vorh==1&&posnum==1){
                    posnum = posnum+1;
                }
                if(vorh==1&&posnum==6){
                    posnum = posnum-1;
                }
                if(vorh==2&&posletter==1){
                    posletter = posletter+1;
                }
                if(vorh==2&&posletter==6){
                    posletter = posletter-1;
                }
                if(vorh==(1)&&AItable[posletter][posnum].equals("~")&&AItable[posletter][posnum+1].equals("~")&&AItable[posletter][posnum-1].equals("~")){
                    AItable[posletter][posnum] = ("X");
                    AItable[posletter][posnum+1] = ("X");
                    AItable[posletter][posnum-1] = ("X");
                    counter = counter + 1;
                }
                if(vorh==(2)&&AItable[posletter][posnum].equals("~")&&AItable[posletter+1][posnum].equals("~")&&AItable[posletter-1][posnum].equals("~")){
                    AItable[posletter][posnum] = ("X");
                    AItable[posletter+1][posnum] = ("X");
                    AItable[posletter-1][posnum] = ("X");
                    counter = counter + 1;
                    }
                if(counter == 4){
                    done=false;
                }
                }
        /*AItable[0][0] = " ";
        System.out.println("The AI's Ships");
        for(int i = 0 ;i < 7 ;i++){
            for(int j = 0 ;j < 7 ;j++){
                System.out.printf("%-4s",AItable[i][j]);
            }
            System.out.println();
        }*/
        System.out.println("\n\nHere are all the coordinates where you may place your ships:\n");
        for(int i = 0 ;i < 7 ;i++){
            for(int j = 0 ;j < 7 ;j++){
                System.out.printf("%-4s",table[i][j]);
            }
            System.out.println();
        }
        boolean choose = true;
        int counter2 = 0;
        int i = 1;
        while(choose){
                System.out.print("\nDo you want ship #"+(i)+" to be vertical or horizontal? (V/H) > ");
                String vorh = a.nextLine().toUpperCase();
                System.out.print("Please enter the centre coordinate of ship #"+(i)+" > ");
                String input = a.nextLine().toUpperCase();
                for(int l = 1 ;l < 7 ;l++){
                    for(int j = 1 ;j < 7 ;j++){
                        if(input.equals(table2[l][j])){
                            if(vorh.equals("V")&&input.charAt(0)==('A')){
                                System.out.print("Sorry, the position you have chosen is out of bounds, please choose another.");
                            }
                            else if(vorh.equals("V")&&input.charAt(0)==('F')){
                                System.out.print("Sorry, the position you have chosen is out of bounds, please choose another.");
                            }
                            else if(vorh.equals("V")){
                                table[l][j] = ("X");
                                table[l+1][j] = ("X");
                                table[l-1][j] = ("X");
                                counter2 = counter2 + 1;
                                i = i+1;
                            }
                            if(vorh.equals("H")&&input.charAt(1)==('0')){
                                System.out.print("Sorry, the position you have chosen is out of bounds, please choose another.");
                            }
                            else if(vorh.equals("H")&&input.charAt(1)==('5')){
                                System.out.print("Sorry, the position you have chosen is out of bounds, please choose another.");
                            }
                            else if(vorh.equals("H")){
                                table[l][j] = ("X");
                                table[l][j+1] = ("X");
                                table[l][j-1] = ("X");
                                counter2 = counter2 + 1;
                                i = i+1;
                            }
                        }
                    }
                }
            //}
            if (counter2 == 4){
                choose = false;
            }
            System.out.println();
            for(int p = 0 ;p < 7 ;p++){
                for(int j = 0 ;j < 7 ;j++){
                    System.out.printf("%-4s",table[p][j]);
                }
                System.out.println();
            }
        }
        System.out.print("\nNow You Must Destroy All Enemy Ships!");
        boolean destroy = true;
        int destroyed = 0;
        int aidestroyed = 0;
        while(destroy){
            System.out.print("\nPlease enter a coordinate > ");
            String input = a.nextLine().toUpperCase();
            for(int l = 1 ;l < 7 ;l++){
                for(int j = 1 ;j < 7 ;j++){
                    if(input.equals(table2[l][j])){
                        if(AItable[l][j].equals("X")){
                            System.out.println("HIT!\n");
                            destroyed = destroyed + 1;
                            hmtable[l][j] = "!";
                        }
                        else{
                            System.out.println("MISS!\n");
                            hmtable[l][j] = "O";
                        }
                        System.out.println();
                        for(int p = 0 ;p < 7 ;p++){
                            for(int q = 0 ;q < 7 ;q++){
                                System.out.printf("%-4s",hmtable[p][q]);
                            }
                            System.out.println();
                        }
                    }
                }
            }
            System.out.println("The Enemy Has Chosen A Coordinate.");
            int posnum = 1 + (int)(Math.random() * 6);
            int posletter = 1 + (int)(Math.random() * 6);
            if(table[posletter][posnum].equals("X")){
                System.out.println("You Have Been Hit By The Enemy!\n");
                table[posletter][posnum] = ("!");
                aidestroyed = aidestroyed + 1;
            }
            else if(table[posletter][posnum].equals("~")){
                System.out.println("The Enemy Missed!\n");
                table[posletter][posnum] = ("O");
            }
            else if(!table[posletter][posnum].equals("!")||!table[posletter][posnum].equals("X")){
                System.out.println("The Enemy Missed!\n");
            }
            System.out.println();
            for(int p = 0 ;p < 7 ;p++){
                for(int q = 0 ;q < 7 ;q++){
                    System.out.printf("%-4s",table[p][q]);
                }
                System.out.println();
            }
            if(destroyed == 12){
                System.out.print("Great Job! You Have Destroyed All Enemy Ships!");
                destroy = false;
                break;
            }
            if(aidestroyed == 12){
                System.out.print("Unlucky! The Enemy Have Destroyed All Of Your Ships!");
                destroy = false;
                break;
            }
        }
  }
}

1 个答案:

答案 0 :(得分:1)

要使用JOptionPane使用来接收用户的输入:

String inputString = JOptionPane.showInputDialog(null, "Enter input");

您只需将scanner.nextXXX()替换为该行即可。

注意:如果用户按下null上的X按钮,结果可能是JOptionPane,您应该检查这样:

String inputString = JOptionPane.showInputDialog(null, "Enter input");
if(inputString!=null){
    //and then use the inputString    
...
}