Project due need assistance

My tutor has given me these steps for one of our end of my year projects I am struggling with c and any help would be appreciated

1) Declare the 2d array (5x5)
2) Initialise the 2d array (put a space in each cell)
3) Randomly generate & store 5 ships in 2d array
4) Ask user to enter their guess (x,y co-ords)
5) Check if it was a hit, miss or a cell already fired upon.
6) Draw the grid to show all events (misses & hits)
7) Have they used their 8 shots yet, if no goto step 4

This is what I have as of now
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
  #include<iostream>
#include<conio.h>
#include<stdlib.h>
#include<ctime>

//Name:
//Author:
//Date:
using namespace std;


main()
{
 int arr[5][5],x,y,rtot=0;
 srand(time(0));
  
 for(x=0;x<5;x++)    //load data into 2d array
 {
  for(y=0;y<5;y++)
   {
        arr[x][y]=rand() % 20 + 1;
   }
  }

  cout<<endl;

 for(x=0;x<5;x++) //display contents of 2d array
 {
  for(y=0;y<5;y++)
   {
	cout<<"Number in cell "<<x<<","<<y<<" = "<<arr[x][y]<<endl;
   	rtot+=arr[x][y];
   }
	 cout<<"Total for row "<<x<<" = "<<rtot<<endl;
     rtot=0;		//reset row total 
     cout<<endl;
 }

getch();
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>
#include <cstdlib>

// Declare constants and map
const unsigned int X = 5;
const unsigned int Y = 5;

const char SHIP = '#';
const char HIT = 'X';
const char MISS = 'O';
typedef char MAP[Y][X];

int main()
{
	// Seed random number generator
	srand(time(0));

	// Declare a map
	MAP MyMap;

	// Init map with spaces
	for(unsigned int i = 0; i < Y; i++)
		for(unsigned int j = 0; j < X; j++)
			MyMap[i][j] = ' ';

	// Count how many ships have been placed
	unsigned int ships_placed = 0;

	// Attempt to randomly place the ships
	while(ships_placed < 5)
	{
		unsigned int tempx, tempy;
		tempx = tempy = 0;

		tempx = rand() % 5;
		tempy = rand() % 5;

		if(MyMap[tempy][tempx] == ' ')
		{
			MyMap[tempy][tempx] = SHIP;
			ships_placed++;
		}
	}

	// Declare the amount of shots the player has
	unsigned int shots_left = 8;
	// Declare amount of ships left on the board
	unsigned int ships_left = 5;

	// Game runs while shots can still be fired
	while(shots_left > 0 && ships_left > 0)
	{
		std::cout << "Please enter x and y coordinates: ";
		unsigned int tempx,tempy;
		std::cin >> tempx >> tempy;

		// Test input for correctess
		if(tempx > X-1 || tempy > Y-1)
		{
			std::cout << "Invalid coordinates, please choose between 0 and 4\n\n";
		}
		else if(MyMap[tempy][tempx] == HIT || MyMap[tempy][tempx] == MISS)
		{
			std::cout << "You have already tried that!\n\n";
		}
		else
		{
			// Coordinates are valid, proceed

			// Take a shot away
			shots_left--;

			// Test if coordinates contain a ship
			if(MyMap[tempy][tempx] == SHIP)
			{
				// Update map to show hit
				MyMap[tempy][tempx] = HIT;
				// Let it be known
				std::cout << "You hit!\n\n";
				// Take a ship away
				ships_left--;
			}
			else
			{
				// Update map to show miss
				MyMap[tempy][tempx] = MISS;
				// Let it be known
				std::cout << "You missed!\n\n";
			}

			// Draw map
			for(unsigned int i = 0; i < Y; i++)
			{
				for(unsigned int j = 0; j < X; j++)
				{
					// "Hide" the ships
					if(MyMap[i][j] == SHIP) std::cout << ' ';
					else std::cout << MyMap[i][j];
				}
				std::cout << "\n";
			}
		}
	}

	// The game is over, display results
	if(shots_left == 0)
	{
		std::cout << "You ran out of shots, you lost!\n\n";
	}
	else if(ships_left == 0)
	{
		std::cout << "You destroyed all the ships, congratulations you won!\n\n";
	}

	// End of program
	return 0;
}
Topic archived. No new replies allowed.