main.c
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <conio.h> #include <process.h> #include <string.h> struct user { char name[20]; int score; }p1,p2; // First we go for the toss. This function basically does the toss as to who would bat or ball first depending on who won the toss. int toss() { srand ( time(NULL) ); char c; int m,ts,k,b,ct; printf("\nChoose ----- ODD OR EVEN (O/E) : "); scanf("%s",&c); printf("\nPlay your move : "); scanf("%d",&m); ts=rand() % 6 + 1; printf("Computer's move : %d",ts); getch(); k=ts+m; if(c=='E'||c=='e') { if(k%2==0) { printf("\n\nYOU WON THE TOSS!!\n\n"); getch(); printf("CHOOSE ---- BAT (1) or BOWL (2) : "); scanf("%d",&b); if(b==1) {printf("%s","you choose to bat first /n"); return 1;} else {printf("%s","you choose to ball first /n"); return 2;} } else { printf("\n\nYOU LOST THE TOSS\n\n"); srand(time(NULL)); ct=rand() % 2 + 1; if(ct==2) { printf("COMPUTER WOULD LIKE TO BAT FIRST\n"); getch(); return 2; } else { printf("COMPUTER WOULD LIKE TO BOWL FIRST\n"); getch(); return 1; } } } else if(c=='O'||c=='o') { if(k%2!=0) { printf("\n\nYOU WON THE TOSS\n\n"); getch(); printf("WHAT WOULD YOU LIKE TO CHOOSE ---- BAT (1) or BOWL (2) : "); scanf("%d",&b); if(b==1) {printf("%s","you choose to bat first /n"); return 1;} else {printf("%s","you choose to ball first /n"); return 2;} return 1; } else { printf("\n\nYOU LOST THE TOSS\n\n"); srand(time(NULL)); ct=rand() % 2 + 1; if(ct==2) { printf("COMPUTER WOULD LIKE TO BAT FIRST\n"); getch(); return 2; } else { printf("COMPUTER WOULD LIKE TO BOWL FIRST\n"); getch(); return 1; } } } } //This is the main menu that welcomes a new player int menu() { int c,series; printf("\t\t\t\t WELCOME\n\n\t\t\tCHOOSE FROM THE MENU BELOW : \n\n\t\t\t1.PLAY A SINGLE GAME vs COMPUTER\n\t\t\t2.PLAY A SERIES GAME\n\t\t\t3.EXIT\n\n\t\t\t"); scanf("%d",&c); printf("\n"); switch(c) { case 1 : return 1; case 2 : printf("HOW MANY MATCHES YOU WANNA PLAY IN THE SERIES (3 OR 5) : "); scanf("%d",&series); break; case 3 : system("cls"); printf("\nEXITING FROM THE GAME\n"); exit(0); } } void pwc() { int t,m,n,cn,b,cs; char ch,name[20]; printf("\t\t\tEnter your name : "); scanf("%s",&p1.name); strcpy(name,p1.name); do{ system("cls"); printf("\t\t\t WELCOME %s\n\n",toupper(name)); printf("\t\t\tIT'S TIME FOR THE TOSS\n"); t=toss(); p1.score=0; cs=0; if(t==1) { system("cls"); printf("\t\t\t---->ITS YOUR TURN TO BAT<----\n\n"); m=0; do { printf("\nPlay your Move : "); scanf("%d",&m); if(m==0) exit(0); srand ( time(NULL) ); n=rand() % 6 + 1; printf("Computer's Move : %d\n",n); p1.score+=m; if(m!=n) printf("\t\t\t\tYOUR SCORE : %d\n",p1.score); }while(m!=n); p1.score-=m; printf("\n\t\t\t!!!!!!! OUT !!!!!!!\n\n"); printf("\n\nYOUR SCORE : %d\n\n",p1.score); getch(); system("cls"); printf("\t\t\t\tTARGET FOR COMPUTER : %d\n\n",p1.score+1); printf("\t\t\t---->Now its Computer's turn to BAT<----\n\n"); cn=0; do{ printf("Play your Move : "); scanf("%d",&b); if(m==0) exit(0); srand ( time(NULL) ); cn=rand() % 6 + 1; printf("Computer's Move : %d\n",cn); cs+=cn; if(cn!=b) { printf("\t\t\t\tCOMPUTER'S SCORE : %d\n\n",cs); if((p1.score-cs)<=10&&p1.score>cs) printf("\t\t\t\tCOMPUTER NEEDS %d MORE RUNS TO WIN\n",p1.score-cs+1); } }while((cn!=b)&&(cs<=p1.score)); if(cn==b) { cs-=cn; printf("\n\n\t\t\t!!!!!!!COMPUTER --- OUT!!!!!!!\n\nCOMPUTER'S SCORE : %d\n\n",cs); if(cs<p1.score) printf("\n\t\t\t\t----------\n\t\t\t\t YOU WON\n\t\t\t\t----------\n"); else if(cs==p1.score) printf("\nMATCH TIE\n"); else { printf("\n\t\t\t\t----------\n\t\t\t\t YOU LOST\n\t\t\t\t----------\n"); printf("\n\t\t\t\tCOMPUTER WON BY 1 WICKET\n"); } } else { printf("\n\t\t\t\t----------\n\t\t\t\t YOU LOST\n\t\t\t\t----------\n"); printf("\n\t\t\t\tCOMPUTER WON BY 1 WICKET\n"); } } else if(t==2) { system("cls"); printf("\t\t\t---->ITS COMPUTER'S TURN TO BAT<----\n\n"); cn=0; do{ printf("\nPlay your Move : "); scanf("%d",&b); if(b==0) exit(0); srand ( time(NULL) ); cn=rand() % 6 + 1; printf("Computer's Move : %d\n",cn); cs+=cn; if(cn!=b) printf("\t\t\t\tCOMPUTER'S SCORE : %d\n",cs); }while(cn!=b); cs-=cn; printf("\n\n\t\t\t!!!!!!!COMPUTER --- OUT!!!!!!!\n\nCOMPUTER'S SCORE : %d\n\n",cs); getch(); system("cls"); printf("\t\t\t\t TARGET : %d\n\n",cs+1); printf("\t\t\t---->Now its Your turn to BAT<----\n\n"); m=0; do { printf("\nPlay your Move : "); scanf("%d",&m); if(m==0) exit(0); srand ( time(NULL) ); n=rand() % 6 + 1; printf("Computer's Move : %d\n",n); p1.score+=m; if(m!=n) { printf("\t\t\t\tYOUR SCORE : %d\n",p1.score); if((cs-p1.score)<=10&&cs>p1.score) printf("\t\t\t\tYOU NEED %d MORE RUNS TO WIN\n",cs-p1.score+1); } }while(m!=n&&(cs>=p1.score)); if(m==n) { p1.score-=m; printf("\n\n\t\t\t!!!!!!! OUT !!!!!!!\n\n"); printf("\n\nYOUR SCORE : %d\n\n",p1.score); if(cs<p1.score) printf("\n\t\t\t\t----------\n\t\t\t\t YOU WON\n\t\t\t\t----------\n"); else if(cs==p1.score) printf("\nMATCH TIE\n"); else { printf("\n\t\t\t\t----------\n\t\t\t\t YOU LOST\n\t\t\t\t----------\n"); printf("\n\t\t\t COMPUTER WON BY %d RUNS\n",cs-p1.score); } } else printf("\n\t\t\t\t----------\n\t\t\t\t YOU WON\n\t\t\t\t----------\n"); } printf("\nWANT TO PLAY AGAIN (Y/N) : "); scanf("%s",&ch); }while(ch=='y'||ch=='Y'); } //This is the main function int main() { int men; men=menu(); if(men==1) pwc(); /*else if(m==2) p2p();*/ return 0; } |