# solitaire.py

Created by schraf

Created on March 22, 2021

1.99 KB

Explications en vidéo

Vous pouvez jouer avec l’émulateur en utilisant les flèches du clavier, la touche Entrée et la touche Retour (Backspace).

```from kandinsky import *
from ion import *
from time import sleep
from random import choice

N,B,A,F = 3*(0,), 3*(255,),(200,90,20),(75,60,170)
BTS = [0,1,2,3,5,17,52]
L = ["2"*7,"0011100","1"*7,"0022200","0021200",\
"2221222","2111112","0111110","1112111","2211122","2212122"]
CODES=[[1,7,2,8,2,7,1],[3,4,9,5,5,3,3],[3,4,5,6,5,4,3],[1,1,9,10,0,3,3],\
[4,1,6,5,5,1,1],[3,4,9,6,2,3,3],[4,1,6,8,6,1,4],[1,1,2,8,2,1,1]]

jeu = [list(L[s]) for s in choice(CODES)]

def nb_de_pions(): return sum(l.count("1") for l in jeu) - 1

def aff_case(c,l,coul):
draw_string(" o."[int(jeu[l][c])], 90 + 20 * c, 40 + 20 * l, B, coul)

def affiche_jeu():
fill_rect(0,0,320,222,F)
for k in range(49): aff_case(k % 7,k // 7,F)

def touche():
while True:
for i in BTS:
if keydown(i):
sleep(.15)
return i

def jouer(depart, c2,l2):
[c1,l1] = depart
c3 = (c1 + c2) // 2
l3 = (l1 + l2) // 2
if (abs(c1 - c2) != 2 and abs(l1 - l2) != 2) or jeu[l3][c3] != "1": return depart
jeu[l1][c1] = jeu[l3][c3] = "2"
jeu[l2][c2] = "1"
for [c,l] in [[c1,l1],[c2,l2],[c3,l3]]: aff_case(c,l,F)
return []

def score(maxi):
nb = nb_de_pions()
txt = "GAGNE !" if nb == 0 else str(int(100 - 100 * nb / maxi)) + " %"
draw_string("Score : " + txt + (" "*3),5,200,B,F)
return nb == 0

def go():
jc,jl = 3,3
depart = []
maxi = nb_de_pions()
affiche_jeu()
while True:
dep = len(depart) > 0
aff_case(jc,jl,N)
if dep: aff_case(depart[0],depart[1],A)
if score(maxi): break
t = touche()
aff_case(jc,jl,F)
if t == 5: return
elif t == 17:
aff_case(depart[0],depart[1],F)
depart = []
elif t < 4:
jc = min(6,max(0, jc + (t==3) - (t==0)))
jl = min(6,max(0, jl + (t==2) - (t==1)))
elif t == 52:
if not(dep) and jeu[jl][jc] == "1": depart += [jc, jl]
elif dep and jeu[jl][jc] == "2": depart = jouer(depart, jc,jl)

go()```