Advertisement
SlavaCat

Python tic-tac-toe

Aug 30th, 2020 (edited)
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.08 KB | None | 0 0
  1. def win_sequence(current_turn):
  2.     print(current_turn + ", you win!->")
  3.     grid_draw()
  4.     input('Press enter to end the game')
  5.     global turn
  6.     turn = 2
  7.  
  8. def grid_draw():
  9.     print("\n  |1||2||3|")
  10.     for rows, values in grid_h.items():
  11.         print(f'{rows}-', end = '')
  12.         for items in values:
  13.             print(f"|{items}|", end = '')
  14.         print("\n")
  15.  
  16. def row_input():
  17.     value = input('Type row and press enter to continue\n')
  18.     if value != '':
  19.         value = int(value)
  20.         if value  in accept_no:
  21.             change_h[0] = int(value)
  22.             change_v[1] = int(value) - 1 #-1 because it deals with the list and not the dict key
  23.             column_input()
  24.         else:
  25.             row_input()
  26.     else:
  27.         row_input()
  28.    
  29.        
  30. def column_input():
  31.     value = input('Type column and press enter to continue\n')
  32.     if value != '':
  33.         value = int(value)
  34.         if value  in accept_no:
  35.             change_h[1] = int(value) - 1
  36.             change_v[0] = int(value)
  37.         else:
  38.             column_input()
  39.     else:
  40.         column_input()
  41.    
  42. def reset():
  43.     global value
  44.     value = 0
  45.     global accept_no
  46.     accept_no = [1,2,3]
  47.     global grid_h
  48.     grid_h = { 1: ['0','0','0'], 2: ['0','0','0'], 3: ['0','0','0']}
  49.     global grid_v
  50.     grid_v = { 1: ['0','0','0'], 2: ['0','0','0'], 3: ['0','0','0']}
  51.     global turn
  52.     turn = 1
  53.     global player_1
  54.     player_1 = input("Player one name:")
  55.     global player_2
  56.     player_2 = input("Player two name:")
  57.     global player_turn
  58.     player_turn = player_1
  59.     global change_h
  60.     change_h = [0,0] #changes with inputs
  61.     global change_v
  62.     change_v = [0,0]
  63.  
  64. reset()
  65. while turn == 1:
  66.     grid_draw()
  67.     #for rows, values in grid_v.items(): #displays the vertical grid without formatting
  68.         #print(values)
  69.     print(f"{player_turn}, it is your turn")
  70.  
  71.     row_input()
  72.  
  73.     if player_turn == player_1:
  74.         if  grid_h[change_h[0]][change_h[1]] == '0':
  75.             grid_h[change_h[0]][change_h[1]] = 'x'
  76.             grid_v[change_v[0]][change_v[1]] = 'x'
  77.         else:
  78.             print('Area was occupied, please try again')
  79.             row_input()
  80.     else:
  81.         if  grid_h[change_h[0]][change_h[1]] == '0':
  82.             grid_h[change_h[0]][change_h[1]] = 'o'
  83.             grid_v[change_v[0]][change_v[1]] = 'o'
  84.         else:
  85.             print('Area was occupied, please try again')
  86.             row_input()
  87.  
  88.     if grid_h[1] == ['x','x','x'] or grid_h[2] == ['x','x','x'] or grid_h[3] == ['x','x','x'] or grid_v[1] == ['x','x','x'] or grid_v[2] == ['x','x','x'] or grid_v[3] == ['x','x','x']:
  89.         win_sequence(player_turn)
  90.     if grid_h[1] == ['o','o','o'] or grid_h[2] == ['o','o','o'] or grid_h[3] == ['o','o','o'] or grid_v[1] == ['o','o','o'] or grid_v[2] == ['o','o','o'] or grid_v[3] == ['o','o','o']:
  91.         win_sequence(player_turn)
  92.     if grid_h[1][2] == 'x' and grid_h[2][1] == 'x' and grid_h[3][0] == 'x' or grid_h[1][0] == 'x' and grid_h[2][1] == 'x' and grid_h[3][2] == 'x':
  93.         win_sequence(player_turn)
  94.     if grid_h[1][2] == 'o' and grid_h[2][1] == 'o' and grid_h[3][0] == 'o' or grid_h[1][0] == 'o' and grid_h[2][1] == 'o' and grid_h[3][2] == 'o':
  95.         win_sequence(player_turn)
  96.    
  97.     if player_turn == player_1:
  98.         player_turn = player_2
  99.     else:
  100.         player_turn = player_1
  101.  
  102.     if '0' not in [x for y in grid_h.values() for x in y]:
  103.         print("Game Over: Draw!")
  104.         grid_draw()
  105.         input('Press enter to end the game')
  106.         turn = 2
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement