mastermind.py

Created by ph-moutou

Created on June 11, 2018

3.09 KB

Pour jouer avec la calculatrice, taper mm(123) : elle choisit une combinaison de 4 pions dont les couleurs sont choisies parmi les 6 proposées. Taper une combinaison de 4 chiffres suivie de EXE, la calculatrice répond par une ou plusieurs fiches blanches (bonne couleur, mal placé) ou noires (bonne couleur, bien placé). Attention, vous n’avez droit qu’à 10 essais!


from random import *
from kandinsky import *
from math import *

def segH(x1,x2,y):
  for i in range(x2-x1):
    set_pixel(x1+i,y,color(0,0,0)) 

def segV(x,y1,y2):
  for i in range(y2-y1):
    set_pixel(x,y1+i,color(0,0,0))

def carre(x,y,c,l):
  for i in range(l):
    for j in range(l):
      set_pixel(x+i,y+j,c) 

def cercle(x0,y0,r,c,e):
  for i in range(2*e):
    xd=x0-int((r-i*0.5)/sqrt(2))
    xf=x0+int((r-i*0.5)/sqrt(2))
    for x in range(xd,xf+1):
      x1=x
      y1=y0+int(sqrt((r-i*0.5)**2-(x-x0)**2))
      set_pixel(x,y1,c)
      for j in range(3):
        x2=x0+y1-y0
        y2=y0+x0-x1
        set_pixel(x2,y2,c)
        x1,y1=x2,y2

def couleur(c):
  cl=[color(255,255,0),color(0,0,255),color(255,0,0),color(0,255,0),
  color(255,255,255),color(0,0,0)]
  return cl[c]      

def plateau():
  carre(5,50,color(200,190,210),150)
  carre(165,50,color(200,190,210),150)
  for i in range(5):
    draw_string(str(i),10,175-i*30)
    draw_string(str(i+5),170,175-i*30)
  for i in range(6):
    segH(5,155,50+i*30)
    segH(165,315,50+i*30)
  for i in range(2):
    segV(5+i*160,50,200)
    segV(25+i*160,50,200)
    segV(125+i*160,50,200)
    segV(155+i*160,50,200)
    set_pixel(155+i*160,200,color(0,0,0))#dernier pt
  for i in range(270):
    for j in range(31):
      set_pixel(20+i,10+j,color(200,190,210))
  for i in range(6):
    segH(20+45*i,65+45*i,10)
    segH(20+45*i,65+45*i,40)
    segV(20+45*i,10,40)
    segV(65+45*i,10,40)
    set_pixel(290,40,color(0,0,0))
    draw_string(str(i+1),27+45*i,17)
    cercle(50+45*i,25,10,couleur(i),10)
    prepare(0)

def prepare(c):    
  for i in range(4):
    cercle(38+25*i+160*(c//5),185-30*(c%5),3,color(0,0,0),3)

def dessine(ch,c):
  for i in range(4):
    cercle(38+25*i+160*(c//5),185-30*(c%5),10,couleur(int(ch[i])-1),10)

def evalue(r,c):
  b=0
  for i in range(4):
    if r[i]>0:
      cercle(135+10*(b%2)+160*((c-1)//5),180-30*((c-1)%5)+10*(b//2),3,couleur(3+r[i]),3)
      b+=1

def entrer():
  c=''
  while (len(c)==4 and 0<int(c[0])<7
   and 0<int(c[1])<7 and 0<int(c[2])<7 
   and 0<int(c[3])<7)==False: c=input()
  return c

def mm(s):
  seed(s)
  plateau()
  combi,R="",4*[0]
  for i in range(4):
    combi+=str(randint(0,5))
  stock=combi
  cp=0
  while sum(R)!=8 and cp<10:
    R=4*[0]#resultat(0:mc,1:bcmp,2bcbp)
    choix=entrer()
    dessine(choix,cp)
    print(cp,choix,end=" - ")
    cp+=1
    bc=0
    #test bcbp 
    for i in range(4):
      if choix[i]==combi[i]:
        R[bc]+=2
        bc+=1
        combi=combi[0:i]+"6"+combi[i+1:4]
        choix=choix[0:i]+"6"+choix[i+1:4]
    #test bcmp 
    for i in range(4):
      for j in range(4):
        if choix[i]==combi[j] and choix[i]!="6":
          R[bc]+=1
          bc+=1
          combi=combi[0:j]+"6"+combi[j+1:4]
          choix=choix[0:i]+"6"+choix[i+1:4]  
    evalue(R,cp)
    if cp<10 and sum(R)!=8:
      prepare(cp)
    for i in range(4):
      print(R[i],end="")
    print("")
    combi=stock
  if cp<10:
    draw_string("Bravo!",140,205)
    print("Solution trouvee en {} coups".format(cp))
  else:
    draw_string("Echec!",140,205)
    print("Il fallait trouver {}".format(combi))

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.