Exemplo n.º 1
0
def test__check_vertical():
    b = Board()
    player = 1
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b._check_vertical(1, 0, 3) is False
    assert b.add_disc(player, 0)
    assert b._check_vertical(1, 0, 2)
Exemplo n.º 2
0
def test_is_col_full():
    b = Board()
    player = 1
    assert b.is_col_full(0) is False
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0) is False
    assert b.is_col_full(0) is True
Exemplo n.º 3
0
def test_add_disc():
    b = Board()
    player = 1
    assert b.add_disc(player, 0)
    assert b.grid[5][0] == player
    assert b.add_disc(player, 0)
    assert b.grid[4][0] == player
    assert b.add_disc(player, 0)
    assert b.grid[3][0] == player
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 0) is False
Exemplo n.º 4
0
def test__check_diagonal_left():
    b = Board()
    player = 1
    assert b._check_diagonal_left(player, 3, 2) is False
    assert b._check_diagonal_left(player, 2, 3) is False
    assert b._check_diagonal_left(player, 1, 4) is False
    assert b._check_diagonal_left(player, 0, 5) is False
    assert b.add_disc(player, 3)
    assert b.add_disc(player, 3)
    assert b.add_disc(player, 3)
    assert b.add_disc(player, 2)
    assert b.add_disc(player, 2)
    assert b.add_disc(player, 1)
    player = 2
    assert b.add_disc(player, 3)
    assert b.add_disc(player, 2)
    assert b.add_disc(player, 1)
    assert b.add_disc(player, 0)
    assert b._check_diagonal_left(player, 3, 2)
    assert b._check_diagonal_left(player, 2, 3)
    assert b._check_diagonal_left(player, 1, 4)
    assert b._check_diagonal_left(player, 0, 5)
Exemplo n.º 5
0
    def play(self):
        b = Board()
        player = None  # first player
        has_won_condition = False
        valid_input_condition = False

        while not has_won_condition:

            if player is None or player == 2:
                player = 1
            else:
                player = 2

            print("Current grid:")
            print(b.__repr__())

            while not valid_input_condition:
                play = input(" Player " + str(player) +
                             ": Please enter column number (0 - 6).")
                if play.isdigit():
                    play = int(play)
                    if 0 <= play <= 6:
                        if b.add_disc(player, play):
                            has_won_condition = b.has_won(
                                player, b.current_col, b.current_row)
                            valid_input_condition = True
                        else:
                            print("Full column, try another column.")
                    else:
                        print("Column not in range.")
                else:
                    print("Column must be a number.")

            valid_input_condition = False

        self._announce_win(player)
Exemplo n.º 6
0
class ServerLogic:
    def __init__(self):
        logging.basicConfig(format="%(levelname)s: %(message)s",
                            level=logging.INFO)
        self.b = Board()

    async def generate_reply(self, player_id, request_data):
        logging.info("recieved request_data:")
        logging.info(request_data)
        logging.info("from player id:")
        logging.info(player_id)

        reply = {}

        if not request_data['op'] == pygame.QUIT:
            if self._is_valid_keydown(request_data):
                disc_added = self.b.add_disc(player_id,
                                             request_data['key'] - 48)
                if disc_added:
                    if self.b.has_won(player_id, self.b.current_col,
                                      self.b.current_row):
                        reply['op'] = ServerOpCodes.USER_WON
                    else:
                        reply['op'] = ServerOpCodes.UPDATE_GRID
                        # reply['grid'] = self.b.grid
                else:
                    reply['op'] = ServerOpCodes.COL_FULL
            else:
                reply['op'] = ServerOpCodes.ILLEGAL_INPUT
        else:
            reply['op'] = ServerOpCodes.GAME_ENDED

        return reply

    def _is_valid_keydown(self, request_data):
        return 48 <= request_data['key'] <= 54
