views:

126

answers:

1

Everything about this code seems to work perfectly, except the tests for diagonal wins. The tests for vertical and horizontal wins seem to be exactly the same concept, and they work perfectly.

The comments should mostly explain it, but the test should basically iterate through the board and check for x's in the bottom left hand corner (the only place that a right facing diagonal can start). it then goes up and to the right one space four times to check for a four in a row.

Here is the function in question.

#for diagonal
#not working! WHYYYY
def winnertest3():
    for i in range(3):
        for e in range(4):
            print i,e
            if board[i][e]=='X' and board[i+1][e+1]=='X' and board[i+2][e+2]=='X' and board[i+3][e+3]=='X':
                print "X wins!!!!"
                return 'over'
    return 'on'

http://github.com/keevie/Computer-Science/blob/master//board1.py

+1  A: 

It worked for me. I started in the bottom right hand corner with an X and worked it up diagonally to the left. I also started one to the left of that initial position. However, when I got 4 X's in a row, it didn't immediately stop - I had to put another O in because it only checks to see if the game should stop after an O is placed. Have you been testing the right diagonal?

Justin Peel
thank you! wow, I am really dumb. This test was supposed to go left to right, apparantly it goes right to left.
keevie