from random import *
from os import makedirs

#version 2.0.0

nbexo=5
langage="FR" #E ou FR
dico={}
dico["E"]="dico_anglais.txt"
dico["FR"]="dico.txt"




fichier = open(dico[langage], "r")
dico=fichier.read().split()
fichier.close()

svg="""
<svg style="margin-left:auto; margin-right:auto; display:block;" width="540" height="460" xmlns="http://www.w3.org/2000/svg" xmlns:svg="http://www.w3.org/2000/svg">
 <!-- Created with SVG-edit - https://github.com/SVG-Edit/svgedit-->
 <g class="layer">
  <title>Boggle</title>
  <g id="svg_3">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="105" y="6" width="99" height="99" id="svg_1"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="133.78235" y="109.17583" id="svg_2" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_4">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="205.5" y="6" width="99" height="99" id="svg_5"/>
   <text fill="#000000" stroke-width="0" x="165.31549" y="109.17583" id="svg_6" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_10">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="304.25" y="6" width="99" height="99" id="svg_11"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="196.29953" y="109.17583" id="svg_12" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_7">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="404.75" y="6" width="99" height="99" id="svg_8"/>
   <text fill="#000000" stroke-width="0" x="227.83266" y="109.17583" id="svg_9" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_22">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="105.625" y="105" width="99" height="99" id="svg_23"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="133.97846" y="143.83516" id="svg_24" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_19">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="204.125" y="104" width="99" height="99" id="svg_20"/>
   <text fill="#000000" stroke-width="0" x="164.88406" y="143.48507" id="svg_21" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_16">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="304.875" y="105" width="99" height="99" id="svg_17"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="196.49563" y="143.83516" id="svg_18" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_13">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="405.375" y="105" width="99" height="99" id="svg_14"/>
   <text fill="#000000" stroke-width="0" x="228.02876" y="143.83516" id="svg_15" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_46">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="104.3125" y="203.5" width="99" height="99" id="svg_47"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="133.56664" y="178.31945" id="svg_48" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_43">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="203.8125" y="203.5" width="99" height="99" id="svg_44"/>
   <text fill="#000000" stroke-width="0" x="164.78601" y="178.31945" id="svg_45" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_40">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="303.5625" y="203.5" width="99" height="99" id="svg_41"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="196.08382" y="178.31945" id="svg_42" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_37">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="404.0625" y="203.5" width="99" height="99" id="svg_38"/>
   <text fill="#000000" stroke-width="0" x="227.61695" y="178.31945" id="svg_39" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_34">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="104.9375" y="302.5" width="99" height="99" id="svg_35"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="133.76274" y="212.97879" id="svg_36" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_31">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="205.4375" y="302.5" width="99" height="99" id="svg_32"/>
   <text fill="#000000" stroke-width="0" x="165.29588" y="212.97879" id="svg_33" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
  <g id="svg_28">
   <rect stroke="#000000" fill="#ffffff" stroke-width="5" x="304.1875" y="302.5" width="99" height="99" id="svg_29"/>
   <text stroke="#000000" fill="#000000" stroke-width="0" x="196.27992" y="212.97879" id="svg_30" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) ">{}</text>
  </g>
  <g id="svg_25">
   <rect fill="#ffffff" stroke="#000000" stroke-width="5" x="404.6875" y="302.5" width="99" height="99" id="svg_26"/>
   <text fill="#000000" stroke-width="0" x="227.81305" y="212.97879" id="svg_27" font-size="24" font-family="serif" text-anchor="middle" xml:space="preserve" transform="matrix(3.1871240623580643,0,0,2.856373300433597,-272.879812863773,-233.3645286848619) " stroke="#000000">{}</text>
  </g>
 </g>
</svg>"""

des=[ "ETUKNO",
      "EVGTIN",
      "DECAMP",
      "IELRUW",
      "EHIFSE",
      "RECALS",
      "ENTDOS",
      "OFXRIA",
      "NAVEDZ",
      "EIOATA",
      "GLENYU",
      "BMAQJO",
      "TLIBRA",
      "SPULTE",
      "AIMSOR",
      "ENHRIS"]

leplateau=""
      
plateau=[[1,4,5],
         [0,2,4,5,6],
         [1,3,5,6,7],
         [2,6,7],
         [0,1,5,8,9],
         [0,1,2,4,6,8,9,10],
         [1,2,3,5,7,9,10,11],
         [2,3,6,10,11],
         [4,5,9,12,13],
         [4,5,6,8,10,12,13,14],
         [5,6,7,9,11,13,14,15],
         [6,7,10,14,15],
         [8,9,13],
         [8,9,10,12,14],
         [9,10,11,13,15],
         [10,11,14]]
regles={}
regles["FR"]="""<p style="margin-left:40px; ">Chaque joueur cherche des mots pouvant être formés à partir de lettres adjacentes du plateau.
Par « adjacentes », il est sous-entendu horizontalement, verticalement, ou en diagonale. Les mots doivent être de 3 lettres au minimum,
peuvent être au singulier ou au pluriel, conjugués ou non, mais ne doivent pas utiliser plusieurs fois le même dé pour le même mot.
Les joueurs écrivent tous les mots qu'ils ont trouvés sur leur feuille personnelle.
Après les 3 minutes de recherche, les joueurs doivent arrêter d'écrire et le jeu entre dans la phase de calcul des points.</p>
<p style="margin-left:40px;">Lors du calcul des points, chaque joueur lit à haute voix les mots trouvés. Si deux joueurs ou plus ont trouvé le même mot, il est rayé des listes le contenant.</p>
<p style="margin-left:40px;">Le gagnant est le joueur ayant le plus grand nombre de points.</p>

"""
regles["E"]="""<p style="margin-left:40px; ">Each player searches for words that can be constructed from the letters of sequentially adjacent cubes,
where "adjacent" cubes are those horizontally, vertically, and diagonally neighboring. Words must be at least three letters long,
may include singular and plural (or other derived forms) separately, but may not use the same letter cube more than once per word.
Each player records all the words they find by writing on a private sheet of paper. After three minutes have elapsed,
all players must immediately stop writing and the game enters the scoring phase.</p>
<p style="margin-left:40px;">In the scoring phase, each player reads off their list of discovered words. If two or more players wrote the same word, it is removed from all players' lists.</p>
<p style="margin-left:40px;">The winner is the player whose point total is highest.</p>
"""

memo_taille="""<table style="display:block; margin-left: 45px;">
<tbody><tr>
<th>{}</th>
<th>Points :
</th></tr>
<tr>
<td>3</td>
<td>1
</td></tr>
<tr>
<td>4</td>
<td>1
</td></tr>
<tr>
<td>5</td>
<td>2
</td></tr>
<tr>
<td>6</td>
<td>3
</td></tr>
<tr>
<td>7</td>
<td>5
</td></tr>
<tr>
<td>8+</td>
<td>11
</td></tr></tbody></table>"""

taille={}
taille["FR"]=memo_taille.format("Lettres :")
taille["E"]=memo_taille.format("Letters :")

phrase_corr={}
phrase_corr["FR"]="""<h3 style="color:red;">Il y a {0} mot{1} de {2} lettres  {3} : </h3><p style="margin-left:80px; font-size:18px; font-weight: bold;">"""
phrase_corr["E"]="""<h3 style="color:red;"> {2}-letter word{1}  {3}: {0}</h3><p style="margin-left:80px; font-size:18px; font-weight: bold;">"""

repertoires={}
repertoires["FR"]=("ennonces_html_FR","corriges_html_FR","exercices_pdf_FR")
repertoires["E"]=("ennonces_html_E","corriges_html_E","exercices_pdf_E")

def sauv_ex(chaine):
#sauvegarde exercice 
    with open (repertoires[langage][0]+"/"+leplateau+".html", "w",encoding='utf-8') as f:
        f.write(chaine)

def sauv_corr(chaine):
#sauvegarde correction n
    with open (repertoires[langage][1]+"/"+leplateau+".html", "w",encoding='utf-8') as f:
        f.write(chaine)
        
def pluriel(x):
    if x>1:
        return "s"
    else:
        return ""
def point(n):
    if n<5:
        return "(1 point)"
    elif n==5:
        return "(2 points)"
    elif n==6:
        return "(3 points)"
    elif n==7:
        return "(7 points)"
    else:
        return "(11 points)"
    
    
def melange():
    global leplateau
    leplateau=""
    for de in des:
        leplateau+=str(choice(de))
    liste=list(leplateau)
    shuffle(liste)
    leplateau="".join(liste)


def affiche():
  
    chaine="""<h1 style="text-align:center; ">BOGGLE </h1>"""
    print("Le plateau :")
    for i in range(len(leplateau)):
        print(leplateau[i],end="")
        if (i+1)%4==0:
            print()
    chaine+=svg.format(*list(leplateau))
    chaine+=regles[langage]
    chaine+=taille[langage]
    
    sauv_ex(chaine)
    return chaine

def lemot(liste):
    ch=""
    for l in liste:
        ch+=str(leplateau[l])
    return ch

def purge(liste):
    retour=[]
    for l in liste:
        l=l.strip()
        if l  not in retour:
            retour.append(l)
    return sorted(retour)

def in_dico(lettres):
    fini=False
    reponse=False
    i=0
    while not fini:
       if i==len(dico):
           fini=True
       else:
            if dico[i][:len(lettres)]==lettres and len(dico[i])>len(lettres) and len(dico[i])<17:
               fini=True
               reponse=True
            else:
                i+=1
    return reponse

def etape_1():
    global listes
   
    liste=[]
    for n in range(16):
        for k in plateau[n]:
            mot=lemot([n,k])
            suite=[m for m in dico if m[:2]==mot and len(m)>2 and len(m)<17]
            if len(suite)!=0:
                liste.append([n,k])
    listes.append(liste)
    
def etape_suivante():
    global listes
   
    print("Mots de {} lettres".format(len(listes)+2))
    liste=[]
    mots=[]
    for j in listes[-1]:
        
        ajout=[j+ [k] for k in plateau[j[-1]] if k not in j]
        
        suite=[a for a in ajout if in_dico(lemot(a))]
        for s in suite:
        
                liste.append(s)
        
        mot_a_ajouter=[lemot(x) for x in ajout if lemot(x) in dico]
        mots.extend(mot_a_ajouter)
    print("Nombre : ",len(purge(mots)))
    chaine=""
    if len(purge(mots))!=0:
        chaine=phrase_corr[langage].format(len(purge(mots)),pluriel(len(purge(mots))),len(listes)+2,point(len(listes)+2))
        #debug=input("debug {}".format(chaine))
        for u in purge(mots):
           print(u,end="; " )
           chaine+=u+"; "
        print()
        chaine=chaine[:-2]
        chaine+=".</p>"
    
    listes.append(liste)    
    return chaine

#preparation des repertoires de sauvegarde
for repertoire in repertoires[langage]:
    try:
        makedirs(repertoire)
    except:
        pass
        
bas_de_page="""<div style="margin-top:-40px; text-align:right">
<a  href="https://site2wouf.fr">site2wouf.fr (C)2020 </a></div>"""


for no_ex in range(nbexo):
    print("-------------------------->",no_ex+1,"/",nbexo)
    pdf="""<html><head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <style>
        
        body {font-size:20px;}
        
    </style></head><body>"""
    
    listes=[]
    melange()
    pdf+=affiche()
    etape_1()
    corr="""<h1 style="text-align:center;">Solutions :</h1>"""
    corr+=svg.format(*list(leplateau))
    
    for i in range(14):
        corr+=etape_suivante()
    sauv_corr(corr)






