[Tutor] help with tic-tac-toe program

Jan Erik Moström lists at mostrom.pp.se
Tue Nov 18 11:07:22 CET 2014


If I understand what you're asking you need to write the current
gameboard and the info you get in 'gameBoard' is the current state of
the game.

There are several ways of doing this (with different degrees of
cleverness) but to keep it simple:

Start by printing out the current state, different ways of doing this
but perhaps a loop within a loop similar to your loadGameBoard code

When you have got this working, add the lines by modifying the code
you just wrote. An observation here is that if you print the first
"--------" line then the rest is board line followed by a new
"--------" line, second observation is that if you start each line
with a "| " the repeating patterns is "Z |" where Z is X/O.

On Sun, Nov 16, 2014 at 7:52 PM, Andrew McReynolds
<amcreynolds3 at yahoo.com.dmarc.invalid> wrote:
> The section of the assignment that I'm working on states:
> 2) Write a function called loadGameBoard (player_marks) where player_marks
> is a dictionary that
> contains the players’ marks. This function creates a 3x3 array with the
> players’ marks populated in
> the correct row/column indices using the formula 3*row+column. This function
> returns the 3x3
> array. You must use a loop at least once.
> 3) Write a function called printGameBoard (gameBoard) where gameBoard is a
> 3x3 array that
> contains the players’ marks. This function draws the game board and returns
> None. You must
> use a loop at least once in this function or in a function that this
> function calls.
> An example is if gameBoardArray[0][1] has ‘X’ and gameBoardArray[1][1] has
> ‘O’, this function should
> draw the game board like:
> ----------------
> | | X | |
>  ----------------
> | | O | |
> ----------------
> | | | |
> ----------------"
>
> What I have so far is:
>
> def loadGameBoard (player_marks):
>       null= " "
>
>       board= [[null,null,null],[null,null,null],[null,null,null]]
>       for row in range(3):
>             for column in range (3):
>                   position = 3*row+column
>                   if position in player_marks["X"]:
>                      board[row][column]="X"
>                   if position in player_marks["O"]:
>                      board[row][column]="O"
>
>       return (board)
>
> def printGameBoard(gameBoard):
>       board=(("-"*8,("| ")*4))*3
>       #for line in board:
>
>       return ()
>
> Any advice for continuing?
>
> Thanks in advance for assistance,
>
> Andrew
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list