Ejemplo n.º 1
0
    def __init__(self, inputs, width, height, piece_list):
        self.display = GuiDisplay(width,
                                  height,
                                  title='Intro to AI -- 67842 -- Ex1')
        self.inputs = inputs

        self.piece_list = piece_list

        self.num_players = len(inputs)
        self.board_w = width
        self.board_h = height
        self.turn_num = 0
        self.passed = [False] * self.num_players
        self.score = [0] * self.num_players
        self.board = Board(self.board_w, self.board_h, self.num_players,
                           self.piece_list)

        # Set up initial corners for each player
        if self.num_players > 1:
            self.board.connected[1, 0, self.board_w - 1] = True
            if self.num_players > 2:
                self.board.connected[2, self.board_h - 1, 0] = True
                if self.num_players > 3:
                    self.board.connected[3, self.board_h - 1,
                                         self.board_h - 1] = True
Ejemplo n.º 2
0
Archivo: game.py Proyecto: noamoalem/ai
def play_approximate_search(problem):
    back_trace = problem.solve()
    display = GuiDisplay(problem.board.board_w, problem.board.board_h, title='Intro to AI -- 67842 -- Ex1')
    board = problem.get_start_state()
    for action in back_trace:
        board.add_move(0, action)
        display.draw_board(board, dots=problem.targets)
    print("Expanded nodes: %d, score: %d" % (problem.expanded, board.score(0)))
Ejemplo n.º 3
0
Archivo: game.py Proyecto: noamoalem/ai
def play_simple_search(problem, search_func):
    back_trace = search_func(problem)
    display = GuiDisplay(problem.board.board_w, problem.board.board_h, title='Intro to AI -- 67842 -- Ex1')
    board = problem.get_start_state()
    if problem.__class__ == BlokusCornersProblem:
        dots = [(board.board_h - 1, board.board_w - 1), (0, board.board_w - 1), (board.board_h - 1, 0)]
    else:
        try:
            dots = problem.targets
        except AttributeError:
            dots = []
    for action in back_trace:
        board.add_move(0, action)
        display.draw_board(board, dots=dots)
    print("Expanded nodes: %d, score: %d" % (problem.expanded, board.score(0)))
Ejemplo n.º 4
0
class GameEngine(object):
    """
    Game engine class stores the current game state and controls when to
    get input/draw output
    """
    def __init__(self, inputs, width, height, piece_list):
        self.display = GuiDisplay(width,
                                  height,
                                  title='Intro to AI -- 67842 -- Ex1')
        self.inputs = inputs

        self.piece_list = piece_list

        self.num_players = len(inputs)
        self.board_w = width
        self.board_h = height
        self.turn_num = 0
        self.passed = [False] * self.num_players
        self.score = [0] * self.num_players
        self.board = Board(self.board_w, self.board_h, self.num_players,
                           self.piece_list)

        # Set up initial corners for each player
        if self.num_players > 1:
            self.board.connected[1, 0, self.board_w - 1] = True
            if self.num_players > 2:
                self.board.connected[2, self.board_h - 1, 0] = True
                if self.num_players > 3:
                    self.board.connected[3, self.board_h - 1,
                                         self.board_h - 1] = True

    def play_turn(self):
        """
        Play a single round of turns.

        Check for empty moves from the inputs (signalling passes) and ask for 
        new moves if illegal moves are provided.
        """
        self.turn_num += 1
        # print "Starting turn %d" % self.turn_num

        for p in range(self.num_players):
            if self.passed[p]:
                continue

            self.display.draw_board(self.board)
            while True:
                move = self.inputs[p].get_move(p, self.board)
                if move is None:
                    self.passed[p] = True
                    break
                if not self.board.pieces[p, move.piece_index]:
                    print("Error: piece has already been used. Try again:")
                    continue
                try:
                    self.score[p] += self.board.add_move(p, move)
                    self.board.pieces[p, move.piece_index] = False
                    break
                except ValueError:
                    print("Error: move is illegal. Try again:")

    def all_players_passed(self):
        """
        Return True if all players have passed.
        """
        for p in range(self.num_players):
            if not self.passed[p]:
                return False
        return True

    def _print_scores(self):
        for p in range(self.num_players):
            print("Player %d: %d pts" % (p + 1, self.score[p]))

    def play_game(self):
        if len(self.inputs) != 4:
            print("Error: Need 4 players for a game. ")
            sys.exit(1)
        while not self.all_players_passed():
            self.play_turn()

        self._print_scores()
        return self.score