Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For your game, you will create a one-person version of the game where the computer will play for the second player. At the beginning of

For your game, you will create a one-person version of the game where the computer will play for the second player. At the beginning of the game, you will read a file called ship_placement.csv which contains the type of ship, the first grid square for the ship placement, and whether the ship is placed vertically or horizontally (V or H in the field). The file will be in csv format (comma separated values). This is a common format and is comma separated (instead of being on separate lines). There will be commas between the values. Blank values will just have a comma noting to go to the next field (the game input should not have blank fields so you should handle the case where a field is blank). If you want to view the file, often this will be opened by a spreadsheet unless you specifically open it with a text editor. Do not open it with Microsoft Word, as this may change the format. The first line of a CSV file notes the data descriptions as follows: TypeOfShip, Location, HorizOrVert I have provided several sample files which contain good scenarios and scenarios with placement issues that you will need to handle using exception handling. Your game should run with any of these files, but should also be able to run with any valid file in the correct format. You will need to check whether all ships were included in the input file (and appropriate action to take if not), whether all ships have been placed, whether they fit on the board in the configuration given, and whether more than one ship occupies a space (which is not allowed) when you read the input file from the user and how to recover if an error occurs. You will then need to randomly position the computers ships on the grid taking into consideration the same factors as you did for the players input. You will need to prompt for and allow for the user to input their next guess in the form of a letter (A through J) and a number (1 10) indicating where they are targeting for their torpedo and you should error check the input. In our simplified game, you will determine if the torpedo shot was a hit or a miss. If the shot was a hit, consider the ship to be sunk. You should display a hit or miss, whether the ship was sunk and which one, and display their tracking grid so they know what they have guessed and where they have made hits. The entire ship which was hit will display as sunk. 4 After the user takes their turn, you must have the computer randomly select a shot that they have not previously taken. Then you must display to the user what the computer guessed, whether it hit any of the players ships, whether a ship was sunk, and then display the players placement grid showing where ships are located and what has been hit. You should continue this until someone wins or quits the game meaning you should allow the player to gracefully quit at any turn. At the end of the game, you should indicate the game is over and who the winner was. You should also allow the user to quit the game by entering a Q when prompted for their next guess. If a player decides to quit the game, the grid with all of their guesses and the locations of the computers ships should be displayed. Ship Type/ Number of Grid Squares Carier 5 BattleShip 4 Cruiser 3 Submarine 3 Destroyer 2 input4_good.csv Carrier,B2,H Battleship,D4,V Cruiser,G4,H Submarine,G5,V Destroyer,G8,H input5_good.csv Carrier,J1,V Battleship,D4,V Cruiser,A4,H Submarine,A9,H Destroyer,A1,H input6_good.csv Carrier,A1,H Battleship,F1,H Cruiser,E2,V Submarine,E5,V Destroyer,E8,V input7_good.csv Carrier,A1,V Battleship,A6,V Cruiser,J1,V Submarine,J4,V Destroyer,J7,V input8_good.csv Carrier,B2,V Battleship,C2,V Cruiser,D2,V Submarine,E2,V Destroyer,F2,V input9_good.csv Carrier,E6,H Battleship,E7,H Cruiser,E8,H Submarine,E9,H Destroyer,E10,H input10_good.csv Carrier,C1,H Battleship,C2,V Cruiser,D5,H Submarine,F2,V Destroyer,G2,V input11_good.csv Carrier,C6,V Battleship,D6,H Cruiser,G7,V Submarine,D10,H Destroyer,E7,V input12_bad.csv Carrier,B2,H Battleship,B3,V Cruiser,G4,H Submarine,G5,V Destroyer,G8,H input13_bad.csv Carrier,J1,H Battleship,D4,V Cruiser,A4,H Submarine,A9,H Destroyer,A1,H linkedlist.cpp #include

using namespace std;

struct ListNode

{

int value;

ListNode *link;

};

void printLinkedList(ListNode *linkedlist)

{

ListNode *current;

current = linkedlist;

while(current != NULL)

{

cout << current -> value << " ";

current = current -> link;

}

cout << endl;

}

void searchInList(ListNode *linkedlist, int value)

{

ListNode *current = linkedlist;

int i = 0;

while(current != NULL)

{

if (current -> value == value)

{

cout << current -> value << " in position " << i << endl;

return;

}

current = current -> link;

i = i + 1;

}

cout << "item " << value << " not found" << endl;

}

void insertList(ListNode *linkedlist, int value, int position)

{

ListNode *current = linkedlist;

// Create a new node that we want to insert in position i;

ListNode *newNode = new ListNode;

newNode -> value = value;

newNode -> link = NULL;

int i = 0;

while(i < position)

{

current = current -> link;

i = i + 1;

}

newNode -> link = current -> link;

current -> link = newNode;

}

void deleteNode(ListNode *linkedlist, int position)

{

ListNode *current = linkedlist;

int i = 0;

while(i < position-1)

{

current = current -> link;

i = i + 1;

}

ListNode *temp = current -> link;

current -> link = current -> link -> link;

delete temp;

}

ListNode* insertListHead(ListNode *linkedlist, int value)

{

ListNode *newNode = new ListNode();

newNode -> value = value;

newNode -> link = linkedlist;

return newNode;

}

void insertListHead2(ListNode *&linkedlist, int value)

{

ListNode *newNode = new ListNode();

ListNode *current = linkedlist;

newNode -> value = value;

newNode -> link = current;

linkedlist = newNode;

//return newNode;

}

ListNode* deleteHeadNode(ListNode *linkedlist)

{

ListNode *current = linkedlist;

linkedlist = linkedlist -> link;

delete current;

return linkedlist;

}

void deleteHeadNode2(ListNode *&linkedlist)

{

ListNode *current = linkedlist;

linkedlist = linkedlist -> link;

delete current;

}

void reverseList(ListNode *&linkedlist)

{

ListNode *current = linkedlist;

ListNode *next = NULL;

ListNode *prev = NULL;

while(current != NULL)

{

next = current -> link;

current -> link = prev;

prev = current;

current = next;

}

linkedlist = prev;

}

int main()

{

ListNode *head;

ListNode *node2 = new ListNode;

ListNode *node3 = new ListNode;

ListNode *node4 = new ListNode;

ListNode *node5 = new ListNode;

head = new ListNode;

head -> value = 10;

head -> link = node2;

node2 -> value = 11;

node2 -> link = node3;

node3 -> value = 12;

node3 -> link = node4;

node4 -> value = 13;

node4 -> link = node5;

node5 -> value = 14;

node5 -> link = NULL;

printLinkedList(head);

cout << head -> value << endl;

searchInList(head, 11);

searchInList(head, 13);

searchInList(head, 16);

cout << "===========After insertion=============" << endl;

insertList(head, 100, 3);

printLinkedList(head);

cout << "===========After insertion at the beginning of the list

=============" << endl;

head = insertListHead(head, 1000);

printLinkedList(head);

insertListHead2(head, 1000);

printLinkedList(head);

cout << "===========After Deletion=============" << endl;

deleteNode(head,2);

printLinkedList(head);

cout << "===========After Deleting Head =============" << endl;

//deleteNode(&head);

//head = head -> link;

head = deleteHeadNode(head);

printLinkedList(head);

deleteHeadNode2(head);

printLinkedList(head);

cout << "===========Reverse the link list=============" << endl;

reverseList(head);

printLinkedList(head);

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions

Question

=+f) What do you conclude at a = 0.05?

Answered: 1 week ago