#Skeleton Program for the AQA AS1 Summer 2016 examination #this code should be used in conjunction with the Preliminary Material #written by the AQA AS1 Programmer Team #developed in a Python 3 programming environment #Version Number 1.0 import random def GetRowColumn(): print() Option = "No" if Option == "No": Column = input("Please enter column: ") ValidColumn = ['0','1','2','3','4','5','6','7','8','9'] while Column not in ValidColumn: print ("Not a Valid option. Please try again: ", end="") Column = input() Row = input("Please enter row: ") ValidRow = ['0','1','2','3','4','5','6','7','8','9'] while Row not in ValidRow: print ("Not a Valid option. Please try again: ", end="") Row = input() Column = int(Column) Row = int (Row) print ("Are you shoot you want to shoot at this cell? Y/N") Option = input() return Row, Column def MakePlayerMove(Board, Ships): Row, Column = GetRowColumn() if Board[Row][Column] == "m" or Board[Row][Column] == "h": print("Sorry, you have already shot at the square (" + str(Column) + "," + str(Row) + "). Please try again.") elif Board[Row][Column] == "-": print("Sorry, (" + str(Column) + "," + str(Row) + ") is a miss.") Board[Row][Column] = "m" else: print("Hit at (" + str(Column) + "," + str(Row) + ").") if Board[Row][Column] == "A": print ("You have hit an Aircraft Carrier") elif Board[Row][Column] == "B": print ("You have hit a battleship") elif Board[Row][Column] == "S": print ("You have hit a submarine") elif Board[Row][Column] == "D": print ("You have hit a Destroyer") elif Board[Row][Column] == "P": print ("You have hit a Patrol Boat") elif Board[Row][Column] == "Y": print ("You have hit a Yacht") Board[Row][Column] = "h" def SetUpBoard(): Board = [] for Row in range(12): BoardRow = [] for Column in range(10): BoardRow.append("-") Board.append(BoardRow) return Board def LoadGame(Filename, Board): BoardFile = open(Filename, "r") for Row in range(12): Line = BoardFile.readline() for Column in range(10): Board[Row][Column] = Line[Column] BoardFile.close() def PlaceRandomShips(Board, Ships): for Ship in Ships: Valid = False while not Valid: Row = random.randint(0, 9) Column = random.randint(0, 9) HorV = random.randint(0, 1) if HorV == 0: Orientation = "v" else: Orientation = "h" Valid = ValidateBoatPosition(Board, Ship, Row, Column, Orientation) print("Computer placing the " + Ship[0]) PlaceShip(Board, Ship, Row, Column, Orientation) def PlaceShip(Board, Ship, Row, Column, Orientation): if Orientation == "v": for Scan in range(Ship[1]): Board[Row + Scan][Column = Ship[0][0] elif Orientation == "h": for Scan in range(Ship[1]): Board[Row][Column + Scan] = Ship[0][0] def ValidateBoatPosition(Board, Ship, Row, Column, Orientation): if Orientation == "v" and Row + Ship[1] > 10: return False elif Orientation == "h" and Column + Ship[1] > 10: return False else: if Orientation == "v": for Scan in range(Ship[1]): if Board[Row + Scan][Column] != "-": return False elif Orientation == "h": for Scan in range(Ship[1]): if Board[Row][Column + Scan] != "-": return False return True def CheckWin(Board): for Row in range(12): for Column in range(10): if Board[Row][Column] in ["A","B","S","D","P","Y"]: return False return True def PrintBoard(Board): print() print("The board looks like this: ") print() print (" ", end="") for Column in range(10): print(" " + str(Column) + " ", end="") print() for Row in range(12): print (str(Row) + " ", end="") for Column in range(10): if Board[Row][Column] == "-": print(" ", end="") elif Board[Row][Column] in ["A","B","S","D","P","Y"]: print(" ", end="") else: print(Board[Row][Column], end="") if Column != 9: print(" | ", end="") print() def DisplayMenu(): print("MAIN MENU") print() print("1. Start new game") print("2. Load training game") print("9. Quit") print() def GetMainMenuChoice(): print("Please enter your choice: ", end="") Choice = input() ValidChoice = ['1','2','9'] while Choice not in ValidChoice: print ("Not a Valid option. Please try again: ", end="") Choice = input() Choice = int(Choice) print() return Choice def PlayGame(Board, Ships,): GameWon = False print ("Please enter your name.") Name = input() Torpedoes = 20 while GameWon == False and Torpedoes != 0: PrintBoard(Board) MakePlayerMove(Board, Ships) Torpedoes = Torpedoes - 1 MovesTaken = 20 - Torpedoes Accuracy = MovesTaken/20 * 100 print ("You have " + str(Torpedoes) + " torpedoes left") print ("You have taken "+ str(MovesTaken)+" moves") print ("Do you want go back to the main menu? Y/N") ValidOption = ['Y','N'] Option = input() if Option not in ValidOption: print ("Not a Valid Option. Please try again: ", end="") else: if Option == 'Y': Menu() else: GameWon = CheckWin(Board) while not GameWon: PrintBoard(Board) MakePlayerMove(Board, Ships) GameWon = CheckWin(Board,) if GameWon: print("All ships sunk - well done " + Name +"!") print("It took you " + MovesTaken) print ("Your accuracy was " + Accuracy +"%") print() def Menu(): TRAININGGAME = "Training.txt" MenuOption = 0 while not MenuOption == 9: Board = SetUpBoard() Ships = [["Aircraft Carrier", 5], ["Battleship", 4], ["Submarine", 3], ["Destroyer", 3], ["Patrol Boat", 2], ["Yacht",1]] DisplayMenu() MenuOption = GetMainMenuChoice() if MenuOption == 1: PlaceRandomShips(Board, Ships) PlayGame(Board,Ships) if MenuOption == 2: LoadGame(TRAININGGAME, Board) PlayGame(Board, Ships) Menu()
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question