Skip to content

Instantly share code, notes, and snippets.

@ana0
Created June 30, 2014 17:24

Revisions

  1. ana0 created this gist Jun 30, 2014.
    67 changes: 67 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    class Sequence(object):

    def __init__(self, query, answers):
    self.query = query
    self.answers = answers

    def text_wrapping(self, text):
    #simple textwrapper, check how much space is left on the x-axis of the screen
    #if a word's length is longer than the remaining space, adds a newline
    maxx = stdscr.getmaxyx()[1] - stdscr.getyx()[1]
    if text not in [",", "?", ".", ":"]:
    text = " " + text
    if len(text) + 1 > maxx:
    stdscr.addstr("\n")
    return text

    def pretty_printing(self, text):
    #splits text, a string, into a list of strings, checks dicts for them, and changes print colour accordingly
    words = text.split(" ")
    for i in words:
    if vocabulary[i.lower()] == "noun":
    stdscr.addstr(self.text_wrapping(i), curses.color_pair(2))
    elif vocabulary[i.lower()] == "adjective":
    stdscr.addstr(self.text_wrapping(i), curses.color_pair(3))
    elif vocabulary[i.lower()] == "verb":
    stdscr.addstr(self.text_wrapping(i), curses.color_pair(4))
    elif vocabulary[i.lower()] == "adverb":
    stdscr.addstr(self.text_wrapping(i), curses.color_pair(5))
    elif vocabulary[i.lower()] == "pronoun":
    stdscr.addstr(self.text_wrapping(i), curses.color_pair(6))
    else:
    stdscr.addstr(self.text_wrapping(i))
    stdscr.refresh()

    def game_play(self, *funcs):
    #fundamental gameplay method, checks answer (int) against a list index to print the
    #corresponding statement, also iterates through a list of possible wrong answers stored above as global variables
    global wrong
    global patience
    stdscr.clear()
    self.pretty_printing(self.query)
    answer = stdscr.getstr(0, 0).decode(encoding = "utf-8")
    range_check = [i for i in range(len(self.answers))]
    while answer not in str(range_check) or answer.isdigit() == False:
    try:
    stdscr.clear()
    stdscr.addstr(0, 1, "ERR: Wrong input \n\n", curses.color_pair(1))
    stdscr.refresh()
    self.pretty_printing(wrong_answers[wrong])
    wrong += 1
    except IndexError:
    stdscr.clear()
    stdscr.addstr(0, 1, "ERR: Wrong input \n\n", curses.color_pair(1))
    stdscr.refresh()
    stdscr.addstr(1,0, "\n " + str(patience), curses.color_pair(1))
    patience += 1
    stdscr.getch()
    stdscr.clear()
    self.pretty_printing(self.query)
    answer = stdscr.getstr(0, 0).decode(encoding = "utf-8")
    else:
    stdscr.clear()
    self.pretty_printing(self.answers[int(answer)])
    stdscr.getch()
    if len(funcs) > 0:
    stdscr.clear()
    eval(funcs[int(answer)])