fork download
  1. class Board:
  2. """Represents one board to a Tic-Tac-Toe game."""
  3.  
  4. def __init__(self):
  5. """Initializes a new board.
  6. A board is a dictionary which the key is the position in the board
  7. and the value can be 'X', 'O' or ' ' (representing an empty position
  8. in the board.)"""
  9. self.board = {
  10. "TL": " ", "TM": " ", "TR": " ",
  11. "ML": " ", "MM": " ", "MR": " ",
  12. "BL": " ", "BM": " ", "BR": " "}
  13.  
  14. def print_board(self):
  15. """Prints the board."""
  16. print(self.board["TL"] + "|" + self.board["TM"] \
  17. + "|" + self.board["TR"] + "|")
  18. print("-+" * 3)
  19. print(self.board["ML"] + "|" + self.board["MM"] \
  20. + "|" + self.board["MR"] + "|")
  21. print("-+" * 3)
  22. print(self.board["BL"] + "|" + self.board["BM"] \
  23. + "|" + self.board["BR"] + "|")
  24.  
  25. def _is_valid_move(self, position):
  26. if self.board[position] is " ":
  27. return True
  28. return False
  29.  
  30. def change_board(self, position, type):
  31. """Receive a position and if the player is 'X' or 'O'.
  32. Checks if the position is valid, modifies the board and returns the modified board.
  33. Returns None if the move is not valid."""
  34. if self._is_valid_move(position):
  35. self.board[position] = type
  36. return self.board
  37. return None
  38.  
  39. def is_winner(self, player):
  40. player_type = player.type
  41. runs = [
  42. #horizontal
  43. ["TL", "TM", "TR"],
  44. ["ML", "MM", "MR"],
  45. ["BL", "BM", "BR"],
  46. #vertical
  47. ["TL", "ML", "BL"],
  48. ["TM", "MM", "BM"],
  49. ["TR", "MR", "BR"],
  50. #diagonal
  51. ["TL", "MM", "BR"],
  52. ["BL", "MM", "TR"]
  53. ]
  54. for a,b,c in runs:
  55. if self.board[a] == self.board[b] == self.board[c] == player_type:
  56. return True
  57. return False
  58.  
  59. class Player:
  60. """Represents one player."""
  61. def __init__(self, type):
  62. """Initializes a player with type 'X' or 'O'."""
  63. self.type = type
  64.  
  65. def __str__(self):
  66. return "Player {}".format(self.type)
  67.  
  68.  
  69. class Game:
  70. """Represents a Tic-Tac-Toe game.
  71. The game defines player 1 always playing with 'X'."""
  72. def __init__(self):
  73. """Initilize 2 Players and one Board."""
  74. self.player1 = Player("X")
  75. self.player2 = Player("O")
  76. self.board = Board()
  77.  
  78. def print_valid_entries(self):
  79. """Prints the valid inputs to play the game."""
  80. print("""
  81. TL - top left | TM - top middle | TR - top right
  82. ML - middle left | MM - center | MR - middle right
  83. BL - bottom left | BM - bottom middle | BR - bottom right""")
  84.  
  85. def printing_board(self):
  86. """Prints the board."""
  87. self.board.print_board()
  88.  
  89. def change_turn(self, player):
  90. """Changes the player turn.
  91. Receives a player and returns the other."""
  92. if player is self.player1:
  93. return self.player2
  94. else:
  95. return self.player1
  96.  
  97. def won_game(self, player):
  98. """Returns True if the player won the game, False otherwise."""
  99. return self.board.is_winner(player)
  100.  
  101. def modify_board(self, position, type):
  102. """Receives position and player type ('X' or 'O').
  103. Returns modified board if position was valid.
  104. Asks to player try a different position otherwise."""
  105. if self.board.change_board(position, type) is not None:
  106. return self.board.change_board(position, type)
  107. else:
  108. position = input("Not available position. Please, try again: ")
  109. return self.board.change_board(position, type)
  110.  
  111.  
  112. def play():
  113. game = Game()
  114. game.print_valid_entries()
  115. player = game.player1
  116. num = 9
  117. while num > 0:
  118. num -= 1
  119. game.printing_board()
  120. position = input("{} turn, what's your move? ".format(player))
  121. game.modify_board(position, player.type)
  122. if game.won_game(player):
  123. print("{} is the Winner!".format(player))
  124. break
  125. else:
  126. player = game.change_turn(player)
  127. if num == 0:
  128. print("Game over! It's a tie!")
  129.  
  130.  
  131. if __name__ == "__main__":
  132. play()
Success #stdin #stdout 0.01s 27656KB
stdin
TL
MM
TM
BR
TR
stdout
            TL - top left    | TM - top middle    | TR - top right
            ML - middle left | MM - center        | MR - middle right
            BL - bottom left | BM - bottom middle | BR - bottom right
 | | |
-+-+-+
 | | |
-+-+-+
 | | |
Player X turn, what's your move? X| | |
-+-+-+
 | | |
-+-+-+
 | | |
Player O turn, what's your move? X| | |
-+-+-+
 |O| |
-+-+-+
 | | |
Player X turn, what's your move? X|X| |
-+-+-+
 |O| |
-+-+-+
 | | |
Player O turn, what's your move? X|X| |
-+-+-+
 |O| |
-+-+-+
 | |O|
Player X turn, what's your move? Player X is the Winner!