My version GhostGame - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Game Development (https://python-forum.io/forum-11.html) +--- Thread: My version GhostGame (/thread-11025.html) |
My version GhostGame - anickone - Jun-18-2018 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? RE: My version GhostGame - nilamo - Jun-18-2018 I think I'd rather write get_ghost() like this, so it's obvious that it always returns a list:def get_ghost(): ghost = [] while len(ghost) < 2: rnd = randint(1, doors) if rnd not in ghost: ghost.append(rnd) return ghost RE: My version GhostGame - nilamo - Jun-18-2018 And running it through pylint would help make it conform to pep8. RE: My version GhostGame - anickone - Jun-18-2018 nilamo thank you RE: My version GhostGame - ichabod801 - Jun-18-2018 My thoughts: There are too many single letter variable names, try to be more descriptive. I don't like the _ after key words so they can be variable names. I would use other names, like low and high instead of min_ and max_. I would put the game loop in a function, and call it from an if __name__ == '__main__' block. Note that you can use random.sample to get two unique numbers without having to code the loop in get_ghost. From the user perspective it works fine, but I would add a description of the game. I was rather clueless playing it the first time. Don't take this as too critical. The only thing that really bothers me is the single letter variable names. Oh, and comments would be good. RE: My version GhostGame - anickone - Jun-20-2018 (Jun-18-2018, 10:20 PM)ichabod801 Wrote: I don't like the _ after key words so they can be variable names. I would use other names, like low and high instead of min_ and max_.I agree, me too. Thank you for low and high, I will use it. (Jun-18-2018, 10:20 PM)ichabod801 Wrote: Note that you can use random.sample to get two unique numbers without having to code the loop in get_ghost.Thank you, I did not know about this possibility. (Jun-18-2018, 10:20 PM)ichabod801 Wrote: There are too many single letter variable names, try to be more descriptive.I use when the variable appears and then disappears. I'm not sure that this is bad. And again thanks nilamo for the version get_ghost(). I like it. RE: My version GhostGame - ichabod801 - Jun-20-2018 (Jun-20-2018, 06:25 PM)anickone Wrote: I use when the variable appears and then disappears. I'm not sure that this is bad. All variables appear and then disappear. RE: My version GhostGame - anickone - Jun-21-2018 (Jun-20-2018, 08:44 PM)ichabod801 Wrote: All variables appear and then disappear.Of course, but I was talking about following several lines. My code :) import re, os def make_log(msg): print(msg) log.append(msg) def search_one_chr(py_file): with open(py_file) as fin: lines=fin.readlines() file_show=1 for i, line in enumerate(lines,1): r=rex.findall(line) if r: if file_show: file_show=0 make_log('%s\nfile = "%s"' % ('*'*50, py_file)) c=r[0] if c in chrs: chrs[c]+=1 else: chrs[c]=1 make_log('line = %s, code = "%s"' % (i, line.rstrip())) def log_save(log, log_file): with open(log_file, 'w') as fout: fout.write('\n'.join(log)) pattern=r"^(?:\t*| *)([a-zA-Z]) = " rex=re.compile(pattern) path='/usr/lib/python3.5/' path_code_source=os.path.abspath(path) chrs={} log=[] for root, dirs, files in os.walk(path_code_source): for next_file in files: if next_file.endswith('.py'): search_one_chr(os.path.join(root,next_file)) cnt=0 for c in sorted(chrs.keys()): cnt+=chrs[c] make_log('letter %s as var used %s times' % (c,chrs[c])) make_log('all used one letter as var %s times' % cnt) log_save(log, 'log_file.txt')For python dir. For Django dir in my pc.
|