Fianl Project

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 36

1/15/2020 PROGRAMMING

FUNDAMENTAL
S
FINAL PROJECT
SNAKES AND LADDERS
CODE OF GAME:
#include <iostream>

#include<iomanip>

#include<cstdlib>

#include <ctime>

#include <string>

#include<fstream>

using namespace std;

string RECORD = "";//DECLARING STRING VARIABLE TO STORE RECORD OF THE PLAYED GAME

int R1, R2, R3, R4;

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 DICE();//DECLARING FUNCTION FOR DICE.

int CHOICE_N = -1; // VARIABLE THAT STORES USERS INPUT

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.

int main() // MAIN FUNCTION OF THE PROGRAM

{//BODY OF PROGRAM

srand(time(NULL)); // SRAND USED TO CHANGE RANDOM NUMBERS WITH TIME

bool flag = false, flag1 = false, flag2 = false, flag3 = false,RUN=true;

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/

//FOR LOOP USED TO STORE ARRAY NUMBERS INTO VARIABLE i /

for (int i = 1; i <= 100; i++)

BLOCK[i] = i;
}

// CALLING FUNCTION TO RUN GAME

CHOICE();

while (RUN)

if (CHOICE_N == 0)

CHOICE();

// USING SWITCH FOR CONTROL MAIN MENU

switch (P_CHOICE)

case 1:

cout << "\t\t" << setw(55) << setfill('-') << "\n\n";

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 <<"\t\t" <<setw(55) << setfill('-') <<"\n\n";

cout << "\n\t\tSnakes and Ladders are at different positions on the board\n\t\tThese Positions
are as :\n"<<endl;

cout << "\t\t"<<"SNAKES\t\t\tLADDERS\n\t\t99-to-12\t\t8-to-23\n\t\t79-to-29\t\t19-to-42\n\t\


t71-to-51\t\t33-to-64\n\t\t51-to-2\t\t\t42-to-69\n\t\t35-to-11\t\t43-to-70\n"<<endl;

cout << "\t\t" << setw(55) << setfill('-') << "\n";

cout << "\t\tEnter names of participents : \n\n";


getline(cin, n1);

cout << "\n\t\tName of Player 1 : ";

getline(cin, n1);

cout <<"\t\t"<< setw(45) << setfill('-') << "\n\n";

cout << "\t\tName of Player 2 : ";

getline(cin, n2);

cout <<"\t\t"<<setw(45) << setfill('-') << "\n\n";

cout << "\t\tName of Player 3 : ";

getline(cin, n3);

cout <<"\t\t"<< setw(45) << setfill('-') << "\n\n";

cout << "\t\tName of Player 4 : ";

getline(cin, n4);

cout <<"\t\t"<< setw(45) << setfill('-') << "\n\n";

cout << "\t\t" << setw(55) << setfill('-') << "\n";

cout << "\t\tPlayers Names: \n\n";

cout << "\t\t1. "<<n1 << "\n" <<"\t\t2. "<< n2 << "\n"<<"\t\t3. " << n3 << "\n" <<"\t\t4. "<< n4
<< endl;

/*while LOOP USED TO ROLL THE DICE

UNTIL SOME PLAYER WINS THE GAME.*/

while (PR_PROGRES == 0 && CHOICE_N != 0)

cout << "\n\tPress any number to roll the dice\n\tPress 0, if you want to quit : ";

cin >> CHOICE_N;

cout << endl;

cout << setw(55) << setfill('-') << "\n";

//Rolling FUNCTION CALLED FOR PLAYER 1 TO ROLL THE DICE.


R1 = DICE();

if (R1 != 6 && flag == false && CHOICE_N != 0)

cout << n1 << "Still has not rolled 6 in this turn." << endl << endl;

if (R1 == 6 || flag == true && CHOICE_N != 0)

if ((R1 + p1) > 100)

p1 = p1;

else

/*IF CONDITION WHERE THE PLAYER ROLLS 6.

ANOTHER IF CONDITION TO CHANGE THE POSITION OF PLAYER.*/

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.

in case of player 1.*/

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;

//Rolling FUNCTION CALLED FOR PLAYER 2 TO ROLL THE DICE.

R2 = DICE();

if (R2 != 6 && flag1 == false && CHOICE_N != 0)

cout << n2 << " still has not rolled 6 in this turn." << endl << endl;

if (R2 == 6 || flag1 == true && CHOICE_N != 0)

if ((R2 + p2) > 100)

p2 = p2;

else

/*IF CONDITION WHERE THE PLAYER ROLLS 6.

ANOTHER IF CONDITION TO CHANGE THE POSITION OF PLAYER.*/

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.

in case of player 2.*/

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;

//Rolling FUNCTION CALLED FOR PLAYER 3 TO ROLL THE DICE.

R3 = DICE();

if (R3 != 6 && flag2 == false && CHOICE_N != 0)

cout << n3 << " still has not rolled 6 in this turn." << endl;

if (R3 == 6 || flag2 == true && CHOICE_N != 0)

