Python Forum
Game not letting player 1 win straight away - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Game not letting player 1 win straight away (/thread-4425.html)



Game not letting player 1 win straight away - mzmingle - Aug-15-2017

I'm making a two player noughts and crosses game, and when player one wins, it asks player two to make a move before ending the game. I don't know how to get it to end the game straight away if player 1 wins. Also, at the end of the code, I'm not sure how to get the code to restart or end depending on the user's input. Any help, please?

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

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

gameRun = True


def show():                                         # creates the gameplay grid
    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():                                       # checks if player 1 (X) has a line

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

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

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

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

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

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

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

    return gameRun

def checkY():                                       # checks if player 2 (Y) has a line

    gameRun = True
    
    if grid[0] == 'Y' and grid[1] == 'Y' and grid[2] == 'Y':
        print("Player one wins!")
        gameRun = False
    
    if grid[3] == 'Y' and grid[4] == 'Y' and grid[5] == 'Y':
        print("Player one wins!")
        gameRun = False

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

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

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

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

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

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

    return gameRun

show()

while gameRun == True:                          # main game

    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!")

    gameRun = checkX()

    show()

    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()

    gameRun = checkX()

    

if gameRun == False: 

    playAgain = input("The game is now finished! Play again? (Y/N)")
    if playAgain == 'Y':
        gameRun = True

    if playAgain == 'N':
        print("Goodbye!")



RE: Game not letting player 1 win straight away - nilamo - Aug-15-2017

I'm not going to rewrite it for you, but it looks like gameRun already tracks whether or not someone won.  So you could just do something like:
while True:
    gameRun = True
    while gameRun:
        # player 1
        gameRun = checkX()
        if gameRun:
            # player 2
            gameRun = checkY()
    keep_playing = input("Want to play again? [Y/n] ")
    if keep_playing.lower() != "y":
        break



RE: Game not letting player 1 win straight away - ichabod801 - Aug-15-2017

A couple things that might help you:

>>> a = [1, 1, 1]
>>> a[0] == a[1] == a[2] == 1
True
and:

lines = [(0, 1, 2), (3, 4, 5), (6, 7, 8), ...]
for a, b, c in lines:
    if grid[a] == grid[b] == grid[c] == 'X':
        print('Player one wins!')
        ...



RE: Game not letting player 1 win straight away - mzmingle - Aug-15-2017

I'm kinda confused, not to be annoying, but I really don't understand the code either of you have sent, or like, where it's supposed to go? This code's really confusing me, haha.


RE: Game not letting player 1 win straight away - ichabod801 - Aug-15-2017

Nilamo's code is an outline of how your program might be run. The player1 and player2 comments show where you would get the moves from those players.

My code has to do with your checkX and checkY functions. They both are a long list of if statements. My first bit of code shows how you could simplify the conditions in those if statements. My second bit of code shows how you could make one loop to to test all the conditions. The lines list would be a list of the index triplets that make a line on the board. That is, the indexes from each of the if conditions in checkX/checkY. Then you can loop through the index triplets and use them to check each line, greatly simplifying your code. All else being equal, simpler code is better. It's easier to maintain, upgrade, and leads to less bugs.


RE: Game not letting player 1 win straight away - nilamo - Aug-15-2017

Mine was to illustrate how you could reorganize the entire end of your program (everything you have starting with the while gameRun == True block).

The basics are:
- you already know if player 1 won.  It's the value of gameRun.  So before doing any processing for player 2, just check to see if gameRun is True, and
- to play the game again, wrap the entire game loop in ANOTHER loop, so once the game's over, a new game starts.


RE: Game not letting player 1 win straight away - mzmingle - Aug-16-2017

I understand now, and my code's working, thank you!