简体   繁体   中英

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

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

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM