Guest User

Untitled

a guest
Jul 15th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.26 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. import random
  4.  
  5. def affiche_joueur(nom, i):
  6.     longueur = len(nom)
  7.     print "C'est le tour du joueur n°" + str(i)
  8.     print '*' * (longueur+4)
  9.     print '* ' + nom + ' *'
  10.     print '*' * (longueur+4)
  11.  
  12. def affiche_main(liste):
  13.     print 'Voici votre main : '
  14.     for i in liste:
  15.         print i,
  16.     print ' '
  17.    
  18. def chargerDico(nomFich):
  19.     f = open(nomFich)
  20.     liste = f.read().split()
  21.     dico = {}
  22.     for mot in liste:
  23.         dico[mot] = None
  24.     return dico
  25.  
  26. def change_lettres(li, sac):
  27.     x = str(raw_input('Voulez-vous changer des lettres de votre chevalet ? (O/N):'))
  28.     if x == 'O':
  29.         liste_a_changer=[]
  30.         y=1
  31.         while y!='0':
  32.             y= raw_input('Quelle(s) lettre(s) voulez-vous changer ?:')
  33.             liste_a_changer.append(y)
  34.         for i in range(len(liste_a_changer)):
  35.             for j in range(len(li)):
  36.                 if li[j]==liste_a_changer[i]:
  37.                     echange_listes(li[j],i,li,sac)
  38.         return li
  39.     else:
  40.        return li
  41.  
  42. def check_joker(mot):
  43.     for i in range(len(mot)):
  44.         if mot[i] == '&':
  45.             x= raw_input('A quelle lettre correspond votre joker (&)? : ')
  46.             mot= mot[:i] + x + mot[i+1:]
  47.     return mot
  48.  
  49. def check_lettres(mot,chevalet):
  50.     mot.upper
  51.     res=True
  52.     for lettre in mot:
  53.         if lettre not in chevalet:
  54.             res=False
  55.     return res
  56.  
  57. def check_taille(mot, direction, pos_x, pos_y):
  58.     if direction=='H':
  59.         return len(mot[0])+pos_x<=15
  60.     else:
  61.         return len(mot[0])+pos_y<=15
  62.    
  63. def check_total(liste, chevalet):
  64.     x = check_joker(liste[0])
  65.     return isInDico(x, dico) and check_lettres(x, chevalet) and check_taille(x, liste[1], liste[2], liste[3]) and check_position(liste)
  66.    
  67. def chevalet(n, sac):
  68.     l = []
  69.     for i in sac[0:n]:
  70.         l.append(i)
  71.     return l
  72.  
  73. def choixJoueur():
  74.     """Cette fonction demande au joueur
  75.    quel mot il veut placer sur la grille
  76.    et à quel endroit"""
  77.     dico = chargerDico("french.dic")
  78.     mot=raw_input("Quel mot désirez-vous placer sur la grille?")
  79.     if isInDico(mot,dico)== True:
  80.         direction=raw_input("Quelle est sa direction : horizontale (H) ou verticale (V)?")
  81.         position_x= int(raw_input("Quelle est la position de ce mot en x?"))
  82.         position_y= int(raw_input("Quelle est la position de ce mot en y?"))
  83.         return [mot, direction, position_x,position_y]
  84.     else:
  85.         print'Votre mot est incorrect'
  86.         return choixJoueur()
  87.    
  88. def compte_points(mot):
  89.     res = 0
  90.     s= lettres_dico('french.let')
  91.     for i in range(len(mot)):
  92.         for j in range (26):
  93.             if mot[i] == s[j][0]:
  94.                 res+= int(s[j][2])
  95.     print 'Votre score est de '+str(res)+'points!'
  96.     return res
  97.  
  98. def compte_sac(sac):
  99.     res = 0
  100.     for i in sac:
  101.         res+=1
  102.     return res
  103.  
  104. def echange(liste, i1, i2):
  105.     liste[i1], liste[i2] = liste[i2], liste[i1]
  106.  
  107. def echange_listes(el1,ind2,liste1,liste2):
  108.     for i in range(len(liste1)):
  109.         if liste1[i]==el1:
  110.             ind1=i
  111.             print ind1
  112.     liste1[ind1]=liste2[ind2]
  113.     liste2[ind2]=el1
  114.     return liste1
  115.  
  116. def ecrire_mot(mot, direction, pos_x, pos_y, li):
  117.     mot= mot.upper()
  118.     if direction == 'H':
  119.         for i in range(len(mot)):
  120.             if li[(pos_x+1)][(pos_y+1)+i]== ' *'or li[(pos_x+1)][(pos_y+1)+i]== ' '+ mot[i]:
  121.                 li[(pos_x+1)][(pos_y+1)+i]= ' '+mot[i]
  122.             else:
  123.                 return False
  124.         return li
  125.     else :
  126.         for i in range(len(mot)):
  127.             if li[(pos_x+1)+i][(pos_y+1)]== ' '+mot[i] or li[(pos_x+1)+i][(pos_y+1)]== ' *':
  128.                 li[(pos_x+1)+i][(pos_y+1)]= ' '+mot[i]
  129.             else:
  130.                 return False
  131.         return li
  132.    
  133. def lettres_dico(nomFichier):
  134.     x=[]
  135.     a = open (nomFichier)
  136.     for i in a.readlines():
  137.         b= i.split()
  138.         x.append(b)
  139.     y = ['&', '2', '0']
  140.     x.append(y)
  141.     return x
  142.  
  143. def liste_jeu():
  144.     table=[['  ',' 0',' 1',' 2',' 3',' 4',' 5',' 6',' 7',' 8',' 9','10','11','12','13','14']]
  145.     for i in range(10):
  146.         table.append([str(i)+' ']+15*[' *'])
  147.     for j in range(10,15):
  148.         table.append([str(j)+'']+15*[' *'])
  149.     return table
  150.  
  151. def isInDico(mot,dico):
  152.     return mot.upper() in dico
  153.  
  154. def melange(listeNombres):
  155.     for i in range(len(listeNombres)-1):
  156.         place = random.randint(i,len(listeNombres)-1)
  157.         echange(listeNombres,i,place)
  158.  
  159. def multijoueur():
  160.     l = []
  161.     nbre= int(raw_input('Combien de joueurs participent à la partie ? : '))
  162.     if nbre > 4 or nbre < 1:
  163.         return "Nombre de joueurs incorrect"
  164.     else:
  165.         for i in range(nbre):
  166.             e= raw_input('Quel est le nom du joueur n°'+ str(i+1)+ ':')
  167.             l.append(e)
  168.     return l
  169.    
  170. def pioche():
  171.     l= []
  172.     s= lettres_dico('french.let')
  173.     for i in range(len(s)):
  174.         a= s[i][0]
  175.         b= int (s[i][1])
  176.         for i in range (b):
  177.             l.append(a)
  178.     melange(l)
  179.     return l
  180.  
  181. def plateau_jeu(liste):
  182.     for ligne in liste:
  183.         for element in ligne:
  184.             print element,
  185.         print ''
  186.         print ''
  187.  
  188. def remplir_chevalet(mot, chevalet, sac):
  189.     for i in range(len(mot)):
  190.         for j in chevalet:
  191.             if mot[i]== chevalet[j]:
  192.                 chevalet[j]=sac[i]
  193.     return chevalet
  194.  
  195. def scrabble():
  196.     m= multijoueur()
  197.     print ''
  198.     lettres_dico('french.let')
  199.     sac=pioche()
  200.     l = liste_jeu()
  201.     plateau_jeu(l)
  202.     li_chevalet_points_sac=[]
  203.    
  204.     while sac != []:
  205.         a=chevalet(7,sac)
  206.         for i in range(len(m)):
  207.             affiche_joueur(m[i], (i+1))
  208.             n=len(a)
  209.             a= a+chevalet(7-n, sac)
  210.             affiche_main(a)
  211.             sac = sac[7:]
  212.             a=change_lettres(a, sac)
  213.             affiche_main(a)
  214.             b = choixJoueur()
  215.             l=ecrire_mot(b[0],b[1],b[2],b[3],l)
  216.             check_joker(b[0])
  217.             plateau_jeu(l)
  218.             c = compte_sac(sac)
  219.             d= compte_points(b[0])
  220.             f= remplir_chevalet(b[0], a , sac)
  221.             print 'Il reste '+str(c)+'lettres dans la pioche'
  222.             print 'Votre score est de '+str(d)
  223.             e= [f, d, sac]
  224.             li_chevalet_points_sac.append(e)
Add Comment
Please, Sign In to add comment