Forum

Catégorie : Programmes
Forum : Aide à la programmation
Vues : 585
Réponses : 2
Type : discussion
Etat : réponses autorisées

Optimisation code morpion.

F-BVXT le 27/05/2012 à 23:15
Bonjour,
J'ai programmé un morpion qui fonctionne mais qui est vraiment lent. Enfin, au début ça va, mais plus on avance dans le jeux, plus il devient lent (10 secondes pour tracer la dernière croix en cas de match nul !). Donc, là j'en appelle à votre aide pour m'aider à l'optimiser parce que je ne vois vraiment pas ce qui le ralentit à ce point.

Voilà le code complet :
Code
:SauveBDG1
:Menu("MORPION","JOUER",A,"SORTIR",B
:LBL A
:1->Xmin
:95->Xmax
:1->Ymin
:63->Ymax
:AxesNAff
:FonctOff
:0->G
:EffDessin
:For(A,24,72,16
:Ligne(A,8,A,56
:End
:EffListe L1
:9->dim(L1
:For(A,8,56,16
:Ligne(24,A,72,A
:End
:1->C
:EffVar FRepeat F ou G=1
:Repeat max([THETA]={72,73,74,82,83,84,92,93,94
:C->B
:0->[THETA]
:codeTouche->[THETA]
:If [THETA]=72 et non(L1(7:B->L1(7
:If [THETA]=73 et non(L1(8:B->L1(8
:If [THETA]=74 et non(L1(9:B->L1(9
:If [THETA]=82 et non(L1(4:B->L1(4
:If [THETA]=83 et non(L1(5:B->L1(5
:If [THETA]=84 et non(L1(6:B->L1(6
:If [THETA]=92 et non(L1(1:B->L1(1
:If [THETA]=93 et non(L1(2:B->L1(2
:If [THETA]=94 et non(L1(3:B->L1(3
:End
:If L1(7)=1
:Then
:Ligne(27,43,37,53
:Ligne(27,53,37,43
:End
:If L1(7)=.1:Cercle(32,48,6
:If L1(8)=1
:Then
:Ligne(43,43,53,53
:Ligne(43,53,53,43
:End
:If L1(8)=.1:Cercle(48,48,6
:If L1(9)=1
:Then
:Ligne(59,43,69,53
:Ligne(59,53,69,43
:End
:If L1(9)=.1:Cercle(64,48,6
:If L1(4)=1
:Then
:Ligne(27,27,37,37
:Ligne(27,37,37,27
:End
:If L1(4)=.1:Cercle(32,32,6
:If L1(5)=1
:Then
:Ligne(43,27,53,37
:Ligne(43,37,53,27
:End
:If L1(5)=.1:Cercle(48,32,6
:If L1(6)=1
:Then
:Ligne(59,27,69,37
:Ligne(59,37,69,27
:End
:If L1(6)=.1:Cercle(64,32,6
:If L1(1)=1
:Then
:Ligne(27,11,37,21
:Ligne(27,21,37,11
:End
:If L1(1)=.1:Cercle(32,16,6
:If L1(2)=1
:Then
:Ligne(43,11,53,21
:Ligne(43,21,53,11
:End
:If L1(2)=.1:Cercle(48,16,6
:If L1(3)=1
:Then
:Ligne(59,11,69,21
:Ligne(59,21,69,11
:End
:If L1(3)=.1:Cercle(64,16,6
:If L1(1)+L1(2)+L1(3)=3 ou L1(1)+L1(2)+L1(3)=.3:1->F
:If L1(4)+L1(5)+L1(6)=3 ou L1(4)+L1(5)+L1(6)=.3:2->F
:If L1(7)+L1(8)+L1(9)=3 ou L1(7)+L1(8)+L1(9)=.3:3->F
:If L1(1)+L1(4)+L1(7)=3 ou L1(1)+L1(4)+L1(7)=.3:4->F
:If L1(2)+L1(5)+L1(8)=3 ou L1(2)+L1(5)+L1(8)=.3:5->F
:If L1(3)+L1(6)+L1(9)=3 ou L1(3)+L1(6)+L1(9)=.3:6->F
:If L1(1)+L1(5)+L1(9)=3 ou L1(1)+L1(5)+L1(9)=.3:7->F
:If L1(7)+L1(5)+L1(3)=3 ou L1(7)+L1(5)+L1(3)=.3:8->F
:0
:For(Z,1,9
:Rép+L1(Z
:End
:If Rép = 5.4 et non(F : 1 -> G
:If B=1:.1->C
:If B=.1:1->C
:End
:Repeat G[DIFFERENT DE]1 ou F=9
:If F=1:Ligne(24,16,72,16
:If F=2:Ligne(24,32,72,32
:If F=3:Ligne(24,48,72,48
:If F=4:Ligne(32,8,32,56
:If F=5:Ligne(48,8,48,56
:If F=6:Ligne(64,8,64,56
:If F=7:Ligne(24,8,72,56
:If F=8:Ligne(24,56,72,8
:If G=1:F+1->F
:End
:Pause
:EffDessin
:EffEcr
:Menu("Rejouer ?","Oui",A,"Non",B
:LBL B
:RappelBDG 1
:FonctOn
:Output(1,1,"


P.S : Non, je n'ai pas posté en double. J'ai posté un premier sujet parce qu'il ne fonctionnait pas, et pour éviter un hors sujet, je poste un deuxième sujet pour l'optimiser (c'est ce que j'ai cru comprendre qu'il fallait faire selon la charte du forum).

Merci d'avance pour toutes les suggestions.
Sangohan38 le 28/05/2012 à 05:19
Les ralentissements sont majoritairement dues au fait que tu trace à chaque fois les symboles.
Pour éviter cela, tu peux rajouter une condition vérifiant que la touche viens d'être pressée:


Code
:SauveBDG1
:Menu("MORPION","JOUER",A,"SORTIR",B
:LBL A
:1->Xmin
:95->Xmax
:1->Ymin
:63->Ymax
:AxesNAff
:FonctOff
:0->G
:EffDessin
:For(A,24,72,16
:Ligne(A,8,A,56
:End
:EffListe L1
:9->dim(L1
:For(A,8,56,16
:Ligne(24,A,72,A
:End
:1->C
:EffVar FRepeat F ou G=1
:Repeat max([THETA]={72,73,74,82,83,84,92,93,94
:C->B
:0->[THETA]
:codeTouche->[THETA]
:If [THETA]=72 et non(L1(7:B->L1(7
:If [THETA]=73 et non(L1(8:B->L1(8
:If [THETA]=74 et non(L1(9:B->L1(9
:If [THETA]=82 et non(L1(4:B->L1(4
:If [THETA]=83 et non(L1(5:B->L1(5
:If [THETA]=84 et non(L1(6:B->L1(6
:If [THETA]=92 et non(L1(1:B->L1(1
:If [THETA]=93 et non(L1(2:B->L1(2
:If [THETA]=94 et non(L1(3:B->L1(3
:End
:If L1(7)=1 et [THETA]=92
:Then
:Ligne(27,43,37,53
:Ligne(27,53,37,43
:End
:If L1(7)=.1 et [THETA]=92:Cercle(32,48,6
:If L1(8)=1 et [THETA]=93
:Then
:Ligne(43,43,53,53
:Ligne(43,53,53,43
:End
:If L1(8)=.1 et [THETA]=93:Cercle(48,48,6
:If L1(9)=1 et [THETA]=94
:Then
:Ligne(59,43,69,53
:Ligne(59,53,69,43
:End
:If L1(9)=.1 et [THETA]=94:Cercle(64,48,6
:If L1(4)=1 et [THETA]=82
:Then
:Ligne(27,27,37,37
:Ligne(27,37,37,27
:End
:If L1(4)=.1 et [THETA]=82:Cercle(32,32,6
:If L1(5)=1 et [THETA]=82
:Then
:Ligne(43,27,53,37
:Ligne(43,37,53,27
:End
:If L1(5)=.1 et [THETA]=83:Cercle(48,32,6
:If L1(6)=1 et [THETA]=84
:Then
:Ligne(59,27,69,37
:Ligne(59,37,69,27
:End
:If L1(6)=.1 et [THETA]=84:Cercle(64,32,6
:If L1(1)=1 et [THETA]=72
:Then
:Ligne(27,11,37,21
:Ligne(27,21,37,11
:End
:If L1(1)=.1 et [THETA]=72:Cercle(32,16,6
:If L1(2)=1 et [THETA]=73
:Then
:Ligne(43,11,53,21
:Ligne(43,21,53,11
:End
:If L1(2)=.1 et [THETA]=73:Cercle(48,16,6
:If L1(3)=1 et [THETA]=74
:Then
:Ligne(59,11,69,21
:Ligne(59,21,69,11
:End
:If L1(3)=.1 et [THETA]=74:Cercle(64,16,6
:If L1(1)+L1(2)+L1(3)=3 ou L1(1)+L1(2)+L1(3)=.3:1->F
:If L1(4)+L1(5)+L1(6)=3 ou L1(4)+L1(5)+L1(6)=.3:2->F
:If L1(7)+L1(8)+L1(9)=3 ou L1(7)+L1(8)+L1(9)=.3:3->F
:If L1(1)+L1(4)+L1(7)=3 ou L1(1)+L1(4)+L1(7)=.3:4->F
:If L1(2)+L1(5)+L1(8)=3 ou L1(2)+L1(5)+L1(8)=.3:5->F
:If L1(3)+L1(6)+L1(9)=3 ou L1(3)+L1(6)+L1(9)=.3:6->F
:If L1(1)+L1(5)+L1(9)=3 ou L1(1)+L1(5)+L1(9)=.3:7->F
:If L1(7)+L1(5)+L1(3)=3 ou L1(7)+L1(5)+L1(3)=.3:8->F
:0
:For(Z,1,9
:Rép+L1(Z
:End
:If Rép = 5.4 et non(F : 1 -> G
:If B=1:.1->C
:If B=.1:1->C
:End
:Repeat G[DIFFERENT DE]1 ou F=9
:If F=1:Ligne(24,16,72,16
:If F=2:Ligne(24,32,72,32
:If F=3:Ligne(24,48,72,48
:If F=4:Ligne(32,8,32,56
:If F=5:Ligne(48,8,48,56
:If F=6:Ligne(64,8,64,56
:If F=7:Ligne(24,8,72,56
:If F=8:Ligne(24,56,72,8
:If G=1:F+1->F
:End
:Pause
:EffDessin
:EffEcr
:Menu("Rejouer ?","Oui",A,"Non",B
:LBL B
:RappelBDG 1
:FonctOn
:Output(1,1,"


On peux améliorer le code en le raccourcissant mais la je me sens pas d'attaque.

Sangohan38
F-BVXT le 28/05/2012 à 11:31
Ah oui effectivement, je n'avais pas du tout pensé au fait que ça retraçait tout à chaque fois...
Merci beaucoup, ça fonctionne beaucoup mieux maintenant.

Voilà donc le code, et j'ai gagné 2 octets en faisant EffVar au lieu de 0-> :
Code
:SauveBDG1
:Menu("MORPION","JOUER",A,"SORTIR",B
:LBL A
:1->Xmin
:95->Xmax
:1->Ymin
:63->Ymax
:AxesNAff
:FonctOff
:EffVar G
:EffDessin
:For(A,24,72,16
:Ligne(A,8,A,56
:End
:EffListe L1
:9->dim(L1
:For(A,8,56,16
:Ligne(24,A,72,A
:End
:1->C
:EffVar FRepeat F ou G=1
:Repeat max([THETA]={72,73,74,82,83,84,92,93,94
:C->B
:EffVar [THETA]
:codeTouche->[THETA]
:If [THETA]=72 et non(L1(7:B->L1(7
:If [THETA]=73 et non(L1(8:B->L1(8
:If [THETA]=74 et non(L1(9:B->L1(9
:If [THETA]=82 et non(L1(4:B->L1(4
:If [THETA]=83 et non(L1(5:B->L1(5
:If [THETA]=84 et non(L1(6:B->L1(6
:If [THETA]=92 et non(L1(1:B->L1(1
:If [THETA]=93 et non(L1(2:B->L1(2
:If [THETA]=94 et non(L1(3:B->L1(3
:End
:If L1(7)=1 et [THETA]=72
:Then
:Ligne(27,43,37,53
:Ligne(27,53,37,43
:End
:If L1(7)=.1 et [THETA]=72:Cercle(32,48,6
:If L1(8)=1 et [THETA]=73
:Then
:Ligne(43,43,53,53
:Ligne(43,53,53,43
:End
:If L1(8)=.1 et [THETA]=73:Cercle(48,48,6
:If L1(9)=1 et [THETA]=74
:Then
:Ligne(59,43,69,53
:Ligne(59,53,69,43
:End
:If L1(9)=.1 et [THETA]=74:Cercle(64,48,6
:If L1(4)=1 et [THETA]=82
:Then
:Ligne(27,27,37,37
:Ligne(27,37,37,27
:End
:If L1(4)=.1 et [THETA]=82:Cercle(32,32,6
:If L1(5)=1 et [THETA]=82
:Then
:Ligne(43,27,53,37
:Ligne(43,37,53,27
:End
:If L1(5)=.1 et [THETA]=83:Cercle(48,32,6
:If L1(6)=1 et [THETA]=84
:Then
:Ligne(59,27,69,37
:Ligne(59,37,69,27
:End
:If L1(6)=.1 et [THETA]=84:Cercle(64,32,6
:If L1(1)=1 et [THETA]=92
:Then
:Ligne(27,11,37,21
:Ligne(27,21,37,11
:End
:If L1(1)=.1 et [THETA]=92:Cercle(32,16,6
:If L1(2)=1 et [THETA]=93
:Then
:Ligne(43,11,53,21
:Ligne(43,21,53,11
:End
:If L1(2)=.1 et [THETA]=93:Cercle(48,16,6
:If L1(3)=1 et [THETA]=94
:Then
:Ligne(59,11,69,21
:Ligne(59,21,69,11
:End
:If L1(3)=.1 et [THETA]=94:Cercle(64,16,6
:If L1(1)+L1(2)+L1(3)=3 ou L1(1)+L1(2)+L1(3)=.3:1->F
:If L1(4)+L1(5)+L1(6)=3 ou L1(4)+L1(5)+L1(6)=.3:2->F
:If L1(7)+L1(8)+L1(9)=3 ou L1(7)+L1(8)+L1(9)=.3:3->F
:If L1(1)+L1(4)+L1(7)=3 ou L1(1)+L1(4)+L1(7)=.3:4->F
:If L1(2)+L1(5)+L1(8)=3 ou L1(2)+L1(5)+L1(8)=.3:5->F
:If L1(3)+L1(6)+L1(9)=3 ou L1(3)+L1(6)+L1(9)=.3:6->F
:If L1(1)+L1(5)+L1(9)=3 ou L1(1)+L1(5)+L1(9)=.3:7->F
:If L1(7)+L1(5)+L1(3)=3 ou L1(7)+L1(5)+L1(3)=.3:8->F
:0
:For(Z,1,9
:Rép+L1(Z
:End
:If Rép = 5.4 et non(F : 1 -> G
:If B=1:.1->C
:If B=.1:1->C
:End
:Repeat G[DIFFERENT DE]1 ou F=9
:If F=1:Ligne(24,16,72,16
:If F=2:Ligne(24,32,72,32
:If F=3:Ligne(24,48,72,48
:If F=4:Ligne(32,8,32,56
:If F=5:Ligne(48,8,48,56
:If F=6:Ligne(64,8,64,56
:If F=7:Ligne(24,8,72,56
:If F=8:Ligne(24,56,72,8
:If G=1:F+1->F
:End
:Pause
:EffDessin
:EffEcr
:Menu("Rejouer ?","Oui",A,"Non",B
:LBL B
:RappelBDG 1
:FonctOn
:Output(1,1,"


Et sinon, ça serait pas mieux de mettre 2 à la place de .1 pour gagner un octet à chaque fois ?