Presentation is loading. Please wait.

Presentation is loading. Please wait.

Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent.

Similar presentations


Presentation on theme: "Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent."— Presentation transcript:

1 Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent where players might be on the game board (like our GameBoard example from earlier) – For more complex games, we store all of the values in an array in tic tac toe requires that we know where each X and O have been stored in chess, we need to know in which square every piece currently resides in a card game, we have to store each card dealt to the player So we will use arrays to represent the game board and any special situations

2 Tic-Tac-Toe We can represent tic-tac-toe as a 3x3 game board of chars (‘X’, ‘O’, ‘ ’ or ‘b’) – char[ ][ ] ttt = new char[3][3]; The user will input their next move as two values, for instance, 2, 2 to represent the middle square, or 1, 3 to represent the middle row, rightmost square – If they are entering values from 1-3,1-3, we have to subtract 1 from each – why? Or we can make them enter values from 0-2 and 0-2 We can also represent tic-tac-toe as a 1D array from 0-8 as shown below – Which is easier? 012 345 678

3 Tic Tac Toe Logic For this game, you need to do the following – Create the board – Repeat until someone has won or the game is a draw (you can keep track of this by counting turns, if turn = 9, then the board is filled) Draw the board on the screen Get player 1’s guess and data verify that the guess is within bounds (0-2, 0-2, or 1-3, 1-3 if you prefer, or between 0 and 8) and that the square is empty (ttt[row][col]==‘ ’) or (ttt[row][col]==‘b’) if you are using b for blank (empty) square Check to see if player 1 has won the game with this move Otherwise, repeat with player 2 (get pick, data verify, check for win) – Output the winner

4 Human vs Computer Version If you want to play against the computer, you replace player 2 with the computer generating a move – How does the computer make a pick? Randomly – just generate a number from 0-8 (or two numbers from 0 to 2) Try to win/not to lose – check to see if player 1 can win on next move, if so, block, else check to see if player 2 can win on next move, if so use it, else randomly generate move Strategy – pick center square if available, else pick whichever corner is available, else pick a middle square Combine “try to win” with “strategy” for the best computer version – For a real elaborate program, ask the user to input a skill level (0 for random, 1 for try to win, 2 for strategy, 3 for combined)

5 How do you check for a win? This depends on whether you have used the 2D or 1D array version – If 2D, you want to compare all 3 squares of a row to see if they are the same and are all Xs or Os Testing row 0: – if(ttt[0][0]==ttt[0][1]&&ttt[0][0]==ttt[0][2]&&(ttt[0][0]==‘X’||ttt[0][0] ==‘O’)) … – Do the same for [1][0], [1][1], [1][2] and [2][0], [2][1], [2] How do you test for a column? – if(ttt[0][0]==ttt[1][0]&&ttt[0][0]==ttt[2][0]&&(ttt[0][0]==‘X’||ttt[0][0] ==‘O’)) … How do you test for the two diagonals? – If you are using the 1D array, its pretty much the same but you have to figure out the indices for each row, column and diagonal Testing column 0: – if(ttt[0]==ttt[3]&&ttt[0]==ttt[6]&&(ttt[0]==‘X’||ttt[0]==‘O’))…

6 Another Game: Battleship In the game of battleship, two players play on 10x10 grids – Each player places their ships somewhere on the grid – Then, players take guessing where a ship might be by calling out coordinates such as 5, 7 – If the other player has a ship at 5, 7, they announce “hit” else they announce “miss” – If you hit a player’s ship, you try to destroy it by hitting it at all coordinates. For instance, a submarine covers 3 grid positions, perhaps at 5, 6, and 5, 7, and 5, 8 – So if found at 5, 7, you would check 4, 7, and 6, 7 or 5, 6 and 5, 8 and then try another neighboring position until you have destroyed it NOTE: ships must either be placed horizontally or vertically, not diagonally, and ships cannot go off the 10x10 grid or overlap each other Ship sizes vary depending on type: aircraft carriers are 5 grid positions long, battleships 4, submarines and destoyers 3, and patrol boats 2 The player who has destroyed all of the other player’s ships wins the game

7 Our Battleship Game Since you and another player cannot both look at the screen (you would see each other’s setup), we will implement a Battleship solitaire game – The computer will randomly position 5 ships on the 10x10 grid – have to make sure that no ships overlap and no ships run off the screen (see the logic in two slides) – Iterate until either you have sunk all of the computer’s ships, your number of picks have elapsed, or you get sick of playing and want to quit Get the user’s next pick and increment the number of picks Check to see if that pick is legal (between 0 and 9) Check to see if the pick is a hit (if so, mark it so in the array) or a miss If a hit, see if it sinks a ship (has the ship been hit in every position?)

8 The Battlefield The battlefield will be a 10x10 array of char’s Each element should store – ‘ ’ if that position has no ship in it and hasn’t been guessed yet – ‘a’-’e’ if that position is part of a boat and hasn’t been guessed yet where the boats are labeled ‘a’ for aircraft carriers, ‘b’ for battleships, ‘c’ for submarines, ‘d’ for destroyers, and ‘e’ for patrol boats – ‘m’ if the user has guessed that position and it is a miss – ‘h’ if the user has guessed that position and it is a hit – ‘s’ if the user has sunk that boat by hitting all parts of it The array is originally declared to be all ‘ ’ and then the computer randomly generates which boats go into which of the slots, so before the game starts, the board is all ‘ ’, ‘a’, ‘b’, …, ‘e’ – While the game is in session, the computer changes squares to store ‘h’, ‘m’ and ‘s’ as needed

9 Computer Placing Ships This is the trickiest part of the program – We have to make sure that the computer places the 5 boats such that none overlap and none go off the board – One strategy is as follows: First select horizontal or vertical Next, select a starting point between 0 and 10 – shipsize – for instance, a battleship could go from 10 – 4 = 6 onward (6 to 9) in either the horizontal or vertical direction Verify that this does not intersect another ship and if so, try again Another tricky part of the game is determining if a ship has been sunk – You can do this by having five variables, one for each boat type, initialized to 5, 4, 3, 3 and 2 For each hit, decrement the appropriate variable based on the ship type and then if that variable is 0, you have sunk that ship

10 Storing Graphics Data in an Array Random generator = new Random(); int i, red, green, blue; int[] x, y, size; Color[] color; x = new int[25]; y = new int[25]; size = new int[25]; color = new Color[25]; for(i=0;i<25;i++) { x[i]=generator.nextInt(480) + 10; y[i]=generator.nextInt(480) + 10; size[i]=generator.nextInt(10) + 3; red=generator.nextInt(256); green=generator.nextInt(256); blue=generator.nextInt(256); color[i]=new Color(red, green, blue); } g.setColor(Color.black); g.fillRect(0,0,500,500); for(i=0;i<25;i++) { g.setColor(color[i]); g.fillOval(x[i], y[i], size[i], size[i]); } We use 4 arrays x, y for the coordinate size for the size of the oval color to store the Color of the oval


Download ppt "Representing a Game Board In a game, we represent the action taking place using an array – In a very simple game, we use individual variables to represent."

Similar presentations


Ads by Google