From d5bc3a8069b0c1c19f3293cc3ce220f1d6b4e402 Mon Sep 17 00:00:00 2001 From: Thomas BARBIER Date: Tue, 22 Nov 2022 17:36:24 +0100 Subject: [PATCH] adding board methods and board unit test --- src/board.py | 30 ++++++++++++++++++++++++++++++ src/rules.py | 14 +++++++------- 2 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/board.py b/src/board.py index 22d9b69..0e57408 100644 --- a/src/board.py +++ b/src/board.py @@ -1,5 +1,35 @@ +import numpy as np + class Board: def __init__(self, nbrow, nbcol): self.nbrow = nbrow self.nbcol = nbcol + self.board = np.zeros((nbrow, nbcol)) + + def check_boundaries(self, row, col): + try: + self.board[row, col] + return True + except: + return False + + def remove_piece(self, row, col): + self.board[row, col] = 0 + + def insert_piece(self, row, col, player_id): + self.board[row, col] = player_id + + def insert_piece_into_column(self, col, player_id): + if player_id <= 0: + return False + if not self.check_boundaries(0, col): + return False + + indices = np.where(self.board[:, col] == 0)[0] + if indices.size > 0: + row = indices[0] + self.insert_piece(row, col, player_id) + return True + else: + return False \ No newline at end of file diff --git a/src/rules.py b/src/rules.py index f53a32e..99047f0 100644 --- a/src/rules.py +++ b/src/rules.py @@ -5,13 +5,13 @@ class Rules: self.nb_col = None def check_board_size(self, board: Board) -> Bool: - """ Checks if the board size is correct. - Entries: - self: the Rules class itself - board -> Board: a board - Output: - correct -> Bool: if True, the board size is correct - """ + """ Checks if the board size is correct. + Entries: + self: the Rules class itself + board -> Board: a board + Output: + correct -> Bool: if True, the board size is correct + """ correct = True if not self.nb_col == board.nb_col: