TIC-TAC-TOE using Python Dictionary

Have you ever played Tic-Tac-Toe? It’s quite an easy game to play. Have you ever tried programming this game?
In this tutorial, we will see how to write a program to create a Tic Tac Toe game in Python.

Tic-Tac-Toe code in Python using dictionary

''' TIC TAC TOE '''

Current_Board= {'T_L': ' ' , 'T_M': ' ' , 'T_R': ' ', 
   'M_L': ' ' , 'M_M': ' ' , 'M_R': ' ',
   'B_L': ' ' , 'B_M': ' ' , 'B_R': ' '}

def Board(Updated_Board):
  print( Updated_Board['T_L'] + "|" + Updated_Board['T_M'] + '|' + Updated_Board['T_R'] )
  print( '-+-+-')
  print( Updated_Board['M_L'] + "|" + Updated_Board['M_M'] + '|' + Updated_Board['M_R'] )
  print( '-+-+-')
  print( Updated_Board['B_L'] + "|" + Updated_Board['B_M'] + '|' + Updated_Board['B_R'] )

turn = 'X'

while True:
  Board(Current_Board)
  if ' ' not in Current_Board.values():
    print(" It's a Draw")
    break
  print( ' T_L = Top Left \n T_M = Top Middle \n T_R = Top Right')
  print( ' M_L = Middle Left \n M_M = Middle Middle \n M_R = Middle Right') 
  print( ' B_L = Bottom Left \n B_M = Bottom Middle \n B_R = Bottom Right')
  pos = input( turn + "'s turn. \n Enter the position where you want to insert:\n")
  if Current_Board.get(pos,' ')!=' ':
    print('The position is already Filled. Please enter another position \n')
    continue

  if Current_Board.get(pos,0):
    Current_Board[pos]=turn
  else:
    print('\n\nEnter Valid input')
    continue
  if turn == 'X':
    if (Current_Board['T_L'] == Current_Board['T_M'] == Current_Board['T_R']!=' ' or 
      Current_Board['M_L'] == Current_Board['M_M'] == Current_Board['M_R']!=' ' or 
      Current_Board['B_L'] == Current_Board['B_M'] == Current_Board['B_R']!=' ' or  
      Current_Board['T_L'] == Current_Board['M_M'] == Current_Board['B_R']!=' ' or 
      Current_Board['T_R'] == Current_Board['M_M'] == Current_Board['B_L']!=' ' or 
      Current_Board['T_L'] == Current_Board['M_L'] == Current_Board['B_L']!=' ' or 
      Current_Board['T_M'] == Current_Board['M_M'] == Current_Board['B_M']!=' ' or 
      Current_Board['T_R'] == Current_Board['M_R'] == Current_Board['B_R']!=' '):
      print( "'"+turn + '\' Wins')
      Board(Current_Board)
      break
    turn = 'O'

  else: 
    if (Current_Board['T_L'] == Current_Board['T_M'] == Current_Board['T_R']!=' ' or 
      Current_Board['M_L'] == Current_Board['M_M'] == Current_Board['M_R']!=' ' or 
      Current_Board['B_L'] == Current_Board['B_M'] == Current_Board['B_R']!=' ' or  
      Current_Board['T_L'] == Current_Board['M_M'] == Current_Board['B_R']!=' ' or 
      Current_Board['T_R'] == Current_Board['M_M'] == Current_Board['B_L']!=' ' or 
      Current_Board['T_L'] == Current_Board['M_L'] == Current_Board['B_L']!=' ' or 
      Current_Board['T_M'] == Current_Board['M_M'] == Current_Board['B_M']!=' ' or 
      Current_Board['T_L'] == Current_Board['M_L'] == Current_Board['B_L']!=' '):
      print("\n\n '"+turn + '\' Wins')
      Board(Current_Board)
      break
    turn = 'X'

 

 

In the above code we can easily noticeĀ  that, we used a dictionary to represent the board.
Here, the notations T_L, T_M, T_R represents TOP LEFT, TOP MIDDLE, TOP RIGHT in the board respectively.

Similarly, ML, MM, MR represents middle row and BL, BM, BR represents the bottom row.

Here in this program, we have to give input( T_L, T_M, T_R, M_L, M_M, M_R, B_L, B_M, B_R) every time until the board is filled.

The Board () function prints the board by updating it every time we enter the input.

 

if ' ' not in Current_Board.values():
    print(" It's a Draw") 
    break

The above statements checks wether the board has completely filled and prints ” It’s a Draw” if the board is completely filled and no one won.

 

if Current_Board.get(pos,0): 
    Current_Board[pos]=turn
else: 
    print('\n\nEnter Valid input') 
    continue

The above statement makes sure that the board values entered cannot be overwritten by a later input.

 

Current_Board['T_L'] == Current_Board['T_M'] == Current_Board['T_R']!=' ' or
Current_Board['M_L'] == Current_Board['M_M'] == Current_Board['M_R']!=' ' or
Current_Board['B_L'] == Current_Board['B_M'] == Current_Board['B_R']!=' ' or
Current_Board['T_L'] == Current_Board['M_M'] == Current_Board['B_R']!=' ' or
Current_Board['T_R'] == Current_Board['M_M'] == Current_Board['B_L']!=' ' or
Current_Board['T_L'] == Current_Board['M_L'] == Current_Board['B_L']!=' ' or
Current_Board['T_M'] == Current_Board['M_M'] == Current_Board['B_M']!=' ' or
Current_Board['T_R'] == Current_Board['M_R'] == Current_Board['B_R']!=' '

The above condition becomes true if any one of the player win’s the game.

Output:

 | |
-+-+-
 | |
-+-+-
 | |
X's turn.
 Enter the position where you want to insert:
M_M
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
 | |
-+-+-
 |X|
-+-+-
 | |
O's turn.
 Enter the position where you want to insert:
B_M
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
 | |
-+-+-
 |X|
-+-+-
 |O|
X's turn.
 Enter the position where you want to insert:
T_R
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
 | |X
-+-+-
 |X|
-+-+-
 |O|
O's turn.
 Enter the position where you want to insert:
B_L
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
 | |X
-+-+-
 |X|
-+-+-
O|O|
X's turn.
 Enter the position where you want to insert:
B_R
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
 | |X
-+-+-
 |X|
-+-+-
O|O|X
O's turn.
 Enter the position where you want to insert:
T_L
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
O| |X
-+-+-
 |X|
-+-+-
O|O|X
X's turn.
 Enter the position where you want to insert:
M_R
 T_L = Top Left
 T_M = Top Middle
 T_R = Top Right
 M_L = Middle Left
 M_M = Middle Middle
 M_R = Middle Right
 B_L = Bottom Left
 B_M = Bottom Middle
 B_R = Bottom Right
'X' Wins
O| |X
-+-+-
 |X|X
-+-+-
O|O|X

Also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *