Commit 0fe81be2 authored by Nathan Demary's avatar Nathan Demary
Browse files

Board class implementation

parent a8ca7722
Pipeline #87211 failed with stage
in 22 seconds
......@@ -2,6 +2,7 @@
This module is in charge of the very basic handling of the go board.
from array import array
from collections import namedtuple
from enum import Enum
......@@ -20,15 +21,16 @@ class Board:
'''The main class to manage a go board.'''
def __init__(self, size: int) -> None:
'''Initialize an empty board of size*size intersections.'''
tableau = [[0 for i in range(19)] for j in range(19)]
if tableau:
self.tableau = [[0 for i in range(size)] for j in range(size)]
raise NotImplementedError
def get_tableau(self) -> array:
return self.tableau
def clear(self) -> None:
'''Clear all intersections such that they all become empty.'''
raise NotImplementedError
for item in self.tableau:
for ligne in item:
item[ligne] = 0
def load(self, board_state: str) -> None:
'''Sets all intersection colors from board_state.
......@@ -36,17 +38,34 @@ class Board:
board_state is a string that contains size*size characters (excluding spacing characters).
each character represents the color of an intersection ('-', 'b' or 'w').
raise NotImplementedError
tableau_count = len(self.tableau)
if len(board_state.splitlines()) == tableau_count:
all_lines = board_state.splitlines()
for line in all_lines:
for item in line:
if item == "-":
self.tableau.set_color(self.EMPTY, all_lines.index(line), line.index(item))
if item == "b":
self.tableau.set_color(self.BLACK, all_lines.index(line), line.index(item))
if item == "w":
self.tableau.set_color(self.WHITE, all_lines.index(line), line.index(item))
raise IndexError("Not valid format")
def set_color(self, color: Color, x: int, y: int) -> None:
'''Set the color of the (x,y) intersection of the board.'''
raise NotImplementedError
self.tableau[x][y] = color
def color_at(self, x: int, y: int) -> Color:
'''Get the color of the (x,y) intersection of the board.'''
raise NotImplementedError
current_color = self.tableau[x][y]
if current_color == 0:
return 0
if current_color == 1:
return 1
if current_color == 2:
return 2
def stone_group_at(self, x: int, y: int) -> [Intersection]:
'''Get the maximum-sized stone group that starts from the (x,y) intersection of the board.'''
......@@ -19,3 +19,8 @@ def test_load():
assert b.color_at(x,y) == Color.WHITE, "(x=1,y=1) should be white"
assert b.color_at(x,y) == Color.EMPTY
def test2():
if __name__ == "__main__":
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment