Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
| lehrkraefte:blc:informatik:ffprg1-2022:wordle:start [2022/06/02 07:00] – [Bryhton] Ivo Blöchliger | lehrkraefte:blc:informatik:ffprg1-2022:wordle:start [2022/06/02 10:19] (current) – [Bryhton] Ivo Blöchliger | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | ====== Wordle ====== | ||
| + | Das Ziel ist es, ein kleines Wordle-Spiel zu programmieren. | ||
| + | Dazu werden wir einzelne Programmteile erstellen, die dann am Schluss zu einem Ganzen zusammengefügt werden können. | ||
| + | |||
| + | ===== Tests ===== | ||
| + | Wir werden für unsere Funktionen immer gleich auch Tests schreiben, so haben wir eine Chance, logische Fehler früh zu erkennen. | ||
| + | |||
| + | |||
| + | ===== Bewertungsfunktion(en) ===== | ||
| + | ==== Richtig Platzierte ==== | ||
| + | Die Funktion '' | ||
| + | |||
| + | * Input: Zwei Strings, (in den Variablen '' | ||
| + | * Programmabbruch mit Fehlermeldung, | ||
| + | * Output: Array mit Einträgen '' | ||
| + | |||
| + | <code python> | ||
| + | def richtig(solution, | ||
| + | if len(solution)!=len(guess): | ||
| + | raise ValueError(" | ||
| + | | ||
| + | # Resultat Array bauen und in der Variablen res speichern | ||
| + | res = [] | ||
| + | # | ||
| + | # ... | ||
| + | # | ||
| + | return res # Resultat zurückgeben und Funktion beenden | ||
| + | | ||
| + | | ||
| + | def tests(): | ||
| + | cases = [[" | ||
| + | | ||
| + | ] | ||
| + | for test in cases: | ||
| + | res = richtig(test[0], | ||
| + | if res!=test[2]: | ||
| + | raise ValueError(" | ||
| + | print(" | ||
| + | | ||
| + | |||
| + | tests() | ||
| + | </ | ||
| + | |||
| + | <WRAP todo> | ||
| + | * Vervollständigen Sie die Funktion '' | ||
| + | * Fügen Sie weitere Tests hinzu. | ||
| + | </ | ||
| + | |||
| + | ==== Vollständige Bewertungsfunktion ==== | ||
| + | <WRAP todo> | ||
| + | Wie oben, schreiben Sie eine Funktion '' | ||
| + | |||
| + | Es gibt zwei Videos, die den Algorithmus erklären. Versuchen Sie erst, die Aufgabe ohne die Videos zu lösen. Wenn das nicht klappt, schauen Sie sich nur den ersten Video an und versuchen Sie dann, die Methode in Python umzusetzen. Wenn das auch nicht klappt, schauen Sie sich das zweite Video auch noch an, schreiben Sie den Code aber nicht aus dem Video ab, sondern versuchen Sie, diesen Code danach selbst zu schreiben. | ||
| + | </ | ||
| + | Die Funktion sollte folgende Tests erfüllen (erfinden Sie gerne eigen, weitere Tests!) | ||
| + | <code python> | ||
| + | def tests(): | ||
| + | cases = [[" | ||
| + | | ||
| + | | ||
| + | | ||
| + | ] | ||
| + | for test in cases: | ||
| + | res = bewerten(test[0], | ||
| + | if res!=test[2]: | ||
| + | raise ValueError(" | ||
| + | print(" | ||
| + | |||
| + | |||
| + | </ | ||
| + | |||
| + | **Erklärvideos**: | ||
| + | * Funktion des Algorithmus (ohne Code): [[https:// | ||
| + | * Erklärung des entsprechen Python-Codes: | ||
| + | |||
| + | |||
| + | ==== Farben im Terminal ausgeben ==== | ||
| + | <code python> | ||
| + | print(" | ||
| + | print(" | ||
| + | </ | ||
| + | ==== Wortliste ==== | ||
| + | * Variante «easy»: Wortliste aus dem JavaScript-Code eines online Wordle kopieren. | ||
| + | * Variante «suchen und finden»: Wortliste auf dem Web suchen. | ||
| + | * Variante «selber bauen»: Wikipedia-Artikel herunterladen, | ||
| + | |||
| + | === Wikipedia «downloaden» === | ||
| + | Folgender Code funktioniert mehr schlecht als recht. Besser wäre es wohl, eine Library oder externes Programm wie ' | ||
| + | |||
| + | Der Vorteil dieses Programm ist, dass man die Wörter gleich mit der Häufigkeit hat. So kann man nur die häufigsten Wörter als zu erratende Wörter zulassen, aber alle als gültige Eingabewörter. | ||
| + | |||
| + | <hidden Code> | ||
| + | <code python crawler.py> | ||
| + | import socket | ||
| + | import ssl | ||
| + | import urllib | ||
| + | import re | ||
| + | import os | ||
| + | import sys | ||
| + | |||
| + | # Or simply set this to True in TigerJython | ||
| + | tigerJython = (sys.executable.find(" | ||
| + | |||
| + | # Download einer Wikipedia-Seite | ||
| + | # URL ist z.B. / | ||
| + | def download(url): | ||
| + | host = " | ||
| + | port = 443 | ||
| + | s = socket.socket(socket.AF_INET, | ||
| + | s.settimeout(0.5) | ||
| + | s.connect((host , port)) | ||
| + | s = ssl.wrap_socket(s) | ||
| + | request = "GET " + url + " HTTP/ | ||
| + | if (tigerJython): | ||
| + | s.sendall(request) | ||
| + | else: | ||
| + | s.sendall(bytes(request, | ||
| + | try: | ||
| + | reply = s.recv(4096) | ||
| + | except socket.timeout: | ||
| + | s.close() | ||
| + | return "" | ||
| + | start = reply.decode(' | ||
| + | if (start==-1): | ||
| + | start = reply.decode(' | ||
| + | if (start> | ||
| + | end = reply.decode(' | ||
| + | #while reply[end]> | ||
| + | # end+=1 | ||
| + | # print(" | ||
| + | numbytes = int(reply[(start+16): | ||
| + | print(" | ||
| + | while reply.decode(' | ||
| + | # print(" | ||
| + | more = s.recv(4096) | ||
| + | # print(" | ||
| + | reply += more | ||
| + | reply = reply[(reply.decode(' | ||
| + | # print(" | ||
| + | while len(reply)< | ||
| + | # print(" | ||
| + | more = s.recv(4096) | ||
| + | reply += more | ||
| + | # print(" | ||
| + | |||
| + | else: | ||
| + | # print(" | ||
| + | # print(reply) | ||
| + | try: | ||
| + | while True: | ||
| + | # print(" | ||
| + | more = s.recv(4096) | ||
| + | reply += more | ||
| + | except socket.timeout: | ||
| + | pass | ||
| + | s.close() | ||
| + | return reply.decode(' | ||
| + | |||
| + | |||
| + | todo = ["/ | ||
| + | words = {} | ||
| + | linksdone = {} | ||
| + | # Load existing data, if any | ||
| + | for l in range(4, | ||
| + | datafile = " | ||
| + | if os.path.isfile(datafile): | ||
| + | with open(datafile, | ||
| + | for line in file.readlines(): | ||
| + | entries = line.split(" | ||
| + | words[entries[0]] = int(entries[1]) | ||
| + | |||
| + | |||
| + | |||
| + | for i in range(200): | ||
| + | url = todo.pop(0) | ||
| + | linksdone[url] = True | ||
| + | print(" | ||
| + | html = download(url) | ||
| + | links = re.findall(r'< | ||
| + | links = [l for l in links if re.search(' | ||
| + | for l in links: | ||
| + | if not l in linksdone: | ||
| + | linksdone[l]=True | ||
| + | #print(l) | ||
| + | todo.append(l) | ||
| + | | ||
| + | text = re.sub('< | ||
| + | text = re.sub('< | ||
| + | ww = [w.upper() for w in text.split() if re.match(' | ||
| + | # print(" | ||
| + | newwords = 0 | ||
| + | for w in ww: | ||
| + | if w in words: | ||
| + | words[w]+=1 | ||
| + | else: | ||
| + | newwords+=1 | ||
| + | words[w] = 1 | ||
| + | | ||
| + | print(" | ||
| + | |||
| + | print(" | ||
| + | print(todo[0: | ||
| + | |||
| + | alle = words.keys() | ||
| + | # print(" | ||
| + | for l in range(4, | ||
| + | wl = [w for w in alle if len(w)==l] | ||
| + | wl.sort() | ||
| + | # print(" | ||
| + | with open(" | ||
| + | for w in wl: | ||
| + | file.write(" | ||
| + | | ||
| + | |||
| + | </ | ||
| + | </ | ||
| + | |||
| + | Ein mögliches Resultat dieses Codes ist {{kurse: | ||
| + | |||
| + | Eine riesige Liste (1.2 Mio) deutscher Wörter gibt es hier: https:// | ||
| + | Diese habe ich gefiltert und folgende Liste extrahiert: {{kurse: | ||
| + | |||
| + | ===== Brython ===== | ||
| + | Brython erlaubt es, Python auf Webseiten einzusetzen. Hier sind die nötigen Funktionen einmal programmiert. Was noch fehlt, ist die Spiel-Logik: | ||
| + | |||
| + | https:// | ||
| + | |||
| + | Hinweis: Öffnen Sie die Developper-Tools im Browser (F12). Im Konsolen-Tab sehen Sie die print-Ausgaben. Das kann bei der Fehlersuche helfen. | ||
| + | ===== JavaScript ===== | ||
| + | Rudimentäre Vorlage: https:// | ||