Question

i need this in C# please can any one help me out and write the full...

i need this in C# please can any one help me out and write the full code start from using system till end i am confused and C# is getting me hard.I saw codes from old posts in Chegg but i need the ful complete code please thanks.

Module 4 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 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.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

using System;
using System.Collections.Generic;
using System.Text;

namespace Battleship2
{
class Ship
{
Dictionary<int, Dictionary<int, bool>> sunk=new Dictionary<int,Dictionary<int,bool>>();
List<Tuple<int, int>> coordinates;
  
public Ship(List<Tuple<int,int>> coords)
{
for (int i = 0; i < coords.Count;i++)
{
int x = coords[i].Item1, y = coords[i].Item2;
if (!sunk.ContainsKey(x))
sunk.Add(x, new Dictionary<int, bool>());
sunk[x].Add(y,false);
}
coordinates = coords;
}

bool containsCoords(int x, int y)
{
if (!sunk.ContainsKey(x))
return false;
if (!sunk[x].ContainsKey(y))
return false;
return true;
}

List<Tuple<int,int>> getCoords()
{
return coordinates;
}

public bool overlaps(Ship otherShip)
{
var coords = otherShip.getCoords();
for (int i = 0; i < coords.Count;i++)
{
int x = coords[i].Item1, y = coords[i].Item2;
if(sunk.ContainsKey(x))
{
if (sunk[x].ContainsKey(y))
return true;
}
}
return false;
}

public bool sink(int x,int y)
{
if (containsCoords(x, y))
{
sunk[x][y] = true;
return true;
}
return false;
}

public bool fullySunk()
{
for(int i=0;i<coordinates.Count;i++)
{
int x = coordinates[i].Item1, y = coordinates[i].Item2;
if (sunk[x][y] == false)
return false;
}
return true;
}
}

class Ships
{
List<Ship> ships=new List<Ship>();
List<bool> sunk=new List<bool>();

public void Add(Ship ship)
{
bool overlapping = false;
for(int i=0;i<ships.Count;i++)
{
if(ship.overlaps(ships[i]))
{
overlapping = true;
break;
}
}
if(overlapping)
return;
ships.Add(ship);
sunk.Add(ship.fullySunk());
}

public void Clear()
{
ships.Clear();
sunk.Clear();
}

public bool sunkMyBattleship()
{
for(int i=0;i<sunk.Count;i++)
{
if (sunk[i] == false)
return false;
}
return true;
}

public bool Attack(int x,int y)
{
bool hit = false;
for(int i=0;i<ships.Count;i++)
{
hit = hit || ships[i].sink(x, y);
sunk[i] = ships[i].fullySunk();
}
return hit;
}

}

class Program
{
static void Main(string[] args)
{
Ship s1 = new Ship(new List<Tuple<int, int>> { new Tuple<int, int>(0, 0), new Tuple<int, int>(0, 1), new Tuple<int, int>(0, 2)});
Ship s2 = new Ship(new List<Tuple<int, int>> { new Tuple<int, int>(3, 2), new Tuple<int, int>(3,3)});
Ships battleship=new Ships();
battleship.Add(s1);
battleship.Add(s2);
while(!battleship.sunkMyBattleship())
{
Console.Write("Enter X: ");
int x = Convert.ToInt32(Console.ReadLine());
Console.Write("Enter Y: ");
int y = Convert.ToInt32(Console.ReadLine());
bool hit=battleship.Attack(x, y);
if (hit)
Console.WriteLine("Hit!");
else
Console.WriteLine("Miss!");
}
Console.WriteLine("Battleships destroyed!");
}
}
}

