furas

Python - Tic Tac Toe

Aug 3rd, 2016
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.47 KB | None | 0 0
  1. #!/usr/bin/env python3
  2.  
  3. #
  4. # https://www.reddit.com/r/learnpython/comments/4vzqr6/strange_indentation_error_when_trying_to_run_tic/
  5. #
  6.  
  7. # --- constants --- UPPER_CASE
  8.  
  9. PLAYER_1 = 'x'
  10. PLAYER_2 = 'o'
  11.  
  12. # --- class --- CamelCase
  13.  
  14.     # empty
  15.    
  16. # --- functions --- lower_case
  17.  
  18. def move(player, game):
  19.  
  20.     coords = input("Give me coordinates of the move that you want to make!\nRow then colomn, in format row,col (e.g. 1,1 up to 3,3): ")
  21.     row, col = coords.split(",")
  22.  
  23.     row = int(row.strip()) - 1
  24.     col = int(col.strip()) - 1
  25.  
  26.     game[row][col] = player
  27.    
  28.  
  29. def display(board):
  30.    
  31.     for row in board:
  32.         print(' '.join(row))
  33.  
  34. # --- main ---
  35.  
  36. if __name__ == "__main__":
  37.        
  38.     print("Welcome to Tic Tac Toe!")
  39.     print()
  40.     print("The game is exactly how you think it works.")
  41.     print()
  42.  
  43.     while True:
  44.        
  45.         choice = input("Wanna play? If no then type `n`").lower()
  46.    
  47.         if choice == 'n':
  48.             break
  49.    
  50.         # gameboard
  51.         gameboard = [
  52.             ['.', '.', '.'],
  53.             ['.', '.', '.'],
  54.             ['.', '.', '.']
  55.         ]
  56.        
  57.         # keeps track of how many moves had been made
  58.         count = 0
  59.  
  60.         while count < 10:
  61.             move(PLAYER_1, gameboard)
  62.             display(gameboard)
  63.             count += 1
  64.  
  65.             move(PLAYER_2, gameboard)
  66.             display(gameboard)
  67.             count += 1
  68.            
  69.         print("End of this game")
Add Comment
Please, Sign In to add comment