From de9c2aa7064d4a841b2692eafe6f06acbe840b04 Mon Sep 17 00:00:00 2001 From: Ben de Roo Date: Sun, 3 May 2026 17:55:57 +0200 Subject: [PATCH] Update benstypcursus.py --- benstypcursus.py | 215 +++++++++++++++++++---------------------------- 1 file changed, 88 insertions(+), 127 deletions(-) diff --git a/benstypcursus.py b/benstypcursus.py index 0abd8da..bd4b2ea 100644 --- a/benstypcursus.py +++ b/benstypcursus.py @@ -1,7 +1,7 @@ -from db import nederlandse_woorden +from db import nederlandse_woorden, woorden import random import time -from db import woorden +import curses RED = "\033[31m" GREEN = "\033[32m" @@ -13,62 +13,6 @@ YELLOW = "\033[33m" MAGENTA = "\033[35m" CYAN = "\033[36m" -def start(aantal): - print(BOLD + "Typ het woord over:" + RESET) - print() - - woorden = random.sample(nederlandse_woorden, aantal) - - goed = 0 - fout = 0 - tijd_start_volledig = time.time() - - for woord in woorden: - print(BOLD + YELLOW + woord + RESET) - print() - - start_tijd = time.time() - - gegeven = input(" > " + UNDERLINE).strip() - print(RESET) - eind_tijd = time.time() - - tijd = round(eind_tijd - start_tijd, 2) - - if gegeven == woord: - print(f"{GREEN}{BOLD}Goed! {RESET}Tijd: {tijd} seconden") - print() - goed += 1 - elif gegeven == "exit": - break - elif gegeven == "quit": - break - elif gegeven == "break": - break - else: - fout += 1 - print(f"{RED}{BOLD}Fout!{RESET} Het juiste woord was: {woord}") - print(f"Tijd: {tijd} seconden") - print() - - tijd_stop_volledig = time.time() - totale_tijd = round(tijd_stop_volledig - tijd_start_volledig, 2) - gem_tijd = round(totale_tijd / len(woorden), 2) - - totaal = goed + fout - - accuracy = round((goed / totaal) * 100, 2) if totaal > 0 else 0 - if accuracy >= 55: - accuracy_kleur = GREEN - else: - accuracy_kleur = RED - - print() - print(f"totaal goed: {GREEN}{BOLD}{goed}{RESET}") - print(f"totaal fout: {RED}{BOLD}{fout}{RESET}") - print(f"totale tijd: {MAGENTA}{BOLD}{totale_tijd}{RESET}") - print(f"gemiddelde tijd per woord: {MAGENTA}{BOLD}{gem_tijd}{RESET}") - print(f"accuracy: {accuracy_kleur}{BOLD}{accuracy}%{RESET}") def random_zinnen(aantal): gemaaktezinnen = [] @@ -76,87 +20,103 @@ def random_zinnen(aantal): for _ in range(aantal): onderwerp = random.choice(woorden["znw"]) werkwoord = random.choice(woorden["ww"]) + lijdend_voorwerp = random.choice(woorden["lv"]) plaats = random.choice(woorden["vz"]) + " " + random.choice(woorden["znw"]) - zin = onderwerp + " " + werkwoord + " " + plaats - + zin = onderwerp + " " + werkwoord + " " + lijdend_voorwerp + " " + plaats gemaaktezinnen.append(zin[0].upper() + zin[1:] + ".") + return gemaaktezinnen -def zinnen(zinnen_lijst): - import curses - def run(stdscr): - curses.curs_set(1) - curses.start_color() - curses.use_default_colors() +def curses_engine(stdscr, items, mode="woorden"): + curses.curs_set(1) + curses.start_color() + curses.use_default_colors() - curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) # nog niet getypt - curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) # correct - curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_RED) # fout - curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) # compleet goed + curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) + curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) + curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK) + curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) - for zin in zinnen_lijst: - typed = "" + total_chars = 0 + correct_chars = 0 - while True: - stdscr.clear() + start_all = time.time() - y, x = 2, 0 - done = typed == zin + for item in items: + typed = "" + mistakes = set() - if done: - stdscr.addstr(y, x, zin, curses.color_pair(4) | curses.A_BOLD) - stdscr.refresh() - curses.curs_set(0) - curses.napms(700) - break - - for i in range(len(zin)): - correct_char = zin[i] - - if i >= len(typed): - ch = correct_char if correct_char != " " else " " - stdscr.addstr( - y, x + i, - ch, - curses.color_pair(1) | curses.A_UNDERLINE - ) + while True: + stdscr.clear() + y, x = 2, 0 + for i in range(len(item)): + if i >= len(typed): + stdscr.addstr(y, x + i, item[i], curses.color_pair(1) | curses.A_UNDERLINE) + else: + if typed[i] == item[i]: + stdscr.addstr(y, x + i, typed[i], curses.color_pair(2)) else: - user_char = typed[i] + stdscr.addstr(y, x + i, typed[i], curses.color_pair(3) | curses.A_BOLD) - if user_char == correct_char: - stdscr.addstr(y, x + i, user_char, curses.color_pair(2)) + stdscr.move(y, x + len(typed)) + stdscr.refresh() - else: - display = user_char if user_char != " " else "ยท" + key = stdscr.get_wch() - stdscr.addstr( - y, x + i, - display, - curses.color_pair(3) | curses.A_BOLD - ) + if isinstance(key, str) and key.isprintable(): + idx = len(typed) - stdscr.move(y, x + len(typed)) + if idx < len(item): + total_chars += 1 + + if key == item[idx]: + correct_chars += 1 + else: + mistakes.add(idx) + + typed += key + + elif key in (curses.KEY_BACKSPACE, "\b", 127): + typed = typed[:-1] + + if typed == item: + stdscr.clear() + y, x = 2, 0 + stdscr.addstr(y, x, item, curses.color_pair(4) | curses.A_BOLD) stdscr.refresh() + curses.napms(700) + break - key = stdscr.get_wch() + stdscr.clear() - if isinstance(key, str) and key.isprintable(): - typed += key + total_time = round(time.time() - start_all, 2) - elif key in ("\n", "\r"): - if typed == zin: - break + accuracy = round((correct_chars / total_chars) * 100, 2) if total_chars else 0 - elif key in (curses.KEY_BACKSPACE, "\b", 127): - typed = typed[:-1] + stdscr.addstr(2, 0, f"modus: {mode}") + stdscr.addstr(3, 0, f"correct chars: {correct_chars}") + stdscr.addstr(4, 0, f"total chars: {total_chars}") + stdscr.addstr(5, 0, f"accuracy (first try): {accuracy}%") + stdscr.addstr(6, 0, f"tijd: {total_time}s") + + stdscr.refresh() + stdscr.getch() + + +def start(aantal): + woorden_list = random.sample(nederlandse_woorden, aantal) + curses.wrapper(lambda stdscr: curses_engine(stdscr, woorden_list, "woorden")) + + +def zinnen(zinnen_lijst): + curses.wrapper(lambda stdscr: curses_engine(stdscr, zinnen_lijst, "zinnen")) - curses.wrapper(run) menu = f""" -bens typcursus +bens typcurses {UNDERLINE}Menu:{RESET} 1. Start 50 woorden @@ -169,25 +129,26 @@ bens typcursus while True: print(menu) - menu_keuze = input(" > ").strip() + keuze = input(" > ").strip() - if not menu_keuze.isdigit(): + if not keuze.isdigit(): print("Voer een nummer in.\n") continue - menu_keuze = int(menu_keuze) + keuze = int(keuze) - if menu_keuze > 5 or menu_keuze < 1: - print("gekozen optie bestaat niet, probeer opnieuw.\n") - elif menu_keuze == 1: + if keuze == 1: start(50) - elif menu_keuze == 4: + + elif keuze == 2: + start(int(input("hoeveel woorden? :"))) + + elif keuze == 4: aantal = int(input("hoeveel zinnen? :")) zinnen(random_zinnen(aantal)) - # elif menu_keuze == 3: - # plek = ("bestand: ") - # import plek - elif menu_keuze == 2: - start(int(input("hoeveel woorden? :"))) - elif menu_keuze == 5: + + elif keuze == 5: break + + else: + print("ongeldige optie\n") \ No newline at end of file