Bonjour à tous,
Je m'amuse à programmer des petits jeux sur python (avec Pygame) pour me faire la main en programmation. Cette semaine, je me suis mis à un petit jeu de tic tac toe.
Le jeu fonctionne super bien sauf que là, personne ne veut jouer avec moi . Alors, je me suis dit eh bien je vais me créer un ami pour jouer . J'ai donc fait une AI.
Le code fonctionne, même très bien mais... il est lourd, et assez ennuyant à lire.
En gros, c'est une fonction qui reçoit en paramètre une liste d'objets et le pion de l'AI "X" ou "O". Ces objets contiennent un attribut "case" qui représente l'endroit où se situe l'objet dans le plateau de jeu (voir figure):
------------
| 0 | 1 | 2 |
------------
| 3 | 4 | 5 |
------------
| 6 | 7 | 8 |
------------
Et chaque objet contient aussi un attribut Tag, qui nous indique si l'objet est un "X" ou un "O".
En récupérant la case et le tag, eh bien on peut savoir quoi est où. J'ai essayé de construire un dictionnaire qui associe la case au tag mais sa marche pas du tout, alors je me suis résigné à construire deux listes, une de cases et une de tags qui sont associées par leur index dans la liste. ex: listeCase[0, 1, 2...] listeTag["X", "X", "O"...] donc la case 1 = "X". Ensuite je vérifie toutes les possibilités pour chacune des cases pour une condition de victoire (si "O" a la case 1 et "O" a la case 2 et pas de "X" a la case 0 alors met "O" a la case 0) et même chose ensuite pour les conditions de défaite (si "X" a la case 1 et "X" a la case 2 et pas de "O" a la case 0 alors met "O" a la case 0) et ainsi de suite pour toutes les possibilités... Autant vous dire que c'est assez plate à écrire ce genre de code... Ensuite, si aucune de ces conditions est remplie, on choisi une case aléatoire (rd.choice = random.choice). Puis à la fin, on converti la position de la case en coordonnées pour créer un nouvel objet.
voici le code de la fonction:
Alors, voici ma question, quelqu'un aurait-il une idée pour faire plus simple ou moins lourd ?
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129 def joueurAI(liste, pionAI): """Simule on joueur""" caseAI = [] caseJoueur = [] for element in liste: if element.tag == pionAI: caseAI.append(element.case) elif element.tag != pionAI: caseJoueur.append(element.case) #si l'AI a un possibilité de gagner if 0 in caseAI and 1 in caseAI and not 2 in caseJoueur: select = 2 elif 0 in caseAI and 2 in caseAI and not 1 in caseJoueur: select = 1 elif 0 in caseAI and 3 in caseAI and not 6 in caseJoueur: select = 6 elif 0 in caseAI and 6 in caseAI and not 3 in caseJoueur: select = 3 elif 0 in caseAI and 4 in caseAI and not 8 in caseJoueur: select = 8 elif 0 in caseAI and 8 in caseAI and not 4 in caseJoueur: select = 4 elif 1 in caseAI and 2 in caseAI and not 0 in caseJoueur: select = 0 elif 1 in caseAI and 4 in caseAI and not 7 in caseJoueur: select = 7 elif 1 in caseAI and 7 in caseAI and not 4 in caseJoueur: select = 4 elif 2 in caseAI and 4 in caseAI and not 6 in caseJoueur: select = 6 elif 2 in caseAI and 6 in caseAI and not 4 in caseJoueur: select = 4 elif 2 in caseAI and 5 in caseAI and not 8 in caseJoueur: select = 8 elif 2 in caseAI and 8 in caseAI and not 5 in caseJoueur: select = 5 elif 3 in caseAI and 4 in caseAI and not 5 in caseJoueur: select = 5 elif 3 in caseAI and 5 in caseAI and not 4 in caseJoueur: select = 4 elif 3 in caseAI and 6 in caseAI and not 0 in caseJoueur: select = 0 elif 4 in caseAI and 5 in caseAI and not 3 in caseJoueur: select = 3 elif 4 in caseAI and 6 in caseAI and not 2 in caseJoueur: select = 2 elif 4 in caseAI and 8 in caseAI and not 0 in caseJoueur: select = 0 elif 4 in caseAI and 7 in caseAI and not 1 in caseJoueur: select = 1 elif 5 in caseAI and 8 in caseAI and not 2 in caseJoueur: select = 2 elif 6 in caseAI and 7 in caseAI and not 8 in caseJoueur: select = 8 elif 6 in caseAI and 8 in caseAI and not 7 in caseJoueur: select = 7 elif 7 in caseAI and 8 in caseAI and not 6 in caseJoueur: select = 6 #sinon l'AI vérifie s'il risque de perdre elif 0 in caseJoueur and 1 in caseJoueur and not 2 in caseAI: select = 2 elif 0 in caseJoueur and 2 in caseJoueur and not 1 in caseAI: select = 1 elif 0 in caseJoueur and 3 in caseJoueur and not 6 in caseAI: select = 6 elif 0 in caseJoueur and 6 in caseJoueur and not 3 in caseAI: select = 3 elif 0 in caseJoueur and 4 in caseJoueur and not 8 in caseAI: select = 8 elif 0 in caseJoueur and 8 in caseJoueur and not 4 in caseAI: select = 4 elif 1 in caseJoueur and 2 in caseJoueur and not 0 in caseAI: select = 0 elif 1 in caseJoueur and 4 in caseJoueur and not 7 in caseAI: select = 7 elif 1 in caseJoueur and 7 in caseJoueur and not 4 in caseAI: select = 4 elif 2 in caseJoueur and 5 in caseJoueur and not 8 in caseAI: select = 8 elif 2 in caseJoueur and 8 in caseJoueur and not 5 in caseAI: select = 5 elif 2 in caseJoueur and 4 in caseJoueur and not 6 in caseAI: select = 6 elif 2 in caseJoueur and 6 in caseJoueur and not 4 in caseAI: select = 4 elif 3 in caseJoueur and 4 in caseJoueur and not 5 in caseAI: select = 5 elif 3 in caseJoueur and 5 in caseJoueur and not 4 in caseAI: select = 4 elif 3 in caseJoueur and 6 in caseJoueur and not 0 in caseAI: select = 0 elif 4 in caseJoueur and 5 in caseJoueur and not 3 in caseAI: select = 3 elif 4 in caseJoueur and 7 in caseJoueur and not 1 in caseAI: select = 1 elif 4 in caseJoueur and 6 in caseJoueur and not 2 in caseAI: select = 2 elif 4 in caseJoueur and 8 in caseJoueur and not 0 in caseAI: select = 0 elif 5 in caseJoueur and 8 in caseJoueur and not 2 in caseAI: select = 2 elif 6 in caseJoueur and 7 in caseJoueur and not 8 in caseAI: select = 8 elif 6 in caseJoueur and 8 in caseJoueur and not 7 in caseAI: select = 7 elif 7 in caseJoueur and 8 in caseJoueur and not 6 in caseAI: select = 6 #si aucun des cas est vrai, alors l'AI place son pion n'importe où else: choix = [0, 1, 2, 3, 4, 5, 6, 7, 8] for case in caseAI: choix.remove(case) for case in caseJoueur: choix.remove(case) select = rd.choice(choix) #conversion de la case en adresse a = 0 for y in range(50, 349, 100): for x in range(0, 299, 100): if select == a: return (x, y) else: a += 1
Merci d'avance a tous
P.S. Je n'ai pas mis tout le code du jeu pour faire simple, mais je peux le mettre si ça peux aider avec les images et tous, vive l'open source
Partager