예제 #1
0
 def _createBasicAI(self):
     from AI.ai import AI
     print("Basic AI Mode")
     self._mode = Mode.BasicAI
     nbInstances = multiprocessing.cpu_count()
     # nbInstances = 1
     DarkLogic.init(nbInstances)
     self._player = AI(nbInstances, LogicGame._AI_TIMEOUT)
예제 #2
0
def test_adapter():
    mode = Director().construct_game_mode(FlexSquareBuilder())
    board = Board(mode)
    adapter = AiAdapter(AI.init())

    move = adapter.get_step(board)
    assert move[:2] != move[2:]
    assert abs(move[0] - move[2]) <= 2 and abs(move[1] - move[3])

    assert board.do_move(*move) is True
예제 #3
0
 def demonstration(self, name, content, nbThreads):
     print("Test AI on " + name + " theorem with " + str(nbThreads) +
           " cores")
     DarkLogic.init(nbThreads)
     ai = AI(nbThreads, 60)
     assert DarkLogic.makeTheorem(
         name, content), "cannot make " + name + " theorem"
     DarkLogic.printTheorem()
     start = time.perf_counter()
     while not DarkLogic.isOver():
         action = ai.play()
         DarkLogic.getActions()
         print(ai.name() + " plays action with id " + str(action.id()))
         DarkLogic.apply(action.id())
         DarkLogic.printTheorem()
         print(
             "____________________________________________________________________________"
         )
     end = time.perf_counter()
     if DarkLogic.hasAlreadyPlayed():
         if DarkLogic.isDemonstrated():
             print(ai.name() + " won! " + ai.name() +
                   " finished the demonstration!")
         elif DarkLogic.isAlreadyPlayed():
             print(ai.name() + " lost! Repetition of theorem!")
         elif DarkLogic.isEvaluated():
             print(
                 ai.name() +
                 " lost! Cannot (\"back-\")demonstrate that a theorem is false with implications"
             )
         elif not DarkLogic.canBeDemonstrated():
             print(
                 ai.name() +
                 " lost! This theorem cannot be demonstrated! " +
                 "It can be true or false according to the values of its variables"
             )
     else:
         if DarkLogic.isDemonstrated():
             print("Game Over! the demonstration is already finished!")
             self._elapsed_seconds = end - start
         elif not DarkLogic.canBeDemonstrated():
             print(
                 "Game Over! This theorem cannot be demonstrated! " +
                 "It can be true or false according to the values of its variables"
             )
     self.pushEvent(Event.EventEnum.STOP)
예제 #4
0
    def __init__(self, screen: pygame.Surface, operator: GuiOperator):
        self.screen = screen
        self.operator = operator
        self.title = Text(screen, (250, 20), "Выбор режима", 50)
        builders = [
            ClassicModeBuilder(),
            AdvancedModeBuilder(),
            FlexSquareBuilder(),
            KingPoliceModeBuilder(),
            WallModeBuilder(),
            AllUnitsModeBuilder()
        ]

        director = Director()

        modes = [director.construct_game_mode(builder) for builder in builders]

        ai_mode = AiDecorator(
            director.construct_game_mode(FlexSquareBuilder()),
            AiAdapter(AI.init()))
        ai_mode.name = "Диагональные уголки с ИИ"

        modes.append(ai_mode)

        buttons = [
            CheckButton(screen, (100, 100 + 60 * i, 440, 45), modes[i].name)
            for i in range(len(modes))
        ]

        self.mod_pairs = list(zip(buttons, modes))

        self.back_button = Button(screen, (590, 590, 100, 60), 'Назад')
        self.go_button = Button(screen, (100, 590, 180, 60), 'Играть!',
                                colors.RED, colors.WHITE)

        self.chosen_mode = None
        self.checked_button = None
예제 #5
0
def test_decorator():
    mode = Director().construct_game_mode(FlexSquareBuilder())
    adapter = AiAdapter(AI.init())
    new_mode = AiDecorator(mode, adapter)
    assert hasattr(new_mode, 'ai')
    assert ModeFeature.AI in new_mode.features
예제 #6
0
from Games.pong import Pong
from AI.ai import AI
import pygame
from pygame.locals import *

from time import time

#testing code
if __name__ == "__main__":
    EPISODES = 100000
    agent=AI(Pong.OUTPUT_SHAPE[1],2)
    batch_size = 64
    agent.load("./save.h5")

    end_learning = time() + 2*60*60

    for e in range(EPISODES):
        pong = Pong(key_bindings = {0 : K_DOWN, 1: K_UP}, max_score = 3)
        state = pong.state
        end = time() + 1 * 60
        reward_per_act = 0
        i = 0
        while not pong.done and time() < end:
            if i == 0:
                act=agent.getAction(state)
            state_new, reward, done = pong.execute(act) 
            pong.draw()
            reward_per_act += reward
            if i == 4:
                agent.remember(state, act, reward_per_act, state_new, done)
                state = state_new
예제 #7
0
from AI.entity import Entity
from AI.ai import AI

player = Entity('Игрок', damage=25)
ai = AI('ИИ')

games_count = 100

for _ in range(games_count):
    while player.health > 0 and ai.health > 0:
        player.turn(ai)
        ai.turn(player)

    if player.health > 0:
        ai.update_stats(is_winner=False)
    elif player.health <= 0:
        ai.update_stats(is_winner=True)
    else:
        print('Ничья')

    player.reset()
    ai.reset()