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. :(
#!/usr/bin/env python
import sys
def main():
quit = False
global x
x = raw_input("How wide would you like your game? (4 - 8) ")
if x.isdigit():
x = int(x)
else:
print "Please enter a non-negative integer!"
main()
# Any smaller than 4 and the game is not very playable, any larger than 8 and it could go on $
global y
y = raw_input("And how tall? (4 - 8) ")
if y.isdigit():
y = int(y)
else:
print "Please enter a non-negative integer between 4 and 8!"
main()
inputPlayerParameters()
def inputPlayerParameters():
z = raw_input("Enter '1' to play the computer or enter '2' to play another human: ")
if z.isdigit():
z = int(z)
if z == 1:
print "You will play the computer"
gameBoard = initialiseGrid(x, y)
while True:
drawGrid(gameBoard)
player1Go(gameBoard)
computerGo(gameBoard)
elif z == 2:
print "You will play another person"
gameBoard = initialiseGrid(x,y)
while True:
drawGrid(gameBoard)
player1Go(gameBoard)
player2Go(gameBoard)
else:
print "You did not enter 1 or 2."
inputPlayerParameters()
else:
print "ERROR: DID NOT ENTER '1' OR '2'"
inputPlayerParameters()
def initialiseGrid(x,y):
rows = [0] * x
i = 0
grid = [0] * y
while i < y:
grid[i] = rows
i=i+1
return grid
def drawGrid(grid):
for element in grid:
print element
for i in range(x):
print "",i+1,
def player1Go(grid):
print "Enter the column number you wish to go in: "
userInput = raw_input(">>> ")
if userInput == "quit":
confirm = raw_input("Are you sure you want to quit? y/n ")
if confirm == "y":
sys.exit(0)
elif userInput == "save":
confirm = raw_input("Save the game? y/n ")
if confirm == "y":
filename = open("connect4game.txt", "wb")
filename.dump(grid)
filename.close()
elif userInput.isdigit():
userInput = int(userInput)
print "You went in column", userInput
if (userInput > x):
print "Please enter a valid column number!"
player1Go(grid)
else:
grid[userInput-1][0] = "o"
checkForWin()
else:
print "Input not recognised! Try again."
player1Go()
def player2Go(grid):
print "Enter the column number you wish to go in: "
userInput = raw_input(">>> ")
if userInput == "quit":
confirm = raw_input("Are you sure you want to quit? y/n ")
if confirm == "y":
sys.exit(0)
else:
checkForWin()
def computerGo(grid):
print "Computer thinking.."
checkForWin()
def quitGame(message, draw = True):
if draw == True:
setGrid()
print message
exit()
def checkForWin():
print "Checking state..."
# if win = True:
# print "GAME OVER!"
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! :)