开发者

simple python connect 4 game. why doesnt this test work?

开发者 https://www.devze.com 2023-01-18 05:51 出处:网络
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.

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.

#f开发者_如何学Pythonor 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


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消