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 @@ ...@@ -2,6 +2,7 @@
This module is in charge of the very basic handling of the go board. This module is in charge of the very basic handling of the go board.
''' '''
from array import array
from collections import namedtuple from collections import namedtuple
from enum import Enum from enum import Enum
...@@ -20,15 +21,16 @@ class Board: ...@@ -20,15 +21,16 @@ class Board:
'''The main class to manage a go board.''' '''The main class to manage a go board.'''
def __init__(self, size: int) -> None: def __init__(self, size: int) -> None:
'''Initialize an empty board of size*size intersections.''' '''Initialize an empty board of size*size intersections.'''
tableau = [[0 for i in range(19)] for j in range(19)] self.tableau = [[0 for i in range(size)] for j in range(size)]
if tableau:
tableau.append(1)
raise NotImplementedError def get_tableau(self) -> array:
return self.tableau
def clear(self) -> None: def clear(self) -> None:
'''Clear all intersections such that they all become empty.''' '''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: def load(self, board_state: str) -> None:
'''Sets all intersection colors from board_state. '''Sets all intersection colors from board_state.
...@@ -36,17 +38,34 @@ class Board: ...@@ -36,17 +38,34 @@ class Board:
board_state is a string that contains size*size characters (excluding spacing characters). board_state is a string that contains size*size characters (excluding spacing characters).
each character represents the color of an intersection ('-', 'b' or 'w'). each character represents the color of an intersection ('-', 'b' or 'w').
''' '''
tableau_count = len(self.tableau)
raise NotImplementedError 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))
else:
raise IndexError("Not valid format")
def set_color(self, color: Color, x: int, y: int) -> None: def set_color(self, color: Color, x: int, y: int) -> None:
'''Set the color of the (x,y) intersection of the board.''' '''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: def color_at(self, x: int, y: int) -> Color:
'''Get the color of the (x,y) intersection of the board.''' '''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]: 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.''' '''Get the maximum-sized stone group that starts from the (x,y) intersection of the board.'''
......
...@@ -19,3 +19,8 @@ def test_load(): ...@@ -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.WHITE, "(x=1,y=1) should be white"
else: else:
assert b.color_at(x,y) == Color.EMPTY assert b.color_at(x,y) == Color.EMPTY
def test2():
len(board_state.splitlines())
if __name__ == "__main__":
print(test_load())
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