Skip to content
This repository has been archived by the owner on Nov 15, 2024. It is now read-only.

Commit

Permalink
Fix error in ADT_code
Browse files Browse the repository at this point in the history
  • Loading branch information
inesiscosta committed Nov 5, 2023
1 parent 153f43d commit 9d1de7c
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 41 deletions.
15 changes: 8 additions & 7 deletions ADT_code/ADT_goban.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from random import randint

def create_empty_goban(n):
if type(n) == int and n in (9, 13, 19):
return [randint(0, 10**6), (n, {})]
Expand All @@ -18,7 +19,7 @@ def create_goban(n, white_intersections, black_intersections):
for i in black_intersections:
if is_player_stone(obtain_stone(goban, i)):
raise ValueError('create_goban: invalid arguments')
place_stone(goban, i, create_neutral_stone())
place_stone(goban, i, create_black_stone())
return goban

raise ValueError('create_goban: invalid arguments')
Expand Down Expand Up @@ -63,26 +64,26 @@ def remove_chain(board, tuplo):
return board

def is_goban(arg):
def intersecao_dentro_limites(i1, i2):
def intersection_within_limits(i1, i2):
return 'A' <= obtain_col(i1) <= obtain_col(i2) and 1 <= obtain_row(i1) <= obtain_row(i2)
return isinstance(arg,list) and len(arg) == 2 and type(arg[0]) == int and \
type(arg[1]) == tuple and len(arg[1]) == 2 and type(arg[1][0]) == int and arg[1][0] in (9, 13, 19) \
and type(arg[1][1]) == dict and all(is_intersection(k) for k in arg[1][1]) and \
all(intersecao_dentro_limites(k, obtain_last_intersection(arg)) for k in arg[1][1]) and \
all(intersection_within_limits(k, obtain_last_intersection(arg)) for k in arg[1][1]) and \
all(is_stone(arg[1][1][k]) for k in arg[1][1])

def is_valid_intersection(board, pos):
def intersecao_dentro_limites(i1, i2):
def intersection_within_limits(i1, i2):
return 'A' <= obtain_col(i1) <= obtain_col(i2) and 1 <= obtain_row(i1) <= obtain_row(i2)
return intersecao_dentro_limites(pos, obtain_last_intersection(board))
return intersection_within_limits(pos, obtain_last_intersection(board))

def gobans_iguais(g1, g2):
def equal_gobans(g1, g2):
if is_goban(g1) and is_goban(g2) and g1[1][0] == g2[1][0]:
if sorted(g1[1][1].keys()) == sorted(g2[1][1].keys()):
return all(equal_stones(g1[1][1][k], g2[1][1][k]) for k in g1[1][1])
return False

