angry_words.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. #!/usr/bin/env python3
  2. # Angry Words, the word replacement game
  3. import sys
  4. def usage():
  5. print("Wrong usage")
  6. exit(0)
  7. def run_game(s):
  8. # This regex may be useful, not sure '\[[^>]*\]'
  9. blank_list = []
  10. for i in s.split('\n'):
  11. # This loop searches for each blank tag
  12. in_tag = False
  13. for n in range(len(i)):
  14. if in_tag:
  15. if i[n] == ']':
  16. # Join the list of characters in the tag
  17. blank_list[-1] = ''.join(blank_list[-1])
  18. in_tag = False
  19. else:
  20. # Keep reading a tag until a ] is reached
  21. blank_list[-1].append(i[n])
  22. elif i[n] == '[':
  23. # [ indicates beginning of a tag
  24. blank_list.append([])
  25. in_tag = True
  26. filled_in = fill_in(blank_list)
  27. filled_in.reverse() # This is to make popping easier
  28. print('\n\n')
  29. for i in s.split('\n'):
  30. in_tag = False
  31. for n in range(len(i)):
  32. if in_tag:
  33. if i[n] == ']':
  34. # At the end of the tag, pop the last word
  35. print(filled_in.pop(), end='')
  36. in_tag = False
  37. elif i[n] == '[':
  38. in_tag = True
  39. else:
  40. print(i[n], end='')
  41. print()
  42. def fill_in(bl):
  43. # This function gets the user's input for each blank
  44. result = []
  45. for i in range(len(bl)):
  46. result.append(input('{0:2}. {1:21}: '.format(i+1,bl[i])))
  47. return result
  48. def play_file(filepath):
  49. f = open(filepath, 'r')
  50. s = f.read()
  51. f.close()
  52. try:
  53. run_game(s)
  54. except (KeyboardInterrupt, EOFError) as e:
  55. print("\n\nInterrupt")
  56. exit(0)
  57. def parse_tsv(s):
  58. result = [i.split('\t') for i in s.split('\n')]
  59. return list(filter(lambda x: x != [''], result))
  60. def play_loop():
  61. games_table = []
  62. try:
  63. f = open("templates/files.tsv")
  64. s = f.read()
  65. f.close()
  66. games_table = parse_tsv(s)[1:]
  67. except FileNotFoundError as e:
  68. print("templates/files.tsv not found")
  69. table_size = len(games_table)
  70. prompt = '\nEnter the number of the template you wish to play: '
  71. while True:
  72. print('\n 0. Exit the program')
  73. for i in range(table_size):
  74. print('{0:3}. {1}'.format(i+1,games_table[i][1]))
  75. try:
  76. user_result = int(input(prompt))
  77. if user_result == 0:
  78. exit(0)
  79. if user_result < 0:
  80. raise ValueError
  81. play_file('templates/' + games_table[user_result-1][0])
  82. except (KeyboardInterrupt, EOFError) as e:
  83. print("\n\nInterrupt")
  84. exit(0)
  85. except (IndexError, ValueError) as e:
  86. print("\nPlease enter an integer between 0 and {0}.".format(table_size))
  87. def main():
  88. if len(sys.argv) < 2:
  89. play_loop()
  90. elif len(sys.argv) == 2:
  91. play_file(sys.argv[1])
  92. else:
  93. usage()
  94. if __name__ == '__main__':
  95. main()