Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class GridPos:
- def __init__(self, x, y):
- """
- This class has the each position on the grid, it also tells whether
- a position has been guessed or whether a position has a boat on it
- or not.
- Parameters:
- x is the x coordinate of the boat
- y is the y coordinate of the boat
- Returns: The type of boat
- Pre-condition: The given x, y variables are on the grid
- Post-condition: The boats have been set in their proper positions
- and .'s have been placed in all other spots on the grid
- """
- self._cord = (x, y)
- self._boat = '.'
- self._guess = False
- def set_boats(self, boat):
- """
- Sets the boat parameter equal to self._boat:
- Parameters:
- boat is the type of boat either: A, B, D, S, or P
- Returns: None
- Pre-condition: boat is either A, B, D, S, or P
- Post-condition: self._boat is equal to the boat parameter
- """
- self._boat = boat
- def get_boat(self):
- '''boat getter'''
- return self._boat
- def __str__(self):
- '''returns the boat'''
- return self._boat
- def guess_upgrader(self):
- """
- Determines whether a position has been guessed or not
- Parameters:
- None
- Returns: True or False
- Pre-condition: The guess has a value of either True or False
- Post-condition: The guess gets changed to True, marking it has being
- guessed already
- """
- self._guess += True
- def get_guess(self):
- '''Guess getter'''
- return self._guess
- def get_position(self):
- '''Position getter'''
- return self._cord
- class Board:
- def __init__(self):
- """
- This class creates the board, places the ships and manages the health
- of each ship on the board.
- Parameters:
- self._grid is the entire grid
- self._health is the health of each ship at a given point
- self._sinks is how many ships have been sunk, once at 5
- the game ends
- Returns: A string representation of the grid
- Post-condition: The board has been created with all the ships placed
- in their appropriate position
- """
- self._grid = {}
- self._health = {'A':5,'B':4,'S':3,'D':3,'P':2}
- self._sinks = 0
- def get_health(self):
- '''boat health getter'''
- return self._health
- def __str__(self):
- '''returns the grid'''
- return str(self._grid)
- def create_board(self):
- """
- This function creates a 10x10 board for the game
- Parameters:
- None
- Returns: None
- Post-condition: A 10x10 grid has been created with a tuple as
- the values of the coordinates.
- """
- for i in range(9, -1, -1):
- for j in range(10):
- spot = (i, j)
- self._grid[spot] = GridPos(i, j)
- def place_ships(self, line, error_line):
- """
- This function places the ships in their spots or calls an error while
- placing the boats
- Parameters:
- Line consists of the ship and its coordinates.
- Error_line consists of the same values but in the proper form for
- exiting.
- Returns: None
- Pre-condition: All the ships are of valid length and there are only
- 5 ships, each being a different type.
- Post-condition: The ships have been placed in their proper spots or
- an overlapping error occurs, ending the program
- """
- x1_y1 = line[1],line[2]
- x2_y2 = line[3],line[4]
- if x1_y1[0] == x2_y2[0]:
- for i in range(min(x1_y1[1], x2_y2[1]), max(x1_y1[1], x2_y2[1])+1):
- if self._grid[x1_y1[0], i].get_boat() == '.':
- self._grid[x1_y1[0], i].set_boats(line[0])
- else:
- print("ERROR: overlapping ship: " + error_line)
- exit(1)
- elif x1_y1[1] == x2_y2[1]:
- for i in range(min(x1_y1[0], x2_y2[0]), max(x1_y1[0], x2_y2[0])+1):
- if self._grid[i, x1_y1[1]].get_boat() == '.':
- self._grid[i, x1_y1[1]].set_boats(line[0])
- else:
- print("ERROR: overlapping ship: " + error_line)
- exit(1)
- def process_guesses(self):
- """
- This function opens the guesses and displays whether they hit, missed,
- hit the same area again, or missed the area again
- Parameters:
- None
- Returns: None
- Pre-condition: The board has been created and all the ships have been placed
- in their proper locations
- Post-condition: All the ships have been sunk and the game is over
- """
- filee = input()
- try:
- file = open(filee)
- except:
- print("ERROR: Could not open file: " + filee)
- exit(1)
- for i in file:
- file_split = i.split()
- if len(file_split) == 0:
- pass
- else:
- guess = int(file_split[0]),int(file_split[1])
- if len(file_split) == 0:
- continue
- elif guess[0] > 9 or guess[0] < 0 or guess[1] > 9 or guess[1] < 0:
- print('illegal guess')
- else:
- if self._grid[guess].get_guess() == True:
- if str(self._grid[guess]) == '.':
- print('miss (again)')
- elif str(self._grid[guess]).isalpha():
- print('hit (again)')
- elif self._grid[guess].get_guess() == False:
- if str(self._grid[guess]) == '.':
- print('miss')
- self._grid[guess].guess_upgrader()
- elif str(self._grid[guess]) in self._health.keys():
- self._health[str(self._grid[guess])] -= 1
- if self._health[str(self._grid[guess])] == 0:
- print("{} sunk".format(str(self._grid[guess])))
- self._sinks += 1
- else:
- print('hit')
- if self._sinks == 5:
- print('all ships sunk: game over')
- self._grid[guess].guess_upgrader()
- class Ship:
- def __init__(self):
- """
- Ship checks the sizes of the ships
- Parameters:
- No parameters
- Returns: None
- Post-condition: The checked ship is the correct length
- """
- self._ship_sizes = {'A':5,'B':4,'S':3,'D':3,'P':2}
- def check_ship_sizes(self, ship):
- '''Boat size getter'''
- return self._ship_sizes[ship]
- def file(boardcall):
- """
- This function opens the file, checks for errors and places the ships
- Parameters:
- boardcall is the initialer for the Board class
- Returns: None
- Post-condition: The ships are a legal size and direction
- """
- places = {}
- all_ships = []
- filee = input()
- try:
- file = open(filee)
- except:
- print("ERROR: Could not open file: " + filee)
- exit(1)
- for j in file:
- i_split = j.split()
- if i_split[0] in all_ships:
- print("ERROR: fleet composition incorrect")
- exit(1)
- else:
- all_ships.append(i_split[0])
- file.close()
- file = open(filee)
- if len(all_ships) != 5:
- print("ERROR: fleet composition incorrect")
- exit(1)
- for i in file:
- error_line = ''.join(i)
- file_split = i.split()
- check(file_split, error_line)
- boat = file_split[0]
- file_split[1] = int(file_split[1])
- file_split[2] = int(file_split[2])
- file_split[3] = int(file_split[3])
- file_split[4] = int(file_split[4])
- shipcall = Ship()
- if file_split[1] == file_split[3]:
- ssize = max(file_split[2], file_split[4]) - min(file_split[2], file_split[4]) + 1
- if ssize != shipcall.check_ship_sizes(boat):
- print('ERROR: incorrect ship size: ' + error_line)
- exit(1)
- else:
- ssize = max(file_split[1], file_split[3]) - min(file_split[1], file_split[3]) + 1
- if ssize != shipcall.check_ship_sizes(boat):
- print('ERROR: incorrect ship size: ' + error_line)
- exit(1)
- boardcall.place_ships(file_split, error_line)
- def check(line, error_line):
- '''This function checks that the pieces are not placed diagonally
- Precondition: the line has 5 values
- Parameters:
- Line consists of the ship and its coordinates.
- Error_line consists of the same values but in the proper form for
- exiting.
- Returns: None
- Post-condition: No errors occured or the program prints and error
- and the line that
- '''
- if (line[1] == line[3]) or (line[2] == line[4]) == True:
- pass
- else:
- print("ERROR: ship not horizontal or vertical: " + error_line)
- exit(1)
- i = 1
- while i < len(line):
- if int(line[i]) > 10:
- print("ERROR: ship out-of-bounds: " + error_line)
- exit(1)
- i += 1
- def main():
- boardcall = Board()
- boardcall.create_board()
- file(boardcall)
- boardcall.process_guesses()
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement