![]() |
Array in tictactoe machine not working? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Array in tictactoe machine not working? (/thread-37368.html) |
Array in tictactoe machine not working? - Illumimint - Jun-01-2022 Hi! I'm new here and I was wondering, I keep on trying different things such as putting in prints in certain points to pinpoint the issue, although the win array is not increasing and for some reason the robot gets more than one turn every round? Even though I tested it and the rmove function only plays once every round... also, the win array returns as all 0, making the move array all -1, if anyone could help this work, that would be great! ![]() x = [0,0,0,0,0,0,0,0,0] game = [" "," "," "," "," "," "," "," "," "] igame = [" "," "," "," "," "," "," "," "," "] win = [0,0,0,0,0,0,0,0,0] loss = [0,0,0,0,0,0,0,0,0] move = [0,0,0,0,0,0,0,0,0] wins = [0,1,2,3,4,5,6,7,8,0,3,6,1,4,7,2,5,8,0,4,8,2,4,6] def wincheck(game1): for i in range(0,len(wins),3): if (game1[wins[i]] == game1[wins[i+1]] == game1[wins[i+2]] and game1[wins[i]] != ""): return game1[wins[i]]; else: for i in range(9): if(game1[i] == " "): break elif(i==8): return "XO" else: return " " def reset(num1, game1): for i in range(num1+1,9): bool1 = True for i1 in range(0,num1): if x[i] == x[i1]: bool1 = False if bool1: game1[x[i]] = " " def pmove(): game[int(input("Your move: "))-1] = "O" def rmove(): win = [0,0,0,0,0,0,0,0,0] loss = [0,0,0,0,0,0,0,0,0] cmove = 0; igame = game for x[0] in range(9): reset(0,igame) if igame[x[0]] == " ": igame[x[0]] = "X" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[1] in range(9): reset(1,igame) if igame[x[0]] == " ": igame[x[0]] = "O" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[2] in range(9): reset(2,igame) if igame[x[0]] == " ": igame[x[0]] = "X" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[3] in range(9): reset(3,igame) if igame[x[0]] == " ": igame[x[0]] = "O" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[4] in range(9): reset(4,igame) if igame[x[0]] == " ": igame[x[0]] = "X" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[5] in range(9): reset(5,igame) if igame[x[0]] == " ": igame[x[0]] = "O" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[6] in range(9): reset(6,igame) if igame[x[0]] == " ": igame[x[0]] = "X" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[7] in range(9): reset(7,igame) if igame[x[0]] == " ": igame[x[0]] = "O" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break for x[8] in range(9): reset(8,igame) if igame[x[0]] == " ": igame[x[0]] = "X" else: continue if wincheck(igame) != " ": if wincheck(igame) == "X": win[x[0]]+=1 elif wincheck(igame) == "O": loss[x[0]]+=1 break igame = [0,0,0,0,0,0,0,0,0] for i in range(0,len(move)): if win[i]>0 : move[i] = win[i]/(win[i]+loss[i]) else: move[i] = -1 print(win[i]) for i in range(0,len(move)): if(move[i] >= move[cmove]): cmove = i game[cmove] = "X" def display(game1): print("."+" "+"."+" "+"."+" "+".") for i in range(3): print( "." +game1[(i*3)+0]+ "." +game1[(i*3)+1]+ "." +game1[(i*3)+2] + ".") while True: pmove() rmove() display(game)and it returns
RE: Array in tictactoe machine not working? - deanhystad - Jun-01-2022 In rmove() you do this: igame = gameNow igame and game are the same list. I do not mean they have the same contents, they are the exact same lists. Changing anything in igame will cause a corresponding change in game, not because they are magically linked somehow, but because the are the same list object. I do not understand what logic you are trying to implement in rmove. Can you describe what you are trying to do there? RE: Array in tictactoe machine not working? - Illumimint - Jun-01-2022 (Jun-01-2022, 06:29 PM)deanhystad Wrote: In rmove() you do this: basically, it takes the game and makes a fake game "igame" which then it goes through every single possible move in the turn then moves to future moves, breaks when something wins or loses and adds accordingly to the two arrays, then it looks at the wins versus the losses and chooses the move with the highest chance of succession in winning. Thanks for your help! ![]() RE: Array in tictactoe machine not working? - deanhystad - Jun-01-2022 It looks like you are trying to play out all possible games for each move, and then picking the move that results in the most wins. This is a viable strategy for a game without many moves, like tic-tac-toe, but you are not implementing the algorithm correctly. You are missing the code that "unwinds" the board after trying a move. Lets say you place your "O" in slot 1. Your robot will first try to place "X" in slot 2. Then it will place an "O" in slot 3 and then an X in slot 4 and so on until there are no open slots remaining and the board looks like this: The game ends in a draw. What the robot should do now is back up to the last marker it placed and try a different solution. It should back up to this: And it should try placing it's last marker in the final slot like this: This game will also end in a draw. The robot should back up to: And it should try placing a marker here: Your code is not rewinding the board to a prior state. Once you place a marker in a slot that slot is unavailable. You cannot back up and try a different move.You do clean up the entire board after the last loop, but that is way too late. Plus you should not be resetting the board (igame) to all zeros. You should reset it to a copy of "game". RE: Array in tictactoe machine not working? - Illumimint - Jun-02-2022 ok, I'll try to clean up my code and better the resetting process, thanks so much for your help! ![]() |