Reputation: 1
I am developing a TicTacToe gamebetween humin and machine(using algorithm minmax) with PyQt5 contain: def bouton (to create a grid),def vinquer(to chek the winner)def minmax and machine , Ithink i have a problem with the "vinqueur"(winner)func. This func always returns zero, even if one of the players wins. help me please to resolv this issue.
%tb
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Tic_tac_toe(QMainWindow):
def __init__(self):
super().__init__()
self.setGeometry(300,100,400,600)
self.a=3
self.Boutton()
self.show()
def Boutton(self):
self.grille=[]
self.joueur = -1
for _ in range(self.a):
ligne = []
for _ in range(self.a):
ligne.append((QPushButton(self)))
self.grille.append(ligne)
for i in range(self.a):
for j in range(self.a):
self.grille[i][j].setGeometry(90*i + 20,90*j + 20,80, 80)
# setting font to the button
self.grille[i][j].setFont(QFont(QFont('Times', 17)))
# adding action
self.grille[i][j].clicked.connect(self.Actions)
def vinqueur(self):
# checking if any row crossed
for i in range(3):
if self.grille[0][i].text() == self.grille[1][i].text() \
and self.grille[0][i].text() == self.grille[2][i].text() \
and self.grille[0][i].text() != "":
if self.grille[0][i].text() == "X":return -1
if self.grille[0][i].text() == "O":return 1
# checking if any column crossed
for i in range(3):
if self.grille[i][0].text() == self.grille[i][1].text() \
and self.grille[i][0].text() == self.grille[i][2].text() \
and self.grille[i][0].text() != "":
if self.grille[i][0].text() == "X":return -1
if self.grille[i][0].text() == "O":return 1
# checking if diagonal crossed
if self.grille[0][0].text() == self.grille[1][1].text() \
and self.grille[0][0].text() == self.grille[2][2].text() \
and self.grille[0][0].text() != "":
if self.grille[0][0].text() == "X":return -1
if self.grille[0][0].text() == "O":return 1
# if other diagonal is crossed
if self.grille[0][2].text() == self.grille[1][1].text() \
and self.grille[1][1].text() == self.grille[2][0].text() \
and self.grille[0][2].text() != "":
if self.grille[0][2].text() == "X":return -1
if self.grille[0][2].text() == "O":return 1
return 0
def Actions(self):
if self.joueur==-1:
button = self.sender()
button.setEnabled(False)
button.setText("X")
self.joueur=1
if self.joueur==1:
self.machine()
self.joueur=-1
#self.tour = self.Tour() à vérifier si tu doit la remettre ou non
# setting text to the label
#self.label.setText(message)
def mini_max(self, joueur):
x=self.vinqueur()
if x != 0:
return x*joueur
x,y=-1,-1
meilleur_score=-10
for i in range(self.a):
for j in range(self.a):
if self.grille[i][j].text() == "":
if joueur==(-1):
self.grille[i][j].setText("x")
else:self.grille[i][j].setText("o")
score = -self.mini_max(joueur*-1)
if score>meilleur_score:
meilleur_score = score
x,y=i,j
self.grille[i][j].setText("")
if (x,y)==(-1,-1): return 0
return meilleur_score
def machine(self):
meilleur_score = -10
x, y = -1, -1
for i in range(self.a):
for j in range(self.a):
if self.grille[i][j].text() == "":
self.grille[i][j].setText("O")
score = -self.mini_max(-1)
self.grille[i][j].setText("")
if score > meilleur_score:
meilleur_score = score
x, y = i, j
self.grille[x][y].setText("O")
self.grille[x][y].setEnabled(False)
App = QApplication(sys.argv)
# create the instance of our Window
tic_Tac_Toe = Tic_tac_toe()
# start the app
sys.exit(App.exec())
Upvotes: 0
Views: 61