i'm a beginner,.. please help!
here is the question.

Download the TICTACTOE program. Complete it by writing the cellOpen and the winnerCheck functions. This does not require any changes to the rest of the program. Modify the program so that the two players enter their names (no validation is needed) and the program prompts them using their name, rather than X and O. Suggestion: use a list of strings for the player names.

problems:
1.the game never ends, unless it's a tie. (there is no winner.)
The "winnerCheck" function needs to be completed.
2.even if a cell is occupied, it's allowed to overwrite.
The "cellOpen" function needs to be completed.
3.i have to let two players to enter their names and the program prompts them using their names, rather than x and o.

TICTACTOE

def display(board):
    print('|' + '-' * 17 + '|')
    for row in board:
        print('|  %c  |  %c  |  %  c  |' % (row[0], row[1], row[2]))
        print('|' + '-' * 17 + '|')
        
def winnerCheck(board):
    return False

def cellOpen(cell, board):
    return True

def validChoice(cell):
        if(not cell in '012345678') or (len(cell) != 1):
            print('Entry must be in range 0..8.')
            return False
        return True    

def getChoice(player, board):
    print('%s: it is your turn.' % player)
    entryOK = False
    while not entryOK:
        cell = input('Enter a number - 0..8:  ')
        if validChoice(cell) and cellOpen(cell,board):
                entryOK =True
        else:
            cell = input('Enter again: ')
    return cell

def playGame(board):
    playermarks = 'XO'
    round = 0
    winner = False
    while round < 9 and not winner:
        mark = playermarks[round % 2]
        cell = int(getChoice(mark, board))
        board[cell // 3][cell % 3] = mark
        display(board)
        winner = winnerCheck(board)
        round = round + 1
    if winner:
        return mark
    else:
        return 'T'

def gameDoneMessage(result):
    if result in 'XO':
        print('Congratulations %s. You are the winner!' % result)
    else:
        print('The game ended in a tie!')
        
def main():   
    board = [['0', '1', '2'], ['3', '4', '5'], ['6', '7', '8']]
    display(board)
    gameResult = playGame(board)
    gameDoneMessage(gameResult)
                 
main()

please help!

Recommended Answers

All 5 Replies

yeah, you are right. i'm sorry,..

Member Avatar for masterofpuppets

I'll give you a hint for the winnerCheck function. something like this

def winnerCheck( board ):
    for row in board:
        if row[ 0 ] == 'X' and row[ 1 ] == 'X' and row[ 2 ] == 'X':
            return "Player1"
    for c in range( 3 ):
        if board[ 0 ][ c ] == 'X' and board[ 1 ][ c ] == 'X' and board[ 2 ][ c ] == 'X':
            return "Player1"
    if board[ 0 ][ 0 ] == 'X' and board[ 1 ][ 1 ] == 'X' and board[ 2 ][ 2 ] == 'X':
        return "Player1"
    elif board[ 0 ][ 2 ] == 'X' and board[ 1 ][ 1 ] == 'X' and board[ 2 ][ 0 ] == 'X':
        return "Player1"


print winnerCheck( [['0', '1', 'X'],
                    ['3', '2', '5'],
                    ['X', '7', '0']] )

i think it works fine but you might want to edit it a little and also check for O's as well

for the cellOpen fn basically you need to check whether the given cell is an X or an O and if it's not then return true and replace its value

Member Avatar for masterofpuppets

hi,
for the second part of your problem, i.e with the openCell() try this

def openCell( cell, board ):
      if board[cell / 3][cell % 3] != "X" and board[cell / 3][cell % 3] != "O":
         return True
      return False

print openCell( 2, [ [ "0", "1", "2" ],
               [ "3", "X", "5" ],
               [ "6", "7", "8" ] ] )

>>>
True
>>>

Actually i made a full TicTacToe game and I am using a different algorithm for that check but this is so much simpler so thanks for that...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.