Simulate a tic-tac-toe game
Requirements
- A player is picked at random as a first player.
- The two players plays tic tac toe game.
- The game ends when the board is full or either one of the player wins.
- Return the board status and result after end of the game.
Player Class
class Player:
def __init__(self, name, symbol):
self.name = name
self.symbol = symbol
def pick_available_grid(self, available_position):
random_index = random.choice(available_position.keys())
return random_index
Board Class
class Board:
def __init__(self):
self.grid = [None]*9
self.available_position = {1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0}
def update_board(self, index, symbol):
self.grid[index-1] = symbol
del self.available_position[index]
def check_if_board_full(self):
len(self.available_position) == 0
def check_for_win_condition(self,symbol):
if (self.grid[0]==symbol and self.grid[1]==symbol and self.grid[2]==symbol):
return True
if (self.grid[3]==symbol and self.grid[4]==symbol and self.grid[5]==symbol):
return True
if (self.grid[6]==symbol and self.grid[7]==symbol and self.grid[8]==symbol):
return True
#vertical grid check
if (self.grid[0]==symbol and self.grid[3]==symbol and self.grid[6]==symbol):
return True
if (self.grid[1]==symbol and self.grid[4]==symbol and self.grid[7]==symbol):
return True
if (self.grid[2]==symbol and self.grid[5]==symbol and self.grid[8]==symbol):
return True
#diagonal grid check
if (self.grid[0]==symbol and self.grid[4]==symbol and self.grid[8]==symbol):
return True
if (self.grid[2]==symbol and self.grid[4]==symbol and self.grid[6]==symbol):
return True
Game Class
class Game:
global SYMBOL
SYMBOL = ['X', 'O']
def __init__(self):
self.board = Board()
self.players = []
self.result = None
def set_players(self):
player1 = Player('akanksha', SYMBOL[0])
player2 = Player('akanksha', SYMBOL[1])
self.players.append(player1)
self.players.append(player2)
def set_current_player(self, current_player=None):
current_player = self.players[1] if current_player == self.players[0] else self.players[0]
return current_player
def set_result(self):
if self.board.check_for_win_condition(self.players[0].symbol):
self.result = self.players[0]
elif self.board.check_for_win_condition(self.players[1].symbol):
self.result = self.players[1]
elif self.board.check_if_board_full():
self.result = 'Draw'
return self.result
def play_game(self):
current_player = self.set_current_player()
while(not self.set_result()):
available_position = self.board.available_position
grid_position = current_player.pick_available_grid(available_position)
self.board.update_board(grid_position, current_player.symbol)
current_player = self.set_current_player(current_player)
def end_game(self):
print(self.board.grid)
if self.result == 'Draw':
print('This game is a draw')
else:
print('player', self.result.name, "with symbol", self.result.symbol, "wins")
def start_game(self):
self.set_players()
self.play_game()
self.set_result()
self.end_game()
Main Program
class MainProgram:
def main():
print("Welcome to TicTacToe Game")
game = Game()
game.start_game()
main()
I am new to oops
, how can I improve this?
Questions:
- Should I create a separate class for
symbol
? - Should
check_for_win_condition
be included inGame
instead ofBoard
class? - Should
self.available_position
be used or it should be method which computes available cells fromgrid