Connect 4 game: how do I set a user specified grid size?

Reply

Join Date: Aug 2009
Posts: 3
Reputation: nessa203 is an unknown quantity at this point 
Solved Threads: 0
nessa203 nessa203 is offline Offline
Newbie Poster

Connect 4 game: how do I set a user specified grid size?

 
0
  #1
Aug 18th, 2009
I thought I'd set myself a challenge as I've been programming in Python a little while now (nothing too heavy, mind you!) and make a 4-in-a-row game which I could play against the computer or another person, on a grid which i specify how big. I though I had the grid sorted, but now I'm coming to put in the gameplay and it's not working.

  1. #!/usr/bin/env python
  2. import sys
  3.  
  4. def main():
  5. quit = False
  6. global x
  7. x = raw_input("How wide would you like your game? (4 - 8) ")
  8. if x.isdigit():
  9. x = int(x)
  10. else:
  11. print "Please enter a non-negative integer!"
  12. main()
  13. # Any smaller than 4 and the game is not very playable, any larger than 8 and it could go on $
  14. global y
  15. y = raw_input("And how tall? (4 - 8) ")
  16. if y.isdigit():
  17. y = int(y)
  18. else:
  19. print "Please enter a non-negative integer between 4 and 8!"
  20. main()
  21. inputPlayerParameters()
  22.  
  23. def inputPlayerParameters():
  24. z = raw_input("Enter '1' to play the computer or enter '2' to play another human: ")
  25. if z.isdigit():
  26. z = int(z)
  27. if z == 1:
  28. print "You will play the computer"
  29. gameBoard = initialiseGrid(x, y)
  30. while True:
  31. drawGrid(gameBoard)
  32. player1Go(gameBoard)
  33. computerGo(gameBoard)
  34. elif z == 2:
  35. print "You will play another person"
  36. gameBoard = initialiseGrid(x,y)
  37. while True:
  38. drawGrid(gameBoard)
  39. player1Go(gameBoard)
  40. player2Go(gameBoard)
  41. else:
  42. print "You did not enter 1 or 2."
  43. inputPlayerParameters()
  44. else:
  45. print "ERROR: DID NOT ENTER '1' OR '2'"
  46. inputPlayerParameters()
  47.  
  48. def initialiseGrid(x,y):
  49. rows = [0] * x
  50. i = 0
  51. grid = [0] * y
  52. while i < y:
  53. grid[i] = rows
  54. i=i+1
  55. return grid
  56.  
  57. def drawGrid(grid):
  58. for element in grid:
  59. print element
  60. for i in range(x):
  61. print "",i+1,
  62.  
  63. def player1Go(grid):
  64. print "Enter the column number you wish to go in: "
  65. userInput = raw_input(">>> ")
  66. if userInput == "quit":
  67. confirm = raw_input("Are you sure you want to quit? y/n ")
  68. if confirm == "y":
  69. sys.exit(0)
  70. elif userInput == "save":
  71. confirm = raw_input("Save the game? y/n ")
  72. if confirm == "y":
  73. filename = open("connect4game.txt", "wb")
  74. filename.dump(grid)
  75. filename.close()
  76. elif userInput.isdigit():
  77. userInput = int(userInput)
  78. print "You went in column", userInput
  79. if (userInput > x):
  80. print "Please enter a valid column number!"
  81. player1Go(grid)
  82. else:
  83. grid[userInput-1][0] = "o"
  84. checkForWin()
  85. else:
  86. print "Input not recognised! Try again."
  87. player1Go()
  88.  
  89. def player2Go(grid):
  90. print "Enter the column number you wish to go in: "
  91. userInput = raw_input(">>> ")
  92. if userInput == "quit":
  93. confirm = raw_input("Are you sure you want to quit? y/n ")
  94. if confirm == "y":
  95. sys.exit(0)
  96. else:
  97. checkForWin()
  98.  
  99. def computerGo(grid):
  100. print "Computer thinking.."
  101. checkForWin()
  102.  
  103. def quitGame(message, draw = True):
  104. if draw == True:
  105. setGrid()
  106. print message
  107. exit()
  108.  
  109. def checkForWin():
  110. print "Checking state..."
  111. # if win = True:
  112. # print "GAME OVER!"
  113.  
  114. main()

For example, if I set the grid to 4x4 and play against myself: when I specify that I want to go in column 1, the entire column fills with "o" instead of just the bottom square.

I would very much appreciate any help!
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is online now Online
Veteran Poster

Re: Connect 4 game: how do I set a user specified grid size?

 
0
  #2
Aug 18th, 2009
The problem is here
rows = [0] * x
run the following code and see what you get
  1. rows = [0] * x
  2. for row in rows:
  3. print id(row)
  4. ##
  5. ## you should see that all of the memory addresses are the same
  6. ## meaning that you have the same object in the list several times,
  7. ## so when you change one, they all change because they are
  8. ## the same object. Instead, try the following
  9. ##
  10. rows = [[0] for x in range(0, 3)]
  11. for row in rows:
  12. print id(row)

Also, you don't allow for errors, as in these lines for example.
z = raw_input("Enter '1' to play the computer or enter '2' to play another human: ")
if z.isdigit():
Do this instead:
  1. z = 0
  2. while z not in ["1", "2"]:
  3. z = raw_input("Enter '1' to play the computer or enter '2' to play another human: ")
  4. z = int(z)
  5. if z == 1:
  6. etc.
And your input for x and y can be done the same way, but consider using one function and passing the string literal "How wide would you like your game? (4 - 8) ", and the check list ["4", "5", "6", "7", "8"] and letting it return the choice, instead of the redundant code.

The final thing that I see, and I didn't take a close look at your code, is
  1. def drawGrid(grid):
  2. for element in grid:
  3. print element
  4. for i in range(x):
  5. print "",i+1,
You do no pass "x" to the function, so it is getting the value from somewhere else (global variables, etc), which is never a good idea. You want to control which variables are used where (which brings up classes. This would be much better in a class structure, but I am guessing that you haven't gotten that far yet).
Last edited by woooee; Aug 18th, 2009 at 1:32 pm.
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 3
Reputation: nessa203 is an unknown quantity at this point 
Solved Threads: 0
nessa203 nessa203 is offline Offline
Newbie Poster

Re: Connect 4 game: how do I set a user specified grid size?

 
0
  #3
Aug 19th, 2009
Thankyou very much for your help! It's really appreciated. The first tip has saved me quite a few lines of code and definitely makes the program more robust.

I have changed the way the grid is intialised, as I realised I was just creating the same element multiple times. Silly!

I currently have x and y set to be global variables. Is this not good syntax?

Anyway, this is the new version:
  1. def initialiseGrid(x,y):
  2. grid = []
  3. for i in range(x):
  4. grid.append([0])
  5. for i in range(x):
  6. for j in range(y-1):
  7. grid[i].extend([0])
  8. return grid
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,008
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 285
woooee woooee is online now Online
Veteran Poster

Re: Connect 4 game: how do I set a user specified grid size?

 
0
  #4
Aug 19th, 2009
Notice the duplicate line
for i in range(x):
You should be able to use
  1. def initialiseGrid(x,y):
  2. grid = []
  3. for h in range(x):
  4. grid.append([0])
  5. for j in range(y-1):
  6. grid[h].extend([0])
  7. return grid
Generally, it is considered bad practice to use "i", "l", or "o" as single digit variable names as they can look like numbers.
Linux counter #99383
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC