how to add data values to the board by declaring and initialising an array.how to create the game board by declaring an array.JavaScript can also be run in a web browser if created in an HTML document. You can work through this example if you have access to a JavaScript or Python IDE. The PDF attachment shows how to make the game using JavaScript or Python. You can practise working with arrays in any language. how are boats represented when they are hit?.how are squares that have not been checked represented?.There are further questions that would need to be answered to make the full game such as: This reflects what you would probably do in Battleship on paper. We'd put these onto the array called 'ocean'. In that case we might use X to mean 'tried and missed' and * to mean 'tried and hit'. If, however, we are tracking where we have launched an attack then we might want to indicate a hit or a miss. To keep track of where our opponent has launched an attack, we can use an "X" and put those into the grid with 'our' ships on - called 'myships'. How do we represent squares we have checked? myships represents the grid where my ships are.ocean represents the grid where the opponent ships are.That suggests that our initialisation might want to include two arrays: In the normal game you have a grid for your ships where you mark enemy attacks and you have another grid for marking where you've hit the opponent. When we've hit all the squares which contain parts of boats, the game is over. You tell your opponent which square you're targeting by giving them a coordinate like 'A2', 'J3' and so on. How do we distinguish between each player's ships?Īs the game goes on, we need to mark which grid squares we have aimed at. The array can be initialised so that the ocean is empty with the following: ocean =. Empty spaces could be represented by using full stops. The next decision is what goes in each blank space on the map. We determine that the grid size should be eight by eight. The paper version of the game uses grid references like A2, H5, C3 but the array uses two numbers to indicate the position of a data element such as, and. retrieve the value of data at individual locations.set the value of data elements at individual locations.It would be best to use a data structure that allows the game to store and update data in a grid. We will work through some of these questions here, one step at a time: How is the game grid represented? how do we represent squares we have checked?.how do we distinguish between each player's ships?.You would need to think about questions such as: If you created this game as a computer program you need to think about how the actual game played on paper would be converted into code. Two players plot their ships on a grid in secret and then take it in turns to guess where their opponent has plotted their ships. Each point on the grid represents a space on a map of the ocean where a ship might be. The following example will show how a two-dimensional array can be used to represent the classic game of Battleship by Hasbro.īattleship is a game which is played on a two-dimensional grid. Public static void deplo圜omputerShips() else if (".".Arrays are used frequently by games developers to structure data. Int field = new int įor (int m = y m (Arrays.toString(m).replace(",", ""))) ![]() Public static int missedGuesses = new int How can I make changes to the computer player's board after receiving coordinates when it's time for the human player? public static int numRows = 10 The character "s" is shown on the screen if it sinks the ship, only with "*" if it hits and "x" if it cannot hit. In the beginning, I automatically place the ships on both boards randomly, then I take the coordinates and put the ships on each other's boards.If the place in the coordinate hits the other player's ship, I will send a message saying this to the screen. ![]() There are two players, human and computer players.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |