snake and ladder game code in c 2b 2b download

Solutions on MaxInterview for snake and ladder game code in c 2b 2b download by the best coders in the world

showing results for - "snake and ladder game code in c 2b 2b download"
Ella
05 Nov 2020
1//***************************************************************
2//                   Source Code
3//****************************************************************
4
5#include<iostream.h>
6#include<conio.h>
7#include<stdlib.h>
8#include<stdio.h>
9#include<time.h>
10
11void draw_line(int n, char ch);
12void board();
13void gamescore(char name1[], char name2[], int p1, int p2);
14void play_dice(int & score);
15
16void main()
17{
18    int player1 = 0, player2 = 0, lastposition;
19    char player1name[80], player2name[80];
20    clrscr();
21    randomize();
22    draw_line(50, '=');
23    cout << "\n\n\n\n\t\tSNAKE LADDER GAME\n\n\n\n";
24    draw_line(50, '=');
25    cout << "\n\n\nEnter Name of player 1 :";
26    gets(player1name);
27    cout << "\n\n\Enter Name of player 2 :";
28    gets(player2name);
29    while (player1 <= 100 && player2 <= 100) 
30	{
31        board();
32        gamescore(player1name, player2name, player1, player2);
33        cout << "\n\n--->" << player1name << " Now your Turn >> Press any key to play ";
34        getch();
35        lastposition = player1;
36        play_dice(player1);
37        if (player1 < lastposition)
38            cout << "\n\aOops!! Snake found !! You are at postion " << player1 << "\n";
39        else if (player1 > lastposition + 6)
40            cout << "\nGreat!! you got a ladder !! You are at position " << player1;#
41        include < iostream.h >
42            cout << "\n\n--->" << player2name << " Now your Turn >> Press any key to play ";
43        getch();
44        lastposition = player2;
45        play_dice(player2);
46        if (player2 < lastposition)
47            cout << "\n\n\aOops!! Snake found !! You are at position " << player2 << "\n";
48        else if (player2 > lastposition + 6)
49            cout << "\n\nGreat!! you got a ladder !! You are at position " << player2 << "\n";
50        getch();
51    }
52    clrscr();
53    cout << "\n\n\n";
54    draw_line(50, '+');
55    cout << "\n\n\t\tRESULT\n\n";
56    draw_line(50, '+');
57    cout << endl;
58    gamescore(player1name, player2name, player1, player2);
59    cout << "\n\n\n";
60    if (player1 >= player2)
61        cout << player1name << " !! You are the winner of the game\n\n";
62    else
63        cout << player2name << " !! You are the winner of the game\n\n";
64    draw_line(50, '+');
65    getch();
66}
67void draw_line(int n, char ch) 
68{
69    for (int i = 0; i < n; i++)
70        cout << ch;
71}
72
73void board() 
74{
75    clrscr();
76    cout << "\n\n";
77    draw_line(50, '-');
78    cout << "\n\t\tSNAKE AT POSITION\n";
79    draw_line(50, '-');
80    cout << "\n\tFrom 98 to 28 \n\tFrom 95 to 24\n\tFrom 92 to 51\n\tFrom 83 to 19\n\tFrom 73 to 1\n\tFrom 69 to 33\n\tFrom 64 to 36\n\tFrom 59 to 17\n\tFrom 55 to 7\n\tFrom 52 to 11\n\tFrom 48 to 9\n\tFrom 46 to 5\n\tFrom 44 to 22\n\n";
81    draw_line(50, '-');
82    cout << "\n\t\t LADDER AT POSITION\n";
83    draw_line(50, '-');
84    cout << "\n\tFrom 8 to 26\n\tFrom 21 to 82\n\tFrom 43 to 77\n\tFrom 50 to 91\n\tFrom 62 to 96\n\tFrom 66 to 87\n\tFrom 80 to 100\n";
85    draw_line(50, '-');
86    cout << endl;
87}
88
89void gamescore(char name1[], char name2[], int p1, int p2) 
90{
91    cout << "\n";
92    draw_line(50, '~');
93    cout << "\n\t\tGAME STATUS\n";
94    draw_line(50, '~');
95    cout << "\n\t--->" << name1 << " is at position " << p1 << endl;
96    cout << "\t--->" << name2 << " is at position " << p2 << endl;
97    draw_line(50, '_');
98    cout << endl;
99}
100
101void play_dice(int & score) 
102{
103    int dice;
104    dice = random(6) + 1;
105    cout << "\nYou got " << dice << " Point !! ";
106    score = score + dice;
107    cout << "Now you are at position " << score;
108    switch (score) 
109	{
110    case 98:
111        score = 28;
112        break;
113    case 95:
114        score = 24;
115        break;
116    case 92:
117        score = 51;
118        break;
119    case 83:
120        score = 19;
121        break;
122    case 73:
123        score = 1;
124        break;
125    case 69:
126        score = 33;
127        break;
128    case 64:
129        score = 36;
130        break;
131    case 59:
132        score = 17;
133        break;
134    case 55:
135        score = 7;
136        break;
137    case 52:
138        score = 11;
139        break;
140    case 48:
141        score = 9;
142        break;
143    case 46:
144        score = 5;
145        break;
146    case 44:
147        score = 22;
148        break;
149    case 8:
150        score = 26;
151        break;
152    case 21:
153        score = 82;
154        break;
155    case 43:
156        score = 77;
157        break;
158    case 50:
159        score = 91;
160        break;
161    case 54:
162        score = 93;
163        break;
164    case 62:
165        score = 96;
166        break;
167    case 66:
168        score = 87;
169        break;
170    case 80:
171        score = 100;
172    }
173}
174
175//***************************************************************
176// END OF PROJECT
177//***************************************************************