Issue
I decided to write a go board game in Python, just for fun, without UI, just CLI :) I want to check if current intersection on the board is empty or not to restrict player putting his stones there. The intersection is represented as ' * ', black is '0', white is '1'. If black is to move, and current intersection is empty ('*'), change it to '0'. Otherwise ask black to move again. The same for the white (Captures count, score etc will be implemented later). Here is the full code:
class GoGame:
board: list
_PLAYER_BLACK = " 0 "
_PLAYER_WHITE = " 1 "
_black_move_done = False
_white_move_done = False
def __init__(self, board):
self.board = board
def display_board(self, board):
for i in range(len(self.board)):
for j in range(len(self.board)):
print(board[i][j], end='')
print()
def black_move(self):
i, j = input("Black to move: ").split()
self.board[int(i)][int(j)] = self._PLAYER_BLACK
self.display_board(self.board)
self._black_move_done = True
def white_move(self):
i, j = input("White to move: ").split()
self.board[int(i)][int(j)] = self._PLAYER_WHITE
self.display_board(self.board)
self._white_move_done = True
def play(self):
while True:
for i in range(len(self.board)):
for j in range(len(self.board)):
if not self._black_move_done and self.board == ' * ':
self.black_move()
self._black_move_done = False
self.white_move()
self._white_move_done = False
board = [
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
],
[
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * ', ' * ', ' * ',
' * '
]
]
go = GoGame(board)
go.display_board(sim.board)
go.play()
But what I get is the game infinitely asking white to move:
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
White to move: 0 0
1 * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
* * * * * * * * * * * * * * * * * * *
White to move:
What am I doing wrong?
Solution
the problem is the board, which is a list, will never be equal to a 3-character string ' * '. hence the if statement never gets executed, and it's white's turn for each element in your board. perhaps you should check whether the entered intersection is legal in the white/black _move function like this:
print("white/black to move: ", end = '')
while True:
i, j = [int(x) for x in input().split()]
if board[i][j] != the empty character:
print("intersection is occupied. please try again: ", end = '')
else:
board[i][j] = white/black character
break
Answered By - Dariush Mazlumi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.