Stuck in middle of new Program

So im making a battleship game to help me learn the language, but im currently stuck on allowing the player to input custom ship positions onto the grid. *Some issues on the copy and paste with indenting*

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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
  #include <iostream>
#include <ctime>
#include <random>
#include <string> 

//Set up Variables 
//Set up Menu
//Set Up AI Grid
//Set up Player Grid
//Set up Playerships

using namespace std;

const int row = 10; // Sets up Grid*
const int collum = 10; // Sets up grid
const char water = 247; // Water on grid
const char hit = 'x'; //Displyed when a ship is hit
const char Pship = 's'; //Displayed where a player places a ship



int maxship = 5; //Sets Max # of ships on AI board
int matrix[row][collum]; //*
const int shipnum = 5;


void clear() // Clears the grid
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			matrix[i][j] = 0;

		}
	}
}




void show() //Displays the Players grid
{
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			cout << matrix[i][j] << " ";
		}
		cout << endl;
	}
}

int numship() //Tells the player how many AI ships are left
{
	int c = 0;
	for (int i = 0; i < row; i++)
	{
		for (int j = 0; j < collum; j++)
		{
			if (matrix[i][j] == 3) //
				c++;
		}
	}

	return c;

}


void setships() //Sets up the ships randomly for the AI player (Sets AI ships to 3 on the array/ AI Grid)
{
	int s = 0;
	while (s < maxship)
	{
		int x = rand() % row;
		int y = rand() % collum;
		if (matrix[x][y] != 3)
		{
			s++;
			matrix[x][y] = 3;

		}
	}
}

bool attack(int x, int y) //Allows the player to fire on X,Y Cords of their choice 
{
	if (matrix[x][y] == 3)
	{
		matrix[x][y] = 2;
		return true;

	}
	return false;
}

int main()
{

	bool Quit = 0; //If Quit = 1 it will exit the program 
	//Player Selection Menu

Begin:
	cout << "1: Play Game \n"
		"2: Quit Game \n"
		"3: Game Credits \n"
		"4: How To Play \n";

	int Select; //Variable to allow player to select a option 
	cout << "Please Enter a Number from the Options: ";
	cin >> Select;


	
	if (Select == 1)
	{ 
		cout << "\n"
			"\n"
			"\n";

		srand(time(NULL));
		clear();
		show();
		cout << "------------------------" << endl;
		setships(); // *
		show(); // Shows AI Board (Testing Feature only)*
		int pos1, pos2;
		while (1)
		{
			cout << "Please input Location (X then Y):"; cin >> pos1 >> pos2; // Asking player where to "fire"
			if (attack(pos1, pos2)) //If the hit is succesful 
				cout << "Hit succesful" << endl; 
			else
				cout << "Hit Failed" << endl; //If there is no hit
			cout << "Remaining Ships: " << numship() << endl;

		}

		system("pause");
		return 0;
	}
}
are you asking to let the player set their starting positions?
just copy setships, and replace the randoms with user input that you validate to be in range.
Something like this is a better structure for main():

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
int main()
{
    srand(time(nullptr));

    for (bool quit = false; !quit; )
    {
        cout << "1: Play Game\n"
                "2: Quit Game\n"
                "3: Game Credits\n"
                "4: How To Play\n";

        int select;
        cout << "Please enter a number from the options: ";

        if (!(cin >> select)) {
            cin.clear();
            cin.ignore(99999, '\n');
            select = -1; // force default case in switch stmt
        }

        switch (select)
        {
        case 1:
            play_game();
            break;
        case 2:
            quit = true;
            break;
        case 3:
            show_credits();
            break;
        case 4:
            show_instructions();
            break;
        default:
            cout << "\nBad input. Try again\n\n";
        }
    }
}


closed account (367kGNh0)
I ask the latter poster, will it make a difference if the contents of the main were too placed with a function?

1
2
3
4
int main()
{
Bojová_loď();
}
Last edited on
Topic archived. No new replies allowed.