The code below started as the Tic Tac Toe exercise in Automate the boring stuff but I wanted the game to check if there was a winner at each turn.
I attempted to put every win combination in it's own list and update each position where applicable to the last move made. The idea then would be to check if one of these lists had all X's or all 0's and the program would declare a winner.
While testing I received the error ValueError: 'top-L' is not in list at the line containing update_wincon = winconditions.index(move)
surrounding code for context:
for each in winconditions:
if move in each:
update_wincon = winconditions.index(move)
winconditions[update_wincon] = turn
if each[1] == 'X' and each[2] =='X' and each[3] == 'X' or each[1] == '0' and each[2] =='0' and each[3] == '0':
print(str(turn) + ' is the winner!')
break
print(winconditions)
In this specific section I am trying to use a for loop to iterate through the lists in winconditions to see if the move the user input is in that list, and if so, update it to the current turn ('X' or '0'). It seems like it's not passing on the lists in winconditions that don't contain move. Not sure what I did wrong?
Complete code below.
#board is stored as a dictionary
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'bot-L': ' ', 'bot-M': ' ', 'bot-R': ' '}
#function to convert dictionary into visual board
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['bot-L'] + '|' + board['bot-M'] + '|' + board['bot-R'])
#winconditions stored as dictionary for reference to check if there is a winner
winconditions = [['top-L', 'top-M', 'top-R'], ['mid-L', 'mid-M', 'mid-R'], ['bot-L', 'bot-M', 'bot-R'],
['top-L', 'mid-L', 'bot-L'], ['top-M', 'mid-M', 'bot-M'], ['top-R', 'mid-R', 'bot-R'],
['top-L', 'mid-M', 'bot-R'], ['top-R', 'mid-M', 'bot-L']]
turn = 'X'
while True:
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = str(input())
if move not in theBoard:
print('please enter top/mid/bot-L/M/R')
continue
theBoard[move] = turn
for each in winconditions:
if move in each:
update_wincon = winconditions.index(move)
winconditions[update_wincon] = turn
if each[1] == 'X' and each[2] =='X' and each[3] == 'X' or each[1] == '0' and each[2] =='0' and each[3] == '0':
print(str(turn) + ' is the winner!')
break
print(winconditions)
if turn == 'X':
turn = '0'
else:
turn = 'X'
Your winconditions is a list of lists, therefore you cannot check if a single location is in there. You iterate through each row of possible winds, which is saved in the 'each' variable, so change this line:
to
update_wincon = winconditions.index(move)
to
update_wincon = each.index(move)
then, you'll be left with the next error.
This implementation should work according to your need:
#board is stored as a dictionary
theBoard = {'top-L': ' ', 'top-M': ' ', 'top-R': ' ',
'mid-L': ' ', 'mid-M': ' ', 'mid-R': ' ',
'bot-L': ' ', 'bot-M': ' ', 'bot-R': ' '}
#function to convert dictionary into visual board
def printBoard(board):
print(board['top-L'] + '|' + board['top-M'] + '|' + board['top-R'])
print('-+-+-')
print(board['mid-L'] + '|' + board['mid-M'] + '|' + board['mid-R'])
print('-+-+-')
print(board['bot-L'] + '|' + board['bot-M'] + '|' + board['bot-R'])
#winconditions stored as dictionary for reference to check if there is a winner
winconditions = [['top-L', 'top-M', 'top-R'], ['mid-L', 'mid-M', 'mid-R'], ['bot-L', 'bot-M', 'bot-R'],
['top-L', 'mid-L', 'bot-L'], ['top-M', 'mid-M', 'bot-M'], ['top-R', 'mid-R', 'bot-R'],
['top-L', 'mid-M', 'bot-R'], ['top-R', 'mid-M', 'bot-L']]
turn = 'X'
play = True
while play:
printBoard(theBoard)
print('Turn for ' + turn + '. Move on which space?')
move = str(input())
if move not in theBoard.keys():
print('please enter top/mid/bot-L/M/R')
continue
theBoard[move] = turn
for each in winconditions:
if (theBoard[each[0]] == 'X' and theBoard[each[1]] =='X' and theBoard[each[2]] == 'X') or (each[0] == '0' and each[1] =='0' and each[2] == '0'):
print(str(turn) + ' is the winner!')
play = False
if turn == 'X':
turn = '0'
else:
turn = 'X'
The problem is that "winning_conditions" contains lists, not a single string. The line should be
update_wincon = winconditions.index(each)
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.