Aug-06-2017, 07:07 PM
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