Python Forum
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
My version GhostGame
#1
from random import randint

def get_msg(msg, subject, subject_mark):
    for i in range(1, doors+1):
        if i in subject:
            msg+=door % subject_mark
        else:
            msg+=door % ' '
    return msg

def get_input(msg, min_, max_):
    while True:
        s=input(msg)
        if s.isdigit() and min_ <= int(s) and int(s) <= max_:
            return int(s)
        else:
            print('Bad choice! Try again.')

def get_ghost():
    ghost = []
    while True:
        rnd = randint(1, doors)
        if rnd not in ghost:
            ghost.append(rnd)
        if len(ghost)==2:
            return ghost

print('GhostGame\n')
door='__[ %s ]__'
score, best_score, game = 0, 0, 1
doors = get_input('Enter the number of door[3..8] >> ', 3, 8)
show_doors=''.join([door % i for i in range(1, doors+1)])
while True:
    print(show_doors)
    choice = get_input('Choose your door from 1 to %s >> ' % doors, 1, doors)
    ghost = get_ghost()
    print(get_msg('Ghost is here:\n', ghost, 'X'))
    print(get_msg('You is here:\n', [choice], 'O'))
    if choice in ghost:
        if score > best_score:
            best_score=score
        print('\n\nGame over.\nGame: %s. Your score: %s.' % (game, score))
        s = input('Would you like to play again ? [Yes]/No >> ')
        if s.lower() in ['no', 'n']:
            break
        score=0
        game+=1
    else:
        score += 1
        print('\n\nGame: %s. Score: %s.' % (game, score))
print('Games: %s. Best score: %s.' % (game, best_score))
What do you think, about code?
Reply


Messages In This Thread
My version GhostGame - by anickone - Jun-18-2018, 08:52 PM
RE: My version GhostGame - by nilamo - Jun-18-2018, 08:58 PM
RE: My version GhostGame - by nilamo - Jun-18-2018, 08:59 PM
RE: My version GhostGame - by anickone - Jun-18-2018, 09:51 PM
RE: My version GhostGame - by ichabod801 - Jun-18-2018, 10:20 PM
RE: My version GhostGame - by anickone - Jun-20-2018, 06:25 PM
RE: My version GhostGame - by ichabod801 - Jun-20-2018, 08:44 PM
RE: My version GhostGame - by anickone - Jun-21-2018, 08:29 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020