adding board methods and board unit test

main
Thomas BARBIER 2 years ago
parent 737d7a8b36
commit d5bc3a8069

@ -1,5 +1,35 @@
import numpy as np
class Board: class Board:
def __init__(self, nbrow, nbcol): def __init__(self, nbrow, nbcol):
self.nbrow = nbrow self.nbrow = nbrow
self.nbcol = nbcol 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

@ -5,13 +5,13 @@ class Rules:
self.nb_col = None self.nb_col = None
def check_board_size(self, board: Board) -> Bool: def check_board_size(self, board: Board) -> Bool:
""" Checks if the board size is correct. """ Checks if the board size is correct.
Entries: Entries:
self: the Rules class itself self: the Rules class itself
board -> Board: a board board -> Board: a board
Output: Output:
correct -> Bool: if True, the board size is correct correct -> Bool: if True, the board size is correct
""" """
correct = True correct = True
if not self.nb_col == board.nb_col: if not self.nb_col == board.nb_col:

Loading…
Cancel
Save