- 11th Jul 2024
- 17:59 pm
In this assignment a number of online references exist that describe the game and the summary below should provide you with enough information to accomplish the final.
- The game begins will each player having a board with NxM grid locations on it and a number of ship pieces to place.
- The ships are placed by each player in secret onto the board. Each player never sees the others board during the game.
- Each ship spans several grid spaces and can only be placed on the board either within a column or within a row.
- The number of grid spaces on the board a ship spans is the number of hits on it required to sink the ship.
- Once the ship pieces are placed by each player the game begins.
- Each player is allowed to select a location on the board as a guess for the location of an enemy ship.
- Each guess is recorded as a hit or miss is noted via a red (hit) or white (miss) marker at the location guessed.
- Each player gets a turn to guess alternating from player 1 to player 2....
- Each player builds up a "mapping" of the board with misses and hits in an attempt to locate and destroy all of the other players ships.
- The game continues until all of the ships for one of the players have received their maximum number of hits.
- In addition when a game is started the existing high score should be displayed. The high score will be the game in which the winning player has the lowest number of guesses.
- You should also develop a test concept and test cases for your application to determine if it functions correctly.
- You should document you design and testing in a word document.
When you are complete please submit all of your Python Modules along with a document showing your application in operation detailing the features noted above and how you verified or tested it.
Python Implementation Of Battleship - Get Assignment Solution
Please note that this is a sample assignment solved by our Python Programmers. These solutions are intended to be used for research and reference purposes only. If you can learn any concepts by going through the reports and code, then our Python Tutors would be very happy.
- Option 1 - To download the complete solution along with Code, Report and screenshots - Please visit our Python Assignment Sample Solution page
- Option 2 - Reach out to our Python Tutors to get online tutoring related to this assignment and get your doubts cleared
- Option 3 - You can check the partial solution for this assignment in this blog below
Free Assignment Solution - Python Implementation Of Battleship
from os import system, name
import copy
"""
GAME INITIATION
Create empty player board
"""
def reset_board():
empty_board = [["-" for spot in range(26)] for col in range(10)]
return empty_board
"""
VIEW BOARD
prints out a player's board to the console
"""
def view_board(player):
letters = [chr(65 + a) for a in range(26)]
numbers = [str(a) for a in range(1,11)]
print(" ", " ".join(numbers))
count = 0
for x in player:
print(letters[count], "", " ".join(x))
count += 1
print("\nKey: '-' = empty spot, 'X' = a hit ship, 'O' = a missed guess")
"""
CHECK VALIDITY OF SETTING SPOT
"""
def check_coordinates(player, coordinates, setting_board):
#check for valid coordinates
try:
row = ord(coordinates[0].upper()) - 65
col = int(coordinates[1:]) - 1
except:
print("Incorrect input format of location")
return False, None, None
#check if location is valid
if row >= 26 or row < 0 or col >= 10 or col < 0:
print("Invalid row or column")
return False, None, None
if setting_board:
if player[row][col] != '-':
print("Location taken")
return False, None, None
return True, row, col
def check_valid_spot(player, coordinates, direction, ship):
#Convert spot to position on board and check if valid
valid_coordinates, row, col = check_coordinates(player, coordinates, True)
if not valid_coordinates:
return False
#check if direction is valid
direction_list = ['LEFT', 'RIGHT', 'UP', 'DOWN', 'U', 'D', 'L', 'R']
direction = direction.upper()
if direction.upper() not in direction_list:
print("Incorrect format of direction")
return False
for length in range(fleet[ship][0]):
try:
#check up
if direction == 'RIGHT' or direction == 'R':
if player[row][col + length] != '-':
print("coordinates out of bounds")
return False
#check down
if direction == 'LEFT' or direction == 'L':
if player[row][col - length] != '-' or col - length < 0:
print("coordinates out of bounds")
return False
#check left
if direction == 'UP' or direction == 'U':
if player[row - length][col] != '-' or row - length < 0:
print("coordinates out of bounds")
return False
#check right
if direction == 'DOWN' or direction == 'D':
if player[row + length][col] != '-':
print("coordinates out of bounds")
return False
except:
print("coordinates out of bounds")
return False
for length in range(fleet[ship][0]):
#add ship to board in up direction
if direction == 'RIGHT' or direction == 'R':
player[row][col + length] = ship
#add ship to board in down direction
if direction == 'LEFT' or direction == 'L':
player[row][col - length] = ship
#add ship to board in left direction
if direction == 'UP' or direction == 'U':
player[row - length][col] = ship
#add ship to board in right direction
if direction == 'DOWN' or direction == 'D':
player[row + length][col] = ship
return True
"""
SET FLEET
"""
def set_fleet(player):
for x in fleet:
spot = input("Pick coordinates for your " + fleet[x][1] + \
" {" + str(fleet[x][0])+ "} units long (i.e. 'A5' or 'D8'): ")
direction = input("Select a direction for your " + fleet[x][1] + "('up', 'down', 'left', 'right'): ")
while not check_valid_spot(player, spot, direction, x):
print("Please try again.")
spot = input("Pick coordinates for your " + fleet[x][1] + \
" {" + str(fleet[x][0])+"} units long (i.e. 'A5' or 'D8'): ")
direction = input("Select a direction for your " + fleet[x][1] + " ('up', 'down', 'left', 'right'): ")
view_board(player)
return
"""
CLEAR SCREEN
"""
# define our clear function
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
"""
CHECK PLAYER'S GUESS
"""
def guess_location(opponent, player_guesses, coordinates, opponent_fleet):
valid_coordinates, row, col = check_coordinates(opponent, coordinates, False)
#check for valid guess
while not valid_coordinates:
coordinates = input("Please enter valid coordinates: ")
valid_coordinates, row, col = check_coordinates(opponent, coordinates, False)
#picked an already guessed spot
while player_guesses[row][col] == 'X' or player_guesses[row][col] == '.':
coordinates = input("You've already guess that spot. Try again: ")
valid_coordinates, row, col = check_coordinates(opponent, coordinates, False)
while not valid_coordinates:
coordinates = input("Please enter valid coordinates: ")
valid_coordinates, row, col = check_coordinates(opponent, coordinates, False)
#picked a ship's location
if opponent[row][col] in opponent_fleet:
player_guesses[row][col] = 'X'
ship = opponent[row][col]
opponent_fleet[ship][0] = opponent_fleet[ship][0] - 1
print("Hit!")
if opponent_fleet[ship][0] == 0:
print("You sunk your opponent's", opponent_fleet[ship][1])
return 1
#picked an empty spot
else:
player_guesses[row][col] = 'O'
print("You missed...")
return 0
"""
START GAME
"""
#defined here in case we want to add/substract ships to game
FLEET = ({'a' : [9, "Aircraft Carrier"],
'b' : [7, "Battleship"],
'd' : [3, "Destroyer"],
'c' : [5, "Cruiser"]
},)
while True:
### Initiate new game
winning_hit_count = 0 #number of hits it takes to win
fleet = FLEET[0]
for i in fleet:
winning_hit_count += fleet[i][0]
player1_score = 0
player2_score = 0
player1, player2, = reset_board(), reset_board()
player1_guesses, player2_guesses = reset_board(), reset_board()
player1_fleet = copy.deepcopy(fleet)
player2_fleet = copy.deepcopy(fleet)
### -----------------
print("Player 1, set your board.")
view_board(player1)
set_fleet(player1)
clear()
print()
view_board(player2)
print("Player 2, set your board.")
set_fleet(player2)
clear()
while True:
print()
print("***PLAYER 1's TURN***")
view_board(player1_guesses)
coordinates = input("Player 1's turn. Guess your opponent's coordinates (i.e. 'A5' or 'D8'): ")
player1_score += guess_location(player2, player1_guesses, coordinates, player2_fleet)
if player1_score == winning_hit_count:
print("Player 1 wins!")
break
print()
print("***PLAYER 2's TURN***")
view_board(player2_guesses)
coordinates = input("Player 2's turn. Guess your opponent's coordinates (i.e. 'A5' or 'D8'): ")
player2_score += guess_location(player1, player2_guesses, coordinates, player1_fleet)
if player2_score == winning_hit_count:
view_board(player1)
print("Player 2 wins!")
break
print("Player 1's board: ")
view_board(player1)
print()
print("Player 2's board: ")
view_board(player2)
print()
f = open("player1.txt", "a")
f.write(player1_score)
f.close()
f = open("player2.txt", "a")
f.write(player2_score)
f.close()
restart = input("Play again? (Y/N)")
if restart not in ['Y', 'y', 'Yes', 'yes', 'YES']:
break
print("Thanks for playing!")
Get the best Python Implementation Of Battleship assignment help and tutoring services from our experts now!
About The Author - Ria Sharma
Ria Sharma is a passionate software developer with a focus on creating interactive and engaging applications. In this assignment, she provides a comprehensive guide on implementing a classic battleship game in Python. The game features a grid-based board setup, ship placement, turn-based guessing, and hit/miss tracking. Ria emphasizes the importance of thorough testing and documentation, ensuring the game's functionality and user experience.