lundi 14 septembre 2020

An if/else statement isn't running the embedded code, even after extensive checking and remapping

Here is my code:

x_or_o = ['x','o']
player_alive = 'yes'
    def game_over():
        player_alive = 'no'
        print('Player '+currentplayer+' wins!')
        player_alive = input('Play again? ')
        
    def check():
        for n in range(2):
            if str(x_or_o[n]) == currentplayer:
                if str(spot1) == currentplayer:
                    game_over()
    def main():
        choice = input('what is your choice?')# just choose 1a for this example
        if choice == '1a':
            spot1 = currentplayer
    currentplayer = 'x'
    check()

For some reason, the second if statement is not running, even though it seems that the string is set correctly and i have tried changing all of the arguments and the furthest i got was the first if statement working correctly; i tried changing the conditions, changing spot1 to a string and back, even alienating the code blocks in a different python file but nothing seems to be working Full code for reference:

import random
import os
spot1 = '1'
spot2 = '2'
spot3 = '3'
spot4 = '4'
spot5 = '5'
spot6 = '6'
spot7 = '7'
spot8 = '8'
spot9 = '9'
currentplayer = 'x'
choice = ''
x_or_o = ['x','o']
player_alive = 'yes'
def game_over():
    player_alive = 'no'
    print('Player '+currentplayer+' wins!')
    player_alive = input('Play again? ')
    
def check():
    for n in range(2):
        if str(x_or_o[n]) == currentplayer:
            if str(spot1) == currentplayer:# and spot4 == currentplayer and spot7 == currentplayer:#colums, left to right
                game_over()
            elif spot2 == currentplayer and spot5 == currentplayer and spot8 == currentplayer:
                game_over()
            elif spot3 == currentplayer and spot6 == currentplayer and spot9 == currentplayer:
                game_over()
            elif spot1 == currentplayer and spot2 == currentplayer and spot3 == currentplayer:#rows, top to bottom
                game_over()
            elif spot4 == currentplayer and spot5 == currentplayer and spot6 == currentplayer:
                game_over()
            elif spot7 == currentplayer and spot8 == currentplayer and spot9 == currentplayer:
                game_over()
            elif spot1 == currentplayer and spot5 == currentplayer and spot9 == currentplayer:
                game_over()
            elif spot3 == currentplayer and spot5 == currentplayer and spot7 == currentplayer:
                game_over()
def engine():
    spot1 = '*'
    spot2 = '*'
    spot3 = '*'
    spot4 = '*'
    spot5 = '*'
    spot6 = '*'
    spot7 = '*'
    spot8 = '*'
    spot9 = '*'
    currentplayer = 'x'
    while player_alive == 'yes':
        print('   a   b   c    \n')
        print('1 ['+spot1+'] ['+spot2+'] ['+spot3+']\n')
        print('2 ['+spot4+'] ['+spot5+'] ['+spot6+']\n')
        print('3 ['+spot7+'] ['+spot8+'] ['+spot9+']\n')
        choice = input('Where would you like to place your piece, '+currentplayer+'?\n')
        if choice == '1a':
            spot1 = currentplayer
        elif choice == '1b':
            spot2 = currentplayer
        elif choice == '1c':
            spot3 = currentplayer
        elif choice == '2a':
            spot4 = currentplayer
        elif choice == '2b':
            spot5 = currentplayer
        elif choice == '2c':
            spot6 = currentplayer
        elif choice == '3a':
            spot7 = currentplayer
        elif choice == '3b':
            spot8 = currentplayer
        elif choice == '3c':
            spot9 = currentplayer
            
        if currentplayer == 'x':
            currentplayer = 'o'
        else:
            currentplayer = 'x'
        check()
while True:
    engine()

Aucun commentaire:

Enregistrer un commentaire