How to get game array to work properly.

  • (2 Pages)
  • +
  • 1
  • 2

25 Replies - 962 Views - Last Post: 06 December 2012 - 09:58 AM Rate Topic: -----

#1 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

How to get game array to work properly.

Posted 19 November 2012 - 04:38 AM

I'm a Computer Science student at Palomar College and am programming a batttleship game with a 2-D array. Could someone tell me how to make the array work properly? Thanks for any help.

Attached File(s)

  • Attached File  Lab7.zip (17.81K)
    Number of downloads: 67

Is This A Good Question/Topic? 0
  • +

Replies To: How to get game array to work properly.

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,437
  • Joined: 23-August 08

Re: How to get game array to work properly.

Posted 19 November 2012 - 04:42 AM

Don't post questions in Introduce Yourself, and you should post the problematic code IN your post, within code tags

:code:/>

I took the trouble of downloading and unzipping your project, because you couldn't even be bothered to tell us the language, never mind exactly what problems you're having.

Moved to Java
Was This Post Helpful? 1
  • +
  • -

#3 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 19 November 2012 - 05:17 AM

View PostJackOfAllTrades, on 19 November 2012 - 04:42 AM, said:

Don't post questions in Introduce Yourself, and you should post the problematic code IN your post, within code tags

:code:/>/>

I took the trouble of downloading and unzipping your project, because you couldn't even be bothered to tell us the language, never mind exactly what problems you're having.

Moved to Java


Thank you for your reply to my question about the battleship array and moving the question to Java. I was having trouble getting the program to locate the shot in the proper space and retaining the game board with the hits and misses. Any help is greatly appreciated.


:code:/>/>/

**
 * Code to play War Ship.
 * 
 * @Quang Pham 
 * @10/8/12
 */
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;

public class WarShip
{
    private Player p1;
    private Player p2;
    //p2 = computer
    private static String id = "ws";
    String firstName;
    String lastName;
    String shipName;

    private static int idNum = 100;
    private Ship s1;
    int numberOfShots = 0;
    int guessCol = 0;
    int guessRow = 0;
    int win = 0;
    int loss = 0;
    int games = 0;

    Scanner keyboard = new Scanner(System.in);

    static final int gameSize = 5;

    char [][] game = new char[gameSize][gameSize];
    char [][] enemy = new char[gameSize][gameSize];

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, I suggest 5 different locations for the ship.  Call
    //the random number generator to choose which of the 5 locations to use.    You may be creative and find
    //different techniques, but the ship is not placed in the same place every game.

    public WarShip()
    { 
        String msg = "Welcome to War Ship! \n\n";
        msg = msg + " You will be playing against the computer, her name is Terrie!";
        JOptionPane.showMessageDialog(null, msg);
        p1 = new Player();
        p2 = new Player("Computer");
        s1 = new Ship();//let the user enter the name of the ship in the default constructor
        initBoards();

    }

    public void playGame()
    {
        String s0 = null;
        int count = 0;
        //int numberOfShots = 0;
        s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15):  ");
        numberOfShots = Integer.parseInt(s0);

