***THIS HAS TO BE CODED IN C#*** Module 7&8 Programming Assignment – OO Design a
ID: 3593334 • Letter: #
Question
***THIS HAS TO BE CODED IN C#***
Module 7&8 Programming Assignment – OO Design and implementation (50 points)
Our Battleship game needs to store a set of ships.
Create a new class called Ships.
Ships should have the following properties:
A way to store a collection of the Ship class from last week.
If you do not have working code from last week, please ask your instructor.
Does the Ships class represent the IS-A (inheritance) or the HAS-A (composition) relationship? Design your class accordingly. Use suitable generic collection classes.
void Add(Ship)
This is the method that allows you to add a ship.
The add method should validate that no ships are overlapping. How you do this is up to you to design. Feel free to discuss it in the forums but do not post code.
If there is a collision this method should throw a suitable exception.
void Clear()
This is the method that allows you to reset the collection.
bool SunkMyBattleship {get; private set}
This readonly property returns true if the battleship has been sunk.
The private set part is optional depending on how you implement it. But the property should be readonly to users of the class.
bool Attack(int x, int y)
This is the method that attacks the collection of ships and marks any positions as hit.
This should also mark the ship as Sunk if all positions are hit.
The method should return an indication of a hit (true) or a miss (false).
Your attack method should validate that x and y are positive integers and throw a suitable exception if they are not.
You should choose the correct types and access modifiers for each type.
You may add as much code and data structures as you like to fulfill all of the requirements.
Create a test program that creates a Ships collection and populates it with some ships. You may hard-code positions although your instructor will test your code with different ones so make sure to test your code thoroughly.
The test code should ask the user for X, Y positions and attack the ship collection until the battleship is hit.
There is no need to create any UI but it might help you test if you do. ;-) Your program does not have to look like an actual Battleship game. Remember this is purely code to help you test your Ships data structure.
Explanation / Answer
from random import randint board = [] for x in range(6): board.append(["O"] * 6) def print_board(board): for row in board: print " ".join(row) print "Let's play Battleship!" print_board(board) def random_row(board): return randint(0, len(board) - 1) def random_col(board): return randint(0, len(board[0]) - 1) #First ship ship_row = random_row(board) ship_col = random_col(board) #Creating the Second ship(Tried by defining another random_row1 and randow_col1 function) ship_row1=random_row(board) ship_col1=random_col(board) if ship_row1==ship_row and ship_col1==ship_col: ship_row1=randow_row(board) ship_col1=random_col(board) elif ship_col1==ship_col: ship_col1=random_col(board) elif ship_row1==ship_row: ship_row1=random_row(board) else: ship_row1=random_row(board) print ship_row, ship_col print ship_row1, ship_col1 # Everything from here on should go in your for loop! # Be sure to indent four spaces! for turn in range(4): print turn guess_row = int(raw_input("Guess Row:")) guess_col = int(raw_input("Guess Col:")) if board[ship_row][ship_col]=="H" and board[ship_row1][ship_col1]=="H" : print "You win" break elif (guess_row == ship_row and guess_col == ship_col) or (guess_row==ship_row1 and guess_col==ship_col1): print "Congratulations! You sunk one of my battleship!" board[guess_row][guess_col]="H" else: if (guess_row < 0 or guess_row > 5) or (guess_col < 0 or guess_col > 5): print "Oops, that's not even in the ocean." elif(board[guess_row][guess_col] == "X"): print "You guessed this location already" else: print "You missed, Please try again." board[guess_row][guess_col] = "X" if turn == 3: print "Game Over" # Print (turn + 1) here! print_board(board) turn += 1 print turnRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.