Esempio n. 1
0
class Player:
    def __init__(self):
        # Données multijoueur
        self.pseudo = ""
        # Initialisation des éléments de jeu du joueur
        self.gridDef = Grid()
        self.gridAtk = Grid()
        self.boats = []
        self.boats.append(Boat("Porte avion", 5, 1))
        self.boats.append(Boat("Croiseur", 4, 2))
        self.boats.append(Boat("Sous marin", 3, 3))
        self.boats.append(Boat("Contre Torpilleur", 3, 4))
        self.boats.append(Boat("Torpilleur", 2, 5))

    def enterPseudo(self):
        self.pseudo = input("Entrez votre pseudo :")

    def setCoordinatesAtk(self):
        xA = input("Lettre (a-j):")
        y = 0
        while True:
            try:
                y = int(input("Chiffre (0-9):"))
                break
            except ValueError:
                print("Oops!  Ce n'est pas un chiffre.  Réessayez...")
        xA = xA.lower()
        if (len(xA) == 1) & (0 <= y <= 9):
            if ord("a") <= ord(xA) <= ord("j"):
                x = int(ord(xA) - ord("a"))
                return [x, y]
        print("Coordonées invalides ! réessayez s'il vous plait")
        return self.setCoordinatesAtk()

    def setCoordinatesBoat(self, boat):
        xA = input("Lettre (a-j):")
        y = 0
        while True:
            try:
                y = int(input("Chiffre (0-9):"))
                break
            except ValueError:
                print("Oops!  Ce n'est pas un chiffre.  Réessayez...")
        xA = xA.lower()
        if (len(xA) == 1) & (0 <= y <= 9):
            if ord("a") <= ord(xA) <= ord("j"):
                x = int(ord(xA) - ord("a"))
                boat.setCoordonees(x, y)
                return boat

        print("Coordonées invalides ! réessayez s'il vous plait")
        return self.setCoordinatesBoat(boat)

    def setDirection(self):
        direction = input("Spécifiez la direction ( N/S/E/W) :")
        direction.upper()
        if len(direction) == 1:
            if (direction == "N") | (direction == "S") | (direction == "E") | (direction == "W"):
                return direction

        print("Direction invalide ! réessayez")
        self.setDirection()

    def tryPlaceBoat(self, boat):
        boatVerified = self.setCoordinatesBoat(boat)
        retour = self.gridDef.setBoat(boatVerified, self.setDirection())
        if retour == False:
            self.tryPlaceBoat(boat)

    def boatPlacement(self):
        self.gridDef.display()
        for i in self.boats:
            print("Ou voulez vous placer votre " + str(i.getNom()) + " ?")
            self.tryPlaceBoat(i)
            self.gridDef.display()

    def isDead(self):
        count = 0
        for i in self.boats:
            if i.down == True:
                count += 1
        if int(count) == int(len(self.boats)):
            return True
        else:
            return False

    def isTouched(self, x, y):
        return self.gridDef.checkEmpty(x, y)

    def gridDefToJson(self):
        return Encoder().encode(self.gridDef)

    def jsonToGrid(obj):
        if "grid" in obj:
            return Grid(obj["grid"])
        return obj