Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 crack the safe - Gangster Nation
#1
hello, I play a game called "Gangster Nation", where the game is like creating mafia community. there is one feature that made me interested, that is crack the safe to get money.

so the game is you guess 4 digit number, the game will tell you clue whether your guess is incorrect, lower or higher. the game tell you randomly, so you can't expect to get clues all the times. when your guess is wrong, you need to wait 5 minutes to guess again.

I try to guess it manualy but it tooks me more than 2 hours to guess. so I create a code for guessing the password to crack the safe:

import random
import json

#load number database
filename = 'number_db.json'
try:
	with open(filename) as f_obj:
		all_number = json.load(f_obj)
except FileNotFoundError:
	all_number = {'check_number' : [], 
		'min_number' : 0, 'max_number' : 10000}
print('{}\n'.format(all_number))

#function to save number
def savejson_number():
	filename = 'number_db.json'
	with open(filename, 'w') as f_obj:
		json.dump(all_number, f_obj)

#guessing function
def start_guessing(min_number, max_number):
	guess_number = random.randrange(all_number['min_number'], 
		all_number['max_number'])
	if guess_number in all_number['check_number']:
		guess_number = random.randrange(all_number['min_number'], 
			all_number['max_number'])
	all_number['check_number'].append(guess_number)
	return guess_number

print('How to Use: \nif lower, type "lower". if higher, type "higher".')
print('if incorrect just enter. if save type "save".')
print('if stop type "stop", if reset type "reset".')

#opening guess
my_guessing = start_guessing(all_number['min_number'], 
	all_number['max_number'])


#loop for running program
active = True
while active:

	print("\nMin number is: {}".format(all_number['min_number']) + 
		" and the Max number is: {}".format(all_number['max_number'] 
			- 1))
	print("is the number: " + str(my_guessing) + " ?")

	your_guess = input()

	if your_guess.lower() == "lower":
		all_number['max_number'] = my_guessing

	if your_guess.lower() == "higher":
		all_number['min_number'] = my_guessing

	if your_guess.lower() == "stop":
		break

	if your_guess.lower() == "save":
		savejson_number()
		print("Saved!")

	if your_guess.lower() == "reset":
		all_number['check_number'] = []
		all_number['min_number'] = 0
		all_number['max_number'] = 10000
		savejson_number()
		print("Reset!")

	if your_guess.lower() == "":
		my_guessing = start_guessing(all_number['min_number'], 
			all_number['max_number'])

	else:
		None

with this, it take me 1,5 hours to crack the safe. next task is to made this program can guess more accurate to cut down waiting time Big Grin
Quote
#2
Well there is a problem when i ran the code. I The output comes as :
Output:
{'check_number': [], 'min_number': 0, 'max_number': 10000} How to Use: if lower, type "lower". if higher, type "higher". if incorrect just enter. if save type "save". if stop type "stop", if reset type "reset". Min number is: 0 and the Max number is: 9999 is the number: 7495 ? lower Min number is: 0 and the Max number is: 7494 is the number: 7495 ? lower Min number is: 0 and the Max number is: 7494 is the number: 7495 ?
pyzyx3qwerty
Need help on the forum? Visit help @ python forum
For learning more and more about python, visit Python docs
Quote
#3
(May-08-2020, 09:59 AM)pyzyx3qwerty Wrote: Well there is a problem when i ran the code. I The output comes as :
Output:
{'check_number': [], 'min_number': 0, 'max_number': 10000} How to Use: if lower, type "lower". if higher, type "higher". if incorrect just enter. if save type "save". if stop type "stop", if reset type "reset". Min number is: 0 and the Max number is: 9999 is the number: 7495 ? lower Min number is: 0 and the Max number is: 7494 is the number: 7495 ? lower Min number is: 0 and the Max number is: 7494 is the number: 7495 ?

you must enter without typing anything to generate new random number, and type "save" to save the result. I will update the code so that you must type something to generate new number.

type "lower" to set the number as max number,
type "higher" to set the number as min number,

and type "reset" to reset the number in array, min and max.

and type stop to stop the program Big Grin
Quote
#4
Updating my source code in line 51:

import random
import json

#load number database
filename = 'number_db.json'
try:
	with open(filename) as f_obj:
		all_number = json.load(f_obj)
except FileNotFoundError:
	all_number = {'check_number' : [], 
		'min_number' : 0, 'max_number' : 10000}
print('{}\n'.format(all_number))

#function to save number
def savejson_number():
	filename = 'number_db.json'
	with open(filename, 'w') as f_obj:
		json.dump(all_number, f_obj)

#guessing function
def start_guessing(min_number, max_number):
	guess_number = random.randrange(all_number['min_number'], 
		all_number['max_number'])
	if guess_number in all_number['check_number']:
		guess_number = random.randrange(all_number['min_number'], 
			all_number['max_number'])
	all_number['check_number'].append(guess_number)
	return guess_number

print('How to Use: \nif lower, type "lower". if higher, type "higher".')
print('if incorrect just enter. if save type "save".')
print('if stop type "stop", if reset type "reset".')

#opening guess
my_guessing = start_guessing(all_number['min_number'], 
	all_number['max_number'])


#loop for running program
active = True
while active:
	try:

		print("\nMin number is: {}".format(all_number['min_number']) + 
			" and the Max number is: {}".format(all_number['max_number'] 
				- 1))
		print("is the number: {} ?".format(int(my_guessing)))

		your_guess = input()

#New Update

		if your_guess.lower() == "lower":
			all_number['max_number'] = int(my_guessing)
			my_guessing = (all_number['max_number'] + 
				all_number['min_number']) / 2

		if your_guess.lower() == "higher":
			all_number['min_number'] = int(my_guessing)
			my_guessing = (all_number['max_number'] + 
				all_number['min_number']) / 2

		if your_guess.lower() == "stop":
			break

		if your_guess.lower() == "save":
			savejson_number()
			print("Saved!")

		if your_guess.lower() == "reset":
			all_number['check_number'] = []
			all_number['min_number'] = 0
			all_number['max_number'] = 10000
			savejson_number()
			print("Reset!")

		if your_guess.lower() == "":
			my_guessing = start_guessing(all_number['min_number'], 
				all_number['max_number'])

		else:
			None

	except ValueError:
		print("Empty Range!")
it will generate new number when you type lower or higher

PS: how to edit my thread?? I cannot find the edit button for thread Think
Quote
#5
(May-20-2020, 03:25 PM)asiaphone12 Wrote: how to edit my thread?
https://python-forum.io/misc.php?action=help&hid=32
https://python-forum.io/misc.php?action=help&hid=31
Quote
#6
(May-20-2020, 03:44 PM)buran Wrote:
(May-20-2020, 03:25 PM)asiaphone12 Wrote: how to edit my thread?
https://python-forum.io/misc.php?action=help&hid=32
https://python-forum.io/misc.php?action=help&hid=31

ok thanks for the info Big Grin to not disrupt the dialog flow, we must not edit the post huh.

so for every edit needed we can just post the new code or edited code into new post.

I still new and did not remember all the rules, thanks for the guidance Dance
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)