Quantcast
Channel: Active questions tagged python - Stack Overflow
Viewing all articles
Browse latest Browse all 19113

Building a Tic-Tac-Toe game but player two's turn gets skipped if he does a wrong turn in a row that is filled out, how do I fix this?

$
0
0

Firstly, I am still new to coding so please don't berate me for not doing some stuff right. Everything in the code is what I have learned so far from my classes.

So I built a Tic-Tac-Toe game and as the title says, player two's turn gets skipped if an entire row is filled out on the board.

Main Part of Code:

print("This is Tic-Tac-Toe, play this with a friend")TicTacToe = [["-", "-", "-"],              ["-", "-", "-"],              ["-", "-", "-"]]for row in TicTacToe:    print(row[0],row[1],row[2])while True:    Player = input("Which row do you want to go; T = top, M = middle, or B = bottom?\n")    if Player not in ["top", "Top", "T", "t", "middle", "Middle", "M", "m", "bottom", "Bottom", "B", "b"]:        print("Wrong input, I'm afraid you are going to have to actually write what you were told to.")        continue    # These are the moves of the player, starting with Player One.    if Player == "top" or Player == "Top" or Player == "T" or Player == "t":        while True:            Movement = input("Where do you want to go, L = left, M = middle or R = right?\n")        # Checks if the movement after the player chose their option is in check            if Movement not in ["left", "Left", "L", "l", "Middle", "middle", "M", "m", "right", "Right", "R", "r"]:                print("That is not one of the options you are allowed to write, please write the options listed.")                continue            if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":                if TicTacToe[0][0] == "-":                    TicTacToe[0][0] = "X"                    break                else:                    print("A move has already been placed there!")    # Now it is the beginning of Player Two's turn    while True:         Player2 = input("Now it's your turn Player Two; T = top, M = middle, or B = bottom?\n")        # This is what happens if Player Two does not input the options originally listed        if Player2 not in ["top", "Top", "T", "t", "middle", "Middle", "M", "m", "bottom", "Bottom", "B", "b"]:            print("Wrong input, I'm afraid you are going to have to actually write what you were told to.")            continue        # Moves of Player Two        if Player2 == "top" or Player2 == "Top" or Player2 == "T" or Player2 == "t":            while True:                Movement = input("Where do you want to go, L = left, M = middle or R = right?\n")            # Checks if the movement after the player chose their option is in check                if Movement not in ["left", "Left", "L", "l", "Middle", "middle", "M", "m", "right", "Right", "R", "r"]:                    print("That is not one of the options you are allowed to write, please write the options listed.")                    continue                if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":                    if TicTacToe[0][0] == "-":                        TicTacToe[0][0] = "O"                        break                    else:                        print("A move has already been placed there!")                        break

So in this code block for Player two as well as the other similar ones to it, I tried changing the break under the else tag to a continue statement instead, thinking it would loop to the original question "Now it's your turn Player Two; T = top, M = middle, or B = bottom?".

Code Block:

                if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":                if TicTacToe[0][0] == "-":                    TicTacToe[0][0] = "O"                    break                else:                    print("A move has already been placed there!")                    break

Altered Code Block:

                if Movement == "left" or Movement == "Left" or Movement == "L" or Movement == "l":                if TicTacToe[0][0] == "-":                    TicTacToe[0][0] = "O"                    break                else:                    print("A move has already been placed there!")                    continue

Instead it loops back to the variable called "Movement" which makes player two soft locked since the row is completely filled out.

If anyone knows how to fix this, please also explain why my method wasn't working and how the new method works as well.


Viewing all articles
Browse latest Browse all 19113

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>