def goban_para_str(board):
def goban_to_str(board):
LETTERS = tuple('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
n_v, n_h = board[1][0], board[1][0]
res = ' ' + ''.join(f'{l} ' for l in LETTERS[:n_v]).rstrip() + '\n'
Expand Down
16 changes: 8 additions & 8 deletions ADT_code/HLF_calculate_points.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def calcula_pontos(board):
def calculate_points(board):
white_points, black_points = obtain_player_stones(board)

for territorio in obtain_territories(board):
limites = obtain_different_adjacents(board, territorio)
if limites:
if all(is_white_stone(obtem_pedra(board,i)) for i in limites):
white_points += len(territorio)
elif all(is_black_stone(obtem_pedra(board,i)) for i in limites):
black_points += len(territorio)
for territory in obtain_territories(board):
limits = obtain_different_adjacents(board, territory)
if limits:
if all(is_white_stone(obtain_stone(board,i)) for i in limits):
white_points += len(territory)
elif all(is_black_stone(obtain_stone(board,i)) for i in limits):
black_points += len(territory)
return white_points, black_points
10 changes: 5 additions & 5 deletions ADT_code/HLF_legal_play.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
def is_legal_play(board, pos, pedra, last_board):
def is_legal_play(board, pos, stone, last_board):
if is_valid_intersection(board, pos) and \
not is_player_stone(obtain_stone(board, pos)):
novo_board = create_copy_goban(board)
play(novo_board, pos, pedra)
if len(obtain_different_adjacents(novo_board, obtain_chain(novo_board, pos))) == 0:
new_board = create_copy_goban(board)
play(new_board, pos, stone)
if len(obtain_different_adjacents(new_board, obtain_chain(new_board, pos))) == 0:
return False
elif equal_gobans(novo_board, last_board):
elif equal_gobans(new_board, last_board):
return False
else:
return True
Expand Down
6 changes: 3 additions & 3 deletions ADT_code/HLF_player_turn.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

def turno_jogador(current, stone, last_board):
def eh_cadeia_intercecao_ok(cad):
def player_turn(current, stone, last_board):
def is_valid_chain(cad):
return isinstance(cad,str) and ((len(cad) == 2 and 'A' <= cad[0] <= 'S' and cad[1] in '0123456789' and 1<= int(cad[1]) <= 9) \
or (len(cad) == 3 and 'A' <= cad[0] <= 'S' and cad[1] == '1' \
and cad[2] in '0123456789' and 1<= int(cad[1:]) <= 19))
Expand All @@ -10,7 +10,7 @@ def eh_cadeia_intercecao_ok(cad):
pos = input(f"Write down an intersection or 'P' to pass the turn [{stone_to_str(stone)}]:")
if pos == 'P':
return False
elif eh_cadeia_intercecao_ok(pos):
elif is_valid_chain(pos):
pos = str_to_intersection(pos)
legal_play = is_legal_play(current, pos, stone, last_board)

Expand Down
12 changes: 6 additions & 6 deletions ADT_code/TF_goban.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def intersection_within_limits(i1, i2):
return 'A' <= obtain_col(i1) <= obtain_col(i2) and 1 <= obtain_row(i1) <= obtain_row(i2)
return intersection_within_limits(pos, obtain_last_intersection(board))

def gobans_iguais(g1, g2):
def equal_gobans(g1, g2):
if is_goban(g1) and is_goban(g2) and g1[1][0] == g2[1][0]:
if sorted(g1[1][1].keys()) == sorted(g2[1][1].keys()):
return all(equal_stones(g1[1][1][k], g2[1][1][k]) for k in g1[1][1])
Expand Down Expand Up @@ -118,11 +118,11 @@ def obtain_different_adjacents(board, stone_chain):
def play(board, pos, stone):
place_stone(board, pos, stone)
for new_pos in obtain_adjacent_intersections(pos, obtain_last_intersection(board)):
outra_pedra = obtain_stone(board, new_pos)
if is_player_stone(outra_pedra) and not equal_stones(stone, outra_pedra):
cadeia = obtain_chain(board, new_pos)
if len(obtain_different_adjacents(board, cadeia)) == 0:
remove_chain(board, cadeia)
other_stone = obtain_stone(board, new_pos)
if is_player_stone(other_stone) and not equal_stones(stone, other_stone):
chain = obtain_chain(board, new_pos)
if len(obtain_different_adjacents(board, chain)) == 0:
remove_chain(board, chain)
return board

def obtain_player_stones(board):
Expand Down
2 changes: 1 addition & 1 deletion ADT_code/TF_intersection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def is_intersection(arg):
return type(arg) == tuple and len(arg) == 3 and arg[0] == 'blabla' \
and type(arg[1]) == tuple and len(arg[1]) == 2 and arg[1][0] == 'nothing' \
and type(arg[1][1]) == int and 1 <= arg[1][1] <= 19 \
and type(arg[2]) == tuple and len(arg[2]) == 1 and type(arg[2][0]) == str and len(arg[2][0]) == 1 and 'A' <= arg[2][0] <= 'S'
and type(arg[2]) == tuple and len(arg[2]) == 1 and type(arg[2][0]) == str and len(arg[2][0]) == 1 and 'A' <= arg[2][0] <= 'S'

def equal_intersections(pos1, pos2):
return is_intersection(pos1) and is_intersection(pos2) and obtain_col(pos1) == obtain_col(pos2) and obtain_row(pos1) == obtain_row(pos2)
Expand Down
12 changes: 6 additions & 6 deletions ADT_code/TF_stone.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def is_white_stone(arg):
def is_black_stone(arg):
return is_stone(arg) and arg[1] == '.X.'

def equal_stones(p1, p2):
return is_stone(p1) and is_stone(p2) and p1[1] == p2[1]
def equal_stones(s1, s2):
return is_stone(s1) and is_stone(s2) and s1[1] == s2[1]

def stone_to_str(p):
return p[1][1]
def stone_to_str(s):
return s[1][1]

def is_player_stone(pedra):
return is_white_stone(pedra) or is_black_stone(pedra)
def is_player_stone(stone):
return is_white_stone(stone) or is_black_stone(stone)

6 changes: 3 additions & 3 deletions Go.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ def obtain_chain(goban: list, intersection: 'tuple[str,int]'):
:rtype: list[tuple[str,int]]
"""
# Tail Recursion
def _find_chain(goban: list, intersection: 'tuple[str,int]', visited: set('tuple[str,int]')) -> list['tuple[str,int]']:
def _find_chain(goban: list, intersection: tuple[str,int], visited: set['tuple[str,int]']) -> list[tuple[str,int]]:
# Auxiliary Internal function that looks for chains by finding adjacents of adjacents
visited.add(intersection)
adjacents = obtain_adjacent_intersections(intersection, obtain_last_intersection(goban))
Expand Down Expand Up @@ -508,7 +508,8 @@ def goban_to_str(goban: list) -> str:
:return: A string that represents the goban.
:rtype: str
"""
num_columns = obtain_row(obtain_last_intersection(goban)) # Can use obtain_row because the goban is square so it has the same number of columns and rows.
# Can use obtain_row because the goban is square so it has the same number of columns and rows.
num_columns = obtain_row(obtain_last_intersection(goban))
# Creates a dictionary that connects the indices of the column to the letters A, B, C, etc.
vertical_label = {i: chr(65+i) for i in range(num_columns)}
res = " " + " ".join([vertical_label[i] for i in range(num_columns)]) + "\n "
Expand Down Expand Up @@ -730,7 +731,6 @@ def player_turn(goban: list, stone: str, prev_goban: list) -> bool:
while not valid_turn:
# Asks the player for a move.
move = input(f"Write down an intersection or 'P' to pass the turn [{stone_to_str(stone)}]:")

# Checks if a player decided to pass their turn.
if move == 'P':
return False
Expand Down
4 changes: 2 additions & 2 deletions test_private.py
Original file line number Diff line number Diff line change
Expand Up @@ -1296,8 +1296,8 @@ def test_4(self):
black_intersections = tuple(str_to_intersection(i) \
for i in ('A1', 'A2', 'B1', 'E4', 'E5', 'F4', 'F5', 'G6', 'G7'))
g = create_goban(9, white_intersections, black_intersections)
b = create_white_stone()
_ = play(g, create_intersection('B', 2), b)
w = create_white_stone()
_ = play(g, create_intersection('B', 2), w)
assert goban_to_str(g) == \
""" A B C D E F G H I
9 . . . . . . . . . 9
Expand Down

0 comments on commit 9d1de7c

Please sign in to comment.