Commit 07f5446a authored by LARIVIERE Axel's avatar LARIVIERE Axel
Browse files
parents 95491dc5 b5466af5
import sys
from abc import abstractmethod
import time
import numpy as np
import PyQt5
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Game:
......@@ -8,42 +12,183 @@ class Game:
0] # boardGame[0-7] : kewai, boardGame[8] : putahi, 1 : pion joueur 1, 2 : pion joueur 2, 0 : vide
actualPlayer = 1 # 1 for player 1, 2 for player 2
# def __init__(self):
@staticmethod
def movePawn(positionToMove):
def __init__(self, joueur1, joueur2):
self.joueur1 = joueur1
self.joueur2 = joueur2
self.window = Window(self)
self.gameManager()
input()#fin du jeu
def gameManager(self):
while(not self.isEndGame(self.actualPlayer)):
self.window.drawTheScene()
if self.actualPlayer == 1:
self.joueur1.takeDecision(self)
self.actualPlayer = 2
else:
self.joueur2.takeDecision(self)
self.actualPlayer = 1
self.window.drawTheScene()
self.endGame()
def isEndGame(self,nextPlayer): #est ce la fin du jeu? est ce que le prochain joueur peut jouer?
for i in range(9): #pour chaque emplacement de pions
if(self.boardGame[i]==nextPlayer): #si c'est un pion au prochain joueur, peut il se déplacer?
if(self.emptyPlaceToMove(i)!=-1):
return False
return True #si aucun déplacement est possible, fin du jeu
def endGame(self):
if self.actualPlayer==1 :
winner=2
else:
winner=1
print("Player "+str(winner)+" win!")
def movePawn(self, positionToMove):
positionToMove = positionToMove-1 # pour la position dans le tableau
newPosition = Game.emptyPlaceToMove(positionToMove)
newPosition = self.emptyPlaceToMove(positionToMove)
if newPosition == -1: #si aucune place n'a été trouvé ou possible
return False
else : #on bouge le pion, son ancienne case est maintenant vide
Game.boardGame[newPosition] = Game.boardGame[positionToMove]
Game.boardGame[positionToMove] = 0
self.boardGame[newPosition] = self.boardGame[positionToMove]
self.boardGame[positionToMove] = 0
return True
@staticmethod
def emptyPlaceToMove(positionToMove): #-1 if impossible
if not Game.boardGame[positionToMove] == Game.actualPlayer: return -1 #si on essaie de bouger autre chose que un de ses pions : False
if Game.boardGame[8] == 0: # si la place au milieu est libre
def emptyPlaceToMove(self, positionToMove): #-1 if impossible
if not self.boardGame[positionToMove] == self.actualPlayer: return -1 #si on essaie de bouger autre chose que un de ses pions : False
if self.boardGame[8] == 0: # si la place au milieu est libre
# seulement si un point adverse autours du joueur est présent
if (Game.boardGame[(positionToMove+1)%8] == Game.actualPlayer) and (Game.boardGame[(positionToMove-1)%8] == Game.actualPlayer): return -1
if (self.boardGame[(positionToMove+1)%8] == self.actualPlayer) and (self.boardGame[(positionToMove-1)%8] == self.actualPlayer): return -1
return 8
else :
if(positionToMove == 8) : # on bouge le pion du milieu, on cherche la case vide
for x in range(8):
if Game.boardGame[x] == 0:
if self.boardGame[x] == 0:
return x
break
# sinon si il y a une position libre adjacente
if Game.boardGame[(positionToMove + 1) % 8] == 0: return (positionToMove + 1) % 8
if Game.boardGame[(positionToMove - 1) % 8] == 0: return (positionToMove - 1) % 8
if self.boardGame[(positionToMove + 1) % 8] == 0: return (positionToMove + 1) % 8
if self.boardGame[(positionToMove - 1) % 8] == 0: return (positionToMove - 1) % 8
return -1
class Player():
@abstractmethod
def takeDecision(self,game):
pass
class RealPlayer(Player):
def takeDecision(self,game):
print("\nAu tour du joueur "+str(game.actualPlayer)+", a vous de jouer")
print("Voici le plateau : ")
for x in range(9):
print("position "+str(x+1)+" : "+str(game.boardGame[x]))
inputUser=0
impossibleMove=0
while(inputUser==0 or impossibleMove==1):
impossibleMove=0
try:
inputUser = int(input("Quelle position voulez vous bouger ?"))
except ValueError:
inputUser=0
print("Mauvaise saisie, veuillez entrer un nombre numérique")
if(game.movePawn(inputUser) == False):
impossibleMove=1 #le déplacement est impossible
print("Déplacement impossible, veuillez choisir un autre pion")
class Agent(Player):
def takeDecision(self,game):
print("\nAu tour du joueur "+str(game.actualPlayer)+", l'IA joue")
pass #TODO IA
class Window(QMainWindow):
index=0
def __init__(self, game):
super().__init__()
self.game = game
# set the title
self.setWindowTitle("Mutorere")
# setting geometry
self.setGeometry(100, 100, 600, 500)
self.drawTheScene()
# creating a label widget
def drawTheScene(self):
#self.paintEvent()
# show all the widgets
self.update()
self.show()
def paintEvent(self, event):
self.index = 0
self.painter = QPainter(self)#1
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,50,"1")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 50, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350,100,"2")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(350, 100, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(400,200,"3")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(400, 200, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(350,300,"4")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(350, 300, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,350,"5")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 350, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150,300,"6")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(150, 300, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(100,200,"7")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(100, 200, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(150,100,"8")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(150, 100, 70, 70)
self.painter.setPen(QPen(Qt.black, 4, Qt.SolidLine))
self.painter.drawText(250,200,"9")
self.painter.setBrush(QBrush(self.chooseColor(), Qt.SolidPattern))
self.painter.drawEllipse(250, 200, 70, 70)
self.painter.end()
#self.painter.drawEllipse(180, 50, 100, 100)
def chooseColor(self):
self.index+=1
if(self.game.boardGame[self.index-1]==0):
return Qt.white
if(self.game.boardGame[self.index-1]==1):
return Qt.green
if(self.game.boardGame[self.index-1]==2):
return Qt.red
if __name__ == "__main__":
print(Game.movePawn(4))
print(Game.boardGame[(0 - 1) % 8] == Game.actualPlayer)
Game.actualPlayer = 2
print(Game.movePawn(5))
Game.actualPlayer = 1
print(Game.movePawn(9))
print(Game.boardGame)
\ No newline at end of file
app = QApplication(sys.argv)
Game(RealPlayer(),RealPlayer())
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment