Ask your own question, for FREE!
MIT 6.189 A Gentle Introduction to Programming Using Python (OCW) 16 Online
OpenStudy (anonymous):

making a game for my project tic tac toe where the human plays against the computer and I'm trying to write a code so I dont replace the computers input and vice versa, so if that is the case it should loop back and give another input where the input box is not taken. Can anyone take a look at my short code?

OpenStudy (rsmith6559):

You may want to normalize the character that the player inputs. Normalizing it would be running it through toUpper() so you're sure that it's a capital letter. [PlayerVertical-65] is technically correct, but unless you know ASCII codes doesn't make sense. [PlayerVertical-'A'] shows what you're doing. Your first do/while loop in PlayBoard will result in a loss of turn if the player enters a taken square. If you want the player to get another chance, you'll have to evaluate the "takenness" of a square before it can be changed, and loop on that. If you're making changes to an array in functions, shouldn't the array be passed by reference? fflush( stdin ); is, at best, of dubious value.

OpenStudy (anonymous):

"If you want the player to get another chance, you'll have to evaluate the "takenness" of a square before it can be changed, and loop on that." Because at first turn if I start, I choose wherever I want, but if at second turn, I chose same square as the turn before, It skips my turn, hence making my while limit useless. I encounter same problem with my do/while loop for the computer. So the computer can only recognize if a square is taken for 'X', so if it inputs its value at a square which it already took in a turn before, it replaces its own square and ends the do/while loop, again making the while limit useless. I sat 9 hours trying to solve this, yesterday...help ?

OpenStudy (anonymous):

'X' is my char and computers marker is with 'O', empty square is marked with '-'. and the game is 3x3.

OpenStudy (rsmith6559):

First, plan on redoing the logic in your function. If you don't have to, buy a lottery ticket. If a square is not filled with a '-', then the selection is wrong. You can make the player lose their turn, or prompt for another try. As always, more smaller functions to handle validating input is probably a good idea.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!