Skip to content

Instantly share code, notes, and snippets.

@croosen
Last active February 25, 2016 10:37
Show Gist options
  • Save croosen/c564fa7e08e30ea1f04a to your computer and use it in GitHub Desktop.
Save croosen/c564fa7e08e30ea1f04a to your computer and use it in GitHub Desktop.
Tic Tac Toe in Python
'''
Tic Tac Toe
A very (very) basic game in Python
'''
def tictactoe():
#create an empty raster
def makeRaster(row, col):
#make 3 rows (lists) with 3 columns (values)
r = []
for i in range(row):
r.append([])
for j in range(col):
r[i].append('-')
return r
#print the raster as a playfield
def printRaster(r, x, y, player):
for i in range(len(r)):
#if the x & y pos is not set with X or O, make move (player)
if r[x][y] == '-':
r[x][y] = player
print r[i]
#players
#checkes for player X or O and switches them each round
def players(player):
if player == '0':
return 'X'
else:
return '0'
#begin the game
raster = makeRaster(5,5)
player = '0'
#loop to get X and Y from players
while True:
print 'Player ' + player + ':'
x = int(raw_input('X: '))
y = int(raw_input('Y: '))
#print raster with 1. empty raster, 2. new x and y pos, 3. player 0 or X
printRaster(raster, x, y, player)
#next player
player = players(player)
tictactoe()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment