Having an issue with the while loop for the win_checker method which I can't seem to figure out. I tried doing 2 while loops but that didn't work, so then I stuck with just one:
# Checking for win
def win_checker():
global is_game_on
if board[0] == board[1] == board[2] != "-":
print(board[0] + " won!")
is_game_on = False
# Start the game using a method
def start_game():
global is_game_on
is_game_on = True
display_board()
while is_game_on:
handle_turn_x()
win_checker()
tie_checker()
handle_turn_o()
start_game()
If I set the while loop to run only when is_game_on is True, and if my win_checker sets is_game_on to False (when appropriate), then why does the game keep going and ask the next player for its turn?
I suspect this is something to do with how I'm trying to jump out of the while loop.
Where am I wrong here?
Thanks for your help!!
The condition of the while loop is only checked when the program is "reading" the line that says
while <condition>:
I would recommend, instead of global variables, you make win_checker and tie_checker return a boolean value (True or False), and then make your while loop look like this:
while True: # run forever
handle_turn_x()
if win_checker() or tie_checker():
break # jump out of the while loop
handle_turn_o()
if win_checker() or tie_checker():
break
Globals are generally a bad idea...
instead of using a global, return True or False from that function.
Then in start_game:
def start_game():
display_board()
while True:
handle_turn_x()
if(not win_checker()):
break
tie_checker()
handle_turn_o()
start_game()
Then you can do the same for the tie checker
You could simply return is_game_on instead of modifying the global value, for example like so:
# Checking for win.
def win_checker():
if board[0] == board[1] == board[2] != "-":
print(board[0] + " won!")
return False
else
return True
# Start the game using a function.
def start_game():
while True:
handle_turn_x()
if win_checker() or tie_checker():
break
handle_turn_o()
if win_checker() or tie_checker():
break
Now this would be a little repetitive, so I think the win and tie checkers should be included to the turn handler. Something like this:
while True:
for player in ("x", "o"):
handle_turn(player)
if win_checker(player) or tie_checker():
break
Related
def timer():
for t in reversed(range(1, 11)):
sleep(1)
print(t)
if foundAllLetters == True:
break
if remaining_attempts == 0:
break
if t == 1:
sleep(1)
print('TIMES UP!')
t1 = Thread(target=timer)
t1.start()
while True:
displayBoard(hang, missedLetters, correctLetters, secretWord)
guess = getGuess(missedLetters + correctLetters)
A short context is I am coding a hangman game. What is going my way is that the timer and loop occurs simultaneously. What is not going my way is that I'm unable to stop the loop and restart the game once the timer is up. gameIsDone = True <- will restart my hangman game. I can't identify what variable to use with it.
I have tried to put gameIsDone = True right under print('TIMES UP!') but nothing happened. I have also tried and added t = 1 in my while loop under else statement, nothing happened.
I am new to python but might have accidentally picked an intermediate activity for my project :\
I have problem with ending loop by outside function. This is for my tick tack toe game.
Below is part of my code, my function win(x) with conditions to win, and I would like it to finish the "while game" loop. I tried many ways, it does print "X wins" if conditions of win_x() are met, but the loop keeps going as by range(5)
How can I change the win_x() function to end loop?
def win_x():
global game
global win
global board_numbers
if board_numbers[30]=="X" and board_numbers[62]=="X" and board_numbers[94]=="X":
os.system('clear')
print(board_numbers)
print("X wins")
game=True
while game:
os.system('clear')
print(board_numbers)
for i in range(5):
if player_marker=="X" or player_marker=="x":
player_X()
os.system('clear')
win_x()
computer_O()
else:
os.system('clear')
computer_X()
player_O()
I kept trying and I figured it out. code looks silly but works:
def win_x():
global game
global board_numbers
if board_numbers[30]=="X" and board_numbers[62]=="X" and board_numbers[94]=="X":
os.system('clear')
print(board_numbers)
print("X wins")
game=False #placing just game=False on its own didn't help
game=True
while game:
os.system('clear')
print(board_numbers)
for i in range(5):
if player_marker=="X" or player_marker=="x":
player_X()
os.system('clear')
win_x()
if game==False: #it is still weird as game=False was in above function
break #so I am not sure why I had to state it here
computer_O()
else:
os.system('clear')
computer_X()
player_O()
Hello I am just starting to try to teach my self python and with one of the resources I read, I saw this dice game to make. So I did the basic but then I wanted to make it more full. My idea was to add a loop and have after each round it would prompt the user to enter at first q but now 0 to try to determine if it is an error in my input.
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
def play():
print('do you want to play a game enter yes to start')
game = '1' #filler value
game=input()
str(game)
if game == "yes": #confirms start of the game
Dice()
else:
print('Ok Goodbye') #plays game anyways will fix after loop issue
gamestate()
______________________________________________________________
while game !=str(0): #cannot escape loop for some reason
if game == str(0) :
break #to break
Dice()
gamestate()
print('ok good bye')
___________________________________________________________
play()
First, sorry if code long for this, but what I expect is 0 as an input to break the loop, what I get is having to kill my console process in spyder in order to stop this from looping
You have the variable name game at 2 different variable scopes and so they have different state. Try returning a game from gamestate() and comparing the value
Short description of the scoping rules?
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
while game !=str(0): #cannot escape loop for some reason
if gamestate() == str(0) :
break #to break
Dice()
print('ok good bye')
You have to return game value from gamestate function and assign it in while loop. Check below code:
def gamestate():
print('enter 0 if you would like to quit anything else to continue')
game = input()
print(game == 0) # diagnostic to check if value is correct
print(type(game)) #diagnostic to make sure type is correct
print(game != str(0))
return game
def play():
print('do you want to play a game enter yes to start')
game = '1' #filler value
game=input()
str(game)
if game == "yes": #confirms start of the game
Dice()
else:
print('Ok Goodbye') #plays game anyways will fix after loop issue
gamestate()
while game !=str(0): #cannot escape loop for some reason
if game == str(0) :
break #to break
Dice()
game = gamestate()
print('ok good bye')
play()
In order to compare with zero as a string you need merely do something like if game == "0":. The issue you may be running into is that of extra whitespace chars like "\n". If you use input().trimspace() you'll remove extraneous chars and do comparisons with the values you want to.
Also another problem in the code is that it will enter the while loop if game does not equal "0" and so the if condition that follows will automatically not be met. So break is never hit.
You need to modify your program to convert input to int and not converting 0 to string at multiple places.changes needs to be done in while loop and gamestate function
For some reason the while loop is not breaking when the condition is met. The while loop should be checking for a players input to fill up a tic tac toe board until the variable "win" reads True.
Once the board reflects one of the winning conditions of tic tac toe, it assigns the variable "win" to True, and in turn should break out of the loop.
For some reason the loop isn't breaking, but the variable "win" is still reading True.
Can someone explain why the loop isn't breaking? I have tried rewriting the condition for the while loop to read "while win == False", but that doesn't seem to resolve the issue either.
I have included some of the functions I am using, and explained some of the simpler ones with a comment next to it.
I am using repl.it to do all of this online and not on a program on my local machine, so I think this may be part of the issue as well.
import os
board = ["#"," "," "," "," "," "," "," "," "," "]
def determine_win(marker):
# Winning Patterns:
# (1,2,3), (4,5,6), (7,8,9), (1,4,7), (2,5,8), (3,6,9), (3,5,7), (1,5,9)
if board[1]== board[2]==board[3]==marker:
return True
elif board[4]== board[5]==board[6]==marker:
return True
elif board[7]== board[8]==board[9]==marker:
return True
elif board[1]== board[4]==board[7]==marker:
return True
elif board[2]== board[5]==board[8]==marker:
return True
elif board[3]== board[6]==board[9]==marker:
return True
elif board[3]== board[5]==board[7]==marker:
return True
elif board[1]== board[5]==board[9]==marker:
return True
else:
return False
player1 = xo() # A Function that takes user input either "X" or O"
if player1 == "X":
player2 = "O"
else:
player2 = "X"
win = False
while not win:
display_board(board) # display_baord(board) takes the list "board" and uses it as input to display the tic tac toe board to the screen.
print("\nPlayer 1")
board[player_turn()] = player1
win = determine_win(player1)
print(win) # used to verify if win is changing
input() # used to pause the screen for troubleshooting
display_board(board)
print("\nPlayer 2")
board[player_turn()] = player2
win = determine_win(player2)
print(win) # used to verify if win is changing
input() # used to pause the screen for troubleshooting
print("Win Declared")
As the comment said, the reason is because while only check the condition of win when you finish the whole iteration of the loop. I would prefer the following way to make the code neater:
# win = False <-- you don't need this now
while True:
display_board(board) # display_baord(board) takes the list "board" and uses it as input to display the tic tac toe board to the screen.
print("\nPlayer 1")
board[player_turn()] = player1
if determine_win(player1):
print("Player 1 won")
break # break out of the loop
display_board(board)
print("\nPlayer 2")
board[player_turn()] = player2
if determine_win(player2):
print("Player 2 won")
break # break out of the loop
if not determine_win(player1):
display_board(board)
print("\nPlayer 2")
board[player_turn()] = player2
win = determine_win(player2)
# player 2 wins
else:
# player 1 wins
win = True
use something like this. It is same as #jasonharper and #Idlehands answered
I'm programming a simple text adventure game with Python 3 and the cmd module.
I need to somehow trigger the game over method but I didn't find a solution on document.
The CMD module got the do_quit() function, but that needs user input, and quit() or exit() kills the whole program, whereas I just need to get out of cmdloop()
Any idea how to deal with this?
Thanks in advance!
def moveDirection(direction):
global location
if direction in rooms[location]:
if rooms[rooms[location][direction]].get(UNLOCKED, True) == True:
print('You move to the %s.' % direction)
location = rooms[location][direction]
if location == 'Hallway' and bGuardAlive == True:
print("Game over! Guard caught you!")
printLocation(location)
else:
print("Door is locked")
else:
print('You cannot move in that direction')
def main():
printLocation(location)
GameLoop().cmdloop()
class GameLoop(cmd.Cmd):
prompt = '\n> '
def do_quit(self, arg):
"""Quit the game."""
return True
Usually (if I understand correctly), you create you own class derived from Exception, you throw the exception at the place you want to exit, and you will have a try clause where you want to land.
After trial and error i get it working. I needed use postcmd()
Here is my code:
def postcmd(self, stop, line):
if bGuardAlive == False and location == 'Hallway':
print("Game over! Guard caught you!")
return True
elif location == 'Tower Ruins':
print("You won!")
return True
return stop
Hopefully this will help somebody