Where does diamond go?

Hey
i am workin on assigment and here is what i have done until 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
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
#include<iostream>
#include <stdlib.h>  
using namespace std;
int main(){
	int r = 5, c = 5;;
	char player = 'P';
	char choice = 0;
	int dr = 2, dc = 2;//diamond in row //dimond in column
	int br = 3, bc = 3;
	int pr = 1, pc = 1;
	int score = 0;
	int k = 1;
	char d = 'D';
	char d1 = 'D';
	char b = 'B';
	int p = 0;


	while (score != 25){
		if (pc == dc && pr == dr) {
			score++;
			int newdc = rand() % 5;
			int newdr = rand() % 5;
			while (newdc == dc) newdc = rand() % 5;
			while (newdr == dr) newdr = rand() % 5;
			dc = newdc;
			dr = newdr;
		}








		for (int i = 1; i <= r; i = i + 1){



			for (int j = 1; j <= c; j = j + 1){




				if (i == pr&&j == pc){


					cout << " " << player;
				}



				else if (i == dr&&j == dc){

					cout << " " << d;
				}
				else if (i == br&&j == bc){
					cout << " " << b;
				}




				else{
					cout << " *";
				}



			}

			cout << endl;



		}

		cout << " " << score;




		cout << "Enter your choice= ";
		cin >> choice;
		if (choice == 'd' || choice == 'D'){
			pr = pr + 1;

		}
		if (choice == 'r' || choice == 'R'){
			pc = pc + 1;
		}
		if (choice == 'l' || choice == 'L'){
			pc = pc - 1;
		}
		if (choice == 'u' || choice == 'U'){
			pr = pr - 1;
		}


	}





}



Now when you will run this program and go to diamond.It will increase your score and give diammond a new positon. You get diamond again but now diamond will disappear from grid. I dont understand why?
Can anyone guide me?
Last edited on
@Deanmon

You are getting a random number, 0 to 5, for row and one for column, which works correctly, but you are couting the grid starting at 1 for row and 1, for column. The diamond is never printed if it is located in column 1 or row 1. Here is your corrected code.
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
#include<iostream>

using std::endl;
using std::cout;
using std::cin;

int main()
{
	int r = 5, c = 5;;
	char player = 'P';
	char choice = 0;
	int dr = 2, dc = 2;//diamond in row //diamond in column
	int br = 3, bc = 3;
	int pr = 1, pc = 1;
	int score = 0;
	int k = 1;
	char d = 'D';
	char d1 = 'D';
	char b = 'B';
	int p = 0;


	while (score != 25)
	{
		if (pc == dc && pr == dr) 
		{
			score++;
			int newdc = rand() % 5;
			int newdr = rand() % 5;
			while (newdc == dc) newdc = rand() % 5;
			while (newdr == dr) newdr = rand() % 5;
			dc = newdc;
			dr = newdr;
		}
		cout << endl;
		for (int i = 0; i < r; i++)
		{
			for (int j = 0; j < c; j++)
			{
				if (i == pr && j == pc)
				{
					cout << " " << player;
				}
				else if (i == dr && j == dc)
				{
					cout << " " << d;
				}
				else if (i == br && j == bc)
				{
					cout << " " << b;
				}
				else
				{
					cout << " *";
				}
			}
			cout << endl;
		}

		cout << "Score : " << score;

		cout << endl << "Enter your choice = ";
		cin >> choice;
		if (choice == 'd' || choice == 'D')
		{
			if(pr+1<5)
				pr++; // Same as pr = pr + 1
			else
				cout << "You can't go there!"<<endl;
		}
		if (choice == 'r' || choice == 'R')
		{
			if(pc+1<5)
				pc++;
			else
				cout << "You can't go there!"<<endl;
		}
		if (choice == 'l' || choice == 'L')
		{
			if(pc-1>=0)
				pc--; // Same as pc = pc - 1
			else
				cout << "You can't go there!"<<endl;
		}
		if (choice == 'u' || choice == 'U')
		{
			if(pr-1>=0)
				pr--;
			else
				cout << "You can't go there!"<<endl;
		}
	}
}
Last edited on
Usually people tell us what the program is supposed to do.

Not <stdlib.h> but <cstdlib>.

What's up with the random, bizarre vertical spacing?

There's no way you need that many variables. You're not even using some of them.

Not i = i + 1; but ++i;, etc.

Don't use 1-based indexing. That's not the C++ way. Always use 0-based indexing unless there's a really good reason not to. (You're not even using the 1-base indexing correctly since rand() % 5 returns 0 to 4, which is why the diamond disappears.)

You allow the player to move right off the board. You need to check that.

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
#include <iostream>
#include <cstdlib>  
using namespace std;

int main() {
	int nrows = 5, ncols = 5;
	char choice = 0;
	int dr = 1, dc = 1;  // diamond row,col
	int br = 2, bc = 2;  // ???
	int pr = 0, pc = 0;  // player row,col
	int score = 0;

	while (score < 25) {
		if (pc == dc && pr == dr) {
			score++;
			int newdc, newdr;
			do newdc = rand() % 5; while (newdc == dc);
			do newdr = rand() % 5; while (newdr == dr);
			dc = newdc;
			dr = newdr;
		}

		for (int r = 0; r < nrows; r++) {
			for (int c = 0; c <= ncols; c++) {
				if (r == pr && c == pc)
					cout << " P";
				else if (r == dr && c == dc)
					cout << " D";
				else if (r == br && c == bc)
					cout << " B";
				else
					cout << " *";
			}
			cout << '\n';
		}

		cout << score << '\n';
		cout << "Enter direction (r,l,u,d): ";
		cin >> choice;

		if (choice == 'd' || choice == 'D')
			if (pr < nrows - 1) pr++;
		if (choice == 'r' || choice == 'R')
			if (pc < ncols - 1) pc++;
		if (choice == 'l' || choice == 'L')
			if (pc > 0) pc--;
		if (choice == 'u' || choice == 'U')
			if (pr > 0) pr--;
	}
}

about that right off the board i did this
if (choice == 'd' || choice == 'D'){
pr = pr + 1;
if (pr > 5){
pr = pr - 1;
cout << "Invalid movment";
cout << endl;
}


}
if (choice == 'r' || choice == 'R'){
pc = pc + 1;
if (pc > 5){
pc = pc - 1;
cout << "Invalid movment";
cout << endl;
}
}
if (choice == 'l' || choice == 'L'){
pc = pc - 1;
if (pc < 1){
pc = pc + 1;
cout << "Invalid movment";
cout << endl;
}
}
if (choice == 'u' || choice == 'U'){
pr = pr - 1;
if (pr < 1){
pr = pr + 1;
cout << "Invalid movement";
cout << endl;
}


but its not working in one case as i tested.
Thank you too you both.
Topic archived. No new replies allowed.