Jump to content
  • Advertisement
Sign in to follow this  
steveworks

what next

This topic is 3658 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to figure out another project for myself to work on before I move on to pygame.Im waiting for my book that I ordered to come in. I understand a good deal about the basics of python (variables, loops, functions, classes, inheritance, the random module, ect...) I have done a few projects so far to practice building my own programs. They were rock paper scissors, tic tac toe, hangman, a basic fight sequence between a monster and a hero(included health, five options for combat and mp). If anyone has any ideas for one more project I would love to hear them. I was thinking of doing connect 4 but I can't seem to figure out how that would work seeing as there is no multi dimentional array like object for python(that I know of). If you suggest me doing connect 4 please add something to point me in the right direction. All help is appreciated.

Share this post


Link to post
Share on other sites
Advertisement
You could allways represent the connect 4 game board with a single dimensional array, I dont know pyhton so I'll just post the math involved in doing so:


gameBoardWidth = 10
gameBoardHeight = 8
gameBoardArray[gameBoardWidth * gameBoardHeight] // enough elements in the array to hold the dimensions of the games board

// To access a certain game board piece you would use the following:
hOffset = 4 // the horizontal offset of the piece you want from the game board
vOffset = 6 // The vertical offset of the piece you want from the game board
gameBoardArray[(vOffset * gameBoardWidth) + hOffset] <-- Gets the element you want



With all that said, I find it hard to believe python doesn't support multidemsional arrays, from a little googling it seems to be supported now using the following syntax would create a 3x3 array:

myArray = [[1, 1, 1], [2, 2, 2], [3, 3, 3]]

Share this post


Link to post
Share on other sites
I'm just learning Python, so correct me if I'm wrong, but couldn't you represent a 2-d array as a list inside another list? (By list I mean the things with the [] brackets)

board = [[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4],[0,1,2,3,4]]

I fired up my Python interpreter and calling board[0][2] would correctly return 2. So maybe you could initialize everything to 0, then when a player moves into the spot, change the value to 'B' or 'R' (or 1 and 2 or whatever, you get my point).

I'd give that a shot. Like I said though, I'm just learning Python myself, so I may be completely wrong!

Share this post


Link to post
Share on other sites
Yup, 2d arrays in python are just arrays of arrays (which is pretty much what they are in any other language anyway...).


width = 4
height = 3
myArray = []
for x in range(width):
myArray.append([])
for y in range(height):
myArray[x].append(0)


Makes myArray a 2d array, 4 units wide and 3 units high.
myArray[x][y] returns that value at that location.

print myArray even returns
[[0,0,0],[0,0,0],[0,0,0],[0,0,0]]

If you want the coordinates to be flipped (myArray[y][x]) just flip the for statements.


for y in range(height):
for x in range(width):

Share this post


Link to post
Share on other sites
You could easily represent the connect 4 board in a single array and use modulus and integer division functions to determine the location.

It's a simple method, first of all, a connect 4 board has 42 cells, 7 columns with 6 rows. Your array would contain indexes 0-41 and the board would look basically like this


0 1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31 32 33 34
35 36 37 38 39 40 41


The player needs only to specify which colum they are using, the row is automatically determined, so once they specify the column, you check the column number +7 array index, this gives you the row immediately below, in the same column. While that next row is empty and you're still in the bounds of the array, move the piece down one row.

By looking at the grid you can see how the checking would work. to check horizontal, you use +/- 1, to check for diagonal from top left to bottom right, you'll use +/- 8, for vertical, use +/- 7 and for diagonal from bottom left to top right use +/- 6. Check that the array index locations are all the same value as the piece just place, and if you get 4 in a row you're done. just another possible method for you ;)


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!