class TicTacToe(object):
    def __init__(self, size):
        self.board = [['-']*size for _ in range(size)]
        self.move_count = 0
        self.size = size

    def print_board(self):
        row = ''
        for row in self.board:
            print ('|'.join(row))

    def move(self, player, row, col):
        self.board[row][col] = player
        self.move_count += 1

    def is_invalid_move(self, row, col):
        if row < 0 or row >= self.size or col < 0 or col >= self.size or self.board[row][col] != '-':
            return False

    def is_board_full(self):
        return self.move_count == self.size ** 2

    def check_winner(self, player):
        if (self.board[0][0] == self.board[0][1] == self.board[0][2] == player or
           self.board[1][0] == self.board[1][1] == self.board[1][2] == player or
           self.board[2][0] == self.board[2][1] == self.board[2][2] == player or
           self.board[0][0] == self.board[1][0] == self.board[2][0] == player or
           self.board[0][1] == self.board[1][1] == self.board[2][1] == player or
           self.board[0][2] == self.board[1][2] == self.board[2][2] == player or
           self.board[0][0] == self.board[1][1] == self.board[2][2] == player or
           self.board[0][2] == self.board[1][1] == self.board[2][0] == player):
            return True
        return False

    def ai_move(self):
        for i in xrange(self.size):
            for j in range(self.size):
                if self.board[i][j] == '-':
                    self.board[i][j] = 'O'
                    return
        raise Exception("no more moves available for ai.")

game = TicTacToe(3)
game.print_board()
while True:
    print('Please input your move (e.g. row col)')
    row, col = map(int, input().split())
    while game.is_invalid_move(row, col):
        print('Please input a valid move (e.g. row col)')
        row, col = input().split()
    game.move('X', row, col)
    if game.check_winner('X'):
        print (("{} has won!").format('X'))
        break
    game.print_board()

results matching ""

    No results matching ""