from abc import ABCMeta, abstractmethod from random import randint class Board(): def display(self, number): firstLine = "-" for c in range(self.__columns): if c < 9: firstLine += ("| " + str(c+1) + " ") else: firstLine += ("|" + str(c+1) + " ") firstLine += "|" print(firstLine) for r in range(self.__rows): print(str(chr(r+65)), end='') for x in self.__board[r]: if self.__playerNumber != number and x == "S": y = "~" else: y = x print("| " + y + " ", end="") print("|") def placeShip(self, size, number, player="CPU"): while True: columnSet = False rowSet = False orientationSet = False if player == "Human": self.display(number) while not columnSet: if player == "Human": try: column = int(input("Enter the column where you would like to position the ship (1-" + str(self.__columns) + "):")) print() if column >= 1 and column <= self.__columns: column = column - 1 columnSet = True else: print("That column doesn't exist. Please try again.") except: print("That column doesn't exist. Please try again.") else: column = randint(0, self.__columns -1) columnSet = True while not rowSet: if player == "Human": try: row = ord(input("Enter the row where you would like to position the ship (A-" + str(chr(self.__rows+65)) + "):").upper()) print() if row >= 65 and row <= self.__rows+65: row = row-65 rowSet = True else: print("That row doesn't exist. Please try again.") except: print("That row doesn't exist. Please try again.") else: row = randint(0, self.__rows -1) rowSet = True validPos = True while not orientationSet: if player == "Human": orientation = input("Do you want to place your ship vertically down or horizontally to the right(v/h)?:") print() else: if randint(0,1) == 0: orientation = "v" else: orientation = "h" if orientation.lower() == "v" or orientation.lower() == "vertical": orientationSet = True try: for r in range(row, row + size): if self.__board[r][column] == "S": validPos = False if validPos == True: for r in range(row, row + size): self.__board[r][column] = "S" return except: pass elif orientation.lower() == "h" or orientation.lower() == "horizontal": orientationSet = True try: for c in range(column, column + size): if self.__board[row][c] == "S": validPos = False if validPos == True: for c in range(column, column + size): self.__board[row][c] = "S" return except: pass else: print("You can only position your ship vertically down (v) or horizontally to the right(h)!") if player == "Human": print("You can't position the ship like that! Try again (The ship is " + size + "tiles long):") class Player(metaclass=ABCMeta): class HumanPlayer(): def _placeShips(self): print("Position your carrier (5 tiles long):") self._playerBoard.placeShip(5, self._playerNumber, "Human") print("Your carrier is in position!") print("Position your battleship (4 tiles long):") self._playerBoard.placeShip(4, self._playerNumber, "Human") print("Your battleship is in position!") print("Position your cruiser (3 tiles long):") self._playerBoard.placeShip(3, self._playerNumber, "Human") print("Your cruiser is in position!") print("Position your submarine (3 tiles long):") self._playerBoard.placeShip(3, self._playerNumber, "Human") print("Your submarine is in position!") print("Position your destroyer (2 tiles long):") self._playerBoard.placeShip(2, self._playerNumber, "Human") print("Your destroyer is in position!") print() class ComputerPlayer(): def _placeShips(self): print("The computer is positioning its ships...") self._playerBoard.placeShip(5, self._playerNumber) self._playerBoard.placeShip(4, self._playerNumber) self._playerBoard.placeShip(3, self._playerNumber) self._playerBoard.placeShip(3, self._playerNumber) self._playerBoard.placeShip(2, self._playerNumber) print("The computer has positioned its ships!") def main(): widthSet = False heightSet = False while not widthSet: try: width = int(input("Enter the width of your game board (10-26):")) print() if width >= 10 and width <= 26: widthSet = True else: print("The width must be an integer from 10-26. Please try again.") except: print("The width must be an integer from 10-26. Please try again.") while not heightSet: try: height = int(input("Enter the height of your game board (10-26):")) print() if height >= 10 and height <= 26: heightSet = True else: print("The height must be an integer from 10-26. Please try again.") except: print("The height must be an integer from 10-26. Please try again.") player1 = HumanPlayer(1, width, height) player2 = ComputerPlayer(2, width, height) board1 = player1.getBoard() board2 = player2.getBoard() while True: print() print("It's your turn:") makeShot = False while not makeShot: result = input("Would you like to take a shot(1), look at the computer's board(2), or look at your board(3)?:") print() if result == "1": makeShot = True elif result == "2": board2.display(player1.getNumber()) elif result == "3": board1.display(player1.getNumber()) else: print("That is not a valid option!") board2.display(player1.getNumber()) player1.takeShot(board2) if board2.checkWinner(): print() board2.display(player1.getNumber()) input("You have won!") return print() print("It's the computer's turn:") player2.takeShot(board1) board1.display(player1.getNumber()) if board1.checkWinner(): print() board1.display(player1.getNumber()) input("You have lost!") return if __name__ == '__main__': main()