2 players should be able to play the game (both sitting at the same computer) The board should be printed out every time a player makes a move You should be able to accept input of the player position and then place a symbol on the board
In [64]:
import random
In [65]:
def game_initiate():
bConfig = [' '] * 10
display_board(bConfig)
print "New game has been started"
return bConfig, choose_first()
In [66]:
print bConfig
In [67]:
def choose_first():
randPlayer = random.randint(1,2)
print "Player %d has been chosen to play first"%(randPlayer)
return randPlayer
In [69]:
# print board
def display_board(bConfig):
print
print " _ _ _ "
print " %s | %s | %s " % (bConfig[1], bConfig[2], bConfig[3])
print " _ _ _ "
print
print " %s | %s | %s " % (bConfig[4], bConfig[5], bConfig[6])
print " _ _ _ "
print
print " %s | %s | %s " % (bConfig[7], bConfig[8], bConfig[9])
print " _ _ _ "
print
In [79]:
def player_input(bConfig, playerNum):
# Using strings because of raw_input
marKers = {1:'X', 2:'O'}
position = ' '
while position not in '1 2 3 4 5 6 7 8 9'.split() or not space_check(bConfig, int(position)):
position = raw_input('Choose your next position: (1-9) ')
return marKers[playerNum], int(position)
In [71]:
def space_check(bConfig, loCation):
return bConfig[loCation] == ' '
In [73]:
def place_marker(bConfig, marKer, loCation):
bConfig[loCation] = marKer
return bConfig
In [74]:
def win_check(board,mark):
'''
This could be really optimized
http://stackoverflow.com/questions/1056316/algorithm-for-determining-tic-tac-toe-game-over
'''
return ((board[7] == mark and board[8] == mark and board[9] == mark) or # across the top
(board[4] == mark and board[5] == mark and board[6] == mark) or # across the middle
(board[1] == mark and board[2] == mark and board[3] == mark) or # across the bottom
(board[7] == mark and board[4] == mark and board[1] == mark) or # down the middle
(board[8] == mark and board[5] == mark and board[2] == mark) or # down the middle
(board[9] == mark and board[6] == mark and board[3] == mark) or # down the right side
(board[7] == mark and board[5] == mark and board[3] == mark) or # diagonal
(board[9] == mark and board[5] == mark and board[1] == mark)) # diagonal
In [84]:
def full_board_check(bConfig):
for ii in range(1,10):
if space_check(bConfig, ii):
return False
return True
In [77]:
def replay():
return raw_input("Do you want to play again? press y to continue :").lower().startswith('y')
In [33]:
marKer, loCation = player_input(bConfig, 1)
place_marker(bConfig, marKer, loCation)
display_board(bConfig)
In [ ]:
# bConfig = place_marker(bConfig, 'x', 5)
# choose_first()
# replay()
mark, loCation = player_input(bConfig, playerNum)
print mark, loCation
In [25]:
dicTCheck = {1:'X',2:'O'}
print dicTCheck[1]
In [85]:
print('Welcome to Tic Tac Toe!')
while True:
# Set the game up here
bConfig, playerNum = game_initiate()
game_on = True
while game_on:
if playerNum == 1:
#Player 1 Turn
print "Player %d turn :"% playerNum
mark, loCation = player_input(bConfig, playerNum)
bConfig = place_marker(bConfig, mark, loCation)
display_board(bConfig)
if win_check(bConfig,mark):
# display_board(bConfig)
print "Player %d Won!!!" % playerNum
game_on = False
else:
if full_board_check(bConfig):
# display_board(bConfig)
print "Board is full. Match drawn!"
game_on = False
else:
playerNum = 2
print 'player check', playerNum
else:
# Player2's turn.
print "Player %d turn :"% playerNum
mark, loCation = player_input(bConfig, playerNum)
bConfig = place_marker(bConfig, mark, loCation)
display_board(bConfig)
if win_check(bConfig,mark):
# display_board(bConfig)
print "Player %d Won!!!" % playerNum
game_on = False
else:
if full_board_check(bConfig):
# display_board(bConfig)
print "Board is full. Match drawn!"
game_on = False
else:
playerNum = 1
print 'player check', playerNum
if not replay():
break