tripeaks.py

Created by schraf

Created on March 14, 2021

3.34 KB

Explications en vidéo


from kandinsky import fill_rect, draw_string, set_pixel
from random import *
from time import sleep
from ion import *

R,N,B,F,G = (255,0,0), 3*(0,), 3*(255,), (80,130,50), 3*(80,)
BTS = [48,42,43,44,36,37,38,30,31,32,49,5]
FIG = [[16,56,124,254,511,254,124,56,16],[56,56,56,471,511,471,16,56,511],\
[16,56,124,254,511,511,443,56,511],[198,495,511,511,511,254,124,56,16]]
COUL = [R,N,N,R]
VAL = [1,2,3,4,5,6,7,8,9,10,'V','D','R']

X = [0,2,4,6,8,10,12,14,16,18,1,3,5,7,9,11,13,15,17,2,4,8,10,14,16,3,9,15]
Y = [3] * 10 + [2] * 9 + [1] * 6 + [0,0,0]
E = [1] * 10 + [[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],\
[10,11],[11,12],[13,14],[14,15],[16,17],[17,18],[19,20],[21,22],[23,24]] 

jeu = sorted(list(range(52)), key = lambda x:random())
cHaut = jeu[:28]
cPile = jeu[28:-1]
cSortie = jeu[-1:]
del jeu

def efface(a,b): fill_rect(0,a,320,b,F)

def milieu(l,t): return (l - 10 * len(t)) // 2

def dos(x,y):
     fill_rect(x,y,25,45,G)
     fill_rect(x,y,25,1,B)
     fill_rect(x,y,1,45,B) 

def carte(x, y, f, t, coul):
  fill_rect(x, y, 25, 45, B)
  for c in range(9):
    for l in range(9):
        if f[l] >> c & 1: 
            set_pixel(x + 2 + c, y + 2 + l, coul)
            set_pixel(x + 14 + c, y + 34 + l, coul)                
  draw_string(t, x + milieu(25, t), y + 14, N, B) 

def touche(x,y,v):
    t = str(v)
    draw_string(t, x + milieu(25, t), 135, B, F) 

def visuel(v):
    return [COUL[v // 13], FIG[v // 13], str(VAL[v % 13])]

def haut():
    efface(0,160)
    nb_visibles = len([i for i in E if i == 1])
    possibles = []
    k = 1
    for n in reversed(range(28)):
        v = cHaut[n]
        x, y = 12 + 15 * X[n], 10 + 25 * Y[n]
        if type(E[n]) == type([]): dos(x,y) 
        elif E[n] == 1: 
            possibles = [n] + possibles
            touche(x, y, nb_visibles - k)
            [coul, f, t] = visuel(v)
            carte(x, y, f, t, coul)
            k += 1
    return possibles

def pile():
    efface(160,220)
    for i in range(len(cPile)): dos(40 + 3 * i, 160)

def sortie():
    [coul, f, t] = visuel(cSortie[-1])
    carte(150,160,f,t,coul)

def jouer():
 while True:
  for i in BTS:
   if keydown(i): 
        sleep(.2)
        return BTS.index(i)

def legal(n):
    return (cSortie[-1] - n) % 13 in [1, 12]

def suivant(v):
    n = cHaut[v]
    if legal(n):
        E[v] = 0
        cSortie.append(n)
        for k in range(28):
          if type(E[k]) == type([]):
            try: 
                E[k].remove(v)
                if len(E[k]) == 0: E[k] = 1
            except: continue

def pioche():
    if len(cPile) > 0: 
        cSortie.append(cPile.pop(0))
        return True
    return False

def perdu(possibles):
    if len(cPile) > 0 or any([legal(cHaut[v]) for v in possibles]): return False
    t = "=== PERDU ==="
    draw_string(t, milieu(320,t), 110, B, R)
    return True

def gagne(): 
    if all(E[n] == 0 for n in range(28)):
        t = "=== GAGNE ==="
        draw_string(t, milieu(320,t), 110, B, N)
        return True
    return False

def go():
   majHaut = majPile = True
   while True: 
    if majHaut: possibles = haut()
    if majPile: pile()
    sortie()
    if perdu(possibles): break
    if gagne(): break  
    t = jouer()
    majHaut = majPile = False
    if t == 11: break
    elif t == 10: majPile = pioche()
    elif t < len(possibles): 
        suivant(possibles[t])  
        majHaut = True   

go()

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 <a href="https://www.numworks.com/legal/cookies-policy/">cookies policy</a>.