Example #1
0
    def brain(self):
        shooter = Shooter()
        gameOver = False
        self.ships = self.getShips(self.amountOfShips)  #places ships
        print(self.allShipCoordinates)
        while (not gameOver):
            #Bots turn
            print("My turn!")
            if not shooter.currentTarget:  # if currentTarget is empty, meaning that ship hasn't been located
                coords = shooter.getRandomCoords()
                self.sendShootingCoordinatesToOpponent(coords)
                print("HIT, MISS OR SUNK?")
                answer = self.getOpponentResponse()
                if answer == "MISS":
                    shooter.missed.append(coords)
                elif answer == "HIT":
                    shooter.hits.append(coords)
                    shooter.currentTarget.append(coords)
                elif answer == "SUNK":
                    self.enemyShipsSunk += 1
                    shooter.hits.append(coords)
                    shooter.reset()
            else:  #if currentTarget isn't empty, meaning that ship has been located
                coords = shooter.getTargetCoords(shooter.shootingDirection,
                                                 shooter.currentTarget[-1])
                self.sendShootingCoordinatesToOpponent(coords)
                answer = self.getOpponentResponse()
                if answer == "MISS" and len(
                        shooter.currentTarget
                ) == 1:  #if there's only been one hit, meaning that the bot is finding out if the ship lays vertically or not
                    shooter.missed.append(coords)
                    shooter.changeDirection()  #update shootingDirection
                elif answer == "MISS" and len(
                        shooter.currentTarget
                ) > 1:  #if there's been more than one hit, meaning that the bot is shooting directly at the ship and has now found the end of it
                    if shooter.endOfShipReached == True:  #if the end of the ship has already been reached, then this is the second time, and something is wrong, since both ends have been found but there's been no SUNK
                        print(
                            "END OF SHIP HAS BEEN FOUND TWICE, SOMETHING IS WRONG!!!!!!!!"
                        )
                        sys.exit()
                    else:  #if this is the first time the end of the ship has been reached
                        shooter.endOfShipReached = True
                    shooter.missed.append(coords)
                    shooter.currentTarget = list(
                        reversed(shooter.currentTarget)
                    )  #reverse currentTarget so that next time the bot will shoot from the other end
                    shooter.reverseDirection(
                    )  #reverse the shooting direction so that next time the bot will shoot in the other direction
                elif answer == "HIT":
                    shooter.hits.append(coords)
                    shooter.currentTarget.append(coords)
                elif answer == "SUNK":
                    self.enemyShipsSunk += 1
                    shooter.hits.append(coords)
                    shooter.reset()
            #Bots turn ends, check for game over
            gameOver = self.isGameOver()
            if gameOver == True:
                print("I WIN!")
                break
            #Opponents turn
            print("Your turn. Send coords: x,y")
            opcoords = self.getOpponentCoords()
            botAnswer = self.hitMissOrSunk(opcoords)
            if botAnswer == "SUNK":
                self.myShipsSunk += 1
            print(botAnswer)
            #Opponents turn end, check for game over
            gameOver = self.isGameOver()
            if gameOver == True:
                print("I LOSE!")
                break

        print("GAME IS OVER")