Update benstypcursus.py
This commit is contained in:
+79
-118
@@ -1,7 +1,7 @@
|
|||||||
from db import nederlandse_woorden
|
from db import nederlandse_woorden, woorden
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
from db import woorden
|
import curses
|
||||||
|
|
||||||
RED = "\033[31m"
|
RED = "\033[31m"
|
||||||
GREEN = "\033[32m"
|
GREEN = "\033[32m"
|
||||||
@@ -13,62 +13,6 @@ YELLOW = "\033[33m"
|
|||||||
MAGENTA = "\033[35m"
|
MAGENTA = "\033[35m"
|
||||||
CYAN = "\033[36m"
|
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):
|
def random_zinnen(aantal):
|
||||||
gemaaktezinnen = []
|
gemaaktezinnen = []
|
||||||
@@ -76,67 +20,46 @@ def random_zinnen(aantal):
|
|||||||
for _ in range(aantal):
|
for _ in range(aantal):
|
||||||
onderwerp = random.choice(woorden["znw"])
|
onderwerp = random.choice(woorden["znw"])
|
||||||
werkwoord = random.choice(woorden["ww"])
|
werkwoord = random.choice(woorden["ww"])
|
||||||
|
lijdend_voorwerp = random.choice(woorden["lv"])
|
||||||
plaats = random.choice(woorden["vz"]) + " " + random.choice(woorden["znw"])
|
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:] + ".")
|
gemaaktezinnen.append(zin[0].upper() + zin[1:] + ".")
|
||||||
|
|
||||||
return gemaaktezinnen
|
return gemaaktezinnen
|
||||||
|
|
||||||
def zinnen(zinnen_lijst):
|
|
||||||
import curses
|
|
||||||
|
|
||||||
def run(stdscr):
|
def curses_engine(stdscr, items, mode="woorden"):
|
||||||
curses.curs_set(1)
|
curses.curs_set(1)
|
||||||
curses.start_color()
|
curses.start_color()
|
||||||
curses.use_default_colors()
|
curses.use_default_colors()
|
||||||
|
|
||||||
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK) # nog niet getypt
|
curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_BLACK)
|
||||||
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK) # correct
|
curses.init_pair(2, curses.COLOR_BLUE, curses.COLOR_BLACK)
|
||||||
curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_RED) # fout
|
curses.init_pair(3, curses.COLOR_RED, curses.COLOR_BLACK)
|
||||||
curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK) # compleet goed
|
curses.init_pair(4, curses.COLOR_GREEN, curses.COLOR_BLACK)
|
||||||
|
|
||||||
for zin in zinnen_lijst:
|
total_chars = 0
|
||||||
|
correct_chars = 0
|
||||||
|
|
||||||
|
start_all = time.time()
|
||||||
|
|
||||||
|
for item in items:
|
||||||
typed = ""
|
typed = ""
|
||||||
|
mistakes = set()
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
stdscr.clear()
|
stdscr.clear()
|
||||||
|
|
||||||
y, x = 2, 0
|
y, x = 2, 0
|
||||||
done = typed == zin
|
|
||||||
|
|
||||||
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]
|
|
||||||
|
|
||||||
|
for i in range(len(item)):
|
||||||
if i >= len(typed):
|
if i >= len(typed):
|
||||||
ch = correct_char if correct_char != " " else " "
|
stdscr.addstr(y, x + i, item[i], curses.color_pair(1) | curses.A_UNDERLINE)
|
||||||
stdscr.addstr(
|
|
||||||
y, x + i,
|
|
||||||
ch,
|
|
||||||
curses.color_pair(1) | curses.A_UNDERLINE
|
|
||||||
)
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
user_char = typed[i]
|
if typed[i] == item[i]:
|
||||||
|
stdscr.addstr(y, x + i, typed[i], curses.color_pair(2))
|
||||||
if user_char == correct_char:
|
|
||||||
stdscr.addstr(y, x + i, user_char, curses.color_pair(2))
|
|
||||||
|
|
||||||
else:
|
else:
|
||||||
display = user_char if user_char != " " else "·"
|
stdscr.addstr(y, x + i, typed[i], curses.color_pair(3) | curses.A_BOLD)
|
||||||
|
|
||||||
stdscr.addstr(
|
|
||||||
y, x + i,
|
|
||||||
display,
|
|
||||||
curses.color_pair(3) | curses.A_BOLD
|
|
||||||
)
|
|
||||||
|
|
||||||
stdscr.move(y, x + len(typed))
|
stdscr.move(y, x + len(typed))
|
||||||
stdscr.refresh()
|
stdscr.refresh()
|
||||||
@@ -144,19 +67,56 @@ def zinnen(zinnen_lijst):
|
|||||||
key = stdscr.get_wch()
|
key = stdscr.get_wch()
|
||||||
|
|
||||||
if isinstance(key, str) and key.isprintable():
|
if isinstance(key, str) and key.isprintable():
|
||||||
typed += key
|
idx = len(typed)
|
||||||
|
|
||||||
elif key in ("\n", "\r"):
|
if idx < len(item):
|
||||||
if typed == zin:
|
total_chars += 1
|
||||||
break
|
|
||||||
|
if key == item[idx]:
|
||||||
|
correct_chars += 1
|
||||||
|
else:
|
||||||
|
mistakes.add(idx)
|
||||||
|
|
||||||
|
typed += key
|
||||||
|
|
||||||
elif key in (curses.KEY_BACKSPACE, "\b", 127):
|
elif key in (curses.KEY_BACKSPACE, "\b", 127):
|
||||||
typed = typed[:-1]
|
typed = typed[:-1]
|
||||||
|
|
||||||
curses.wrapper(run)
|
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
|
||||||
|
|
||||||
|
stdscr.clear()
|
||||||
|
|
||||||
|
total_time = round(time.time() - start_all, 2)
|
||||||
|
|
||||||
|
accuracy = round((correct_chars / total_chars) * 100, 2) if total_chars else 0
|
||||||
|
|
||||||
|
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"))
|
||||||
|
|
||||||
|
|
||||||
menu = f"""
|
menu = f"""
|
||||||
bens typcursus
|
bens typcurses
|
||||||
|
|
||||||
{UNDERLINE}Menu:{RESET}
|
{UNDERLINE}Menu:{RESET}
|
||||||
1. Start 50 woorden
|
1. Start 50 woorden
|
||||||
@@ -169,25 +129,26 @@ bens typcursus
|
|||||||
while True:
|
while True:
|
||||||
print(menu)
|
print(menu)
|
||||||
|
|
||||||
menu_keuze = input(" > ").strip()
|
keuze = input(" > ").strip()
|
||||||
|
|
||||||
if not menu_keuze.isdigit():
|
if not keuze.isdigit():
|
||||||
print("Voer een nummer in.\n")
|
print("Voer een nummer in.\n")
|
||||||
continue
|
continue
|
||||||
|
|
||||||
menu_keuze = int(menu_keuze)
|
keuze = int(keuze)
|
||||||
|
|
||||||
if menu_keuze > 5 or menu_keuze < 1:
|
if keuze == 1:
|
||||||
print("gekozen optie bestaat niet, probeer opnieuw.\n")
|
|
||||||
elif menu_keuze == 1:
|
|
||||||
start(50)
|
start(50)
|
||||||
elif menu_keuze == 4:
|
|
||||||
|
elif keuze == 2:
|
||||||
|
start(int(input("hoeveel woorden? :")))
|
||||||
|
|
||||||
|
elif keuze == 4:
|
||||||
aantal = int(input("hoeveel zinnen? :"))
|
aantal = int(input("hoeveel zinnen? :"))
|
||||||
zinnen(random_zinnen(aantal))
|
zinnen(random_zinnen(aantal))
|
||||||
# elif menu_keuze == 3:
|
|
||||||
# plek = ("bestand: ")
|
elif keuze == 5:
|
||||||
# import plek
|
|
||||||
elif menu_keuze == 2:
|
|
||||||
start(int(input("hoeveel woorden? :")))
|
|
||||||
elif menu_keuze == 5:
|
|
||||||
break
|
break
|
||||||
|
|
||||||
|
else:
|
||||||
|
print("ongeldige optie\n")
|
||||||
Reference in New Issue
Block a user