        while (count < numberOfShots)
        {
            initBoards();
            generateRandomShipLocation();
            displayMenu();
            hitOrMiss();
            //displayResults();
            count++;
        }
    }     

    private void initBoards()
    {
        int [][]GameBoard = new int [6][6];
        int [][]EnemyBoard = new int [6][6];

        final int ROWS = 6;
        final int COLS = 6;

        //initialize both boards
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                GameBoard[col][row] = 0;
            }
        }

        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                EnemyBoard[col][row] = 0;
            }
        }
    }

    public String generateRandomShipLocation()
    {
        //5 ship locations
        String p2Choice = " ";
        String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
        int shipLocation = 0;
        int shipValues;
        initBoards();
        int[][]EnemyBoard = new int[5][5];

        shipLocation = (int)(Math.random() * 10)%5;

        switch (shipLocation)
        {
            case 0:
            p2Choice =  A2;
            //shipValues = A2, A3, B2, B3 
            shipValues = EnemyBoard[1][2];
            shipValues = EnemyBoard[1][3]; 
            shipValues = EnemyBoard[2][2]; 
            shipValues = EnemyBoard[2][3];
            break;
            case 1:
            p2Choice =  B3;
            //shipValues = B3, B4, C3, C4;
            shipValues = EnemyBoard[2][3];
            shipValues = EnemyBoard[2][4];
            shipValues = EnemyBoard[3][3];
            shipValues = EnemyBoard[3][4];
            break;
            case 2:
            p2Choice =  C4;
            //shipValues = C3, D3, C4, D4;
            shipValues = EnemyBoard[3][4];
            shipValues = EnemyBoard[4][4];
            shipValues = EnemyBoard[3][4];
            shipValues = EnemyBoard[4][4];
            break;
            case 3:
            p2Choice =  A1;
            //shipValues = A1, A2, B1, B2;
            shipValues = EnemyBoard[1][1];
            shipValues = EnemyBoard[1][2];
            shipValues = EnemyBoard[2][1];
            shipValues = EnemyBoard[2][2];
            break;
            case 4:
            p2Choice =  B2;
            //shipValues = B2, B3, C2, C3;
            shipValues = EnemyBoard[2][2];
            shipValues = EnemyBoard[2][3];
            shipValues = EnemyBoard[3][2];
            shipValues = EnemyBoard[3][3];
            break;

        }
        return p2Choice;
        //return shipValues[][];
    }

    public int displayMenu()
    //method to display the main menu
    {
        GameBoard();
        //EnemyBoard();

        //displayGameBoard(game);
        //displayEnemyBoard(game);

        //displayWarShipArray();

        int choice = 0;
        int menu = 0;
        //int guessCol = 0;
        //int guessRow = 0;
        int COLS;
        int ROWS;
        String p2Choice = "  ";
        System.out.println("Menu:");
        System.out.println("1. Fire Shot");
        System.out.println("2. Show Solution");
        System.out.println("3. Quit");
        System.out.println("Enter menu choice.");

        choice = keyboard.nextInt();
            switch(choice)
            {
                case 1:  System.out.println("Enter a Column Number:  ");
                guessCol = keyboard.nextInt();
                //guessCol = columnCheck();
                System.out.println("Enter a Row Number:  ");
                guessRow = keyboard.nextInt();
                //guessRow = rowCheck();
                hitOrMiss(); 
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the war ship was.");
                locationOfEnemyShip();
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                break;
                default: System.out.println("Invalid Input.");
            }   
        return menu;
    }

    public static void GameBoard()
    {
        System.out.println("   WAR SHIP GAME (Game Board) ");
        final int ROWS = 6;
        final int COLS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};
        System.out.println("  ");
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            { System.out.print("  " + values[row][col]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public static void EnemyBoard()
    {
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        final int ROWS = 6;
        final int COLS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};
        System.out.println("  ");
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            { System.out.print("  " + values[row][col]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public void displayGameBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Game Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayEnemyBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Enemy Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayWarShipArray()
    {
        int number = 1;
        System.out.println("  ");
        System.out.println("   A   B   C   D   E   ");
        for (int i = 0; i < 5; i++)
        {  
            System.out.println(number +  " __" + "  " + "__" + "  " + "__" + "  " + "__" + "  " + "__" );
            number++;
        }
        System.out.println("  ");
    }

    public void locationOfEnemyShip()
    {
        int COLS = 5;
        int ROWS = 5;
        int col = 0;
        int row = 0;

        generateRandomShipLocation();

        int [][]EnemyBoard = new int[6][6];
        int [][]shipValues = new int[6][6];
        EnemyBoard[col][row] = shipValues[col][row];

        EnemyBoard[col][row] = '+';

        for(col = 0; col < COLS; col++)
        {
            for(row = 0; row < ROWS; row++)
            {
                System.out.print("  " + EnemyBoard[col][row]);
            }
            System.out.println("\n");
        }

        EnemyBoard(); 
        displayMenu();
    }

    public  void hitOrMiss()
    {
        //int guessCol = 0;
        //int guessRow = 0;
        int [][]GameBoard = new int[6][6];
        int shipValues = 0;
        int COLS = 5;
        int ROWS = 5;
        //print an x for shots
        //print an * for hits
        if(GameBoard[guessCol][guessRow] == shipValues)
        {
            GameBoard[guessCol][guessRow] = '*';
            System.out.println("Hit!");
            win++;
            for(int col = 0; col < COLS; col++)
            {
                for(int row = 0; row < ROWS; row++)
                {
                    System.out.print("__" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
        }
        else
        {
            GameBoard[guessCol][guessRow] = 'x';  
            System.out.println("Miss!");
            loss++;
            for(int col = 0; col < COLS; col++)
            {
                for(int row = 0; row < ROWS; row++)
                {
                    System.out.print("__" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
        }
        displayMenu();
        games++;
    }

    //public static int columnCheck();
    {
    }

    //public static int rowCheck();
    {
    }

    public void displayResults()
    {
        boolean WarShipSunk;

        System.out.println( numberOfShots + " shots were fired.");
        System.out.println( win + " shots hit.");
        System.out.println( loss + " shots missed.");
        System.out.println( games + " games played.");
        if (WarShipSunk= true)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
            System.out.println(p1);
            System.out.println(p2);
            System.out.println(s1);
            System.out.println( firstName );
            System.out.println( lastName );
            System.out.println( shipName );
        }
        else if(WarShipSunk = false)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
        }
    }

}


Attached File(s)

  • Attached File  Lab7.zip (17.81K)
    Number of downloads: 33

Was This Post Helpful? 0
  • +
  • -

#4 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 20 November 2012 - 03:44 PM

View PostQuang Pham, on 19 November 2012 - 04:38 AM, said:

I'm a Computer Science student at Palomar College and am programming a batttleship game with a 2-D array. Could someone tell me how to make the array work properly? Thanks for any help.
This is my updated code. Do you have ideas on how to make it better?

/**
 * Code to play War Ship.
 * 
 * @Quang Pham 
 * @10/8/12
 */
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;

public class WarShip
{
    private Player p1;
    private Player p2;
    //p2 = computer
    private static String id = "ws";
    private static int idNum = 100;
    private Ship s1;
    int numberOfShots = 0;
    int Win = 0;
    int Loss = 0;
    int guessCol = 0;
    int guessRow = 0;

    Scanner keyboard = new Scanner(System.in);

    static final int gameSize = 5;

    char [][] game = new char[gameSize][gameSize];
    char [][] enemy = new char[gameSize][gameSize];

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, I suggest 5 different locations for the ship.  Call
    //the random number generator to choose which of the 5 locations to use.    You may be creative and find
    //different techniques, but the ship is not placed in the same place every game.

    public WarShip()
    { 
        String msg = "Welcome to War Ship! \n\n";
        msg = msg + " You will be playing against the computer, her name is Terrie!";
        JOptionPane.showMessageDialog(null, msg);
        p1 = new Player(newId());
        p2 = new Player(newId(),"Terrie","Computer");
        s1 = new Ship();//let the user enter the name of the ship in the default constructor
        initBoards();

    }

    public void playGame()
    {
        String s0 = "  ";
        int count = 0;
        //int numberOfShots = 0;
        s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15):  ");
        numberOfShots = Integer.parseInt(s0);

        while (count < numberOfShots)
        {
            initBoards();
            generateRandomShipLocation();
            displayMenu();
            hitOrMiss();
            //displayResults();
            count++;

        }
    }     

    private void initBoards()
    {
        char [][]GameBoard = new char [6][6];
        char [][]EnemyBoard = new char [6][6];

        int COLS = 6;
        int ROWS = 6;

        //initialize both boards
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                GameBoard[col][row] = 0;
            }
        }

        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                EnemyBoard[col][row] = 0;
            }
        }
    }

    public void generateRandomShipLocation()
    {
        //5 ship locations
        String p2Choice = " ";
        String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
        int shipLocation = 0;
        int values =2;
        char shipValues = 0;
        initBoards();
        
        char[][]EnemyBoard = new char[6][6];
        //shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];

        shipLocation = (int)(Math.random() * 10)%5;
        switch (shipLocation)
        {
            case 0:
            p2Choice =  A2;
            //shipValues = A2, A3, B2, B3 
            shipValues = EnemyBoard[1][2] = '+';
            shipValues = EnemyBoard[1][3] = '+'; 
            shipValues = EnemyBoard[2][2] = '+'; 
            shipValues = EnemyBoard[2][3] = '+';
            break;
            case 1:
            p2Choice =  B3;
            //shipValues = B3, B4, C3, C4;
            shipValues = EnemyBoard[2][3] = '+';
            shipValues = EnemyBoard[2][4] = '+';
            shipValues = EnemyBoard[3][3] = '+';
            shipValues = EnemyBoard[3][4] = '+';
            break;
            case 2:
            p2Choice =  C4;
            //shipValues = C3, D3, C4, D4;
            shipValues = EnemyBoard[3][4] = '+';
            shipValues = EnemyBoard[4][4] = '+';
            shipValues = EnemyBoard[3][4] = '+';
            shipValues = EnemyBoard[4][4] = '+';
            break;
            case 3:
            p2Choice =  A1;
            //shipValues = A1, A2, B1, B2;
            shipValues = EnemyBoard[1][1] = '+';
            shipValues = EnemyBoard[1][2] = '+';
            shipValues = EnemyBoard[2][1] = '+';
            shipValues = EnemyBoard[2][2] = '+';
            break;
            case 4:
            p2Choice =  B2;
            //shipValues = B2, B3, C2, C3;
            shipValues = EnemyBoard[2][2] = '+';
            shipValues = EnemyBoard[2][3] = '+';
            shipValues = EnemyBoard[3][2] = '+';
            shipValues = EnemyBoard[3][3] = '+';
            break;

        }

        /*
        return p2Choice;

        return shipLocation;

        return shipValues;

        return values;

         */

    }
    public int displayMenu()
    //method to display the main menu
    {
        //GameBoard();
        //EnemyBoard();

        //displayGameBoard(game);
        //displayEnemyBoard(game);

        //displayWarShipArray();

        int choice = 0;
        int menu = 0;
        //int guessCol = 0;
        //int guessRow = 0;
        int COLS;
        int ROWS;
        String p2Choice = "  ";
        System.out.println("Menu:");
        System.out.println("1. Fire Shot");
        System.out.println("2. Show Solution");
        System.out.println("3. Quit");
        System.out.println("Enter menu choice.");

        choice = keyboard.nextInt();

        switch(choice)
        {
            case 1:  System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            //guessCol = columnCheck();
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            //guessRow = rowCheck();
            hitOrMiss(); 
            break;
            case 2:  System.out.println("Solution Shown:  ");
            System.out.println("Here's where the enemy ship was.");
            displayLocationOfEnemyShip();
            break;
            case 3:  System.out.println("Program Ended.");
            break;
            default: System.out.println("Invalid Input.");
        }   

        displayResults();
        return menu;
    }  

    public static void GameBoard()
    {
        System.out.println("   WAR SHIP GAME (Game Board) ");
        final int COLS = 6;
        final int ROWS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};

       
        System.out.println("  ");
        for (int col = 0; col < COLS; col++)
        {  for (int row = 0; row < ROWS; row++)
            { System.out.print("  " + values[col][row]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public static void EnemyBoard()
    {
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        final int COLS = 6;
        final int ROWS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};

        
        System.out.println("  ");
        for (int col = 0; col < COLS; col++)
        {  for (int row = 0; row < ROWS; row++)
            { System.out.print("  " + values[col][row]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public void displayGameBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Game Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayEnemyBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Enemy Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayWarShipArray()
    {
        int number = 1;
        System.out.println("  ");
        System.out.println("   A   B   C   D   E   ");
        for (int i = 0; i < 5; i++)
        {  
            System.out.println(number +  " __" + "  " + "__" + "  " + "__" + "  " + "__" + "  " + "__" );
            number++;
        }
        System.out.println("  ");
    }

    public  void hitOrMiss()
    {
        //int guessCol = 0;
        //int guessRow = 0;
        final int shipCOL = 2;
        final int shipROW = 2;
        char [][]GameBoard = new char[6][6];
        //int [][]shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
        int shipValues = 0;
        final int COLS = 6;
        final int ROWS = 6;
        //print an x for shots
        //print an * for hits

        System.out.println(" WAR SHIP GAME (Game Board) ");

        System.out.println("  ");

        if(GameBoard[guessCol][guessRow] == shipValues)
        {
           
            System.out.println("Hit!");
            
            GameBoard[guessCol][guessRow] = '*';
            GameBoard[1][0] = 'A';
            GameBoard[2][0] = 'B';
            GameBoard[3][0] = 'C';
            GameBoard[4][0] = 'D';
            GameBoard[5][0] = 'E';
            GameBoard[0][1] = '1';
            GameBoard[0][2] = '2';
            GameBoard[0][3] = '3';
            GameBoard[0][4] = '4';
            GameBoard[0][5] = '5';
          
            for(char col = 0; col < COLS; col++)
            {
                for(char row = 0; row < ROWS; row++)
                {
                    System.out.print("__" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addWin();
        }
        else
        {
             
            System.out.println("Miss!");
            System.out.println("  ");
            GameBoard[guessCol][guessRow] = 'x'; 
            GameBoard[1][0] = 'A';
            GameBoard[2][0] = 'B';
            GameBoard[3][0] = 'C';
            GameBoard[4][0] = 'D';
            GameBoard[5][0] = 'E';
            GameBoard[0][1] = '1';
            GameBoard[0][2] = '2';
            GameBoard[0][3] = '3';
            GameBoard[0][4] = '4';
            GameBoard[0][5] = '5';
            
            for(char col = 0; col < COLS; col++)
            {
                for(char row = 0; row < ROWS; row++)
                {
                    System.out.print(" _" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addLoss();
        }
        System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
        
        displayMenu();
    }
        
    //public static int columnCheck();
    {
    }

    //public static int rowCheck();
    {
    }

    public void displayLocationOfEnemyShip()
    {
        int shipLocation;
        int shipValues;
        final int COLS = 6;
        final int ROWS = 6;
        char[][]EnemyBoard = new char [6][6];
        char[][]EnemyShip = new char [2][2];

        generateRandomShipLocation();
        
        //shipValues = '+';  
        
        System.out.println(" ");

        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        
        //EnemyBoard[int col][int row] = '+';
        
            EnemyBoard[1][0] = 'A';
            EnemyBoard[2][0] = 'B';
            EnemyBoard[3][0] = 'C';
            EnemyBoard[4][0] = 'D';
            EnemyBoard[5][0] = 'E';
            EnemyBoard[0][1] = '1';
            EnemyBoard[0][2] = '2';
            EnemyBoard[0][3] = '3';
            EnemyBoard[0][4] = '4';
            EnemyBoard[0][5] = '5';
            
            for(char col = 0; col < COLS; col++)
            {
                for(char row = 0; row < ROWS; row++)
                {
                    System.out.print(" _" + EnemyBoard[col][row]);
                }
                System.out.println("\n");
            }
            
        System.out.println("  ");
        
    }

    public void displayResults()
    {
        int x = 0;
        int y = 0;
        boolean warShipSunk;
        System.out.println( numberOfShots + " shots were fired.");
        System.out.println( Win + " shots hit.");
        System.out.println( Loss + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk= true)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
        }
        else if(warShipSunk = false)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
        }
        System.out.println("  ");
    }

    private static String newId()
    {
        idNum++;
        return id + String.valueOf(idNum);
    }
}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#5 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 21 November 2012 - 03:35 PM

View PostQuang Pham, on 19 November 2012 - 04:38 AM, said:

I'm a Computer Science student at Palomar College and am programming a batttleship game with a 2-D array. Could someone tell me how to make the array work properly? Thanks for any help.


I'm trying to make the battleship (a 4 point 2x2 object) appear on the EnemyBoard. Does someone know of code which will do this? This is what I have so far.
[code]
public void displayLocationOfEnemyShip()
{
char col = 0;
char row = 0;
final int COLS = 6;
final int ROWS = 6;
char[][]EnemyBoard = new char [6][6];
char[][]EnemyShip = new char [2][2];

generateRandomShipLocation();

System.out.println(" ");

System.out.println(" WAR SHIP GAME (Enemy Board) ");


char shipValues1 = 0;
char shipValues2 = 0;
char shipValues3 = 0;
char shipValues4 = 0 ;

EnemyBoard[col][row] = '+';
EnemyShip[col][row] = '+';

EnemyBoard[1][0] = 'A';
EnemyBoard[2][0] = 'B';
EnemyBoard[3][0] = 'C';
EnemyBoard[4][0] = 'D';
EnemyBoard[5][0] = 'E';
EnemyBoard[0][1] = '1';
EnemyBoard[0][2] = '2';
EnemyBoard[0][3] = '3';
EnemyBoard[0][4] = '4';
EnemyBoard[0][5] = '5';

for(row = 0; row < ROWS; row++)
{
for(col = 0; col < COLS; col++)
{
System.out.print(" _" + EnemyBoard[col][row]);
}
System.out.println("\n");
}

System.out.println(" ");
System.out.println("p2Choice: " + p2Choice);
System.out.println("Ship location: " + shipLocation);
System.out.println("Ship Values1: " + shipValues1);
System.out.println("Ship Values2: " + shipValues2);
System.out.println("Ship Values3: " + shipValues3);
System.out.println("Ship Values4: " + shipValues4);


//System.out.println(EnemyBoard[col][row]);
//System.out.println(EnemyShip[col][row]);
System.out.println(" ");
}
I'd appreciate any help on this topic.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to get game array to work properly.

Posted 21 November 2012 - 08:19 PM

You will have to define this

Quote

Could someone tell me how to make the array work properly?

As far as I am concerned arrays work properly in Java so you will have to tell us what is your expected behaviour and what is the actual one.
Was This Post Helpful? 2
  • +
  • -

#7 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 22 November 2012 - 12:35 AM

I'm trying to connect my hitOrMiss method to my displayLocationOfEnemyShip method so that the computer knows when the EnemyShip has been hit. How do I connect the 2x2 ship to the guessCol and guessRow so that a hit (*) or a miss (x) is displayed on the GameBoard? Also, the GameBoard does not retain the hits and misses, but a new GameBoard is displayed each time. Do you know how to resolve any of these problems? I'd appreciate any insight you could give. Quang

/**
 * Code to play War Ship.
 * 
 * @Quang Pham 
 * @10/8/12
 */
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;

public class WarShip
{
    private Player p1;
    private Player p2;
    //p2 = computer
    private static String id = "ws";
    private static int idNum = 100;
    private Ship s1;
    int numberOfShots = 0;
    int Win = 0;
    int Loss = 0;
    int guessCol = 0;
    int guessRow = 0;
    int shipLocation = 0;
    char shipValues = 0;
    int p2Choice = 0;
       
    Scanner keyboard = new Scanner(System.in);

    static final int gameSize = 5;

    char [][] game = new char[gameSize][gameSize];
    char [][] enemy = new char[gameSize][gameSize];

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, I suggest 5 different locations for the ship.  Call
    //the random number generator to choose which of the 5 locations to use.    You may be creative and find
    //different techniques, but the ship is not placed in the same place every game.

    public WarShip()
    { 
        String msg = "Welcome to War Ship! \n\n";
        msg = msg + " You will be playing against the computer, her name is Terrie!";
        JOptionPane.showMessageDialog(null, msg);
        p1 = new Player(newId());
        p2 = new Player(newId(),"Terrie","Computer");
        s1 = new Ship();//let the user enter the name of the ship in the default constructor
        initBoards();

    }

    public void playGame()
    {
        String s0 = "  ";
        int count = 0;
        //int numberOfShots = 0;
        s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15):  ");
        numberOfShots = Integer.parseInt(s0);

        while (count < numberOfShots)
        {
            initBoards();
            generateRandomShipLocation();
            displayMenu();
            hitOrMiss();
            //displayResults();
            count++;

        }
    }     

    private void initBoards()
    {
        char [][]GameBoard = new char [6][6];
        char [][]EnemyBoard = new char [6][6];

        int COLS = 6;
        int ROWS = 6;

        //initialize both boards
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                GameBoard[col][row] = 0;
            }
        }

        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                EnemyBoard[col][row] = 0;
            }
        }
    }

    public String generateRandomShipLocation()
    {
        //5 ship locations
        String p2Choice = " ";
        String A2 = " ", B3= " ", C4= " ", A1= " ", B2 = " ";
        int shipLocation = 0;
        char shipValues = 0;
        initBoards();
        
        char[][]EnemyBoard = new char[6][6];
        //shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];

        shipLocation = (int)(Math.random() * 10)%5;
        switch (shipLocation)
        {
            case 0:
            p2Choice =  A2;
            //shipValues = A2, A3, B2, B3 
            shipValues = EnemyBoard[1][2] = '+';
            shipValues = EnemyBoard[1][3] = '+'; 
            shipValues = EnemyBoard[2][2] = '+'; 
            shipValues = EnemyBoard[2][3] = '+';
            break;
            case 1:
            p2Choice =  B3;
            //shipValues = B3, B4, C3, C4;
            shipValues = EnemyBoard[2][3] = '+';
            shipValues = EnemyBoard[2][4] = '+';
            shipValues = EnemyBoard[3][3] = '+';
            shipValues = EnemyBoard[3][4] = '+';
            break;
            case 2:
            p2Choice =  C4;
            //shipValues = C3, D3, C4, D4;
            shipValues = EnemyBoard[3][4] = '+';
            shipValues = EnemyBoard[4][4] = '+';
            shipValues = EnemyBoard[3][4] = '+';
            shipValues = EnemyBoard[4][4] = '+';
            break;
            case 3:
            p2Choice =  A1;
            //shipValues = A1, A2, B1, B2;
            shipValues = EnemyBoard[1][1] = '+';
            shipValues = EnemyBoard[1][2] = '+';
            shipValues = EnemyBoard[2][1] = '+';
            shipValues = EnemyBoard[2][2] = '+';
            break;
            case 4:
            p2Choice =  B2;
            //shipValues = B2, B3, C2, C3;
            shipValues = EnemyBoard[2][2] = '+';
            shipValues = EnemyBoard[2][3] = '+';
            shipValues = EnemyBoard[3][2] = '+';
            shipValues = EnemyBoard[3][3] = '+';
            break;

        }

        
        return p2Choice;

        //return shipLocation;

        //return shipValues;
       

    }
    public int displayMenu()
    //method to display the main menu
    {
        //GameBoard();
        //EnemyBoard();

        //displayGameBoard(game);
        //displayEnemyBoard(game);

        //displayWarShipArray();

        int choice = 0;
        int menu = 0;
        //int guessCol = 0;
        //int guessRow = 0;
        int COLS;
        int ROWS;
        String p2Choice = "  ";
        System.out.println("Menu:");
        System.out.println("1. Fire Shot");
        System.out.println("2. Show Solution");
        System.out.println("3. Quit");
        System.out.println("Enter menu choice.");

        choice = keyboard.nextInt();

        switch(choice)
        {
            case 1:  System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            //guessCol = columnCheck();
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            //guessRow = rowCheck();
            hitOrMiss(); 
            break;
            case 2:  System.out.println("Solution Shown:  ");
            System.out.println("Here's where the enemy ship was.");
            displayLocationOfEnemyShip();
            break;
            case 3:  System.out.println("Program Ended.");
            break;
            default: System.out.println("Invalid Input.");
        }   

        displayResults();
        return menu;
    }  

    public static void GameBoard()
    {
        System.out.println("   WAR SHIP GAME (Game Board) ");
        final int COLS = 6;
        final int ROWS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};

       
        System.out.println("  ");
        for (int col = 0; col < COLS; col++)
        {  for (int row = 0; row < ROWS; row++)
            { System.out.print("  " + values[col][row]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public static void EnemyBoard()
    {
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        final int COLS = 6;
        final int ROWS = 6;
        char[][] values = { { ' ', 'A', 'B', 'C', 'D', 'E' },
                { '1', '_', '_', '_', '_', '_' }, 
                { '2', '_', '_', '_', '_', '_' },   
                { '3', '_', '_', '_', '_', '_' },  
                { '4', '_', '_', '_', '_', '_' },  
                { '5', '_', '_', '_', '_', '_' }};

        
        System.out.println("  ");
        for (int col = 0; col < COLS; col++)
        {  for (int row = 0; row < ROWS; row++)
            { System.out.print("  " + values[col][row]);
            }
            System.out.println("\n");
        }
        System.out.println("  ");
    }

    public void displayGameBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Game Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayEnemyBoard(char[][] board)
    { 
        System.out.println(" WAR SHIP GAME (Enemy Board)");
        int i = 0, j = 0;
        for (i = 0; i < board.length; i++)
        { for (j = 0; j < board.length; j++)
                System.out.print(board[i][j] + "__");
            System.out.println();
        }
        System.out.println();
    }

    public void displayWarShipArray()
    {
        int number = 1;
        System.out.println("  ");
        System.out.println("   A   B   C   D   E   ");
        for (int i = 0; i < 5; i++)
        {  
            System.out.println(number +  " __" + "  " + "__" + "  " + "__" + "  " + "__" + "  " + "__" );
            number++;
        }
        System.out.println("  ");
    }

    public  void hitOrMiss()
    {
        //int guessCol = 0;
        //int guessRow = 0;
        final int shipCOL = 2;
        final int shipROW = 2;
        char [][]GameBoard = new char[6][6];
        //int [][]shipValues = new int[(int)Math.random() + values.length][(int)Math.random() * values[0].length];
        int shipValues = 0;
        final int COLS = 6;
        final int ROWS = 6;
        //print an x for shots
        //print an * for hits

        

        System.out.println("  ");

        if(GameBoard[guessCol][guessRow] == shipValues)
        {
           
            System.out.println("Hit!");
            System.out.println("  ");
            
            System.out.println(" WAR SHIP GAME (Game Board) ");
            GameBoard[guessCol][guessRow] = '*';
            GameBoard[1][0] = '1';
            GameBoard[2][0] = '2';
            GameBoard[3][0] = '3';
            GameBoard[4][0] = '4';
            GameBoard[5][0] = '5';
            GameBoard[0][1] = 'A';
            GameBoard[0][2] = 'B';
            GameBoard[0][3] = 'C';
            GameBoard[0][4] = 'D';
            GameBoard[0][5] = 'E';
          
            for(char col = 0; col < COLS; col++)
            {
                for(char row = 0; row < ROWS; row++)
                {
                    System.out.print("__" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addWin();
        }
        else
        {
             
            System.out.println("Miss!");
            System.out.println("  ");
            
            System.out.println(" WAR SHIP GAME (Game Board) ");
            GameBoard[guessCol][guessRow] = 'x'; 
            GameBoard[1][0] = '1';
            GameBoard[2][0] = '2';
            GameBoard[3][0] = '3';
            GameBoard[4][0] = '4';
            GameBoard[5][0] = '5';
            GameBoard[0][1] = 'A';
            GameBoard[0][2] = 'B';
            GameBoard[0][3] = 'C';
            GameBoard[0][4] = 'D';
            GameBoard[0][5] = 'E';
            
            for(char col = 0; col < COLS; col++)
            {
                for(char row = 0; row < ROWS; row++)
                {
                    System.out.print(" _" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addLoss();
        }
        System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
        
        displayMenu();
    }
        
    //public static int columnCheck();
    {
    }

    //public static int rowCheck();
    {
    }

    public void displayLocationOfEnemyShip()
    { 
        char col = 0;
        char row = 0;
        final int COLS = 6;
        final int ROWS = 6;
        char[][]EnemyBoard = new char [6][6];
        char[][]EnemyShip = new char [2][2];

        generateRandomShipLocation();
           
        System.out.println(" ");

        System.out.println("   WAR SHIP GAME (Enemy Board) ");
         
        shipValues = '+'; 
        EnemyBoard[col][row] = '+';
        EnemyShip[col][row] = '+';
        
            EnemyBoard[1][0] = '1';
            EnemyBoard[2][0] = '2';
            EnemyBoard[3][0] = '3';
            EnemyBoard[4][0] = '4';
            EnemyBoard[5][0] = '5';
            EnemyBoard[0][1] = 'A';
            EnemyBoard[0][2] = 'B';
            EnemyBoard[0][3] = 'C';
            EnemyBoard[0][4] = 'D';
            EnemyBoard[0][5] = 'E';
            
            for(col = 0; col < COLS; col++)
            {
                for(row = 0; row < ROWS; row++)
                {
                    System.out.print(" _" + EnemyBoard[col][row]);
                }
                System.out.println("\n");
            }
            
        System.out.println("  ");
        System.out.println("Ship location: " + shipLocation);
        System.out.println("Ship Values: " + shipValues);
        System.out.println("p2Choice: " + p2Choice);
        //System.out.println(EnemyBoard[col][row]);
        //System.out.println(EnemyShip[col][row]);
        System.out.println("  ");
    }

    public void displayResults()
    {
        int x = 0;
        int y = 0;
        boolean warShipSunk;
        System.out.println( numberOfShots + " shots were fired.");
        System.out.println( Win + " shots hit.");
        System.out.println( Loss + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk= true)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
        }
        else if(warShipSunk = false)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
        }
        System.out.println("  ");
        displayMenu();
    }

    private static String newId()
    {
        idNum++;
        return id + String.valueOf(idNum);
    }
}


Was This Post Helpful? 0
  • +
  • -

#8 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 22 November 2012 - 12:40 AM

This is my latest code in developing the War Ship game.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#9 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: How to get game array to work properly.

Posted 22 November 2012 - 02:46 AM

After a brief look at your code, it seems you're creating new game or enemy boards several times as local variables. To retain the game's status, one game and enemy board should be created and made available to all parts of the program that need it. That could be done by making the game and enemy board instance variables, initialized in the constructor, or a few other ways.

Then to determine hits or misses, you check the location of the shot in the correct board and determine if there's a ship at that location.
Was This Post Helpful? 1
  • +
  • -

#10 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 22 November 2012 - 10:55 AM

Thanks for your insights concerning my program. I was thinking I may need to modify the program line: if(EnemyBoard[guessCol][guessRow] == '+') . . . Hit! else. . . Miss! and replace '+' with shipLocation, p2Choice, shipValues, etc. to more accurately assess the location of the enemy ship. What variable do you think I should use to link hitOrMiss() with displayLocationOfEnemyShip()?
I may need to use a global game board which is static to show the location of the hits and misses. I'm not sure how to go about this. I've been told that I need to refer to the method as an argument inside a method or something like that, or use a separate fire() method, in order to retain the board containing the hits and misses.
I will work on the program some more this weekend and reread the chapter in the book about arrays.
Anyways, wish you a Happy Thanksgiving! I hope it is filled with much turkey and family and friends.
Thanks for trying to help me with the program.
Sincerely,
Quang Pham
Was This Post Helpful? 0
  • +
  • -

#11 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2197
  • View blog
  • Posts: 5,224
  • Joined: 10-September 10

Re: How to get game array to work properly.

Posted 23 November 2012 - 04:41 AM

Now after a slightly less brief look at your code, I'll suggest specifics:

Look at WarShip.initBoards(). The GameBoard and EnemyBoard created there are local variables, visible only to that method. Create and initialize instance variables GameBoard and EnemyBoard in the constructor and use those variables throughout the program.

There are other examples of items to fix in this method: variable names should begin with lowercase letters. The game board size is hardcoded and then the constant variables ROWS and COLS are set. Don't hardcode those "magic numbers" throughout your program. Instead, set the constants early and use those throughout your program.

Then look at the method WarShip.generateRandomShipLocation(). Another local variable EnemyBoard is created with the ships "randomly" (hardcoded?) placed on it. Again, this variable is only visible to this method unless the method returns it, which it doesn't.

Until you fix the program so that there is a single reference to the game's critical elements throughout - like the game and enemy boards - the game will never work, and there's no answer to your last question that will matter.
Was This Post Helpful? 1
  • +
  • -

#12 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 27 November 2012 - 04:48 AM

View PostGregBrannon, on 23 November 2012 - 04:41 AM, said:

Now after a slightly less brief look at your code, I'll suggest specifics:

Look at WarShip.initBoards(). The GameBoard and EnemyBoard created there are local variables, visible only to that method. Create and initialize instance variables GameBoard and EnemyBoard in the constructor and use those variables throughout the program.

There are other examples of items to fix in this method: variable names should begin with lowercase letters. The game board size is hardcoded and then the constant variables ROWS and COLS are set. Don't hardcode those "magic numbers" throughout your program. Instead, set the constants early and use those throughout your program.

Then look at the method WarShip.generateRandomShipLocation(). Another local variable EnemyBoard is created with the ships "randomly" (hardcoded?) placed on it. Again, this variable is only visible to this method unless the method returns it, which it doesn't.

Until you fix the program so that there is a single reference to the game's critical elements throughout - like the game and enemy boards - the game will never work, and there's no answer to your last question that will matter.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#13 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to get game array to work properly.

Posted 27 November 2012 - 04:51 AM

Post your updated code don't attach it
And don't forget :code:
Was This Post Helpful? 1
  • +
  • -

#14 Quang Pham  Icon User is offline

  • D.I.C Head

Reputation: -1
  • View blog
  • Posts: 68
  • Joined: 18-November 12

Re: How to get game array to work properly.

Posted 27 November 2012 - 05:11 AM

View Postpbl, on 27 November 2012 - 04:51 AM, said:

Post your updated code don't attach it
And don't forget :code:/>

Quote

Thanks for the pointer. Here is my WarShip code:


/**
 * Code to play War Ship.
 * 
 * @Quang Pham 
 * @10/8/12
 */
import java.util.Scanner;
import javax.swing.*;
import static java.lang.Math.*;

public class WarShip
{
    private Player p1;
    private Player p2;
    //p2 = computer
    private static String id = "ws";
    private static int idNum = 100;
    private Ship s1;
    int numberOfShots = 0;
    int Win = 0;
    int Loss = 0;
    int guessCol = 0;
    int guessRow = 0;

    int shipLocation = 0;
    String p2Choice = " ";
    char shipValues1 = 0;
    char shipValues2 = 0;
    char shipValues3 = 0;
    char shipValues4 = 0;

    public static char [][]GameBoard = new char[6][6];
    public static char [][]EnemyBoard = new char[6][6];

    Scanner keyboard = new Scanner(System.in);

    //static final int gameSize = 5;

    //char [][] game = new char[gameSize][gameSize];
    //char [][] enemy = new char[gameSize][gameSize];

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, I suggest 5 different locations for the ship.  Call
    //the random number generator to choose which of the 5 locations to use.    You may be creative and find
    //different techniques, but the ship is not placed in the same place every game.

    public WarShip()
    { 
        String msg = "Welcome to War Ship! \n\n";
        msg = msg + " You will be playing against the computer, her name is Terrie!";
        JOptionPane.showMessageDialog(null, msg);
        p1 = new Player(newId());
        p2 = new Player(newId(),"Terrie","Computer");
        s1 = new Ship();//let the user enter the name of the ship in the default constructor
        //initBoards();

    }

    public void playGame()
    {
        String s0 = "  ";
        int count = 0;
        //int numberOfShots = 0;
        s0 = JOptionPane.showInputDialog("Enter the number of shots you want to make (usually under 15):  ");
        numberOfShots = Integer.parseInt(s0);

        while (count < numberOfShots)
        {
            initBoards();
            displayMenu();
            displayLocationOfEnemyShip();
            hitOrMiss();
            colCheck();
            rowCheck();

            count++;

        }

    }     

    private void initBoards()
    {
        char [][]GameBoard = new char [6][6];
        char [][]EnemyBoard = new char [6][6];

        int COLS = 6;
        int ROWS = 6;

        //initialize both boards
        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                GameBoard[col][row] = 0;
            }
        }

        for (int row = 0; row < ROWS; row++)
        {  for (int col = 0; col < COLS; col++)
            {
                EnemyBoard[col][row] = 0;
            }
        }
    }

    public int displayMenu()
    //method to display the main menu
    {
        //GameBoard();
        //EnemyBoard();

        //displayGameBoard(game);
        //displayEnemyBoard(game);

        //displayWarShipArray();

        int choice = 0;
        int menu = 0;
        //int guessCol = 0;
        //int guessRow = 0;
        int COLS;
        int ROWS;
        String p2Choice = "  ";
        System.out.println("Menu:");
        System.out.println("1. Fire Shot");
        System.out.println("2. Show Solution");
        System.out.println("3. Quit");
        System.out.println("Enter menu choice.");

        choice = keyboard.nextInt();

        switch(choice)
        {
            case 1:  System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            //guessCol = columnCheck();
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            //guessRow = rowCheck();
            hitOrMiss(); 
            break;
            case 2:  System.out.println("Solution Shown:  ");
            System.out.println("Here's where the enemy ship was.");
            displayLocationOfEnemyShip();
            break;
            case 3:  System.out.println("Program Ended.");
            displayResults();
            break;
            default: System.out.println("Invalid Input.");
        }   
        return menu;

    }  

    public String displayLocationOfEnemyShip()
    { 
        char col = 0;
        char row = 0;
        final int COLS = 6;
        final int ROWS = 6;
        char[][]EnemyBoard = new char [6][6];
        char[][]EnemyShip = new char [2][2];

        //generateRandomShipLocation
        //5 ship locations
        int shipLocations = 0;
        String p2Choice = " ";

        shipLocation = (int)(Math.random() * 10)%5;
        switch (shipLocation)
        {
            case 0:
            p2Choice =  "A2";
            //shipValues = A2, A3, B2, B3 
            EnemyBoard[1][2] = '+'; 
            EnemyBoard[1][3] = '+';  
            EnemyBoard[2][2] = '+'; 
            EnemyBoard[2][3] = '+'; 
            break;
            case 1:
            p2Choice =  "B3";
            //shipValues = B3, B4, C3, C4;
            EnemyBoard[2][3] = '+'; 
            EnemyBoard[2][4] = '+'; 
            EnemyBoard[3][3] = '+'; 
            EnemyBoard[3][4] = '+'; 
            break;
            case 2:
            p2Choice =  "C4";
            //shipValues = C3, D3, C4, D4;
            EnemyBoard[3][4] = '+'; 
            EnemyBoard[4][4] = '+'; 
            EnemyBoard[3][5] = '+';
            EnemyBoard[4][5] = '+'; 
            break;
            case 3:
            p2Choice =  "A1";
            //shipValues = A1, A2, B1, B2;
            EnemyBoard[1][1] = '+'; 
            EnemyBoard[1][2] = '+'; 
            EnemyBoard[2][1] = '+'; 
            EnemyBoard[2][2] = '+'; 
            break;
            case 4:
            p2Choice =  "B2";
            //shipValues = B2, B3, C2, C3;
            EnemyBoard[2][2] = '+'; 
            EnemyBoard[2][3] = '+'; 
            EnemyBoard[3][2] = '+'; 
            EnemyBoard[3][3] = '+'; 
            break;

        }
        System.out.println("p2Choice is:  " + p2Choice);

        System.out.println(" ");
        System.out.println("   WAR SHIP GAME (Enemy Board) ");

        //EnemyShip[col][row] = '+';

        EnemyBoard[1][0] = 'A';
        EnemyBoard[2][0] = 'B';
        EnemyBoard[3][0] = 'C';
        EnemyBoard[4][0] = 'D';
        EnemyBoard[5][0] = 'E';
        EnemyBoard[0][1] = '1';
        EnemyBoard[0][2] = '2';
        EnemyBoard[0][3] = '3';
        EnemyBoard[0][4] = '4';
        EnemyBoard[0][5] = '5';

        for(row = 0; row < ROWS; row++)
        {
            for(col = 0; col < COLS; col++)
            {
                System.out.print(" _" + EnemyBoard[col][row]);
            }
            System.out.println("\n");
        }

        System.out.println("  ");
        System.out.println("Ship location: " + shipLocation);
        System.out.println("p2Choice: " + p2Choice);
        System.out.println("EnemyBoard[guessCol][guessRow] is:  " + EnemyBoard[guessCol][guessRow]);
        System.out.println("ShipValues are: " + 
        //System.out.println(EnemyShip[col][row]);
        return p2Choice;
        //return shipLocation;
        return shipValues1;
        return shipValues2;
        return shipValues3;
        return shipValues4;
    }

    public  void hitOrMiss()
    {
        final int shipCOL = 2;
        final int shipROW = 2;
        char [][]GameBoard = new char[6][6];
        char [][]EnemyBoard = new char[6][6];
        final int COLS = 6;
        final int ROWS = 6;
        //print an x for shots
        //print an * for hits

        System.out.println("  ");
        if(EnemyBoard[guessCol][guessRow] == '+')
        {

            System.out.println("Hit!");
            System.out.println("  ");

            System.out.println(" WAR SHIP GAME (Game Board) ");
            GameBoard[guessCol][guessRow] = '*';
            GameBoard[1][0] = 'A';
            GameBoard[2][0] = 'B';
            GameBoard[3][0] = 'C';
            GameBoard[4][0] = 'D';
            GameBoard[5][0] = 'E';
            GameBoard[0][1] = '1';
            GameBoard[0][2] = '2';
            GameBoard[0][3] = '3';
            GameBoard[0][4] = '4';
            GameBoard[0][5] = '5';

            for(char row = 0; row < ROWS; row++)
            {
                for(char col = 0; col < COLS; col++)
                {
                    System.out.print("__" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addWin();
        }
        else
        {

            System.out.println("Miss!");
            System.out.println("  ");

            System.out.println(" WAR SHIP GAME (Game Board) ");
            GameBoard[guessCol][guessRow] = 'x'; 
            GameBoard[1][0] = 'A';
            GameBoard[2][0] = 'B';
            GameBoard[3][0] = 'C';
            GameBoard[4][0] = 'D';
            GameBoard[5][0] = 'E';
            GameBoard[0][1] = '1';
            GameBoard[0][2] = '2';
            GameBoard[0][3] = '3';
            GameBoard[0][4] = '4';
            GameBoard[0][5] = '5';
            for(char row = 0; row < ROWS; row++)
            {
                for(char col = 0; col < COLS; col++)
                {
                    System.out.print(" _" + GameBoard[col][row]);
                }
                System.out.println("\n");
            }
            p1.addLoss();
        }

        System.out.println(" guessCol is " + guessCol + " guessRow is " + guessRow);
        System.out.println(" GameBoard[guessCol][guessRow] is " +  GameBoard[guessCol][guessRow]);
        System.out.println(" EnemyBoard[guessCol][guessRow] is " + EnemyBoard[guessCol][guessRow]);

        displayMenu();
    }

    public boolean colCheck()
    {
        boolean validshot = true;
        if (guessCol > 0 && guessCol < 6)
        {
            validshot = true;
        }
        else
            validshot = false;
        System.out.println("Your shot must be between columns 1 and 5.");

        return validshot;
    }

    public boolean rowCheck()
    {
        boolean validshot = true;
        if (guessRow > 0 && guessRow < 6)
        {
            validshot = true;
        }
        else
            validshot = false;
        System.out.println("Your shot must be between rows 1 and 5.");
        return validshot;
    }

    public void displayResults()
    {
        int x = 0;
        int y = 0;
        boolean warShipSunk;
        System.out.println( numberOfShots + " shots were fired.");
        System.out.println( Win + " shots hit.");
        System.out.println( Loss + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk= true)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a winner!!!");
            System.out.println("Player sank " + s1 + "!");
            System.out.println("Ship is: " + s1);
        }
        else if(warShipSunk = false)
        {
            System.out.println("For " + numberOfShots + " shots " + p1 + " was a loser.");
        }
        System.out.println("  ");
    }

    private static String newId()
    {
        idNum++;
        return id + String.valueOf(idNum);
    }
}


Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: How to get game array to work properly.

Posted 27 November 2012 - 10:52 AM

That won't compile

        return p2Choice;
        //return shipLocation;
        return shipValues1;
        return shipValues2;
        return shipValues3;
        return shipValues4;
    }


Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2