Add a comment
Know the answer?
Add Answer to:
i need this in C# please can any one help me out and write the full...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use...

    In C++ please.. I only need the game.cpp. thanks. Game. Create a project titled Lab8_Game. Use battleship.h, battleship.cpp that mentioned below; add game.cpp that contains main() , invokes the game functions declared in battleship.h and implements the Battleship game as described in the introduction. ——————————————- //battleship.h #pragma once // structure definitions and function prototypes // for the battleship assignment // 3/20/2019 #include #include #ifndef BATTLESHIP_H_ #define BATTLESHIP_H_ // // data structures definitions // const int fleetSize = 6; // number...

  • Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every...

    Make sure to include: Ship.java, ShipTester.java, Location.java, LocationTester.java, Grid.java, GridTester.java, Player.java, PlayerTester.java, battleship.java. please do every part, and include java doc and comments Create a battleship program. Include Javadoc and comments. Grade 12 level coding style using java only. You will need to create a Ship class, Location class, Grid Class, Add a ship to the Grid, Create the Player class, the Battleship class, Add at least one extension. Make sure to include the testers. Starting code/ sample/methods will be...

  • Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next...

    Assignment - Battleship In 1967, Hasbro toys introduced a childrens game named “Battleship”. In the next two assignments you will be creating a one-player version of the game. The game is extremely simple. Each player arranges a fleet of ships in a grid. The grid is hidden from the opponent. Here is an ASCII representation of a 10x10 grid. The ‘X’s represent ships, the ‘~’s represent empty water. There are three ships in the picture: A vertical ship with a...

  • C++ Trek Wars Project: 100% code coverage unit testing is required for this project. once a ship has reached 0 health it...

    C++ Trek Wars Project: 100% code coverage unit testing is required for this project. once a ship has reached 0 health it is considered blown up -throw an exception if the move method is called on it. -repair ships cannot repair ships at 0 health There will be no chaotic corvette ships Trek Wars Purpose: classes, inheritance, class diagrams, virtual, testing Description Universal Merriment Development (UMD) is creating an online space battle game called (Trek Wars) You are tasked with...

  • I need help finishing my C++ coding assignment! My teacher has provided a template but I...

    I need help finishing my C++ coding assignment! My teacher has provided a template but I need help finishing it! Details are provided below and instructions are provided in the //YOU: comments of the template... My teacher has assigned a game that is similar to battleship but it is single player with an optional multiplayer AI... In the single player game you place down a destroyer, sub, battleship, and aircraft carrier onto the grid.... Then you attack the ships you...

  • For your Project, you will develop a simple battleship game. Battleship is a guessing game for...

    For your Project, you will develop a simple battleship game. Battleship is a guessing game for two players. It is played on four grids. Two grids (one for each player) are used to mark each players' fleets of ships (including battleships). The locations of the fleet (these first two grids) are concealed from the other player so that they do not know the locations of the opponent’s ships. Players alternate turns by ‘firing torpedoes’ at the other player's ships. The...

  • C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The...

    C#: Implement a multiplayer Battleship game with AI The rules are the same as before. The game is played on an NxN grid. Each player will place a specified collection of ships: The ships will vary in length (size) from 2 to 5; There can be any number or any size ship. There may be no ships of a particular size; EXCEPT the battleship – which there will always be 1 and only 1. Player order will be random but...

  • Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can...

    Lanuage C++ (Beginner level) Please...I need help with this assignment If I still have question, can i contact you? -------------------------- Program 1 - WRITE ALL THE CODE in ONE FILE...   MyArrayPtrArith1.cpp Step 1 - Define the class Write a class, myArrayClass, that creates an integer array. * Add an 'arraySize' variable, initialize to zero ( default constructor function ) * Create int * ptrArray ( default constructor set to NULL ) * Add a default constructor ( see above for...

  • I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In...

    I haven't code in awhile and would like some help getting back on my feet. Need to code this in C++. Thank you! In this homework, you will implement a simple version of the game Battleship, in which the player tries to destroy ships hidden on a map using a system of coordinates. In this program, the map will be represented by a 4x4 matrix of integers. A 1 in the matrix represents the presence of a ship. There are...

  • I need help with the Implementation of an Ordered List (Template Files) public interface Ordered...

    I need help with the Implementation of an Ordered List (Template Files) public interface OrderedStructure { public abstract int size(); public abstract boolean add( Comparable obj ) throws IllegalArgumentException; public abstract Object get( int pos ) throws IndexOutOfBoundsException; public abstract void remove( int pos ) throws IndexOutOfBoundsException; public abstract void merge( OrderedList other ); } import java.util.NoSuchElementException; public class OrderedList implements OrderedStructure { // Implementation of the doubly linked nodes (nested-class) private static class Node { private Comparable value; private...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT