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

These are my guess

  • get input
  • start game
  • initiate board
  • initiate player
  • correct wrong input
  • update board
  • switch user

These are from Portilla's walk through steps

  • display_board
  • player_input
  • place_marker
  • win_check
  • choose_first
  • space_check
  • full_board_check
  • player_choice
  • replay

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


[' ', 'X', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ']

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)


Enter your input : 1
Location Already Taken. Enter Different Number
Choose location between 0-8
Enter your input : 2
Location Already Taken. Enter Different Number
Choose location between 0-8
Enter your input : 3

 _   _   _ 
   | O | X 
 _   _   _ 

 X |   |   
 _   _   _ 

   |   |   
 _   _   _ 


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]


X

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


Welcome to Tic Tac Toe!

 _   _   _ 
   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

New game has been started
Player 1 has been chosen to play first
Player 1 turn :
Choose your next position: (1-9) 1

 _   _   _ 
 X |   |   
 _   _   _ 

   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

player check 2
Player 2 turn :
Choose your next position: (1-9) 1
Choose your next position: (1-9) 2

 _   _   _ 
 X | O |   
 _   _   _ 

   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

player check 1
Player 1 turn :
Choose your next position: (1-9) 3

 _   _   _ 
 X | O | X 
 _   _   _ 

   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

player check 2
Player 2 turn :
Choose your next position: (1-9) 4

 _   _   _ 
 X | O | X 
 _   _   _ 

 O |   |   
 _   _   _ 

   |   |   
 _   _   _ 

player check 1
Player 1 turn :
Choose your next position: (1-9) 5

 _   _   _ 
 X | O | X 
 _   _   _ 

 O | X |   
 _   _   _ 

   |   |   
 _   _   _ 

player check 2
Player 2 turn :
Choose your next position: (1-9) 6

 _   _   _ 
 X | O | X 
 _   _   _ 

 O | X | O 
 _   _   _ 

   |   |   
 _   _   _ 

player check 1
Player 1 turn :
Choose your next position: (1-9) 8

 _   _   _ 
 X | O | X 
 _   _   _ 

 O | X | O 
 _   _   _ 

   | X |   
 _   _   _ 

player check 2
Player 2 turn :
Choose your next position: (1-9) 7

 _   _   _ 
 X | O | X 
 _   _   _ 

 O | X | O 
 _   _   _ 

 O | X |   
 _   _   _ 

player check 1
Player 1 turn :
Choose your next position: (1-9) 9

 _   _   _ 
 X | O | X 
 _   _   _ 

 O | X | O 
 _   _   _ 

 O | X | X 
 _   _   _ 

Player 1 Won!!!
I entered this part
Do you want to play again? press y to continue :y

 _   _   _ 
   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

   |   |   
 _   _   _ 

New game has been started
Player 1 has been chosen to play first
Player 1 turn :
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-85-329ae8baf106> in <module>()
     11             #Player 1 Turn
     12             print "Player %d turn :"% playerNum
---> 13             mark, loCation = player_input(bConfig, playerNum)
     14             bConfig = place_marker(bConfig, mark, loCation)
     15             display_board(bConfig)

<ipython-input-79-691cb44a7831> in player_input(bConfig, playerNum)
      4     position = ' '
      5     while position not in '1 2 3 4 5 6 7 8 9'.split() or not space_check(bConfig, int(position)):
----> 6         position = raw_input('Choose your next position: (1-9) ')
      7     return marKers[playerNum], int(position)

/usr/local/lib/python2.7/site-packages/ipykernel/kernelbase.pyc in raw_input(self, prompt)
    692             self._parent_ident,
    693             self._parent_header,
--> 694             password=False,
    695         )
    696 

/usr/local/lib/python2.7/site-packages/ipykernel/kernelbase.pyc in _input_request(self, prompt, ident, parent, password)
    722             except KeyboardInterrupt:
    723                 # re-raise KeyboardInterrupt, to truncate traceback
--> 724                 raise KeyboardInterrupt
    725             else:
    726                 break

KeyboardInterrupt: