Results 1 to 5 of 5
  1. #1
    stepjerd1 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default Battleship help..im confused

    I hope this is the right forums for posting this, if not i apologize. I am currently in the process of making a battleship program for school in the console. We are using arrays to make battleship, 0 representing water and 3 representing the battleship. Key's 1-4 move the battleship up, down, left, right, not respectively. The bolded part is where i'm stuck. This is due tomorrow, he gave is 1 day to finish it :(.

    So here's what i need it to do.

    When you scroll down to the 4 cases, you see I have an "if" statement in each case, When the battleship goes "out of bounds" I need it to display a message saying "You have reached the edge". Am i missing something simple, or am i completely off? Thanks for your help, its much appreciated.

    Java Code:
    /*
    Name:		Stephen
    Class/Time:	Java 1:00
    Program:	Battleship
    
    Key:
    1 = YPOS++
    2 = XPOS++
    3 = YPOS--
    4 = XPOS--
    */
    import java.io.*;
    import java.util.*;
    
    public class Battleship
    {
    	static Scanner console = new Scanner(System.in);
    
    	public static void main(String[] args)
    	{
    		System.out.println("Welcome to Battleship");
    		System.out.println("Pressing Key 9 will quit the game");
    		System.out.println("Keys 1, 2, 3, 4, move the battleship");
    		System.out.println("Key:             ___                ");
    		System.out.println("1 = YPOS++      /   \\              ");
    		System.out.println("2 = XPOS++     /     \\             ");
    		System.out.println("3 = YPOS--    /-------\\            ");
    		System.out.println("4 = XPOS--   |         |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |   /-\\   |            ");
    		System.out.println("             |  |   |  |            ");
    		System.out.println("             |  -----  |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |    _    |            ");
    		System.out.println("             |   / \\   |            ");
    		System.out.println("             |__|   |__|            ");
    		System.out.println("                 ###                ");
    		System.out.println();
    
    		int[][] map = new int[4][3];							//Declare a 2D array of 12 elements
    
    		int move;												//Declare variable move
    		int XPOS = 1;											//Declare variable XPOS and initialize to 1
    		int YPOS = 1;											//Declare variable YPOS and initialize to 1
    		map[XPOS][YPOS] = 3;
    
    
    		for(int x = 0; x <= 3; x++)								//For loop
    		{
    		for(int y = 0; y <= 2; y++)
    			System.out.print(map[x][y]);
    			System.out.println();
    			if(XPOS == -1 || XPOS >= 3) {
    				System.out.print("You have reached the edge.");
    			}
    		}
    
    		do {
    			move = console.nextInt();							//Prompt user for integer then store in variable move
    [B]			switch (move)
    			{
    			case 1:												//Case 1
    				map[XPOS][YPOS] = 0;
    				XPOS--;
    				map[XPOS][YPOS] = 3;
    
    				if(YPOS >= -1) {
    					System.out.println("You have reached the edge.");
    				}
    				break;
    			case 2:												//Case 2
    				map[XPOS][YPOS] = 0;
    				YPOS++;
    				map[XPOS][YPOS] = 3;
    
    				if(YPOS == 4) {
    					System.out.println("You have reached the edge.");
    				}
    				break;
    			case 3:												//Case 3
    				map[XPOS][YPOS] = 0;
    				XPOS++;
    				map[XPOS][YPOS] = 3;
    
    				if(XPOS >= 2) {
    					System.out.println("You have reached the edge.");
    				}
    				break;
    			case 4:												//Case 4
    				map[XPOS][YPOS] = 0;
    				YPOS--;
    				map[XPOS][YPOS] = 3;
    
    				if(YPOS >= -1) {
    					System.out.println("You have reached the edge.");
    				}
    				break;
    			}[/B]													//End Switch
    
    			for(int x = 0; x <= 3; x++)							//For loop
    			{
    				for(int y = 0; y <= 2; y++)
    				System.out.print(map[x][y]);
    				System.out.println();
    			}
    
    			System.out.println();
    
    			}
    			while (move != 9);									//While move is not equal to 9 keep doing the loop
    	}
    }

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I don't understand the need or benefit for using the array.
    Two cases are misplaced and some boundry conditions seem to be inaccurate.
    Java Code:
            int move;//Declare variable move
            int XPOS = 1;//Declare variable XPOS and initialize to 1
            int YPOS = 1;//Declare variable YPOS and initialize to 1
    
            System.out.println("starting position: XPOS = " + XPOS +
                               "  YPOS = " + YPOS);
            System.out.println("boundries are: XPOS == -1 and XPOS >= 3");
    
            do {
                //Prompt user for integer then store in variable move
                move = console.nextInt();
                switch (move)
                {
                    case 3:  // move/case # should match your Key
                        XPOS--;
                        // test XPOS <= -1 instead of YPOS:
                        if(YPOS >= -1) {
                            System.out.println("You have reached the edge.");
                        }
                        break;
                    case 2://Case 2
                        YPOS++;
                        // what happens if user moves to YPOS == 5?
                        if(YPOS == 4) {
                            System.out.println("You have reached the edge.");
                        }
                        break;
                    case 1:
                        XPOS++;
                        // above it says limit for is: XPOS >= 3
                        if(XPOS >= 2) {
                            System.out.println("You have reached the edge.");
                        }
                        break;
                    case 4://Case 4
                        YPOS--;
                        // if YPOS is > -1 then we are still in bounds
                        // if YPOS <= -1 we are out of bounds
                        if(YPOS >= -1) {
                            System.out.println("You have reached the edge.");
                        }
                        //break;
                }//End Switch
    
                System.out.println("XPOS = " + XPOS + " YPOS = " + YPOS);            
    
                System.out.println();
    
            }
            while (move != 9);

  3. #3
    stepjerd1 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    The reason we are using arrays is because arrays is what we are currently learning in java II Game Development. TY for quick reply
    Last edited by stepjerd1; 01-22-2009 at 01:04 AM. Reason: MT

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    What do you want to do with the array? I don't understand what you are trying to do with it or how you want to use it.

  5. #5
    stepjerd1 is offline Member
    Join Date
    Jan 2009
    Posts
    4
    Rep Power
    0

    Default

    I figured it out...took a while though...I'm sure i will be back here much more for help :D...here's the finished work:

    Java Code:
    /*
    Name:		Stephen Jerde
    Class/Time:	Java 1:00
    Program:	Battleship
    
    Key:
    1 = YPOS++
    2 = XPOS++
    3 = YPOS--
    4 = XPOS--
    */
    import java.io.*;
    import java.util.*;
    
    public class BattleshipS
    {
    	static Scanner console = new Scanner(System.in);
    
    	public static void main(String[] args)
    	{
    		System.out.println("Welcome to Battleship");
    		System.out.println("Pressing Key 9 will quit the game");
    		System.out.println("Keys 1, 2, 3, 4, move the battleship");
    		System.out.println("Key:             ___                ");
    		System.out.println("1 = YPOS++      /   \\              ");
    		System.out.println("2 = XPOS++     /     \\             ");
    		System.out.println("3 = YPOS--    /-------\\            ");
    		System.out.println("4 = XPOS--   |         |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |   /-\\   |            ");
    		System.out.println("             |  |   |  |            ");
    		System.out.println("             |  -----  |            ");
    		System.out.println("             |         |            ");
    		System.out.println("             |    _    |            ");
    		System.out.println("             |   / \\   |            ");
    		System.out.println("             |__|   |__|            ");
    		System.out.println("                 ###                ");
    		System.out.println();
    
    		int[][] map = new int[4][3];							//Declare a 2D array of 12 elements
    
    		int move;												//Declare variable move
    		int XPOS = 1;											//Declare variable XPOS and initialize to 1
    		int YPOS = 1;											//Declare variable YPOS and initialize to 1
    		map[XPOS][YPOS] = 3;
    
    
    		for(int x = 0; x <= 3; x++)								//For loop
    		{
    		for(int y = 0; y <= 2; y++)
    			System.out.print(map[x][y]);
    			System.out.println();
    			if(XPOS == -1 || XPOS >= 3) {
    				System.out.print("You have reached the edge.");
    			}
    		}
    
    		do {
    			move = console.nextInt();							//Prompt user for integer then store in variable move
    			switch (move)
    			{
    			case 1:												//Case 1
    				map[XPOS][YPOS] = 0;
    				if(XPOS -1 < 0 )
    					System.out.println("You have reached the edge.");
    				else
    				XPOS--;
    				map[XPOS][YPOS] = 3;
    
    				break;
    			case 2:												//Case 2
    				map[XPOS][YPOS] = 0;
    				if(YPOS +1 > 2)
    				System.out.println("You have reached the edge.");
    				else
    				YPOS++;
    				map[XPOS][YPOS] = 3;
    
    				break;
    			case 3:												//Case 3
    				map[XPOS][YPOS] = 0;
    				if(XPOS +1 > 3)
    				System.out.println("You have reached the edge.");
    				else
    				XPOS++;
    				map[XPOS][YPOS] = 3;
    
    				break;
    			case 4:												//Case 4
    				map[XPOS][YPOS] = 0;
    				if(YPOS -1 < 0)
    				System.out.println("You have reached the edge.");
    				else
    				YPOS--;
    				map[XPOS][YPOS] = 3;
    
    				break;
    			}													//End Switch
    
    			for(int x = 0; x <= 3; x++)							//For loop
    			{
    				for(int y = 0; y <= 2; y++)
    				System.out.print(map[x][y]);
    				System.out.println();
    			}
    
    			System.out.println();
    
    			}
    			while (move != 9);									//While move is not equal to 9 keep doing the loop
    	}
    }

Similar Threads

  1. Confused
    By coldfire in forum New To Java
    Replies: 3
    Last Post: 01-13-2009, 01:00 PM
  2. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM
  3. Java Battleship Game Help PLEASE
    By mars_red in forum New To Java
    Replies: 0
    Last Post: 02-12-2008, 01:09 AM
  4. Java BattleShip game help
    By mars_red in forum Advanced Java
    Replies: 0
    Last Post: 02-12-2008, 12:58 AM
  5. a lot confused
    By vineethraj in forum New To Java
    Replies: 4
    Last Post: 01-18-2008, 12:36 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •