Пример #1
0
class Connect4(Connect4Base):
    def __init__(self, config, agent):
        super().__init__(config)
        self.agent = agent
        self.agent.setup(2)
        self.gui = Gui(config)

    def draw_board_check_if_winning(self, board, piece):
        self.gui.draw_board(board)
        if self.check_if_winning(board, piece):
            self.gui.display(10, 10, RED if piece == 1 else YELLOW,
                             "Player {} wins".format(piece), True)
            return True
        return False

    def run(self):
        while True:
            board = np.full((config.rows, config.columns), 0, np.int)
            self.gui.draw_board(board)
            while True:
                # human
                col = self.gui.drop_piece(RED)
                board = self.drop_piece(board, col, 1)
                if self.draw_board_check_if_winning(board, 1):
                    self.agent.game_over(1)
                    break

                # agent
                if len(self.valid_moves(board)) == 0:
                    self.gui.display(10, 10, WHITE, "Draw", False)
                    self.agent.game_over(0)
                    break
                col = self.agent.move(board)
                board = self.drop_piece(board, col, 2)
                if self.draw_board_check_if_winning(board, 2):
                    self.agent.game_over(2)
                    break
            if self.gui.wait_for_event(10000) == False:
                break

    def end(self):
        self.agent.teardown()
Пример #2
0
class Connect4(Connect4Base):
    def __init__(self, config, agent1, agent2):
        super().__init__(config)
        self.agent1 = agent1
        self.agent2 = agent2
        self.agent1.setup(1)
        self.agent2.setup(2)
        self.gui = Gui(config)

    # player 1 - red, player 2 - yellow
    def run(self, step_wait):
        while True:
            board = np.full((config.rows, config.columns), 0, np.int)
            self.gui.draw_board(board)
            self.gui.display(10, 10, RED,
                             "Player 1 - {}".format(self.agent1.name()), True)
            self.gui.display(10, 50, YELLOW,
                             "Player 2 - {}".format(self.agent2.name()), False)
            piece = 1  # starts first
            while True:
                if len(self.valid_moves(board)) == 0:
                    self.gui.display(10, 10, WHITE, "Draw", False)
                    break
                agent = self.agent1 if piece == 1 else self.agent2
                col = agent.move(board)
                board = self.drop_piece(board, col, piece)
                self.gui.draw_board(board)
                if self.check_if_winning(board, piece):
                    self.gui.display(10, 10, RED if piece == 1 else YELLOW,
                                     "Player {} wins".format(piece), True)
                    break
                piece = piece % 2 + 1
                self.gui.wait_for_event(step_wait)
            self.gui.wait_for_event(10000)

    def end(self):
        self.agent1.teardown()
        self.agent2.teardown()
Пример #3
0
__author__ = 'jdubois'

from grid import SudokuGrid
from gui import Gui
from algorithms import Solver

file_to_open = "data/test_easy.txt"
file_to_open = "data/test_hard.txt"

grille = SudokuGrid(file_to_open)

g = Gui()

print("Displaying original grid")
g.display(grille)

Solver.deduction_solve(grille)

print("Displaying grid after first logical deductions")
g.display(grille)

#print("overall possibilities:",sum([len(cell.possibilities) for cell in grille.empty_cells],0),"for a total of",len(grille.empty_cells))

ordered_cells = sorted(grille.empty_cells, key = lambda cell: len(cell.possibilities))
if len(ordered_cells) > 0:
    print("Trying remaining possibilities on empty cells")
    Solver.try_to_fill_randomly(grille, ordered_cells = ordered_cells)

if grille.isFilled():
    print("Displaying final grid")
    g.display(grille)
Пример #4
0
class Tracker:
    def __init__(self):
        self.gui = Gui(self)
        self.gui.show()

    def read_lsen_list(self, file_name):
        lsen_cemis = []

        with open(file_name, 'r') as f:
            line = f.readline()
            while line:
                line = f.readline().strip()
                lsen_cemis.append(line)

        return lsen_cemis

    def parse_files(self, lsen_cemis, dir="../data/"):
        try:
            files = [f for f in listdir(dir) if isfile(join(dir, f))]
        except:
            raise OSError('Could not parse shared files list!')
        else:
            all_learners = []

            self.gui.set_prog_len(len(files) + 2)

            for f in files:
                # read in excel
                self.gui.inc_prog("reading " + f)
                file_in = dir + f
                info = InfoSheet(file_in)

                # select lsen_learners as list of objects
                lsen_learners = info.get_learners(lsen_cemis)

                if info.type == TYPE_SCHEDULE:
                    schedule = ScheduleSheet(file_in)
                    schedule.get_codes(lsen_learners)

                elif info.type == TYPE_COMBO:
                    combo = ComboSheet(file_in)
                    combo.get_codes(lsen_learners)

                else:
                    continue

                all_learners += lsen_learners

            return all_learners

    def process(self, lst, cemis_path, records_dir, track_path):
        # read in cemis
        self.gui.init_prog()
        lsen_cemis = self.read_lsen_list(cemis_path)

        t0 = time.time()

        # read in learners
        learners = self.parse_files(lsen_cemis, records_dir)

        # get school
        school = (learners[0]).school_name

        # checked missed
        cemis = [l.cemis for l in learners]
        missed = [l for l in lsen_cemis if l not in cemis]

        # export
        self.gui.inc_prog(msg="Exporting tracking file")
        track = TrackingSheet(track_path, lst, school)
        track.export(learners)
        self.gui.complete()

        t1 = time.time()
        elapsed = t1 - t0
        self.gui.display("File written to 'out/'")
        self.gui.display("%d learners processed in %.2fs" %
                         (len(learners), elapsed))

        if len(missed) > 0:
            self.gui.display("Missed: " + str(missed)[1:-1])
Пример #5
0
import tkinter as tk

from solver import Solver
from gui import Gui

if __name__ == '__main__':

    BLANK = [[0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0, 0],
             [0, 0, 0, 0, 0, 0, 0, 0, 0]]

    ROOT = tk.Tk()
    SOLVER = Solver(BLANK)
    GUI = Gui(ROOT, SOLVER)
    GUI.display()
    ROOT.mainloop()