Fianl Project
Fianl Project
Fianl Project
FUNDAMENTAL
S
FINAL PROJECT
SNAKES AND LADDERS
CODE OF GAME:
#include <iostream>
#include<iomanip>
#include<cstdlib>
#include <ctime>
#include <string>
#include<fstream>
string RECORD = "";//DECLARING STRING VARIABLE TO STORE RECORD OF THE PLAYED GAME
int BLOCK[101]; //ARRAY OF SIZE 101 INDEXES FROM 0 TO 100 FOR THE PLAY-BORAD
int CHOICE();
int CHECK_F();
void P_BOARD();
int P_CHOICE;//VARIABLE TO TAKE INPUT FROM THE USER CHOICE FOR VARIOUS FUNCTIONS
int PR_PROGRES; //THIS VARIABLE WILL BE USED TO STORE THE PROGRESS OF PLAYER.
{//BODY OF PROGRAM
int p1 = -1, p2 = -1, p3 = -1, p4 = -1; // VARIABLES FOR THE POSITIONS OF THE PLAYERS /
string record, n1, n2, n3, n4; // STRING VARIABLES FOR NAMES OF PLAYERS AND RECORD IN FILES/
BLOCK[i] = i;
}
CHOICE();
while (RUN)
if (CHOICE_N == 0)
CHOICE();
switch (P_CHOICE)
case 1:
cout << "\t\tWELCOME TO SNAKE AND LADDER GAME :)\n\t\tPress any Number to roll the dice
and press 0 to quit.\n"<<endl;
cout << "\n\t\tSnakes and Ladders are at different positions on the board\n\t\tThese Positions
are as :\n"<<endl;
getline(cin, n1);
getline(cin, n2);
getline(cin, n3);
getline(cin, n4);
cout << "\t\t1. "<<n1 << "\n" <<"\t\t2. "<< n2 << "\n"<<"\t\t3. " << n3 << "\n" <<"\t\t4. "<< n4
<< endl;
cout << "\n\tPress any number to roll the dice\n\tPress 0, if you want to quit : ";
cout << n1 << "Still has not rolled 6 in this turn." << endl << endl;
p1 = p1;
else
if (flag == false)
flag = true;
p1 = 1;
else
BLOCK[p1] = p1;
p1 = p1 + R1;
}
}
/*SWITCH-CASE USED TO REPRESENT THE SNAKES AND LADDERS ON THE BOARD FOR THE
PLAYERS.
switch (p1)
case 99:
p1 = 12;
cout << n1 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 79:
p1 = 29;
cout << n1 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 71:
p1 = 51;
cout << n1 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 51:
p1 = 2;
cout << n1 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 35:
p1 = 19;
cout << n1 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 17:
p1 = 42;
cout << n1 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 33:
p1 = 64;
cout << n1 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 42:
p1 = 69;
cout << n1 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 53:
p1 = 80;
cout << n1 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 8:
p1 = 23;
cout << n1 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
default:
break;
R2 = DICE();
cout << n2 << " still has not rolled 6 in this turn." << endl << endl;
p2 = p2;
else
if (flag1 == false)
{
flag1 = true;
p2 = 1;
else
BLOCK[p2] = p2;
p2 = p2 + R2;
/*SWITCH-CASE USED TO REPRESENT THE SNAKES AND LADDERS ON THE BOARD FOR THE
PLAYERS.
switch (p2)
case 99:
p2 = 12;
cout << n2 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 79:
p2 = 29;
cout << n2 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 71:
p2 = 51;
cout << n2 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 51:
p2 = 2;
cout << n2 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 35:
p2 = 19;
cout << n2 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 17:
p2 = 42;
cout << n2 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 33:
p2 = 64;
cout << n2 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 42:
p2 = 69;
cout << n2 << " CONGRATULATIONS !! You climbed a ladder:)" << endl;
break;
case 53:
p2 = 80;
cout << n2 << " CONGRATULATIONS!!! You climbed a ladder :)" << endl;
break;
case 8:
p2 = 23;
cout << n2 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
default:
break;
R3 = DICE();
cout << n3 << " still has not rolled 6 in this turn." << endl;
p3 = p3;
}
else
if (flag2 == false)
flag2 = true;
p3 = 1;
else
BLOCK[p3] = p3;
p3 = p3 + R3;
/*SWITCH-CASE USED TO REPRESENT THE SNAKES AND LADDERS ON THE BOARD FOR THE
PLAYERS.
switch (p3)
case 99:
p3 = 12;
cout << n3 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 79:
p3 = 29;
cout << n3 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 71:
p3 = 51;
cout << n3 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 51:
p3 = 2;
cout << n3 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 35:
p3 = 19;
cout << n3 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 17:
p3 = 42;
cout << n3 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 33:
p3 = 64;
cout << n3 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 42:
p3 = 69;
cout << n3 << " CONGRATULATIONS !! You climbed a ladder:)" << endl;
break;
case 53:
p3 = 80;
cout << n3 << " CONGRATULATIONS!!! You climbed a ladder :)" << endl;
break;
case 8:
p3 = 23;
cout << n3 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
default:
break;
R4 = DICE();
{
cout << n4 << " still has not rolled 6 in this turn." << endl;
p4 = p4;
else
if (flag3 == false)
flag3 = true;
p4 = 1;
else
BLOCK[p4] = p4;
p4 = p4 + R4;
}
/*SWITCH-CASE USED TO REPRESENT THE SNAKES AND LADDERS ON THE BOARD FOR THE
PLAYERS.
switch (p4)
case 99:
p4 = 12;
cout << n4 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 79:
p4 = 29;
cout << n4 << "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 71:
p4 = 51;
cout << n4<< "BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 51:
p4 = 2;
cout << n4 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 35:
p4 = 19;
cout << n4 << " BEST OF LUCK FOR NEXT TIME, YOU GOT SNAKE STING :(" << endl;
break;
case 17:
p4 = 42;
cout << n4 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 33:
p4 = 64;
cout << n4 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
case 42:
p4 = 69;
cout << n4 << " CONGRATULATIONS !! You climbed a ladder:)" << endl;
break;
case 53:
p4 = 80;
cout << n4 << " CONGRATULATIONS!!! You climbed a ladder :)" << endl;
break;
case 8:
p4 = 23;
cout << n4 << " CONGRATULATIONS !! You climbed a ladder :)" << endl;
break;
default:
break;
}
BLOCK[p1] = 1000;
BLOCK[p2] = 2000;
BLOCK[p3] = 3000;
BLOCK[p4] = 4000;
if (flag)
cout << n1 << " has rolled " << R1 << "\t Position of player 1 is " << p1 << endl;
if (flag1)
cout << n2 << " has rolled " << R2 << "\t Position of player 2 is " << p2 << endl;
if (flag2)
cout << n3 << " has rolled " << R3 << "\t Position of player 3 is " << p3 << endl;
if (flag3)
cout << n4 << " has rolled " << R4 << "\t Position of player 4 is " << p4 << endl;
}
// CALLED THE playboard FUNCTION TO DISPLAY THE BOARD /
P_BOARD();
PR_PROGRES = CHECK_F();
/* WHILE LOOP USED TO STORE THE NAME OF THE PLAYER WHO WON THE GAME IN TEXT FILE
*/
ofstream file_record;
/* IN CASE OF PLAYER 1 */
if (BLOCK[100] == 1000)
cout << "\n\t" << n1 << " HAS WON THE GAME ! :) " << endl << endl;
CHOICE_N = 0;
record = n1;
p1 = 0; p2 = 0; p3 = 0; p4 = 0;
{
BLOCK[i] = i;
file_record.open("winning_record.txt", ios::app);
if (file_record.is_open())
file_record << n1 << " won the game. " << endl;
/* IN CASE OF PLAYER 2 */
cout << "\n\t" << n2 << " HAS WON THE GAME ! :) " << endl << endl;
CHOICE_N = 0;
record = n2;
p1 = 0; p2 = 0; p3 = 0; p4 = 0;
file_record.open("winning_record.txt", ios::app);
if (file_record.is_open())
file_record << n2 << " won the game. " << endl;
}
/* IN CASE OF PLAYER 3 */
cout << "\n\t" << n3 << " HAS WON THE GAME ! :) " << endl << endl;
CHOICE_N = 0;
record = n3;
p1 = 0; p2 = 0; p3 = 0; p4 = 0;
file_record.open("winning_record.txt", ios::app);
if (file_record.is_open())
file_record << n3 << " won the game. " << endl;
/* IN CASE OF PLAYER 4 */
cout << "\n\t" << n4 << " HAS WON THE GAME ! :) " << endl << endl;
CHOICE_N = 0;
record = n4;
p1 = 0; p2 = 0; p3 = 0; p4 = 0;
file_record.open("winning_record.txt", ios::app);
if (file_record.is_open())
file_record << n4 << " won the game. " << endl;
else if (CHOICE_N == 0)
break;
case 2:
CHOICE_N = 0;
break;
case 3:
{
cout << "\n\t\tAvoid snakes and climb more ladders to win." << endl;
cout << "\n\t\tThe one who reaches 100 first, will win the game :)" << endl << endl;
CHOICE_N = 0;
break;
case 4:
if (record == "")
cout << "\n\t FIRST PLAY THE GAME TO SEE THE RECORD OF GAME \n" << endl;
CHOICE_N = 0;
else
ifstream RECORD;
RECORD.open("winning_record.txt", ios::in);
char ch;
while (!RECORD.eof())
RECORD.get(ch);
cout << ch;
break;
case 5:
cout << "\n YOU CHOOSE TO END THE GAME." << endl;
remove("winning_record.txt");
RUN = false;
break;
return 0;
return 0;
cout << "\n\t 1.\tStart game\n\t 2.\tShow the credits for the game\n\t 3.\tInstructions of game\n\t
4.\tPerivous record of players\n\t 5.\tExit" << endl << endl;
return P_CHOICE;
cout << BLOCK[100] << "\t" << BLOCK[99] << "\t" << BLOCK[98] << "\t" << BLOCK[97] << "\t" <<
BLOCK[96] << "\t"
<< BLOCK[95] << "\t" << BLOCK[94] << "\t" << BLOCK[93] << "\t" << BLOCK[92] << "\t" << BLOCK[91]
<< endl << endl;
cout << BLOCK[81] << "\t" << BLOCK[82] << "\t" << BLOCK[83] << "\t" << BLOCK[84] << "\t" <<
BLOCK[85] << "\t"
<< BLOCK[86] << "\t" << BLOCK[87] << "\t" << BLOCK[88] << "\t" << BLOCK[89] << "\t" << BLOCK[90]
<< endl << endl;
cout << BLOCK[80] << "\t" << BLOCK[79] << "\t" << BLOCK[78] << "\t" << BLOCK[77] << "\t" <<
BLOCK[76] << "\t"
<< BLOCK[75] << "\t" << BLOCK[74] << "\t" << BLOCK[73] << "\t" << BLOCK[72] << "\t" << BLOCK[71]
<< endl << endl;
cout << BLOCK[61] << "\t" << BLOCK[62] << "\t" << BLOCK[63] << "\t" << BLOCK[64] << "\t" <<
BLOCK[65] << "\t"
<< BLOCK[66] << "\t" << BLOCK[67] << "\t" << BLOCK[68] << "\t" << BLOCK[69] << "\t" << BLOCK[70]
<< endl << endl;
cout << BLOCK[60] << "\t" << BLOCK[59] << "\t" << BLOCK[58] << "\t" << BLOCK[57] << "\t" <<
BLOCK[56] << "\t"
<< BLOCK[55] << "\t" << BLOCK[54] << "\t" << BLOCK[53] << "\t" << BLOCK[52] << "\t" << BLOCK[51]
<< endl << endl;
cout << BLOCK[41] << "\t" << BLOCK[42] << "\t" << BLOCK[43] << "\t" << BLOCK[44] << "\t" <<
BLOCK[45] << "\t"
<< BLOCK[46] << "\t" << BLOCK[47] << "\t" << BLOCK[48] << "\t" << BLOCK[49] << "\t" << BLOCK[50]
<< endl << endl;
cout << BLOCK[40] << "\t" << BLOCK[39] << "\t" << BLOCK[38] << "\t" << BLOCK[37] << "\t" <<
BLOCK[36] << "\t"
<< BLOCK[35] << "\t" << BLOCK[34] << "\t" << BLOCK[33] << "\t" << BLOCK[32] << "\t" << BLOCK[31]
<< endl << endl;
cout << BLOCK[21] << "\t" << BLOCK[22] << "\t" << BLOCK[23] << "\t" << BLOCK[24] << "\t" <<
BLOCK[25] << "\t"
<< BLOCK[26] << "\t" << BLOCK[27] << "\t" << BLOCK[28] << "\t" << BLOCK[29] << "\t" << BLOCK[30]
<< endl << endl;
cout << BLOCK[20] << "\t" << BLOCK[19] << "\t" << BLOCK[18] << "\t" << BLOCK[17] << "\t" <<
BLOCK[16] << "\t"
<< BLOCK[15] << "\t" << BLOCK[14] << "\t" << BLOCK[13] << "\t" << BLOCK[12] << "\t" << BLOCK[11]
<< endl << endl;
cout << BLOCK[1] << "\t" << BLOCK[2] << "\t" << BLOCK[3] << "\t" << BLOCK[4] << "\t" << BLOCK[5] <<
"\t"
<< BLOCK[6] << "\t" << BLOCK[7] << "\t" << BLOCK[8] << "\t" << BLOCK[9] << "\t" << BLOCK[10] <<
endl << endl;
int DICE()
return rand() % 6 + 1;
int CHECK_F()
return 1;
else
return 0;
}
OUTPUT: