We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,555 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Pyschools-tictactoe

In the pencil-and-paper game, Tic-tac-toe, 2 players take turns to mark 'X' and 'O' on a board of 3x3 squares. The player who succeeds in marking 3 successive 'X' or 'O' in vertical, horizontal or diagonal stripe wins the game. Write a function that determines the outcome of a tic-tac-toe game.

Examples

>>> tictactoe([('X', ' ', 'O'), 
               (' ', 'O', 'O'), 
               ('X', 'X', 'X') ])
"'X' wins (horizontal)."
>>> tictactoe([('X', 'O', 'X'), 
...            ('O', 'X', 'O'), 
...            ('O', 'X', 'O') ])
'Draw.'
>>> tictactoe([('X', 'O', 'O'), 
...            ('X', 'O', ' '), 
...            ('O', 'X', ' ') ])
"'O' wins (diagonal)."
>>> tictactoe([('X', 'O', 'X'), 
...            ('O', 'O', 'X'), 
...            ('O', 'X', 'X') ])
"'X' wins (vertical)."



def tictactoe(moves):
    for r in range(len(moves)):
        for c in range(len(moves[r])):      
            if moves[0][c]==moves[1][c]==moves[2][c]:
                a="'%s' wins (%s)."%((moves[0][c]),'vertical')
            elif moves[r][0]==moves[r][1]==moves[r][2]:
                a="'%s' wins (%s)."%((moves[r][0]),'horizontal')
            elif moves[0][0]==moves[1][1]==moves[2][2]:
                a="'%s' wins (%s)."%((moves[0][0]),'diagonal')
            elif moves[0][2]==moves[1][1]==moves[2][0]:
                a="'%s' wins (%s)."%((moves[0][2]),'diagonal')
            else:
                a='Draw.'
    print(a)

I wrote a code like this and my range is not working(i think). because, it takes the value for r and c as 3, not 0,1,2,3. So, please anyone can help me with this ?
Thank you

2
Contributors
1
Reply
3 Hours
Discussion Span
10 Months Ago
Last Updated
2
Views
fasna
Newbie Poster
3 posts since Aug 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Add a few print statements to see what's going on

def tictactoe(moves):
    for r in range(len(moves)):
        for c in range(len(moves[r])):
            print("(r, c) =", (r, c))
            if moves[0][c]==moves[1][c]==moves[2][c]:
                a="'%s' wins (%s)."%((moves[0][c]),'vertical')
            elif moves[r][0]==moves[r][1]==moves[r][2]:
                a="'%s' wins (%s)."%((moves[r][0]),'horizontal')
            elif moves[0][0]==moves[1][1]==moves[2][2]:
                a="'%s' wins (%s)."%((moves[0][0]),'diagonal')
            elif moves[0][2]==moves[1][1]==moves[2][0]:
                a="'%s' wins (%s)."%((moves[0][2]),'diagonal')
            else:
                a='Draw.'
            print("currently, a =", repr(a))
    print(a)
Gribouillis
Posting Maven
Moderator
3,101 posts since Jul 2008
Reputation Points: 1,130
Solved Threads: 761
Skill Endorsements: 11

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0593 seconds using 2.74MB