def ui_place_user_destroyer(self):
     print("Place your destroyer")
     letter = {'A' : 0 , 'B' : 1, 'C' : 2, 'D' : 3, 'E' : 4, 'F' : 5, 'G' : 6, 'H' : 7}
     try:
         l = input("Give the x coordinate of the head of the destroyer: ")
         y = int(input("Give the y coordinate of the head of the destroyer: "))
         dir = input("Give the direction of the destroyer:")
         x = letter[l]
         p = Point(x, y - 1)
         error = self.__game.user_place_destroyer(p, dir)
         if p.outside():
             print("The head of the battleship is outside of the board")
             return 1#EXIT_FAILURE
         if dir != 'up' and dir != 'down' and dir != 'left' and dir != "right":
             print("The direction must be up, down, left, or right")
             return 1#EXIT_FAILURE
         if error == 1:
             print("Cannot place a destroyer there!")
             return error
         
         return 0 #EXIT_SUCCESS
     except ValueError:
         print("The coordinates must be integers!")
         return 1 #EXIT_FAILURE
     except KeyError:
         print ("The x coordinate must be a capital letter from A to H!")
         return 1#EXIT_FAILURE
 def ui_user_move(self):
     print("Make your move")
     letter = {'A' : 0 , 'B' : 1, 'C' : 2, 'D' : 3, 'E' : 4, 'F' : 5, 'G' : 6, 'H' : 7}
     try:
         l = input("Give the x coordinate: ")
         y = int(input("Give the y coordinate: "))
         if len(l) > 1:
             print("Invalid move! The x coordinate must be a capital letter from A to H!")
             return 1 #EXIT_FAILURE
         x = letter[l]
         p = Point(x, y - 1)
         ret = self.__game.user_makes_move(p)
         if ret == 1:
             print("Invalid move. The coordinates are out of range")
             return 1#EXIT_FAILURE
         print(ret)
         return 0 #EXIT_SUCCESS
     except ValueError:
         print("Invalid move! The y coordinate must be an integer")
         return 1 #EXIT_FAILURE
     except KeyError:
         print ("Invalid move! The x coordinate must be a capital letter from A to H!")
         return 1 #EXIT_FAILURE
     except IndexError:
         print ("Invalid move! The y coordinate must be a natural number from 1 to 8")
         return 1 #EXIT_FAILURE
Example #3
0
    def computer_makes_inteligent_move(self):
        #
        directions = [(1, 0), (-1, 0), (0, -1), (0, 1)]
        for i in range(8):
            for j in range(8):
                if self.__computer_help_board[i][j] == 'X':
                    for it in range(4):
                        x = i + directions[it][0]
                        y = j + directions[it][1]
                        p = Point(x, y)
                        if p.outside(
                        ) == False and self.__computer_help_board[x][y] == '~':
                            if self.__player_board[x][y] == 'H':
                                self.__computer_help_board[x][y] = 'X'
                                self.__player_board[x][y] = 'X'
                                return (x, y, "Hit!")
                            else:
                                self.__computer_help_board[x][y] = 'O'
                                self.__player_board = 'O'
                                return (x, y, "Miss!")

        return self.computer_makes_move()
Example #4
0
    def computer_place_destroyer(self):
        numbers = [0, 1, 2, 3, 4, 5, 6, 7]
        directions = ["up", "down", "left", "right"]
        x = random.choice(numbers)
        y = random.choice(numbers)
        dir = random.choice(directions)
        head = Point(x, y)
        if dir == "up":
            if head.get_x() > 0:
                i = head.get_x()
                j = head.get_y()
                if self.__computer_board[i][
                        j] == '~' and self.__computer_board[i - 1][j] == '~':
                    self.__computer_board[i][j] = 'H'
                    self.__computer_board[i - 1][j] = 'H'
                    return 0  #EXIT_SUCCESS
            return 1  #EXIT_FAILURE

        elif dir == "down":
            if head.get_x() < 7:
                i = head.get_x()
                j = head.get_y()
                if self.__computer_board[i][
                        j] == '~' and self.__computer_board[i + 1][j] == '~':
                    self.__computer_board[i][j] = 'H'
                    self.__computer_board[i + 1][j] = 'H'
                    return 0  #EXIT_SUCCESS
            return 1  #EXIT_FAILURE

        elif dir == "left":
            if head.get_y() > 0:
                i = head.get_x()
                j = head.get_y()
                if self.__computer_board[i][
                        j] == '~' and self.__computer_board[i][j - 1] == '~':
                    self.__computer_board[i][j] = 'H'
                    self.__computer_board[i][j - 1] = 'H'
                    return 0  #EXIT_SUCCESS
            return 1  #EXIT_FAILURE

        elif dir == "right":
            if head.get_y() < 7:
                i = head.get_x()
                j = head.get_y()
                if self.__computer_board[i][
                        j] == '~' and self.__computer_board[i][j + 1] == '~':
                    self.__computer_board[i][j] = 'H'
                    self.__computer_board[i][j + 1] = 'H'
                    return 0  #EXIT_SUCCESS
            return 1  #EXIT_FAILURE

        return 1  #EXIT_FAILURE