|
@@ -436,6 +436,15 @@ Erreur! |
|
|
|
|
|
|
|
|
\end{frame} |
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Découper une string en liste} |
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
>>> message = "un deux trois" |
|
|
|
|
|
>>> message.split() # espaces |
|
|
|
|
|
["un", "deux", "trois"] |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
\begin{frame}[fragile] |
|
@@ -548,6 +557,145 @@ Aussi: |
|
|
\end{frame} |
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Squelette} |
|
|
|
|
|
|
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item Lire les mots possibles depuis un fichier texte |
|
|
|
|
|
\item Choisir un mot au hasard |
|
|
|
|
|
\item Afficher un "hint" |
|
|
|
|
|
\item Démarrer une boucle |
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item Demander une lettre |
|
|
|
|
|
\item Afficher le "hint" mis à jour |
|
|
|
|
|
\item Vérfier si le jeu est fini |
|
|
|
|
|
\item Repartir au début de la boucle |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Une fonction par tâche} |
|
|
|
|
|
|
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item \texttt{read\_words()} |
|
|
|
|
|
\item \texttt{choose\_word()} |
|
|
|
|
|
\item \texttt{display\_hint()} |
|
|
|
|
|
\item \texttt{has\_won()} |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Structure de données} |
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item Une liste pour les mots dans le fichier |
|
|
|
|
|
\item Un ensemble pour les bonnes lettre devinées |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Implémentation: lire un fichier} |
|
|
|
|
|
|
|
|
|
|
|
\begin{itemize} |
|
|
|
|
|
\item \texttt{open()} renvoie un "objet fichier" |
|
|
|
|
|
\item \texttt{read()} lit le fichier \textbf{en entier} et retourne une string |
|
|
|
|
|
\end{itemize} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
def read_words(): |
|
|
|
|
|
stream = open("noms.txt") |
|
|
|
|
|
contents = stream.read() |
|
|
|
|
|
stream.close() |
|
|
|
|
|
words = splitlines() |
|
|
|
|
|
return words |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
|
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Choisir un mot au hasard dans la liste} |
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
import random # remember me? |
|
|
|
|
|
def choose_word(words): |
|
|
|
|
|
n = len(words) |
|
|
|
|
|
index = random.randint(0, n-1) |
|
|
|
|
|
return words[index] |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Afficher les lettres devinées} |
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
def display_hint(word, letters): |
|
|
|
|
|
for letter in word: |
|
|
|
|
|
if letter in letters: |
|
|
|
|
|
print(letter, end="") |
|
|
|
|
|
else: |
|
|
|
|
|
print("_", end="") |
|
|
|
|
|
print("") |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
>>> display_hint("vache", {"a"}) |
|
|
|
|
|
_a___ |
|
|
|
|
|
>>> display_hint("vache", {"a", "c"}) |
|
|
|
|
|
_ac__ |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{L'utilisateur a-t-il gagné?} |
|
|
|
|
|
|
|
|
|
|
|
L'utilisateur a gagné si toutes les lettres ont |
|
|
|
|
|
été devinées: |
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
def has_won(word, letters): |
|
|
|
|
|
for letter in word: |
|
|
|
|
|
if letter not in letters: |
|
|
|
|
|
return False |
|
|
|
|
|
return True |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Boucle principale} |
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
words = read_words() |
|
|
|
|
|
word = choose_word(words) |
|
|
|
|
|
|
|
|
|
|
|
letters = set() |
|
|
|
|
|
display_hint(word, letters) |
|
|
|
|
|
|
|
|
|
|
|
while True: |
|
|
|
|
|
new_letter = input() |
|
|
|
|
|
letters.add(new_letter) |
|
|
|
|
|
display_hint(word, letters) |
|
|
|
|
|
if has_won(word, letters): |
|
|
|
|
|
print("OK") |
|
|
|
|
|
return |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
\begin{frame}[fragile] |
|
|
|
|
|
\frametitle{Une tradition} |
|
|
|
|
|
Traditionnelement, on appelle ce genre de fonction \texttt{main()}: |
|
|
|
|
|
|
|
|
|
|
|
\begin{lstlisting} |
|
|
|
|
|
def main(): |
|
|
|
|
|
words = read_words() |
|
|
|
|
|
word = choose_word(words) |
|
|
|
|
|
... |
|
|
|
|
|
|
|
|
|
|
|
main() # ne pas oublier de l'appeler! |
|
|
|
|
|
\end{lstlisting} |
|
|
|
|
|
\end{frame} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|