Reputation:
I'm trying to make a connect four game. At this point, I am trying to make the game for console interaction only and am having trouble making the grid to look like this format:
Create 7 columns and each containing '.' till the time replaced by either color(just in case the formatting is not shown correctly):
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . Y . . . .
. Y R . . . .
. R Y . . . .
. R R . . . .
here is what I have so far:
NONE = ' '
RED = 'R'
YELLOW = 'Y'
BOARD_COLUMNS = 7
BOARD_ROWS = 6
# board=two dimensional list of strings and
# turn=which player makes next move'''
ConnectFourGameState = collections.namedtuple('ConnectFourGameState',
['board', 'turn'])
def new_game_state():
'''
Returns a ConnectFourGameState representing a brand new game
in which no moves have been made yet.
'''
return ConnectFourGameState(board=_new_game_board(), turn=RED)
def _new_game_board():
'''
Creates a new game board. Initially, a game board has the size
BOARD_COLUMNS x BOARD_ROWS and is comprised only of strings 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
Upvotes: 1
Views: 5644
Reputation: 387795
You will want to set NONE
to '.'
, not a space. Then, you could make such a print function for the board:
def printBoard (b):
print(' '.join(map(lambda x: str(x + 1), range(BOARD_COLUMNS))))
for y in range(BOARD_ROWS):
print(' '.join(b[x][y] for x in range(BOARD_COLUMNS)))
Used like this:
>>> x = _new_game_board()
>>> printBoard(x)
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
. . . . . . .
And when reconstructing your example state:
>>> x[1][-1] = RED
>>> x[1][-2] = RED
>>> x[1][-3] = YELLOW
>>> x[2][-1] = RED
>>> x[2][-2] = YELLOW
>>> x[2][-3] = RED
>>> x[2][-4] = YELLOW
>>> printBoard(x)
1 2 3 4 5 6 7
. . . . . . .
. . . . . . .
. . Y . . . .
. Y R . . . .
. R Y . . . .
. R R . . . .
If you are interested, I made a simple implementation of the whole game based on this idea. You can see it here.
Upvotes: 2