[EMAIL PROTECTED] writes: > Greetings! I am trying to make a multiplayer (no AI, 2 person) game of > tic tac toe in Python. So far it has been pretty simple. My only > concern is with the win checking to see if a person has won. At first > it looked like it was working, but now it sometimes assigns a win when > you enter an X or O (doesn't matter) on certain tiles (row 1, column 1 > won't be an error, but row 2, column 3 will be...). If you can find > the problem, I'd be very thankful! Here's the code:
You've got two problems. One of them causes your problem. The other caues you to duplicate a *lot* of code. The first problem is that logical and equality tests don't interoperate the way you think they do: (gameboard[0] and gameboard[1] and gameboard[2]) == 'X') does *not* verify that each of gameboard[0], gameboard[1] and gameboard[2] are all equal to 'X'. It tests whether or not the value (gameboard[0] and gamerboard[1] and gameboard[2]) is equal to 'X'. The former is almost certainly the value of gameboard[2], unless gameboard[0] or gameboard[1] evaluates to a false value, in which case that's the value you're comparing to 'X'. You'll have to break this one out into a set of three tests. Similar comments apply to tests like "gameboard[0] != ('O' or 'X')". The rhs of that is a constant - 'X'. This one you can rewrite as "gameboard[0] not in '0X'". The second problem is that you're using code where you should be using data. Whenver you see a long stretch of very similar code repeated over and over, you should consider replacing the code with data. For instance, all those things that look like: if row == X and column == Y: if gameboard[V] not in 'OX': gammeboard[V] = 'O' else: print "This cell is already filled." turnnumber -= 1 [repeate multiple times. And while I'm at it, the second and further tests in this string should be elif, not if.] Can be replaced by a dictionary (or a list of lists): boardmap = {(1, 1): 0, (2, 1): 3, (3, 1): 6, (1, 2): 1, (2, 2): 4, (3, 2): 7, (1, 3): 2, (2, 3): 5, (3, 3): 8 } cell = boardmap[row, column] if gameboard[cell] not in 'OX': gameboard[cell] = 'O' else: print "This cell is already filled." turnnumber -= 1 The representation as a list of lists is left as an exercise for the reader. <mike > # TIC TAC TOE > # Started: 10/31/05 > # Ended: still in progress > > loop = 1 > > while loop == 1: > print "TIC TAC TOE" > print "1 - Play Multiplayer" > print "2 - Quit" > option = input("> ") > > if option == 2: > # QUIT > loop = 0 > > if option == 1: > # MAIN GAME LOOP > print "Rules: You will alternate turns." > print "On your turn, you can place your letter (O = Player 1 or > X = Player 2)", > print "in any unoccupied square." > print "The first to get 3 in a row wins. Good luck!" > > gameboard = [' ',' ',' ',' ',' ',' ',' ',' ',' '] > > win = 0 > turnnumber = 0 > > while win != 1: > if turnnumber % 2 == 0: > print " " > print "Player 1" > print " " > print > "[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]" > print > "[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]" > print > "[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]" > print "What row?" > row = input("> ") > print "What column?" > column = input("> ") > > if (row > 3 or row < 1) or (column > 3 or column < 1): > print "Exceeeded limits." > turnnumber = turnnumber - 1 > > if row == 1 and column == 1: > if gameboard[0] != ('O' or 'X'): > gameboard[0] = ('O') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 1: > if gameboard[3] != ('O' or 'X'): > gameboard[3] = ('O') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > if row == 3 and column == 1: > if gameboard[6] != ('O' or 'X'): > gameboard[6] = ('O') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 1 and column == 2: > if gameboard[1] != ('O' or 'X'): > gameboard[1] = ('O') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 2: > if gameboard[4] != ('O' or 'X'): > gameboard[4] = ('O') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 3 and column == 2: > if gameboard[7] != ('O' or 'X'): > gameboard[7] = ('O') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 1 and column == 3: > if gameboard[2] != ('O' or 'X'): > gameboard[2] = ('O') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 3: > if gameboard[5] != ('O' or 'X'): > gameboard[5] = ('O') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 3 and column == 3: > if gameboard[8] != ('O' or 'X'): > gameboard[8] = ('O') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > turnnumber = turnnumber + 1 > > if (((gameboard[0] and gameboard[1] and gameboard[2]) == > 'O') or ((gameboard[3] and gameboard[4] and gameboard[5]) == 'O') or > ((gameboard[6] and gameboard[7] and gameboard[8]) == 'O')\ > or ((gameboard[0] and gameboard[3] and gameboard[6]) == > 'O') or ((gameboard[1] and gameboard[4] and gameboard[7]) == 'O') or > ((gameboard[2] and gameboard[5] and gameboard[8]) == 'O')\ > or ((gameboard[0] and gameboard[4] and gameboard[8]) == > 'O') or ((gameboard[2] and gameboard[4] and gameboard[6]) == 'O')): > print "Player 1 wins!" > win = 1 > > if ((gameboard[0:9]) == ('O' or 'X')) and (win == 0): > print "Tie." > win = 1 > > if turnnumber % 2 == 1 and win != 1: > print " " > print "Player 2" > print " " > print > "[",gameboard[0],"]","[",gameboard[1],"]","[",gameboard[2],"]" > print > "[",gameboard[3],"]","[",gameboard[4],"]","[",gameboard[5],"]" > print > "[",gameboard[6],"]","[",gameboard[7],"]","[",gameboard[8],"]" > print "What row?" > row = input("> ") > print "What column?" > column = input("> ") > > if (row > 3 or row < 1) or (column > 3 or column < 1): > print "Exceeeded limits." > turnnumber = turnnumber - 1 > if row == 1 and column == 1: > if gameboard[0] != ('O' or 'X'): > gameboard[0] = ('X') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 1: > if gameboard[3] != ('O' or 'X'): > gameboard[3] = ('X') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 3 and column == 1: > if gameboard[6] != ('O' or 'X'): > gameboard[6] = ('X') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 1 and column == 2: > if gameboard[1] != ('O' or 'X'): > gameboard[1] = ('X') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 2: > if gameboard[4] != ('O' or 'X'): > gameboard[4] = ('X') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 3 and column == 2: > if gameboard[7] != ('O' or 'X'): > gameboard[7] = ('X') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 1 and column == 3: > if gameboard[2] != ('O' or 'X'): > gameboard[2] = ('X') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 2 and column == 3: > if gameboard[5] != ('O' or 'X'): > gameboard[5] = ('X') > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > if row == 3 and column == 3: > if gameboard[8] != ('O' or 'X'): > gameboard[8] = ('X') > > else: > print "This cell is already filled." > turnnumber = turnnumber - 1 > > turnnumber = turnnumber + 1 > > if (((gameboard[0] and gameboard[1] and gameboard[2]) == > 'X') or ((gameboard[3] and gameboard[4] and gameboard[5]) == 'X') or > ((gameboard[6] and gameboard[7] and gameboard[8]) == 'X')\ > or ((gameboard[0] and gameboard[3] and gameboard[6]) == > 'X') or ((gameboard[1] and gameboard[4] and gameboard[7]) == 'X') or > ((gameboard[2] and gameboard[5] and gameboard[8]) == 'X')\ > or ((gameboard[0] and gameboard[4] and gameboard[8]) == > 'X') or ((gameboard[2] and gameboard[4] and gameboard[6]) == 'X')): > print "Player 2 wins!" > win = 1 > -- Mike Meyer <[EMAIL PROTECTED]> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. -- http://mail.python.org/mailman/listinfo/python-list