コード例 #1
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_input_format(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 0, 3, 0)
         board.move_current_figure(6, 0, 5, 0)
         board.move_current_figure(0, 1, 2, 0)
         board.move_current_figure(6, 3, 5, 2)
コード例 #2
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_knight_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 1, 2, 1)
         board.move_current_figure(7, 1, 5, 0)
         board.move_current_figure(0, 1, 2, 2)
         board.move_current_figure(5, 0, 5, 2)
コード例 #3
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_rock_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 0, 3, 0)
         board.move_current_figure(6, 0, 4, 0)
         board.move_current_figure(0, 0, 2, 0)
         board.move_current_figure(7, 0, 5, 0)
         board.move_current_figure(2, 0, 3, 4)
コード例 #4
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_king_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 4, 3, 4)
         board.move_current_figure(6, 1, 5, 1)
         board.move_current_figure(0, 4, 1, 4)
         board.move_current_figure(6, 3, 6, 3)
         board.move_current_figure(1, 4, 3, 4)
コード例 #5
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_queen_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 3, 3, 3)
         board.move_current_figure(6, 3, 4, 3)
         board.move_current_figure(0, 3, 2, 3)
         board.move_current_figure(7, 3, 5, 3)
         board.move_current_figure(2, 3, 3, 5)
コード例 #6
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_bishop_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 3, 3, 3)
         board.move_current_figure(6, 2, 5, 2)
         board.move_current_figure(0, 2, 3, 5)
         board.move_current_figure(7, 2, 3, 5)
         board.move_current_figure(3, 5, 3, 3)
コード例 #7
0
    def __init__(self, turn_time=1000.0):
        """
        Parameters
        ----------
        turn_time : float
            Time for one turn [ms]
        """

        self.turn_time = turn_time
        self.game = GameBoard()
        self.game.init_game()

        self.timer = 0.0

        # Game timer
        self.size = self.width, self.height = 640, 400
        self.center = np.array([self.width / 2, self.height / 2])
        # TODO: this should scale with game.max_dist
        self.scale = min(self.width, self.height) / 22.0
コード例 #8
0
    def test_game_serialize(self):
        # TODO: fully test
        test_game = GameBoard()
        test_game.init_game()

        troop_source = test_game.factories[0]
        troop_dest = test_game.factories[1]

        bomb_source = test_game.factories[2]
        bomb_dest = test_game.factories[3]

        test_game.troops.append(Troop(11, troop_source, troop_dest))
        test_game.bombs.append(Bomb(None, bomb_source, bomb_dest))

        jsonized = test_game.to_json()

        self.assertEqual(jsonized, json.loads(json.dumps(jsonized)))

        new_game = GameBoard.from_json(jsonized)

        np.testing.assert_allclose(
            [fac.position for fac in test_game.factories],
            [fac.position for fac in new_game.factories])
コード例 #9
0
ファイル: main.py プロジェクト: AntonVanke/python-7days
 def human_computer_mode():
     # 人机对战模式
     hc_game = GameBoard(mainmenu)
     hc_game.show()
     hc_game.return_mainmenu_singal.connect(hc_game.hide)
コード例 #10
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_check_chess(self):
     board = GameBoard()
     board.move_current_figure(1, 4, 3, 4)
     board.move_current_figure(6, 5, 4, 5)
     board.move_current_figure(0, 3, 4, 7)
     self.assertEqual('Chess', board.get_status())
コード例 #11
0
ファイル: main.py プロジェクト: hamid779977/4connect-ai
import os
from game import GameBoard
from player import AIPlayer
from player import Person
import sys
import pygame

gameBoard = GameBoard()

# ----------- py game
pygame.init()
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
cycleRadius = int(
    ((SCREEN_HEIGHT - SCREEN_HEIGHT / 6) / len(gameBoard.game_map)) / 2)
arrow = pygame.image.load(os.path.join("images", "arrow.png")).convert()
arrow = pygame.transform.scale(arrow, (cycleRadius * 2, cycleRadius * 2))
arrowRed = pygame.image.load(os.path.join("images", "arrow-red.png")).convert()
arrowRed = pygame.transform.scale(arrowRed, (cycleRadius * 2, cycleRadius * 2))
arrowBlue = pygame.image.load(os.path.join("images",
                                           "arrow-blue.png")).convert()
arrowBlue = pygame.transform.scale(arrowBlue,
                                   (cycleRadius * 2, cycleRadius * 2))

arrowsColor = [0] * len(gameBoard.game_map[0])

myFont = pygame.font.SysFont("monospace", 75)


def refresh_screen():
コード例 #12
0
from game import GameBoard, Player, Game

gameboard = GameBoard(final_state=None, color_pool=[1, 2, 3, 4, 5, 6], color_num=4)
print(gameboard)

p1 = Player("felix")
print(p1)

game = Game(gameboard, p1)

guess = p1.guess(gameboard.color_pool)
print(guess)
gameboard.add_guess(guess)
full_correct, half_correct = gameboard.give_feedback(guess)
gameboard.add_feedback([full_correct, half_correct])
print(full_correct, half_correct)
game.state
コード例 #13
0
ファイル: main.py プロジェクト: majid-fathi/4connect-ai
# colors
RED = (255, 100, 100)
BLUE = (100, 100, 255)
GREEN = (100, 255, 100)
WHITE = (255, 255, 255)
BLACK = (10, 10, 10)
GRAY = (100, 100, 100)
GRAY_FILL = (80, 80, 80)
LIGHT_RED = (255, 150, 150)
LIGHT_BLUE = (150, 150, 255)
LIGHT_GREEN = (150, 255, 150)
GRAY_RED = (120, 100, 100)
GRAY_BLUE = (100, 100, 120)
# ------
gameBoard = GameBoard()

SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600

ROW_COUNT = len(gameBoard.game_map)
COLUMN_COUNT = len(gameBoard.game_map[0])

pygame.init()
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("4 connect")
clock = pygame.time.Clock()
cycleRadius = int(((SCREEN_HEIGHT - SCREEN_HEIGHT / 6) / ROW_COUNT) / 2)
arrow = pygame.image.load(os.path.join("images", "arrow.png")).convert()
arrow = pygame.transform.scale(arrow, (cycleRadius * 2, cycleRadius * 2))
arrowRed = pygame.image.load(os.path.join("images", "arrow-red.png")).convert()
コード例 #14
0
ファイル: main.py プロジェクト: majid-fathi/4connect-ai
def run_game():
    global currentPlayer
    global gameBoard
    global game_running
    game_running = True
    currentPlayer = player1
    fake_wait = 0
    dropped_count = 0
    gameBoard = GameBoard()
    while dropped_count != allSlots:
        dropped = False
        if type(currentPlayer) == Person:
            for ev in pygame.event.get():
                if ev.type == pygame.QUIT:
                    sys.exit()

                if ev.type == pygame.MOUSEBUTTONDOWN:
                    if ev.pos[0] < COLUMN_COUNT * cycleRadius * 2:
                        try:
                            input_column = int(ev.pos[0] / (cycleRadius * 2))
                            gameBoard.drop_piece(currentPlayer.number,
                                                 input_column)
                            dropped = True
                        except Exception as exception:
                            print(exception)

        else:  # currentPlayer == AIPlayer
            for ev in pygame.event.get():
                if ev.type == pygame.QUIT:
                    sys.exit()
            fake_wait += 1
            if fake_wait == AI_FAKE_TIME:
                input_column = currentPlayer.get_drop_input(gameBoard.game_map)
                gameBoard.drop_piece(currentPlayer.number, input_column)
                dropped = True

        refresh_screen()

        if dropped:
            fake_wait = 0
            if gameBoard.check_win(currentPlayer.number):
                break
            if currentPlayer == player1:
                currentPlayer = player2
            else:
                currentPlayer = player1
            dropped_count += 1

        clock.tick(30)

    if dropped_count == allSlots:
        label = finalFont.render("Game draw", True, WHITE)
    elif currentPlayer == player1:
        label = finalFont.render("Player 1 won !!", True, LIGHT_BLUE)
    else:
        label = finalFont.render("Player 2 won !!", True, LIGHT_RED)

    screen.blit(label, (40, ROW_COUNT * cycleRadius))
    pygame.display.update()
    pygame.time.wait(1000)
    game_running = False
コード例 #15
0
ファイル: main.py プロジェクト: GDawg4/aiProyecto
# Rodrigo Garoz 18102
# Primer proyecto/Inteligencia Artificial

from game import GameBoard

r = GameBoard()
for i in range(0):
    print(i)
r.play()
#print(r.play_best_move())
#r.get_possible_moves(5, 1)
#print(r.__str__())

# print(r.board_score(r.game_board, r.current_turn))

#for i in r.board_score(r.game_board, r.current_turn):
#print(i)
コード例 #16
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_pawn_move(self):
     board = GameBoard()
     with self.assertRaises(InvalidMove):
         board.move_current_figure(1, 0, 3, 0)
         board.move_current_figure(6, 4, 5, 4)
         board.move_current_figure(3, 0, 3, 1)
