Problem Solvers Needed; Are You Up For A Challenge? Experts Needed
I am programming an AI. I don't know if the right term would actually be AI but it is the first time I am ever programming something like this. Those who cannot handle a long, specific, probably incomprehensible, and definitely complicated explanation can't help me, as well as unorganized code. This is my problem:
So the problem is that for some reason, my code seems to have a mind of its own. I know I took the lazy approach in the definition checkWins and I know my code is gross and maybe hard to understand at parts, but bear with me.
I wrote print statements at all the right spots for you to be able to see this. What I want you to do is play a game of TicTacToe against the computer. Choose X as your piece. Then play a game and win (it makes random moves so that shouldn't be too hard). For some reason, sometimes when you win the game it outputs "Good So Far" and other times when you win it outputs "Something's up." Even if you stop the program and start it again after the first game instead of inputting "yes" when it asks you for another game, it sometimes outputs a different result. I have no idea why. I have narrowed the problem down to a certain part of the code. When it runs a method called checkWins, the last move (stored in the list called pastMoves) is not the player's move. To clarify the previous sentence, when the player inputs the game winning move, the main game loop should draw the player's move (drawMove method) and check it to determine that the player has won (checkWins method), then break out of the inner nested while loop before the computer has a chance to choose another move, which is back at the start of the inner nested while loop. So the last item in the list titled pastMoves when the player wins the game should always be the player's last move, not the computer's, right? But it is not.
Sometimes the code will print "Computer is moving" after you input the game winning move, then will print "Player Wins!". The "Player Wins!" statement is the message that gets printed outside the inner while loop, when the game ends. It is supposed to break the inner while loop before "Computer is moving" has a chance to print, and before the computer can make another move. The reason this is so important is because it affects a process that happens up top in another method called getBoardPositionBeforeLastComputerMove which is just a method designed to get a string (the string containing information to figure out the game position, what spots are blank, what spots have an X or O in them) of the position of the game before the computer's last move and store it in a list. But if this position that it gets it already in that list, then it should go back 2 more moves, to another position earlier in the game when it is before the computer's move. Eventually, when more games are played with the computer, the computer is supposed to accumulate more of a larger set of data of all the positions it has had that led to losing games, and not make moves to get into those positions. This method isn't working because the values of certain lists, such as the pastMoves list aren't up to date the way they should be as I explained earlier.
I know this is very complicated stuff (maybe for some experts it is relatively simple and if it is simple to you, I'm very glad you're here reading this) and maybe I explained it horribly but if anyone can comprehend the horrible explanation I gave here and is able to understand my messy code, I would be so so thankful if you could help me fix it. Ask as many questions as you need to in order to get a better sense of my code, or my problem. Thank you so much to anyone who is willing to help.