I've been learning Python lately, and I thought I should let everyone take a look at a very primitive program that I did. I thought that maybe it could help a few newbies, eh? Since I'm used to C, it uses a list within a list to simulate a two dimmensional array.
/msg Nanosecond with bugs.
"""Tic Tac Toe v0.2 by Nanosecond in 2001 in Python
-TODO: Add a second method for Connect 4 like play
-TODO: Make it so not entering input doesn't crash it
-TODO: Make it so entering non-integer input doesn't crash it
-TODO: Figure out why NE to SW doesn't work
-TODO: Figure out why doesn't detect win if height, width, and row are equal
/msg Nanosecond if you figure out any of these TODOs =)"""
def space_calc(boardsize):
"Calculates number of spaces to use when drawing board."
i, x = 10, 1
while boardsize > i:
x = x + 1
i = i * 10
return x
def square_calc(pmove, board):
"Calculates pmove into two integers that can be used, example: board[x][y]"
if pmove >= (len(board) * len(board[1])) or pmove < 0:
return (-1, -1) #Invalid Board Location
return divmod(pmove, len(board))
def check_square(board, pmove):
"Takes square_calc tuple and checks if already taken."
if pmove[0] == -1 or pmove[1] == -1:
return 0
#Out of Range
if type(board[pmove[0]][pmove[1]]) == type(0):
return 1
#Already Taken
elif type(board[pmove[0]][pmove[1]]) != type(0):
return 0
#No Problem
print "Entering the twilight zone!"
return 0
#Function to work on:
def checkwin(board, stats):
"Checks to see if a player has won."
#Stats is a tuple
#Stats[0] player
#Stats[1] How many in a row required
i, j, k, x = 0, 0, 0, 0
for i in range(len(board)):
for j in range(len(board[i])):
if type(board[i][j]) == type(0): #Check if taken
break
if j+stats[1] < len(board[0]): #Check range
#Horizantal
for k in range(stats[1]):
if board[i][j+k] == stats[0]:
x = x + 1
else:
break
if x == stats[1]:
return 1
x = 0
if i+stats[1] < len(board):
#Vertical
for k in range(stats[1]):
if board[i+k][j] == stats[0]:
x = x + 1
else:
break
if x == stats[1]:
return 1
x = 0
if i+stats[1] < len(board) and j+stats[1] < len(board[0]):
#NW to SE
for k in range(stats[1]):
if board[i+k][j+k] == stats[0]:
x = x + 1
else:
break
if x == stats[1]:
return 1
x = 0
if i+stats[1] < len(board) and j-stats[1] in range(len(board[0])):
#NE to SW
print "test"
for k in range(stats[1]):
if board[i+k][j-k] == stats[0]:
x = x + 1
else:
break
if x == stats[1]:
return 1
x = 0
return 0
def printboard(board, boardspace):
"Function for printing the board onto the screen."
i, j, k = 0, 0, 0
print #Has to be here to show up correctly out of IDLE, wierd
for i in range(len(board)):
print "|",
for j in range(len(board[i])):
print ("%%%ds |" % boardspace) % board[i][j],
print
print
height, width, row = 3, 3, 3 # Default Values
player, player1, player2 = 'X', 'X', 'O' #Player is the current player
while 1:
height = int(raw_input("Please enter height: "))
width = int(raw_input("Please enter width: "))
row = int(raw_input("How many in a row to win: "))
if height < 0 or width < 0 or row < 2:
print "Error, Try Again."
else:
break
print #Just for spacing
print
####Begin Board Creation####
board = [] # Init board
for i in range(height):
board.append([0] * width) # Create board
i, j, k = 0, 0, 0
for i in range(len(board)): # Number board
for j in range(len(board[i])):
board[i][j] = k+j
k = k+j+1
del i, j # Clean up
####End Board Creation####
printboard(board, space_calc(height * width))
k = ()
gameend = 0
######Begin Game Loop######
while not (gameend):
pmove = 0
while 1:
print "Go Player " + player + ": ",
pmove = int(raw_input())
if (check_square(board, square_calc(pmove, board))):
break
k = square_calc(pmove, board)
board[k[0]][k[1]] = player
if checkwin(board, (player, row)) == 1:
printboard(board, space_calc(height * width))
print "\nPlayer " + player + " wins!"
gameend = 1
break
if (player == player1):
player = player2
elif (player == player2):
player = player1
else:
print "ERROR, Player equals non-player variable."
break
printboard(board, space_calc(height * width))
######End Game Loop########