コード例 #17
0
class App:
    def __init__(self, turn_time=1000.0):
        """
        Parameters
        ----------
        turn_time : float
            Time for one turn [ms]
        """

        self.turn_time = turn_time
        self.game = GameBoard()
        self.game.init_game()

        self.timer = 0.0

        # Game timer
        self.size = self.width, self.height = 640, 400
        self.center = np.array([self.width / 2, self.height / 2])
        # TODO: this should scale with game.max_dist
        self.scale = min(self.width, self.height) / 22.0

    def pygame_init(self):
        pygame.init()
        pygame.font.init()

        self.clock = pygame.time.Clock()
        self.clock.tick()

        self._display_surf = pygame.display.set_mode(
            self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)

        bgfile = os.path.join("res", "background.png")
        self._background = pygame.image.load(bgfile)

        self._running = True

        self.font = pygame.font.SysFont("arial", 12)
        self.load_sprites()

    def load_sprites(self):
        self.factory_sprites = {}

        for team, color in [(-1, "blue"), (0, "grey"), (1, "red")]:
            self.factory_sprites[team] = []
            for i in range(4):
                filename = "{}_fac_{}.png".format(color, i)
                path = os.path.join("res", filename)
                image = pygame.image.load(path)

                self.factory_sprites[team].append(image)

        self.troop_sprites = {}
        self.bomb_sprites = {}

        for team, color in [(-1, "blue"), (1, "red")]:
            filename = "{}_troop.png".format(color)
            path = os.path.join("res", filename)
            image = pygame.image.load(path)

            self.troop_sprites[team] = image

            filename = "{}_bomb.png".format(color)
            path = os.path.join("res", filename)
            image = pygame.image.load(path)

            self.bomb_sprites[team] = image

    def on_event(self, event):
        pass

    def loop(self):
        # TODO: handle player input

        # Update the game once per ``self.turn_time`` ms.
        self.clock.tick()
        self.timer += self.clock.get_time()
        while self.timer > self.turn_time:
            self.timer -= self.turn_time

            # TODO: testing input
            for factory in self.game.factories:
                if factory.team in [-1, 1]:
                    self.game.orders[factory.team].append(Inc(factory.id))
                    if factory.production == 3:
                        targets = self.game.factories.copy()
                        random.shuffle(targets)
                        if self.game.current_turn % 10 == 0:
                            self.game.orders[factory.team].append(
                                SendBomb(factory.id, targets[0].id))
                        for t_factory in targets:
                            if factory.id != t_factory.id:
                                self.game.orders[factory.team].append(
                                    Move(
                                        factory.id,
                                        t_factory.id,
                                        t_factory.stock + 1,
                                    ))
            # TODO: end testing input

            self.game.update()

        if self.game.game_over:
            self._running = False

    def render(self):
        # Clear screen
        self._display_surf.blit(self._background, (0, 0))

        for factory in self.game.factories:
            self.draw_factory(factory)

        for unit in self.game.bombs + self.game.troops:
            self.draw_unit(unit)

        pygame.display.flip()

    def draw(self, img, position, rotation=0.0):
        center = np.array(position) * self.scale + self.center
        topleft = (center[0] - img.get_width() / 2,
                   center[1] - img.get_height() / 2)

        image = pygame.transform.rotate(img, rotation)

        self._display_surf.blit(image, topleft)

    def draw_factory(self, factory):
        team_facs = self.factory_sprites[factory.team]
        factory_sprite = team_facs[factory.production]

        self.draw(factory_sprite, factory.position)

        status = " (D)" if factory.disabled_turns > 0 else ""
        text = self.font.render(
            "{}{}".format(factory.stock, status),
            True,
            (255, 255, 255),
        )
        self.draw(text, factory.position)

    def draw_unit(self, unit):
        if type(unit) == Bomb:
            sprite = self.bomb_sprites[unit.team]
            status_text = ""
        elif type(unit) == Troop:
            sprite = self.troop_sprites[unit.team]
            status_text = str(unit.strength)
        else:
            raise ValueError("Unknown unit type {}".format(type(unit)))

        # Make the unit smoothly move to the destination
        draw_position = np.array(unit.get_position())

        direction = np.array(unit.destination.position) \
            - np.array(unit.source.position)

        # Timer in ms
        slide = direction * self.timer / (1000.0 * unit.distance)
        draw_position += slide

        rotation = math.degrees(math.atan2(direction[0], direction[1])) + 180

        self.draw(sprite, draw_position, rotation=rotation)

        text = self.font.render(
            status_text,
            True,
            (255, 255, 255),
        )
        self.draw(text, draw_position)

    def cleanup(self):
        self.font = None
        pygame.font.quit()
        pygame.quit()

    def execute(self):
        if self.pygame_init() is False:
            self._running = False

        self.render()
        while (self._running):
            for event in pygame.event.get():
                self.on_event(event)
            self.loop()
            self.render()
        self.cleanup()
コード例 #18
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_next_turn(self):
     board = GameBoard()
     board.move_current_figure(1, 0, 2, 0)
     self.assertEqual('Next turn', board.get_status())
     board.move_current_figure(6, 0, 5, 0)
     self.assertEqual('Next turn', board.get_status())
コード例 #19
0
ファイル: tests.py プロジェクト: fearfac7or/chess
 def test_check_turn(self):
     board = GameBoard()
     with self.assertRaises(NotYourTurn):
         board.move_current_figure(1, 1, 3, 1)
         board.move_current_figure(1, 3, 3, 3)