This is a simple HANGMAN game, when you input a character it checks whether the character is true or false, if it's true you can continue without any penalty if it's not, you can proceed with -1 health penalty until you finish the game.
The problem is, when it meets 0 health, it cannot print a message that I wrote as it should be.
import random
from words import words_list
def play_game():
random_num = random.randint(0, (len(words_list) - 1))
current_word = list(words_list[random_num].upper())
tries = 0
lifes = 8
tebakan_salah = 0
first_input = ""
picked_letters = []
correct_ones = []
print()
print('MEMILIKI ', str(len(current_word)), ' HURUF')
print('TEBAKAN SALAH : ',str(tebakan_salah))
print('SISA TEBAKAN ANDA :',str(lifes))
print()
for i in range(len(current_word)):
correct_ones.append('_ ')
while tries == 0 :
for i in range(len(current_word)):
print('_ ', end='')
tries += 1
continue
while lifes == 0 :
lifes -=1
return 'TEBAKAN ANDA GAGAL, SILAHKAN COBA LAGI'
while lifes > 0:
if lifes == 0 :
lifes -=1
print('TEBAKAN ANDA GAGAL, SILAHKAN COBA LAGI')
break
else:
# Check if all characters have been already guessed
if current_word == correct_ones:
print('\nSELAMAT ANDA BERHASIL!')
break
current_letter = input('\nMASUKKAN TEBAKAN ANDA : ').upper()
# Check if character has already been chosen
if current_letter in picked_letters:
print('\nANDA TELAH MEMILIH HURUF INI!')
print()
continue
# Check if character is in word
if current_letter in current_word:
index_list = []
for i in range(len(current_word)): # Get indexes of character in word
if current_word[i] == current_letter:
index_list.append(i)
picked_letters.append(current_letter) # Append to keep track of chosen characters
for i in index_list:
correct_ones[i] = current_letter.upper() # Append to correct position
print('BENAR!')
for i in correct_ones:
print(i + ' ', end='')
# Incorrect character
else:
picked_letters.append(current_letter)
lifes -= 1
tebakan_salah += 1
print()
print('SALAH!')
print('TEBAKAN SALAH : ',str(tebakan_salah))
print('SISA TEBAKAN ANDA : ',str(lifes))
print()
continue
play_game()
Related
How do I stop input from asking data for the 10th time? I am trying to build a board and want X or O to be updated only 9 times for a string that has 10 elements. Element 0 been empty.
It seems I need to figure out a way to stop or break the while loop when test_board[9] != "" (in def space_check():). This position if filled by the first player but the while (in #Test Run) loop is still active and so the user input is requested for the 10th position for the 2nd player.
enter code here
#Board
def display_board(board):
print (board[7] + '|' + board[8] + '|' + board[9])
print (board[4] + '|' + board[5] + '|' + board[6])
print (board[1] + '|' + board[2] + '|' + board[3])
pass
#Player input slection
def player_input():
marker = ''
#Asking player 1 to choose X or O
while marker not in ["X","O"]:
marker = input('Player 1 choose X or O ')
#Assign Player 2 a marker
player1 = marker
if player1 == 'X':
player2 = 'O'
else:
player2 = 'X'
return (player1,player2)
#Player position selection
def user_position():
# variables
#Initial
position ="WRONG"
acceptable_range = range(1,10)
within_range = False
#Two condition to check Digit or within range
while position.isdigit() == False or within_range == False:
position = input("Please enter a number (1-10):")
#Digit check
if position.isdigit() == False:
print("Sorry that is not a digit!")
#Range check
if position.isdigit()==True:
if int(position) in acceptable_range:
within_range = True
else:
within_range =False
print("Sorry that is not acceptable range!")
return int(position)
def place_marker(board, marker, position):
board[position]=marker
#print (board[position])
pass
def space_check():
for i in range(1,10):
if test_board[i] =='':
empty_position = True
#print (f'{i}')
else:
empty_position = False
return bool(empty_position)
Test run
from IPython.display import clear_output
clear_output()
def test_game_run():
#get the player input
player1_marker, player2_marker = player_input()
#get player 1 position
position_p1 = 0
position_p2 = 0
empty_position = True
counter = 0
#while empty_position == True:
#
while (position_p1 == position_p2 or empty_position == True): #and (counter <8): # need to contunue the game
print("player 1 choose a position")
position_p1 = user_position()
place_marker(test_board,player1_marker,position_p1)
empty_position = space_check()
print("player 2 choose a position")
position_p2 = user_position()
place_marker(test_board,player2_marker,position_p2)
empty_position = space_check()
# counter =counter + 1
#print(f'{counter}')
#step3
#print position by player 1
print(len(test_board))
#function call
test_board =['']*10
test_game_run()
display_board(test_board)
Output
def get_input(prompt,max_tries,allowed_values):
for i in range(max_tries,-1,-1):
result = input(prompt):
if result in allowed_values:
return result
print(f"{i} tries left")
return "default"
Is one strategy for this
Explanation
I am currently writing a code that simulates a game of hangman, but cheats by attempting to evade the player's guesses by changing the word. The class and methods are defined in hangman.py, while play_hangman.py calls the instance of Hangman and the play method (cannot be changed per the instructions of the assignment).
Problem
In the play method, I received the following error: "
TypeError: askForWordLength() takes 1 positional argument but 2 were given "
I know that this means I am giving too many arguments into the call of the method, but I am unsure of how to change it. I have tried rewriting the 5th line of the following code multiple times to fix this error, but it does not cease:
Specific Section of Code*
def play(self):
MODE = 1
openSession = 1
while(openSession == 1):
word_length = self.askForWordLength(self.words)
num_guesses = self.askForNumOfGuesses()
wordStatus = self.wordStatus(word_length)
letters_already_guessed = []
print()
gameOver = 0
while (gameOver == 0):
if (MODE == 1):
self.printCountOfRemainingWords(self.remainingWords)
self.printGameStats(self.remainingWords. letters_already_guessed,
self.num_guesses, self.wordStatus)
guess = self.askPlayerForGuess(letters_already_guessed)
letters_already_guessed.append(guess)
num_guesses -= 1
remainingWords = self.retrieveRemainingWords(guess, self.remainingWords,
num_guesses, word_length)
wordStatus = self.wordStatus(remainingWords[0], letters_already_guessed)
print()
if (guess in wordStatus):
num_guesses += 1
if ('-' not in wordStatus):
game_over = 1
print('Congratulations! You won!')
print('Your word was: ' + wordStatus)
if (num_guesses == 0 and game_over == 0):
game_over = 1
print('Haha! You Lose')
print('Your word was: ' + remainingWords[0])
print('Thanks for playing Hangman!')
ENTIRE CODE
hangman.py
import re
class Hangman:
# hangman self method
def hangman(self):
self.hangman = Hangman() # object of the Hangman class
def words(self):
with open('dictionary.txt') as file: # opens dictionary text file
file_lines = file.read().splitlines() # reads and splits each line
all_words = [] # empty list to contain all words
valid_words = [] # empty list to contain all valid words
for word in file_lines: # traverses all words in the file lines
if len(word) >= 3: # accepts word if it has at least 3 letters
all_words.append(word) # appends accepted word to list
# list of all invalid characters in python
CHARACTERS = ["~", "`", "!", "#", "#", "$", "%", "^", "&", "*", "(",
")", "-", "_", "=", "+", "[", "]", "{", "}", "|", "\","
"", "'", "?", "/", ">", ".", "<", ",", "", ";", ":"]
for i in CHARACTERS: # traverse list of invalids
for word in all_words:
if i not in word: # if invalid character is not in word
valid_words.append(word) # accept and append to list
return valid_words # return list of valid words
def askForWordLength(self, valid_words):
word_lengths = [] # empty list for possible word lengths
for word in valid_words: # traverse list of valid words
length = word.__len__() # record length of current word
if (length not in word_lengths):
word_lengths.append(length) # accept and append to list
word_lengths.sort()
# inform user of possible word lengths
print('The available word lengths are: ' + str(word_lengths[0]) + '-'
+ str(word_lengths[-1]))
print()
# have user choose from possible word lengths
while(1):
try:
length = int(input('Please enter the word length you want: '))
if (length in word_lengths):
return length
except ValueError:
print('Your input is invalid!. Please use a valid input!')
print()
def askForNumberOfGuesses(self):
while(1):
try:
num_guesses = int(input('Enter number of guesses you want: '))
if (num_guesses >= 3):
return num_guesses
except ValueError:
print('Your input is invalid!. Please use a valid input!')
print()
def wordStatus(self, length):
status = '-'
for i in range(0, length):
status += '-'
return
def remainingWords(self, file_lines, length):
words = []
for word in file_lines:
if (word.__len__() == length):
words.append(word)
return words
def printGameStats(self, letters_guessed, status, num_guesses):
print('Game Status: ' + str(status))
print()
print('Attempted Guesses' + str(letters_guessed))
print('Remaining Guesses' + str(num_guesses))
def askPlayerForGuess(self, letters_guessed):
letter = str(input('Guess a letter: ')).lower()
pattern = re.compile("^[a-z]{1}$")
invalid_guess = letter in letters_guessed or re.match(pattern, letter) == None
if (invalid_guess):
while (1):
print()
if (re.match(pattern, letter) == None):
print('Invalid guess. Please enter a correct character!')
if (letter in letters_guessed):
print('\nYou already guessed that letter' + letter)
letter = str(input('Please guess a letter: '))
valid_guess = letter not in letters_guessed and re.match(pattern, letter) != None
if (valid_guess):
return letter
return letter
def retrieveWordStatus(self, word_family, letters_already_guessed):
status = ''
for letter in word_family:
if (letter in letters_already_guessed):
status += letter
else:
status += '-'
return status
def retrieveRemainingWords(self, guess, num_guesses, remaining_words,
wordStatus, guesses_num, word_length,
createWordFamiliesDict,
findHighestCountWordFamily,
generateListOfWords):
word_families = createWordFamiliesDict(remaining_words, guess)
family_return = wordStatus(word_length)
avoid_guess = num_guesses == 0 and family_return in word_families
if (avoid_guess):
family_return = wordStatus(word_length)
else:
family_return = findHighestCountWordFamily(word_families)
words = generateListOfWords(remaining_words, guess, family_return)
return words
def createWordFamiliesDict(self, remainingWords, guess):
wordFamilies = dict()
for word in remainingWords:
status = ''
for letter in word:
if (letter == guess):
status += guess
else:
status += '-'
if (status not in wordFamilies):
wordFamilies[status] = 1
else:
wordFamilies[status] = wordFamilies[status] + 1
return wordFamilies
def generateListOfWords(self, remainingWords, guess, familyToReturn):
words = []
for word in remainingWords:
word_family = ''
for letter in word:
if (letter == guess):
word_family += guess
else:
word_family += '-'
if (word_family == familyToReturn):
words.append(word)
return words
def findHighestCountWordFamily(self, wordFamilies):
familyToReturn = ''
maxCount = 0
for word_family in wordFamilies:
if wordFamilies[word_family] > maxCount:
maxCount = wordFamilies[word_family]
familyToReturn = word_family
return familyToReturn
def printCountOfRemainingWords(self, remainingWords):
show_remain_words = str(input('Want to view the remaining words?: '))
if (show_remain_words == 'yes'):
print('Remaining words: ' + str(len(remainingWords)))
else:
print()
def play(self, askForWordLength, askForNumberOfGuesses, remainingWords,
words, wordStatus, printCountOfRemainingWords, printGameStats,
askPlayerForGuess, retrieveRemainingWords):
MODE = 1
openSession = 1
while (openSession == 1):
word_length = askForWordLength(words)
num_guesses = askForNumberOfGuesses()
wordStatus = wordStatus(word_length)
letters_already_guessed = []
print()
game_over = 0
while (game_over == 0):
if (MODE == 1):
printCountOfRemainingWords(remainingWords)
printGameStats(remainingWords, letters_already_guessed,
num_guesses, wordStatus)
guess = askPlayerForGuess(letters_already_guessed)
letters_already_guessed.append(guess)
num_guesses -= 1
remainingWords = retrieveRemainingWords(guess, remainingWords,
num_guesses, word_length)
wordStatus = wordStatus(remainingWords[0], letters_already_guessed)
print()
if (guess in wordStatus):
num_guesses += 1
if ('-' not in wordStatus):
game_over = 1
print('Congratulations! You won!')
print('Your word was: ' + wordStatus)
if (num_guesses == 0 and game_over == 0):
game_over = 1
print('Haha! You Lose')
print('Your word was: ' + remainingWords[0])
print('Thanks for playing Hangman!')```
It looks like you don't understand how classes work or there is a piece of code not shown here. When you define a method in a class, the first argument always refers to the object on which the method operates, which is conventionally called self. Any subsequent arguments are defined how you want. Usually, you don't need to pass the first self argument because it is passed according to the object you use. Any remaining arguments are your responsibility though.
For example:
class Student:
def __init__(self, name, age):
# Initializer (Commonly called Constructor in other languages)
# This is the first method that will run when you create an object and
# it runs automatically (You don't need to call it).
# This is where you'd initialize the state of the object, for example:
# Create a student with name and age.
# name and age are regular parameters of the __init__ method. We'd like
# to save them as attributes of our student object which is represented
# by self.
self.name = name
self.age = age
# Maybe we'd like to save a list of grades too
self.grades = []
def add_grade(self, grade):
# self is our object, and grade is the parameter
self.grades.append(grade)
def get_average(self):
# If we don't need additional parameters, we don't have to, but self is
# mandatory
return sum(self.grades) / len(self.grades)
def print_status(self):
# Note I am calling get_average() and I don't specify self. It's determined automatically.
print("Name:", self.name)
print("Age:", self.age)
print("Average:", self.get_average())
# We created the class, but we need to create some objects to use it.
s1 = Student("Dan", 15)
s2 = Student("Maya", 14)
# Note we only pass the custom parameters we specified which are `grade`
# self is determined by the object we are using (s1 or s2)
s1.add_grade(81)
s1.add_grade(86)
s2.add_grade(89)
s2.add_grade(93)
s1.print_status()
s2.print_status()
I hope this example helps you understand how methods work. In your code, I don't understand why you pass the methods as arguments of the play method and I don't see where you call this method or where you even create a Hangman object, so I can't help further.
I'm making an isWin function that checks if there is a character that appears 5 consecutive times (either horizontally, vertically or diagonally).
I've tried using this code:
#VERTICAL WIN
count = 0
for row in range(1,grid_height):
print(row)
for col in range(1,grid_width):
print(col)
if grid[row][col-2] == p_char:
count += 1
if count == 5:
return True
else:
count = 0
continue
#HORIZONAL WIN
count=0
for col in range(0,grid_width):
for row in range(0,grid_height):
if grid[row][col-2] == p_char:
count += 1
if count == 5:
return True
else:
count = 0
continue
And this is where i place it in my main program:
def play():
grid,grid_height,grid_width,p1_name,p1_char,p2_name,p2_char=getGameSettings()
displayGrid(grid,grid_height,grid_width)
print('WELCOME TO THE GAME!')
playerA = Player(p1_name, PLAYING)
playerB = Player(p2_name, WAITING)
grid=[]
for row in range(grid_height): # FOR ROW
z =[]
for col in range(grid_width): # FOR COLUMN
z.append(" ")
grid.append(z)
numColFull = 0
turn=0
while turn < grid_height*grid_width:
player = playerA
if turn % 2 == 0 : #IF TURN IS AN ODD NUMBER, THEN IT IS player 1's turn, IF TURN IS EVEN, THEN IT IS player 2's turn
p_char= p1_char
player = playerA
playerA.setState(PLAYING)
playerB.setState(WAITING)
else :
p_char= p2_char
player = playerB
playerB.setState(PLAYING)
playerA.setState(WAITING)
print(".................................................. ")
print("User to play : ", player.playerInfo() , " SEQ : ", str(turn)) # TO COUNT THE TOTAL NUMBER OF MOVES
print(".................................................. ")
if numColFull == grid_width: #THE isDRAW function but embedded into the main function
# IF the numColFull is equal to gridwidth, it means that all of the columns has been occupied, meaning
#that every space has already been occupied, thus, game is over.
print('........All spaces had been occupied...........')
print('................THE GAME IS DRAW...............')
print('.................GAME OVER.....................')
break
else:
while True:
try:
move=int(input('Enter your move: '))
except ValueError:
print('Please enter a valid input.')
if move < 1 or move > grid_width:
print('Please enter a valid input.')
continue
break
updateGrid(grid,grid_height-1,grid_width,move,p_char)
while True:
if grid[0][move-2] == p_char: #IF THE TOP ROW OF A COLUMN HAS A PIECE IN IT, IT MEANS ITS ALREADY FULL
displayGrid(grid,grid_height, grid_width)
print('Column is full. Please choose another column for the next move.')
numColFull += 1
break
elif isWin == True: #IF THE IF CONDITION DIDNT HOLD TRUE, THEN THE FUNCTION CONTINUES AS USUAL
print(player, 'WINS!!!')
('.................GAME OVER.....................')
else:
displayGrid(grid,grid_height, grid_width)
break #GOES BACK TO THE THE WHILE CONDITION
turn += 1 #INCREMENTS 1 TO TURN SO IT WILL SWITCH BETWEEN BEING ODD AND EVEN
And this is my grid:
def displayGrid(grid,grid_height,grid_width):
for row in range(1,grid_height):
#print(row) #for checking
for col in range(grid_width):
print("|", end="")
print(str(grid[row-1][col-1]),end = "")
print("|")
print(" "+" ".join([str(i) for i in range(1, grid_width+1)]))
return grid
def updateGrid(grid,grid_height,grid_width,move,p_char):
for i in range(1,grid_height+1):
print(i)
#print(i) #ROW COUNTING STARTS FROM 1
if grid[grid_height-i][move-2] == " ":
grid[grid_height-i][move-2]= p_char #REPLACES THE " " TO THE CURRENT PLAYER'S CHARACTER (p_char)
else:
continue
break
return grid
I guess you are writing Five In a Row? But anyway, this code should work:
def isWin():
# Horizontal
for i in range(grid_height):
for j in range(grid_width - 4):
if set(grid[i][j:j+5]) == {p_char}:
return True
# Vertical
for i in range(grid_height - 4):
for j in range(grid_width):
if { grid[i+k][j] for k in range(5) } == {p_char}:
return True
# Diagonal
for i in range(grid_height - 4):
for j in range(grid_width - 4):
if { grid[i+k][j+k] for k in range(5) } == {p_char}:
return True
return False
# Simplified
def isWin():
return any(set(grid[i][j:j+5]) == {p_char} for i in range(grid_height) for j in range(grid_width - 4)) or \
any({ grid[i+k][j] for k in range(5) } == {p_char} for i in range(grid_height - 4) for j in range(grid_width)) or \
any({ grid[j+k][i+k] for k in range(5) } == {p_char} for i in range(grid_width - 4) for j in range(grid_width - 4))
Since set cannot have duplicates, using expressions like { grid[j+k][i+k] for k in range(5) } will put 5 consecutive pieces into a set. If there are 5 consecutive p_chars, then the set will become {p_char}.
So I've been working on reworking Hangman in Python and I've run into an issue.
The output isn't updating at all. It always remains this mesh of underscores, although other parts of the code, such as the number of tries decreasing or used letters being added to the used list seem to function flawlessly.
Here's the code:
# IMPORT GUARDS
from random import choice
from os import system
from time import sleep
# DECLARATIONS
wordList = ["apple", "pear"]
gameWord = choice(wordList)
strList = list(gameWord)
strOut = "_" * len(gameWord)
tries = 5
used = []
alphabet = "abcdefghijklmnopqrstuvwxyz"
while True:
system ("cls")
print (strOut + "\n")
print ("Tries Left:", str(tries))
print ("Letters Used:", used)
Ltr = input ("Letter: ")
# INPUT CHECK
if len(Ltr) != 1 and Ltr.lower() in alphabet:
print ("Input is of incorect size.")
sleep(0.5)
elif len(Ltr) == 0 and Ltr.lower() in alphabet:
print ("No input value given.")
sleep(0.5)
if len(Ltr) == 1 and Ltr.lower() not in alphabet:
print ("Invalid character input.")
sleep(0.5)
if len(Ltr) != 1 and Ltr.lower() not in alphabet:
print ("Input is both too large and contains invalid characters.")
sleep(0.5)
# CORRECT INPUT
if len(Ltr) == 1 and Ltr.lower() in alphabet:
ltrPos = ( [pos for pos, char in enumerate(gameWord) if char == Ltr.lower])
# DECLARATIONS
Counter = 0
strcounter = 0
# CHECKING THE NUM OF TIMES THE INPUT APPEARS IN THE WORD
while Counter < len(strList):
if gameWord[Counter].lower() == Ltr.lower():
strcounter += 1
Counter += 1
# INPUT DOES APPEAR
if strcounter != 0:
strcounter -= 1
strOut = list(strOut)
for i in ltrPos:
strOut[ltrPos[strcounter]] = Ltr.upper()
strcounter += 1
strOut = "".join(strOut)
# INPUT DOES NOT APPEAR AND/OR IS USED
elif strcounter == 0:
if Ltr not in used:
print ("Letter not in word.")
used.append(Ltr.lower())
tries -= 1
else:
print ("Letter Already Used.")
sleep(0.5)
# OUT OF TRIES
if tries == 0:
system("cls")
print ("Game Over. \nWord: " + gameWord)
break
# VICTORY
if "_" not in strOut:
system("cls")
print ("Congratulations!")
break
system ("pause")
All advice is appreciated. Thanks in advance.
The main problem is that you forgot to call the lower function in one place:
ltrPos = [pos for pos, char in enumerate(gameWord) if char == Ltr.lower()]
But there's more. First, you can simplify the calculation of strCounter to
strcounter = len(ltrPos)
Or just check if ltrPos: instead of if strcounter != 0:. Also, you can use else instead of elif.
The next problem is strOut[ltrPos[strcounter]]. Here, you try to access lrtPos[strcounter], which will produce an index error as you decrement strcounter just once, instead of setting it back to zero. Instead, just iterate the indices in ltrPos directly:
for i in ltrPos:
strOut[i] = Ltr.upper()
Also, note that you only add the letter to the used list if it is not in the word.
So I'm attempting to make a Brainfuck interpreter, however in the while loop that I am using to execute the Brainfuck loop, it is breaking out even though only one condition is true.
Example:
+++[>+<-]
Should result in:
[0, 3]
However, when the loop begins at [, it will create a new cell so the structure goes from [3] to [3, 0]. Thus, the current working cell is 0 and the loop is breaking out. However, I have it to only break if it is 0 and the current character is ].
cells = [0] # Array of data cells in use
brainfuck = str(input("Input Brainfuck Code: ")) # Brainfuck code
workingCell = 0 # Data pointer position
count = 0 # Current position in code
def commands(command):
global cells
global workingCell
if command == ">":
workingCell += 1
if workingCell > len(cells) - 1:
cells.append(0)
elif command == "<":
workingCell -= 1
elif command == "+":
cells[workingCell] += 1
elif command == "-":
cells[workingCell] -= 1
elif command == ".":
print(chr(cells[workingCell]))
elif command == ",":
cells[workingCell] = int(input("Input: "))
def looper(count):
global cells
global workingCell
print("START LOOP", count)
count += 1
looper = loopStart = count
while brainfuck[looper] != "]" and cells[workingCell] != 0: # This line is causing trouble
if brainfuck[looper] == "]":
looper = loopStart
commands(brainfuck[looper])
count += 1
looper += 1
return count
while count < len(brainfuck):
if brainfuck[count] == "[":
count = looper(count)
print("END LOOP", count)
else:
commands(brainfuck[count])
count += 1
Thank you in advance.
I have it to only break if it is 0 and the current character is ]
If that's what you want, you have the logic in your while wrong. It should be:
while not (brainfuck[looper] == "]" and cells[workingCell] == 0):
And according to deMorgan's Laws, when you distribute not across and, you invert each of the conditions and change and to or, so it should be:
while brainfuck[looper] != "]" or cells[workingCell] != 0:
If this is confusing, you could just write:
while True:
if brainfuck[looper] == "]" and cells[workingCell] == 0:
break
This mirrors what you said in the description exactly.