if ((R3 + p3) > 100)

p3 = p3;
}

else

/*IF CONDITION WHERE THE PLAYER ROLLS 6.

ANOTHER IF CONDITION TO CHANGE THE POSITION OF PLAYER.*/

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.

In case of player 3.*/

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;

/*Rolling FUNCTION CALLED FOR PLAYER 4 TO ROLL THE DICE.*/

R4 = DICE();

if (R4 != 6 && flag3 == false && CHOICE_N != 0)

{
cout << n4 << " still has not rolled 6 in this turn." << endl;

if (R4 == 6 || flag3 == true && CHOICE_N != 0)

if ((R4 + p4) > 100)

p4 = p4;

else

/*IF CONDITION WHERE THE PLAYER ROLLS 6.

ANOTHER IF CONDITION TO CHANGE THE POSITION OF PLAYER.*/

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.

In case of player 4.*/

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;
}

// DIFFERENT NUMBERS USED TO REPRESENT THE POSITIONS OF PLAYERS.

BLOCK[p1] = 1000;

BLOCK[p2] = 2000;

BLOCK[p3] = 3000;

BLOCK[p4] = 4000;

// IF CONDITIONS USED TO REPRESENT VALUE OF DICE AS WELL AS POSITION OF PLAYERS

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();

/* CALLED THE Check FUNCTION FOR THE PROGRESS OF THE PLAYERS.

STORED IT IN THE VARIABLE progress. */

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 << setw(50) << setfill('-') << "\n";

cout << "\n\t" << n1 << " HAS WON THE GAME ! :) " << endl << endl;

cout << setw(50) << setfill('-') << "\n";

CHOICE_N = 0;

record = n1;

p1 = 0; p2 = 0; p3 = 0; p4 = 0;

flag = false; flag1 = false; flag2 = false; flag3 = false;

for (int i = 1; i <= 100; i++)

{
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 */

else if (BLOCK[100] == 2000)

cout << setw(50) << setfill('-') << "\n";

cout << "\n\t" << n2 << " HAS WON THE GAME ! :) " << endl << endl;

cout << setw(50) << setfill('-') << "\n";

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 */

else if (BLOCK[100] == 3000)

cout << setw(50) << setfill('-') << "\n";

cout << "\n\t" << n3 << " HAS WON THE GAME ! :) " << endl << endl;

cout << setw(50) << setfill('-') << "\n";

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 */

else if (BLOCK[100] == 4000)

cout << setw(50) << setfill('-') << "\n";

cout << "\n\t" << n4 << " HAS WON THE GAME ! :) " << endl << endl;

cout << setw(50) << setfill('-') << "\n";

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;

/* IF 0 IS ENTERED BY THE USER.

THE GAME WILL BE FINISHED. */

else if (CHOICE_N == 0)

cout << "\n\t\t!!...GAME ENDED ! :)\n\t\t!!....GOOD LUCK....!!\n" << endl;

break;

/* SHOW THE CREDITS FOR THE GAME IF USER ENTERS 2 */

case 2:

cout << "\n\t\tMirza Noman Haider\n" << endl << endl;

CHOICE_N = 0;

break;

/* SHOWING THE INSTRUCTIONS OF THE GAME IF USER ENTERS 3 */

case 3:
{

cout << "\n\t\tFours players game " << endl;

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;

/* SHOW THE RECORD STORED IN THE FILE IF USER ENTERS 4.

IF NO RECORD IS PRESENT, A MESSAGE WILL BE DISPLAYED. */

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;

cout << endl;

break;

/* IF USER ENTERS 5, THE GAME ENDS.*/

case 5:

cout << "\n YOU CHOOSE TO END THE GAME." << endl;

remove("winning_record.txt");

RUN = false;

break;

return 0;

return 0;

/* DEFINNING THE CHOICE FUNCTION*/


int CHOICE()

cout << "\t\t" << setw(55) << setfill('-') << "\n";

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;

cout << "\t\t" << setw(55) << setfill('-') << "\n";

cout << "\t\tEnter your choice between 1 to 5 : ";

cin >> P_CHOICE;

return P_CHOICE;

/* DEFINING THE playboard FUNCTION AND DRAWING BOARD*/

void P_BOARD()//define the board drawing mechanism!

cout << "----------------------------------------------------------------------------" << endl << endl;

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;

cout << "----------------------------------------------------------------------------" << endl;

/* DEFINING THE Rolling FUNCTION.

IT GENERATES RANDOM NUMBERS BETWEEN 1 AND 6*/

int DICE()

return rand() % 6 + 1;

/* DEFINING THE Check FUNCTION.

IT ENDS THE PROGRAM IF ANY PLAYER HAS REACHED 100.

THE GAME CONTINUES OTHERWISE.*/

int CHECK_F()

if (BLOCK[100] == 1000 || BLOCK[100] == 2000 || BLOCK[100] == 3000 || BLOCK[100] == 4000)

return 1;

else

return 0;

}
OUTPUT:

You might also like