CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    17

    Battleship Game Engine

    I'm doing a project designing a game engine for a simple battleship game and I'm stuck on the method called incoming to record the opponents shot at a players fleet I was wondering if someone could give me some direction on it as I'm not sure how to approach it! Looking for ideas not code!

    Here's the brief for the method:

    incoming has the following header
    public String incoming(String targetRef )
    incoming is a method used by opponents to shoot at a players fleet. The method should check if the square identified by targetRef contains part of one of the ships in the players fleet. if it does then the opponent has had a hit and incoming should return an appropriate message, otherwise it should return the word miss.

    My main problem is checking the square identified by targetRef.

    Thanks in advance!

    Here's what I have so far:

    Code:
    import java.util.Random ;
    
    public class Player
    {
        private int[][] grid ;
        public Fleet fleet = new Fleet() ;
        public Ship ship ;
        
        public Player()
        {
            grid = new int[10][10] ;
            placeShips() ;
        }
        
        public Player(int gridSize)
        {
            if(gridSize > 26 || gridSize < 10)
            {
                if(gridSize > 26)
                {
                    grid = new int[12][12] ;
                    placeShips() ;
                } else {
                    grid = new int[10][10] ;
                    placeShips() ;
                }
            } else {
                grid = new int[gridSize][gridSize] ;
                placeShips() ;
            }
        }
        
        public void placeShips()
        {
            int row, col, i, j ;
            boolean placed = false ;
            Random rand = new Random() ;
            for(i = 1 ; i < fleet.size() ; i++)
            {
                int direction = rand.nextInt(2) ;
                while(placed == false)
                {
                    ship = fleet.shipAt(i) ;
                    row = rand.nextInt(grid[0].length) ;
                    col = rand.nextInt(grid[0].length) ;
                    if(grid[row][col] == 0)
                    {
                        if(direction == 0)
                        {
                            if(row + ship.getLength() < grid[0].length)
                            {
                                placed = true; // We say at this stage that we CAN place the ship, but we still need to check if a ship is in the way
                                for(j = 1; j < ship.getLength() && placed == true; j++)
                                {
                                    if(grid[row + j][col] == 0)
                                    {
                                        placed = true;
                                    }
                                    else
                                    {
                                        placed = false;
                                    }
                                }
                            }
                            if(placed == true)
                            {
                                for(j = 1 ; j <= ship.getLength() ; j++)
                                {
                                    grid[row][col] = i ;
                                    row++ ;
                                }
                            }
                            
                        } else if(direction == 1) {
                            if(col + ship.getLength() < grid[0].length)
                            {
                                placed = true ;
                                for(j = 1 ; j < ship.getLength() && placed == true ; j++)
                                {
                                    if(grid[row][col + j] == 0)
                                    {
                                        placed = true ;
                                    } else {
                                        placed = false ;
                                    }
                                }
                            }
                            if(placed == true)
                            {
                                for(j = 1 ; j <= ship.getLength() ; j++)
                                {
                                    grid[row][col] = i ;
                                    col++ ;
                                }
                            }
                        }
                    }
                }
            }
        }
        
        public int shipsRemaining()
        {
            int amountShips = fleet.size() ;
            for(int i = 1 ; i < fleet.size() ; i++)
            {
                ship = fleet.shipAt(i) ;
                if(ship.isDestroyed() == true)
                {
                    amountShips-- ;
                }
            }
            return amountShips ;
        }
        
        public void display()
        {
            String columnLabel = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
            String[][] displayGrid = new String[grid[0].length][grid[0].length] ;
            int rowNums = 0 ;
            for(int i = 0 ; i < grid.length + 1 ; i++)
            {
                System.out.print(columnLabel.substring(i, i+1) + "\t") ;
            }
            System.out.println() ;
            
            System.out.println() ;
            
            for(int i = 0 ; i < grid.length ; i++)
            {
                System.out.print(rowNums + "\t") ;
                rowNums++ ;
                for(int j = 0 ; j < grid[0].length ; j++)
                {
                    if(grid[i][j] == 0)
                    {
                        displayGrid[i][j] = "-" ;
                    } else if(grid[i][j] == -1) {
                        displayGrid[i][j] = "O" ;
                    } else if(grid[i][j] == -2) {
                        displayGrid[i][j] = "X" ;
                    } else if(grid[i][j] >=1 && grid[i][j] <= fleet.size()) {
                        for(int k = 1 ; k < fleet.size() ; k++)
                        {
                            if(grid[i][j] == k)
                            {
                                displayGrid[i][j] = fleet.shipAt(k).getType().substring(0, 1) ;
                            }
                        }
                    }
                    System.out.print(displayGrid[i][j] + "\t") ;
                }
                System.out.println() ;
            }
        }
        
        public String nextTarget()
        {
            String colLabel = " ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
            int rowNums = 0 ;
            Random rand = new Random() ;
            int row = rand.nextInt(grid.length) ;
            rowNums = row ;
            int col = rand.nextInt(grid.length) ;
            colLabel = colLabel.substring(col, col + 1) ;
            return rowNums + colLabel ;
        }
        
        public String incoming(String targetRef)
        {
            String result = "" ;
            Random rand = new Random() ;
        }
                        
    }

  2. #2
    Join Date
    May 2006
    Location
    UK
    Posts
    4,473

    Re: Battleship Game Engine

    Code:
    String[][] displayGrid = new String[grid[0].length][grid[0].length] ;
    Should be:
    String[][] displayGrid = new String[grid.length][grid[0].length] ;

    My main problem is checking the square identified by targetRef.
    You need to convert the passed in reference to x & y coordinates. Presumably the reference is a single letter (X axis) and a number (Y axis), so split the reference into the letter and number. Convert the letter part into upper case and then convert it to a number by getting the letter as a char and subtracting the char 'A'. This will give you a 0 based number for the x axis. The Y axis part you can convert directly into a number and if the reference is 1 based subtract 1. You now have 2 numbers that you can use with the grid array to find if there is a ship at that location.
    Posting code? Use code tags like this: [code]...Your code here...[/code]
    Click here for examples of Java Code

  3. #3
    Join Date
    Jan 2012
    Posts
    17

    Re: Battleship Game Engine

    Cheers!
    Was going in the direction of converting the separate elements but realized the letters wouldn't give me the correct value forgot about subtracting 'A'.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured