Snakes and Ladders Through 2D Array

Hi,

I have an assignment to submit on a Snakes and Ladders game built through 2D array. I have got the game running without 2D Array but having issues calling these arrays when switching in 2D. Could you guys help me out and point out the issues?


The code is given below (Haven't called the arrays yet):

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;

void read(int[][5],ifstream&);
int rand(int&);
void printboard(int);

int main()
{
ifstream a;
int rows=4;
int cols=5;

int score[rows][cols];

string player1;
string player2;

int player1_score;
int player2_score;

cout<<"Enter the name of player 1:"<<endl;
cin>>player1;
cout<<"Enter the name of the player 2:"<<endl;
cin>>player2;

while(player1_score<20 && player2_score<20)
{

cout<<player1<<"Please enter a key to generate a random number:"<<endl;
cin.get();

rand(player1_score);

cout<<"Your score is:"<<player1_score<<endl;
cout<<player2<<"Please enter a key to generate a random number:"<<endl;
cin.get();

rand(player2_score);

cout<<"Your score is:"<<player2_score<<endl;

}
if(player1_score>player2_score)
{
cout<<player1<<"is the winner."<<endl;
}
if(player2_score>player1_score)
{
cout<<player2<<"is the winner."<<endl;
}
if(player1_score==player2_score)
{
cout<<"The match is a draw."<<endl;
}

return 0;
}

int rand(int &score)
{
int random;
random=rand()%6;

cout<<"Your random number is:"<<random<<endl;
score=random+score;
return score;

}

void printboard(int score[4][5])
{
score[0][0]=1;
score[0][1]=2;
score[0][2]=3;
score[0][3]=4;
score[0][4]=5;
score[1][0]=6;
score[1][1]=7;
score[1][2]=8;
score[1][3]=9;
score[1][4]=10;
score[2][0]=11;
score[2][1]=12;
score[2][2]=13;
score[2][3]=14;
score[2][4]=15;
score[3][0]=16;
score[3][1]=17;
score[3][2]=18;
score[3][3]=19;
score[3][4]=20;

cout<<score[0][0]<<" "<<score[0][1]<<" "<<score[0][2]<<" "<<score[0][3]<<" "<<score[0][4]<<endl;
cout<<endl;
cout<<score[1][0]<<" "<<score[1][1]<<" "<<score[1][2]<<" "<<score[1][3]<<" "<<score[1][4]<<endl;
cout<<endl;
cout<<score[2][0]<<" "<<score[2][1]<<" "<<score[2][2]<<" "<<score[2][3]<<" "<<score[2][4]<<endl;
cout<<endl;
cout<<score[3][0]<<" "<<score[3][1]<<" "<<score[3][2]<<" "<<score[3][3]<<" "<<score[3][4]<<endl;
}

void read(int score[4][5],ifstream&a)
{
a.open("values.txt");
if(!a)
{
cerr<<"File location is invalid."<<endl;
}
while(!a.eof())
{
for(int i=0;i<4;i++)
{
for(int j=0;j<5;j++)
{
a>>score[i][j];
}
}
}
}
I will try to point out some things.

1. It looks like you created a user-defined function, int rand ( int& ), which takes
param: Referenced integer variable
return: integer

However, in the main () function, you have the following lines
rand(player1_score);
rand(player2_score);

Basically, you called a value-returning function as if they were void functions.
Do something like
int random = rand ( player1_score); // Without referencing

OR

void rand ( player1_score ); // Just turn it into a void function

Also on that note, take a look at the definition of int rand ( int& ) function:
1
2
3
4
5
6
7
8
9
10
int rand ( int &score )
{
      int random;
      random=rand()%6;

      cout<<"Your random number is:"<<random<<endl;
      score=random+score;
 
      return score;
}


You simply find a random number using rand () % 6 from the Library, display the random number, and return random + score

The code inside the function is so simple that it could be better off to just use the logic in the main function without having this user-defined function at all.

2. For the rand () to return a random number, you need to provide seed (integer) using the following:
srand ( time ( NULL ) ); // time ( NULL ) can be replaced by an integer

3. Why do you have rows and cols variables in the main function?
Couldn't you do just
int score [4][5];

instead of

int rows=4;
int cols=5;

int score[rows][cols];

4. You might want to initialize player1_score and player2_score variables before you use them for comparison for the first while loop


So, I rewrote the main function. But please understand that I am not on a computer that has a compiler / IDE, so I was never able to test the code. It might not compile and you might have to make some adjustment. See if this is what you wanted to accomplish.

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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string.h>
using namespace std;

int main()
{
	ifstream a;		// Not Used in this program

	int score [4][5];

	int key;	// This will be used to store key (an integer) from player and used as seed for srand ();

	string player1;
	string player2;

	// Initialize scores
	int player1_score = 0;
	int player2_score = 0;

	cout << "Enter the name of player 1: " << endl;
	cin >> player1;

	cout << "Enter the name of player 2: " << endl;
	cin >> player2;

	while ( player1_score < 20 && player2_score < 20 )
	{
		cout << player1 << " Please enter a key to generate a random number: " << endl;
		cin >> key;

		srand ( key );
		int random = rand ( ) % 6;

		player1_score += random;

		cout << "Your random number is: " << random << endl;
		cout << "Your score is: " << player1_score << endl;


		cout << player2 << " Please enter a key to generate a random number: " << endl;
		cin >>  key;

		srand ( key );
		random = rand () % 6;

		player2_score += random;

		cout << "Your random number is: " << random << endl;
		cout << "Your score is: " << player2_score << endl;
	}

	if ( player1_score > player2_score )
		cout << player1 << " is the winner." << endl;
	else if ( player2_score > player1_score )
		cout << player2 << " is the winner." << endl;
	else
		cout << "The match is a draw." << endl;

	return 0;
}
Last edited on
Topic archived. No new replies allowed.