Game Development Reference
In-Depth Information
we assign a single space string to extraSpace . Otherwise, we set extraSpace to be
a blank string. This way, all of our rows will line up when we print them.
The getRow() function will return a string representing the row number we pass it. Its
two parameters are the board data structure stored in the board variable and a row
number. We will look at this function next.
Drawing the X-coordinates Along the Bottom
27. # print the numbers across the bottom
28. print()
29. print(' ' + ('0123456789' * 6))
30. print(hline)
This code is similar to lines 14 to 17. This will print the X-axis coordinates along the
bottom of the screen.
Getting the State of a Row in the Ocean
33. def getRow(board, row):
34. # Return a string from the board data structure at a
certain row.
35. boardRow = ''
36. for i in range(60):
37. boardRow += board[i][row]
38. return boardRow
This function constructs a string called boardRow from the characters stored in board .
First we set boardRow to the blank string. The row number (which is the Y coordinate) is
passed as a parameter. The string we want is made by concatenating board[0][row] ,
board[1][row] , board[2][row] , and so on up to board[59][row] . (This is
because the row is made up of 60 characters, from index 0 to index 59 .)
The for loop iterates from integers 0 to 59 . On each iteration the next character in the
board data structure is copied on to the end of boardRow . By the time the loop is done,
extraSpace is fully formed, so we return it.
How the Code Works: Lines 40 to 62
Now that we have a function to print a given game board data structure to the string, let's
turn to the other functions that we will need. At the start of the game, we will need to create
a new game board data structure and also place treasure chests randomly around the board.
We should also create a function that can tell if the coordinates entered by the player are a
valid move or not.
Search WWH ::




Custom Search