How to get game array to work properly.

  • (2 Pages)
  • +
  • 1
  • 2

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

#16 pbl  Icon User is offline

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

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

Re: How to get game array to work properly.

Posted 27 November 2012 - 11:11 AM

Never though of

    private void fillHeader(char[][] game) {
    	char header = 'A';
    	for(int i = 1; i < game[0].length; ++i)
    		game[0][i] = header++;
    	header = '1';
    	for(int i = 1; i < game.length; ++i)
    		game[i][0] = header++;
    }


Was This Post Helpful? 0
  • +
  • -

#17 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 28 November 2012 - 01:36 PM

I am working hard to get the computer to display * and x for shots which hit and miss the enemy warship. I have not been able to do this and perhaps I should use a better if statement than if(EnemyBoard[guessCol][guessRow] == '+') then "Hit!" else "Miss!". I was thinking if shipLocation, p2Choice, shipValues1,2,3,4, or some other indicator would be better to use than '+'. Thanks for all your pointers concerning the program. Also, is there something more I should do to assign the ship name to s1? Here is the latest code I have developed:
 
/**
 * 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);

    //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;
        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 String generateRandomShipLocation()
    {
        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];

        //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);
        return p2Choice;
        
    }

    public int displayMenu()
    {
        //method to display the main menu
        {
            int choice = 0;
            int menu = 0;
            String p2Choice = "  ";
            boolean done = false;

            while(done = false);
            {
                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("Make a Shot!");
                makeShot();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                done = true;
                break;
                default: System.out.println("Invalid Input.");
            }  
            return menu;
        }
    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();
            //return guessCol;
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();
            //return guessRow;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    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];

        initBoards();
        //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]);

        displayMenu();

        return p2Choice;
        //return shipLocation;

    }  

    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);
    }
}

Attached File(s)


Was This Post Helpful? -1
  • +
  • -

#18 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 28 November 2012 - 02:37 PM

I got the program to display * and x for hits and misses by copying the code of the generateRandomShipLocation method into the hitOrMiss method. However, I still have trouble getting the game board to display all the shots that were made and show location of enemy ship to just show one ship. This if my new 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);

    //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;
        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();
            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 String generateRandomShipLocation()
    {
        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];

        //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);
        return p2Choice;
        
    }

    public int displayMenu()
    {
        //method to display the main menu
        {
            int choice = 0;
            int menu = 0;
            String p2Choice = "  ";
            boolean done = false;

            while(done = false);
            {
                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("Make a Shot!");
                makeShot();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                done = true;
                break;
                default: System.out.println("Invalid Input.");
            }  
            return menu;
        }
    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();
            //return guessCol;
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();
            //return guessRow;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    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];

        initBoards();
        //generateRandomShipLocation();
        //5 ship locations
        int shipLocation = 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]);

        displayMenu();

        return p2Choice;
        //return shipLocation;

    }  

    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
        
        int shipLocation = 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("  ");
        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);
    }
}

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#19 pbl  Icon User is offline

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

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

Re: How to get game array to work properly.

Posted 28 November 2012 - 08:03 PM

How many times will we have to tell you NOT TO ATTACH code
and to post the RELEVANT code here

:v:
Was This Post Helpful? 0
  • +
  • -

#20 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 29 November 2012 - 05:37 PM

Thanks for your replies. They have helped alot. The game program now runs but the ship doesn't move around from game to game and there is no indicator when the ship is sunk. Any ideas?

/**
 * 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;
    private String sname;
    private String shipName;
    private String NameOfShip;
    int numberOfShots = 0;
    int shotNumber = 0;
    int win = 0;
    int loss = 0;
    int hits = 0;
    int misses = 0;
    int guessCol = 0;
    int guessRow = 0;
    int COLS = 0;
    int ROWS = 0;
    boolean done = false;
    boolean getSunk = false;
    boolean warShipSunk = false;
    int shipLocation = 0;
    String p2Choice = " ";

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

    Scanner keyboard = new Scanner(System.in);

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, let there be 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
    }

    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()
    {
        char col = 0;
        char row = 0;

        //5 ship locations
        int shipLocation = 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;

        }
        return p2Choice;

    }

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

        int choice = 0;
        int menu = 0;
        String p2Choice = "  ";

        while(done == false)
        {
            System.out.println("Menu:");
            System.out.println("1. Fire Shot");
            System.out.println("2. Show Solution");
            System.out.println("3. Quit (display results)");
            System.out.println("Enter menu choice.");

            choice = keyboard.nextInt();

            switch(choice)
            {
                case 1: 
                System.out.println("Make a Shot!");
                makeShot();
                numberOfShots++;
                done = s1.getSunk();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                done = true;
                break;
                default: System.out.println("Invalid Input.");
            }  
           
        }return choice;
    } 

    public void playGame()
    {

        //initBoards();
        //makeShot();
        //displayMenu();
        //generateRandomShipLocation();
        //displayLocationOfEnemyShip();
        //hitOrMiss();
        //colCheck();
        //rowCheck();
        //count++;

    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();
           
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();
           
            numberOfShots++;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    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];

        //initBoards();

        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(" ");
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        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]);

        displayMenu();
        return p2Choice;
    }  

    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

        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("  ");

        if(EnemyBoard[guessCol][guessRow] == '+')
        {

            System.out.println("Hit!");
            System.out.println("  ");
            hits++;
            //s1.setHits();

            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");
            }
            if (s1.getSunk() == true)
                {
                    System.out.println("  ");
                    System.out.println("YOU SUNK THE BATTLESHIP!!!");
                    System.out.println("  ");
                    done = true;
                }
            p1.addWin();
        }
        else 
        {

            System.out.println("Miss!");
            System.out.println("  ");
            //s1.setMisses();
            misses++;

            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(hits + " shots hit.");
        System.out.println(misses + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk= true)
        {
            System.out.println("For " + numberOfShots + " shots attempted " + p1 + " was a winner!!!");
            System.out.println("Player sank the " + s1.shipName() + "!");
        }
        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);
    }
}


The zip file allows you to run the game program.

Attached File(s)


Was This Post Helpful? 0
  • +
  • -

#21 pbl  Icon User is offline

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

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

Re: How to get game array to work properly.

Posted 29 November 2012 - 05:58 PM

What are those methods supposed to do ?

   public void setSize(int n1)
    {
        n1 = 4;
    }

    public void setHits(int n1)
    {
        n1 = n1 + 1;
    }
    
    public void setMisses(int n1)
    {
        n1 = n1 + 1;
    }



About the sunk that does not work.. what about that one ?
    public void setSunk(Boolean s1)
    {

    }



Tip:
You write a GUI or a console application but not both
Mixing console read/write and JOptionPane will pissed off any user quitre fast

You need a lot more robustness

Make a Shot!
Enter a Column Number:  
a
Exception in thread "main" java.util.InputMismatchException
	at java.util.Scanner.throwFor(Scanner.java:840)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at WarShip.makeShot(WarShip.java:202)
	at WarShip.displayMenu(WarShip.java:164)
	at GameDriver.main(GameDriver.java:15)



Exception in thread "main" java.util.NoSuchElementException
	at java.util.Scanner.throwFor(Scanner.java:838)
	at java.util.Scanner.next(Scanner.java:1461)
	at java.util.Scanner.nextInt(Scanner.java:2091)
	at java.util.Scanner.nextInt(Scanner.java:2050)
	at WarShip.displayMenu(WarShip.java:158)
	at WarShip.hitOrMiss(WarShip.java:432)
	at WarShip.makeShot(WarShip.java:210)
	at WarShip.displayMenu(WarShip.java:164)
	at WarShip.hitOrMiss(WarShip.java:432)
	at WarShip.makeShot(WarShip.java:210)
	at WarShip.displayMenu(WarShip.java:164)
	at GameDriver.main(GameDriver.java:15)


Was This Post Helpful? 1
  • +
  • -

#22 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 01 December 2012 - 04:55 PM

Thanks for pointing out the getters and setters. I will try to figure them out so the program will determine when the warship has been sunk (hits == size). Currently, the program declares the player the winner if a hit has been made. In making a shot, the column guess must be numerical rather than alpabetical. I haven't figured out how to make it alphabetical.
Thanks for your help with the program.
Quang
Was This Post Helpful? 0
  • +
  • -

#23 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 03 December 2012 - 10:41 AM

I'm still working on the battleship sunk part. The program reports that the battleship is sunk after the first hit. The code for the program is this:
/**
 * 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;
    private String sname;
    private String shipName;
    private String NameOfShip;
    int numberOfShots = 0;
    int shotNumber = 0;
    int win = 0;
    int loss = 0;
    int hits = 0;
    int misses = 0;
    int n2 = 0;
    int n3 = 0;
    int guessCol = 0;
    int guessRow = 0;
    int COLS = 0;
    int ROWS = 0;
    boolean done = false;
    boolean getSunk = false;
    boolean warShipSunk = false;
    int shipLocation = 0;
    String p2Choice = " ";

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

    Scanner keyboard = new Scanner(System.in);

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, let there be 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
    }

    public 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 generateRandomShipLocation()
    {

        //5 possible ship locations

        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;

        }
        return shipLocation;

    }

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

        int choice = 0;
        int menu = 0;

        while(done == false)
        {
            System.out.println("Menu:");
            System.out.println("1. Fire Shot");
            System.out.println("2. Show Solution");
            System.out.println("3. Quit (display results)");
            System.out.println("Enter menu choice.");

            choice = keyboard.nextInt();

            switch(choice)
            {
                case 1: 
                System.out.println("Make a Shot!");
                makeShot();
                numberOfShots++;
                done = s1.getSunk();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                done = true;
                break;
                default: System.out.println("Invalid Input.");
            }  
           
        }return choice;
    } 

    public void playGame()
    {

        initBoards();
        //makeShot();
        //displayMenu();
        generateRandomShipLocation();
        //displayLocationOfEnemyShip();
        //hitOrMiss();
        //colCheck();
        //rowCheck();
        //count++;

    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();
           
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();
           
            numberOfShots++;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    public void displayLocationOfEnemyShip()
    { 
        char col = 0;
        char row = 0;
        final int COLS = 6;
        final int ROWS = 6;
        initBoards();
        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(" ");
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        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]);

        displayMenu();
    }  

    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

        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("  ");

        if(EnemyBoard[guessCol][guessRow] == '+')
        {

            System.out.println("Hit!");
            System.out.println("  ");
            hits++;
            //s1.getHits();

            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");
            }
            s1.getSunk();
            if (warShipSunk = true)
                {
                    System.out.println("  ");
                    System.out.println("YOU SUNK THE BATTLESHIP!!!");
                    System.out.println("  ");
                    displayMenu();
                    done = true;
                }
            p1.addWin();
        }
        else 
        {

            System.out.println("Miss!");
            System.out.println("  ");
            misses++;
            //s1.setMisses();
           

            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(hits + " shots hit.");
        System.out.println(misses + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk = true)
        {
            System.out.println("For " + numberOfShots + " shots attempted " + p1 + " was a winner!!!");
            System.out.println("Player sank the " + s1.shipName() + "!");
        }
        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
  • +
  • -

#24 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 03 December 2012 - 06:29 PM

This is my best version. I haven't made the board retain the hits and missed shots. Perhaps I can figure this out later.

/**
 * 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;
    private String sname;
    private String shipName;
    private String NameOfShip;
    int numberOfShots = 0;
    int shotNumber = 0;
    int win = 0;
    int loss = 0;
    int hits = 0;
    int misses = 0;
    int n2 = 0;
    int n3 = 0;
    int guessCol = 0;
    int guessRow = 0;
    int COLS = 0;
    int ROWS = 0;
    boolean done = false;
    boolean getSunk = false;
    boolean warShipSunk = false;
    int shipLocation = 0;
    String p2Choice = " ";

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

    Scanner keyboard = new Scanner(System.in);

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, let there be 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
    }

    public 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 generateRandomShipLocation()
    {

        //5 possible ship locations

        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;

        }
        return shipLocation;

    }

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

        int choice = 0;
        int menu = 0;

        while(done == false)
        {
            System.out.println("Menu:");
            System.out.println("1. Fire Shot");
            System.out.println("2. Show Solution");
            System.out.println("3. Quit (display results)");
            System.out.println("Enter menu choice.");

            choice = keyboard.nextInt();

            switch(choice)
            {
                case 1: 
                System.out.println("Make a Shot!");
                makeShot();
                numberOfShots++;
                done = s1.getSunk();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                done = true;
                quit();
                break;
                default: System.out.println("Invalid Input.");
            }  

        }return choice;
    } 

    public void quit()
    {
        System.out.println("Bye bye!");   
        System.exit(1);
    } 

    public void playGame()
    {

        initBoards();
        //makeShot();
        //displayMenu();
        generateRandomShipLocation();
        //displayLocationOfEnemyShip();
        //hitOrMiss();
        //colCheck();
        //rowCheck();
        //count++;

    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();

            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();

            numberOfShots++;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    public void displayLocationOfEnemyShip()
    { 
        char col = 0;
        char row = 0;
        final int COLS = 6;
        final int ROWS = 6;
        initBoards();
        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(" ");
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        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]);

        displayMenu();
    }  

    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

        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("  ");

        if(EnemyBoard[guessCol][guessRow] == '+')
        {

            System.out.println("Hit!");
            System.out.println("  ");
            hits++;
            //s1.getHits();

            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");
            }
            s1.getSize();
            s1.getHits();
            s1.getSunk();
            if (warShipSunk = true)
            {
                System.out.println("  ");
                System.out.println("YOU HIT THE BATTLESHIP!!!");
                System.out.println("  ");
                displayMenu();
                done = true;
            }
            p1.addWin();
        }
        else 
        {

            System.out.println("Miss!");
            System.out.println("  ");
            misses++;
            //s1.getMisses();

            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(hits + " shots hit.");
        System.out.println(misses + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk = true)
        {
            System.out.println("For " + numberOfShots + " shots attempted " + p1 + " was a winner!!!");
            System.out.println("Player sank the " + s1.shipName() + "!");
        }
        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)


This post has been edited by Quang Pham: 03 December 2012 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#25 pbl  Icon User is offline

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

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

Re: How to get game array to work properly.

Posted 03 December 2012 - 08:30 PM

Don't want to be rude Quang but until you

- correctly organize your code
- correctly identify your problem

nobody will spend hours reading your horribly organized code to help you


This is NOT how program development goes
start by having method to display your game state
then method to change it

You can't just throw at us few hundred lines of code just saying "they don't work"
Was This Post Helpful? 1
  • +
  • -

#26 Quang Pham  Icon User is offline

  • D.I.C Head

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

Re: How to get game array to work properly.

Posted 06 December 2012 - 09:58 AM

Thanks pbl for your insights on program development. I have come to a suitable battleship game and will try to be more organized and focused in my programming. My WarShip game is programmed like this so far. I managed to get the game to retain the hits and misses by declaring the game and enemy boards only once at the beginning of the class WarShip. I commented out the declarations in the hitOrMiss method.

/**
 * 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;
    private String sname;
    private String shipName;
    private String NameOfShip;
    int numberOfShots = 0;
    int shotNumber = 0;
    int win = 0;
    int loss = 0;
    int hits = 0;
    int misses = 0;
    int n2 = 0;
    int n3 = 0;
    int guessCol = 0;
    int guessRow = 0;
    int COLS = 0;
    int ROWS = 0;
    boolean done = false;
    boolean getSunk = false;
    boolean warShipSunk = false;
    int shipLocation = 0;
    String p2Choice = " ";

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

    Scanner keyboard = new Scanner(System.in);

    //Warship constructor- call the constuctor for the ship 
    //To randomize the enemy ship placement, let there be 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
    }

    public 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 generateRandomShipLocation()
    {

        //5 possible ship locations

        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;

        }
        return shipLocation;

    }

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

        int choice = 0;
        int menu = 0;

        while(done == false)
        {
            System.out.println("Menu:");
            System.out.println("1. Fire Shot");
            System.out.println("2. Show Solution");
            System.out.println("3. Quit (display results)");
            System.out.println("Enter menu choice.");

            choice = keyboard.nextInt();

            switch(choice)
            {
                case 1: 
                System.out.println("Make a Shot!");
                makeShot();
                numberOfShots++;
                done = s1.getSunk();
                break;
                case 2:  System.out.println("Solution Shown:  ");
                System.out.println("Here's where the enemy ship was.");
                displayLocationOfEnemyShip();
                done = true;
                break;
                case 3:  System.out.println("Program Ended.");
                displayResults();
                quit();
                done = true;
                break;
                default: System.out.println("Invalid Input.");
            }  
           
        }return choice;
    } 
    
    public void quit()
       {
        System.out.println("Bye bye!");   
        System.exit(1);
        } 
    
    public void playGame()
    {

        initBoards();
        //makeShot();
        //displayMenu();
        generateRandomShipLocation();
        //displayLocationOfEnemyShip();
        //hitOrMiss();
        //colCheck();
        //rowCheck();
        //count++;

    }

    public int makeShot()
    {  
        {
            System.out.println("Enter a Column Number:  ");
            guessCol = keyboard.nextInt();
            colCheck();
           
            System.out.println("Enter a Row Number:  ");
            guessRow = keyboard.nextInt();
            rowCheck();
           
            numberOfShots++;
            hitOrMiss();

        }
        return guessCol;
        //return guessRow;
    }

    public void displayLocationOfEnemyShip()
    { 
        char col = 0;
        char row = 0;
        final int COLS = 6;
        final int ROWS = 6;
        initBoards();
        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(" ");
        System.out.println("   WAR SHIP GAME (Enemy Board) ");
        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]);

        displayMenu();
    }  

    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

        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("  ");

        if(EnemyBoard[guessCol][guessRow] == '+')
        {

            System.out.println("Hit!");
            System.out.println("  ");
            hits++;
            //s1.getHits();

            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");
            }
            s1.getSize();
            s1.getHits();
            s1.getSunk();
            if (warShipSunk = true)
                {
                    System.out.println("  ");
                    System.out.println("You hit the battleship!!!");
                    System.out.println("  ");
                    displayMenu();
                    done = true;
                }
            p1.addWin();
        }
        else 
        {

            System.out.println("Miss!");
            System.out.println("  ");
            misses++;
            //s1.getMisses();
           

            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(hits + " shots hit.");
        System.out.println(misses + " shots missed.");
        warShipSunk = s1.getSunk();
        if (warShipSunk = true)
        {
            System.out.println("For " + numberOfShots + " shots attempted " + p1 + " was a winner!!!");
            System.out.println("Player sank the " + s1.shipName() + "!");
        }
        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
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2