Reputation: 51
I'm supposed to make a connect four console and for some reason my board is displaying 0s instead of "."s. I'm not sure what I did wrong. There are 3 modules, but I'll only show this one since I assume that's where the underlying issue is.
import connectfour
def _print_board_num():
for i in range(len(connectfour.new_game().board)):
print(str(i+1), end = ' ')
print()
def print_board(game: 'connectfour.GameState')->[(str)]:
_print_board_num()
for row in range(connectfour.BOARD_ROWS):
for column in range(connectfour.BOARD_COLUMNS):
if game.board[column][row] == ' ':
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')
print()
def move()->str:
input('Drop or Pop? ')
def column1()->int:
int(input('What number of column? '))
My board is printing like:
1 2 3 4 5 6 7
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
0 0 0 0 0 0 0
but it's supposed to be printing like :
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
This is the game board function for the connectfour module
def _new_game_board() -> [[int]]:
'''
Creates a new game board. Initially, a game board has the size
BOARD_COLUMNS x BOARD_ROWS and is comprised only of integers with the
value NONE
'''
board = []
for col in range(BOARD_COLUMNS):
board.append([])
for row in range(BOARD_ROWS):
board[-1].append(NONE)
return board
def _copy_game_board(board: [[int]]) -> [[int]]:
'''Copies the given game board'''
board_copy = []
for col in range(BOARD_COLUMNS):
board_copy.append([])
for row in range(BOARD_ROWS):
board_copy[-1].append(board[col][row])
I would assume it's either one of these two. Mostly the drop function since I commented out the player error but it shows up for the drop function.
def player(game: 'connectfour.GameState') -> None:
while True:
player = input('Would you like to drop(d) or pop(p)?')
if player == 'd':
drop(game)
return
elif player == 'p':
pop(game)
return
else:
connectfour.InvalidMoveError(Exception)
#print('Invalid Move')
def drop(game: 'connectfour.GameState') -> bool:
try:
col = connectfouroverlap.column1()
board = connectfour.drop(game,col-1)
connectfouroverlap.print_board(board)
if gameover(board) != connectfour.NONE:
return
else:
player(board)
except:
connectfour.InvalidMoveError(Exception)
print('Invalid Move')
This is what the output looks like when I run it.
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
Would you like to drop(d) or pop(p)?d
What number of column? 2
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. 1 . . . . .
Invalid Move
Upvotes: 1
Views: 136
Reputation: 15443
After checking your code, I assume that game.board
is a double array that only contains 0
(integers?) values.
If that's true, try replacing :
if game.board[column][row] == ' ':
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')
with :
if game.board[column][row] == 0: # <--- change the condition here
print('.', end = ' ')
else:
print(game.board[column][row], end = ' ')
EDIT: the ->[str]
annotation in your print_board
function definition suggests that this function is meant to return a string array. But it doesn't.
You're creating a whole new board to print the col numbers. That's a bad practice, use the existing one instead.
You also could use for in
loops directly on the array rather than using indexes. That would require far less code and would be much clearer and easier to write :
for row in game.board: # <-- since game.board is your [[int]]
for number in row:
do_stuff_on(number)
Upvotes: 3