Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. import os
  2.  
  3. # Definition of variables and dictionary
  4. board = {
  5. 1: " ", 2: " ", 3: " ", 4: " ", 5: " ", 6: " ", 7: " ", 8: " ", 9: " "
  6. }
  7. game_on = True
  8. turn = "p1"
  9.  
  10.  
  11. # Function to clear the print the board
  12. def print_board():
  13. os.system('clear')
  14. print("")
  15. print(f" {board[7]} | {board[8]} | {board[9]}")
  16. print("---------------")
  17. print(f" {board[4]} | {board[5]} | {board[6]}")
  18. print("---------------")
  19. print(f" {board[1]} | {board[2]} | {board[3]}")
  20. print("")
  21. print("")
  22.  
  23.  
  24. # Function that will set "X" or "O" to players
  25. def set_player():
  26. p1 = ""
  27. while p1 != "X" and p1 != "O":
  28. p1 = input("Player 1: Choose 'X' or 'O' and type it down on the "
  29. "keyboard: ")
  30. if p1 == "X":
  31. p2 = "O"
  32. elif p1 == "O":
  33. p2 = "X"
  34. os.system('clear')
  35. print("Ready! \n \nPlayer 1 =", p1, ", Player 2 =", p2)
  36. return p1, p2
  37.  
  38.  
  39. # Function to place an "X" or an "O" on the board
  40. def players_turn(p1, p2, turn):
  41. valid_position = False
  42. while valid_position is False:
  43. if turn == "p1":
  44. pos = int(input("Player 1: It's your turn. "
  45. "Choose the position to play (1-9): "))
  46. valid_position = validate_open_position_on_board(board, pos)
  47. # Validates an open and valid position to place user's choice
  48. if valid_position:
  49. board[pos] = p1
  50. turn = "p2"
  51. else:
  52. pos = int(input("Player 2: It's your turn. "
  53. "Choose the position to play (1-9): "))
  54. valid_position = validate_open_position_on_board(board, pos)
  55. # Validates an open and valid position to place user's choice
  56. if valid_position:
  57. board[pos] = p2
  58. turn = "p1"
  59. return board, turn
  60.  
  61.  
  62. # Function that validates if someone has already won
  63. def validate_win(board):
  64. # Horizontal validation
  65. if (((board[7] == board[8] and board[8] == board[9]) and
  66. (board[7] == "X" or board[7] == "O")) or
  67. ((board[4] == board[5] and board[5] == board[6]) and
  68. (board[4] == "X" or board[4] == "O")) or
  69. ((board[1] == board[2] and board[2] == board[3]) and
  70. (board[2] == "X" or board[2] == "O"))):
  71. horizontal_validation = True
  72. else:
  73. horizontal_validation = False
  74.  
  75. # Vertical validation
  76. if (((board[7] == board[4] and board[4] == board[1]) and
  77. (board[7] == "X" or board[7] == "O")) or
  78. ((board[8] == board[5] and board[5] == board[2]) and
  79. (board[8] == "X" or board[8] == "O")) or
  80. ((board[9] == board[6] and board[6] == board[3]) and
  81. (board[9] == "X" or board[9] == "O"))):
  82. vertical_validation = True
  83. else:
  84. vertical_validation = False
  85.  
  86. # Diagonal validation
  87. if (((board[7] == board[5] and board[5] == board[3]) and
  88. (board[7] == "X" or board[7] == "O")) or
  89. ((board[1] == board[5] and board[5] == board[9]) and
  90. (board[1] == "X" or board[1] == "O"))):
  91. diagonal_validation = True
  92. else:
  93. diagonal_validation = False
  94.  
  95. return horizontal_validation or vertical_validation or diagonal_validation
  96.  
  97.  
  98. # Function that validates tie
  99. def validate_tie(board):
  100. board_piece_count = 0
  101. if " " in board.values():
  102. return False
  103. else:
  104. return True
  105.  
  106. # Function that validates if position as already being taken
  107. def validate_open_position_on_board(board, pos):
  108. return True if board[pos] == " " else False
  109.  
  110.  
  111. # Main code that will loop through and ask if players want to continue
  112. while game_on is True:
  113. os.system('clear')
  114. print("Welcome to Tic Tac Toe!")
  115. p1, p2 = set_player()
  116. while validate_win(board) is False and validate_tie(board) is False:
  117. print_board()
  118. board, turn = players_turn(p1, p2, turn)
  119. print_board()
  120. # Validate if the board is full and there has being a tie
  121. if validate_tie(board) is True:
  122. print("It's a tie!")
  123. # Otherwise, check who's turn is it, to determine the winner
  124. else:
  125. if turn == "p1":
  126. print("Congratulations, Player 2! You are the winner! \n")
  127. else:
  128. print("Congratulations, Player 1! You are the winner! \n")
  129. # End of game. Clears the board
  130. board = {
  131. 1: " ", 2: " ", 3: " ", 4: " ", 5: " ", 6: " ", 7: " ", 8: " ", 9: " "
  132. }
  133. # Validates if users want to play again
  134. end_response = input("Do you want to play again? (y/n): ")
  135. if end_response == "y":
  136. game_on = True
  137. else:
  138. game_on = False
  139. print("\nBye!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement