Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Won't let me end a loop
#1
I've been given a task to make a text based two player game of noughts & crosses. I made a variable called gameRun which would keep the game going if it was true. I then tried to make this variable become false when one of the players (only player one at the moment) gets a line, and therefore end the game, but it just prints that player one wins and then continues the game. Any idea how to fix this?

grid = [1, 2, 3,
        4, 5, 6,
        7, 8, 9]

gridSave = [1, 2, 3,
            4, 5, 6,
            7, 8, 9]

gameRun = True

def show():
    print(grid[0], '|', grid[1], '|', grid[2])
    print('----------')
    print(grid[3], '|', grid[4], '|', grid[5])
    print('----------')
    print(grid[6], '|', grid[7], '|', grid[8])

def checkX():
    
    if grid[0] == 'X' and grid[1] == 'X' and grid[2] == 'X':
        print("Player one wins!")
        gameTrue = False
    
    if grid[3] == 'X' and grid[4] == 'X' and grid[5] == 'X':
        print("Player one wins!")
        gameTrue = False

    if grid[6] == 'X' and grid[7] == 'X' and grid[8] == 'X':
        print("Player one wins!")
        gameTrue = False
        

    if grid[0] == 'X' and grid[3] == 'X' and grid[6] == 'X':
        print("Player one wins!")
        gameTrue = False

    if grid[1] == 'X' and grid[4] == 'X' and grid[7] == 'X':
        print("Player one wins!")
        gameTrue = False

    if grid[2] == 'X' and grid[5] == 'X' and grid[8] == 'X':
        print("Player one wins!")
        gameTrue = False
        

    if grid[0] == 'X' and grid[4] == 'X' and grid[8] == 'X':
        print("Player one wins!")
        gameTrue = False

    if grid[2] == 'X' and grid[4] == 'X' and grid[6] == 'X':
        print("Player one wins!")
        gameTrue = False
        

show()

while gameRun == True:

    inputX = input("Player one, please select a spot: ")
    inputX = (int(inputX) - 1)
    
    if grid[inputX] != 'X' and grid[inputX] != 'O':
        grid[inputX] = 'X'

    else:
        print("This space is taken!")

    show()

    checkX()

    inputY = input("Player two, please select a spot: ")
    inputY = (int(inputY) - 1)
    
    if grid[inputY] != 'X' and grid[inputY] != 'O':
        grid[inputY] = 'O'

    else:
        print("This space is taken!")

    show()

    

if gameRun == False: 

    print("The game is now finished! Play again!")
    grid = gridSave
    print(grid)
    gameRun = True
Reply


Messages In This Thread
Won't let me end a loop - by mzmingle - Aug-06-2017, 07:07 PM
RE: Won't let me end a loop - by ichabod801 - Aug-06-2017, 07:55 PM
RE: Won't let me end a loop - by mzmingle - Aug-06-2017, 08:03 PM
RE: Won't let me end a loop - by ichabod801 - Aug-06-2017, 08:05 PM
RE: Won't let me end a loop - by mzmingle - Aug-06-2017, 08:28 PM
RE: Won't let me end a loop - by ichabod801 - Aug-06-2017, 08:48 PM
RE: Won't let me end a loop - by mzmingle - Aug-06-2017, 08:56 PM
RE: Won't let me end a loop - by ichabod801 - Aug-06-2017, 09:58 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020