Exemplo n.º 7
0
def test__check_horizontal():
    b = Board()
    player = 1
    assert b._check_horizontal(player, 3, 5) is False
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 1)
    assert b._check_horizontal(player, 1, 5) is False
    assert b.add_disc(player, 2)
    assert b.add_disc(player, 3)
    assert b._check_horizontal(player, 3, 5)
    player = 2
    assert b.add_disc(player, 0)
    assert b.add_disc(player, 1)
    assert b._check_horizontal(player, 1, 4) is False
    assert b.add_disc(player, 2)
    assert b.add_disc(player, 3)
    assert b._check_horizontal(player, 3, 4)
    b1 = Board()
    assert b1._check_horizontal(player, 3, 5) is False
    assert b1.add_disc(player, 6)
    assert b1.add_disc(player, 5)
    assert b1.add_disc(player, 4)
    assert b1.add_disc(player, 3)
    assert b1._check_horizontal(player, 3, 5)
Exemplo n.º 8
0
class GameWithGui:
    def __init__(self):
        self.SQUARE_SIZE = 100
        self.RADIUS_SIZE = self.SQUARE_SIZE / 2 - 10
        self.b = Board()

        pygame.init()

        height = self.b.NUM_OF_ROWS * self.SQUARE_SIZE
        width = self.b.NUM_OF_COLS * self.SQUARE_SIZE
        size = (width, height)

        self.screen = pygame.display.set_mode(size)
        self.screen.fill('Lavender')
        self.myfont = pygame.font.SysFont("monospace", 75)

        self.draw_grid(self.b.grid)
        pygame.display.update()

    async def play(self):
        player = 1  # first player
        has_won_condition = False
        valid_input_condition = False

        while not has_won_condition:

            for event in pygame.event.get():
                self.draw_grid(self.b.grid)
                pygame.display.update()
                if event.type == pygame.QUIT:
                    # Close the program any way you want, or troll users who want to close your program.
                    raise SystemExit
                elif event.type == pygame.KEYDOWN:
                    if event.key == pygame.K_0:
                        print("Player " + str(player) + ": Column 0")
                        if self.b.add_disc(player, 0):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_1:
                        print("Player " + str(player) + ": Column 1")
                        if self.b.add_disc(player, 1):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_2:
                        print("Player " + str(player) + ": Column 2")
                        if self.b.add_disc(player, 2):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_3:
                        print("Player " + str(player) + ": Column 3")
                        if self.b.add_disc(player, 3):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_4:
                        print("Player " + str(player) + ": Column 4")
                        if self.b.add_disc(player, 4):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_5:
                        print("Player " + str(player) + ": Column 5")
                        if self.b.add_disc(player, 5):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]
                    elif event.key == pygame.K_6:
                        print("Player " + str(player) + ": Column 6")
                        if self.b.add_disc(player, 6):
                            has_won_condition = self.b.has_won(
                                player, self.b.current_col, self.b.current_row)
                            player = [2, 1][player - 1]

        # label = self.myfont.render("Player " + str(player) + " wins!!", 1, pygame.Color("Yellow"))
        # self.screen.blit(label, (40, 10))
        pygame.time.wait(5000)

    def draw_grid(self, grid):
        # print(self.b.__repr__())
        for i in range(7):
            text = self.myfont.render(str(i), True, pygame.Color("White"),
                                      pygame.Color("Black"))
            self.screen.blit(text, (100 * i, 0))
        for row in range(self.b.NUM_OF_ROWS):
            for col in range(self.b.NUM_OF_COLS):
                pygame.draw.rect(
                    self.screen, pygame.Color('Black'),
                    (col * self.SQUARE_SIZE, row * self.SQUARE_SIZE,
                     self.SQUARE_SIZE, self.SQUARE_SIZE), 10)

                if grid[row][col] == 2:
                    pygame.draw.circle(self.screen, pygame.Color('Red'),
                                       (col * self.SQUARE_SIZE + 50,
                                        row * self.SQUARE_SIZE + 50),
                                       self.RADIUS_SIZE)
                elif grid[row][col] == 1:
                    pygame.draw.circle(self.screen, pygame.Color('Blue'),
                                       (col * self.SQUARE_SIZE + 50,
                                        row * self.SQUARE_SIZE + 50),
                                       self.RADIUS_SIZE)