Pages

Wednesday, May 28, 2014

Connect 4 Game In C++ Program

Two players play that game , it’s slightly different from tic tac toe. The difference is:
1. The player who plot 4 of his/her marks (cross or zero) horizontaly , verticaly , diagonaly , counter diagonal any way but in a straight line makes first wins.
2.There is the base from where every player start the game.

  1. /**********************************

  2. SOURCE CODE

  3. **********************************/


  4. #include

  5. #include

  6. #include

  7. #include



  8. char re_assign[31] = {'o','1','2','3','4','5','6'};

  9. char square[31];

  10. //horizontal //diagonal //opp diagonal //for vetical

  11. int check[39][4] = { {1,2,3,4} , {2,3,4,5} ,{3,4,5,6} ,{7,8,9,10} ,{8,9,10,11} , {9,10,11,12} , {13,14,15,16} , {14,15,16,17} , {15,16,17,18} , {19,20,21,22} , {20,21,22,23} , {21,22,23,24} ,{25,26,27,28} , {26,27,28,29} , {27,28,29,30} , {1,8,15,22} , {2,9,16,23} , {3,10,17,24} , {7,14,21,28} , {8,15,22,29} ,{9,16,23,30} , {4,9,13,18} , {5,10,14,19} , {6,11,15,20} , {10,15,20,25} , {11,16,21,26} , {12,17,22,27} , {1,7,13,19} , {7,13,19,25} , {2,8,14,20} , {8,14,20,26} , {3,9,15,21} , {9,15,21,26} , {4,10,16,22} , {10,16,22,28} , {5,11,17,23} , {11,17,23,29} , {6,12,18,24} , {12,18,24,30} };

  12. char plr1[40],plr2[40];

  13. int score1=0,score2=0, game=0 ,games;

  14. int choice_m,choice_vm;

  15. int checkwin();

  16. void input_name();

  17. void show_name(int);

  18. void new_game();

  19. void new_match();

  20. void new_series();

  21. void show();

  22. void board();


  23. struct player

  24. {

  25. char playername[50];

  26. int score;

  27. int game,games;

  28. };

  29. player pl1,pl2;


  30. void main()

  31. {

  32. do

  33. {


  34. show();

  35. cout<<"\n\n 1.Continue previous series.";

  36. cout<<"\n 2.Start a new game.";

  37. cout<<"\n 3.Quit Game.";

  38. cout<<"\n\n Enter your choice.";

  39. cin>>choice_vm;

  40. switch(choice_vm)//choice_vm=choice of void main

  41. {

  42. case 1:

  43. clrscr();

  44. show();

  45. fstream fin;

  46. fin.open("player_info.dat",ios::binary|ios::in);

  47. if(!fin)

  48. {

  49. cout<<"\n\n NO! saved sereis found ???";

  50. getch();

  51. break;

  52. }

  53. fin.read((char*)&pl1,sizeof(pl1));

  54. strcpy(plr1,pl1.playername);

  55. score1=pl1.score;

  56. game=pl1.game;

  57. games=pl1.games;

  58. fin.read((char*)&pl2,sizeof(pl1));

  59. strcpy(plr2,pl2.playername);

  60. score2=pl2.score;

  61. fin.close();

  62. new_series();

  63. break;


  64. case 2:

  65. new_game();

  66. break;


  67. case 3:


  68. show();

  69. cout<<"\n\n Bye...";

  70. getch();

  71. break;


  72. default:

  73. cout<<"\n Wrong choice";

  74. getch();


  75. }

  76. }

  77. while(choice_vm != 3);

  78. }


  79. /***************************************************/

  80. //starting of a new game

  81. /***************************************************/

  82. void new_game()

  83. {

  84. input_name();

  85. do

  86. {


  87. show();

  88. cout<<"\n\n NEW GAME MENU";

  89. cout<<"\n\n 1.Start a match up game.";

  90. cout<<"\n 2.Start a series of this game.";

  91. cout<<"\n 3.To Quit.";

  92. cout<<"\n\n Enter your choice";

  93. cin>>choice_m;

  94. switch (choice_m)

  95. {

  96. case 1:

  97. new_match();

  98. break;


  99. case 2:

  100. new_series();

  101. break;


  102. case 3:

  103. break;


  104. default:

  105. cout<<"\n Wrong choice";

  106. getch();

  107. }

  108. }

  109. while(choice_m!=3);

  110. }



  111. /*********************************************/

  112. //to start a new game

  113. /*********************************************/

  114. void new_match()

  115. {


  116. int player = 1,i,s1=1,s2=2,s3=3,s4=4,s5=5,s6=6;

  117. char mark,choice_g;


  118. for(int z=0;z<=31;z++) //to reassign value

  119. square[z]=re_assign[z];

  120. if(choice_m == 2)

  121. player=(game%2)?2:1;

  122. do

  123. {

  124. board();

  125. player=(player%2)?1:2;

  126. cout <<"\n It's ";

  127. show_name(player);

  128. cout<< "'s turn , enter a number: ";

  129. cin >> choice_g;

  130. mark=(player == 1) ? 'X' : 'O';

  131. if (choice_g == '1' && s1 <= 25)

  132. {

  133. square[s1] = mark;

  134. s1+=6;

  135. square[s1]='1';

  136. }

  137. else if (choice_g == '2' && s2 <= 26)

  138. {

  139. square[s2] = mark;

  140. s2+=6;

  141. square[s2]='2';

  142. }

  143. else if (choice_g == '3' && s3 <= 27)

  144. {

  145. square[s3] = mark;

  146. s3+=6;

  147. square[s3]='3';

  148. }

  149. else if (choice_g == '4' && s4 <= 28)

  150. {

  151. square[s4] = mark;

  152. s4+=6;

  153. square[s4]='4';

  154. }

  155. else if (choice_g == '5' && s5 <= 29)

  156. {

  157. square[s5] = mark;

  158. s5+=6;

  159. square[s5]='5';

  160. }

  161. else if (choice_g == '6' && s6 <= 30)

  162. {

  163. square[s6] = mark;

  164. s6+=6;

  165. square[s6]='6';

  166. }

  167. else if (choice_g == 'q' || choice_g == 'Q')

  168. {

  169. show();

  170. cout<<"\n\n\a ==>Game quited.";

  171. getch();

  172. if(choice_m == 2)

  173. game--;

  174. return;

  175. }

  176. else

  177. {

  178. cout<<"\n Invalid move ";

  179. player--;

  180. getch();

  181. }

  182. i=checkwin();

  183. player++;

  184. }while(i==-1);

  185. board();

  186. if(i==1)

  187. {

  188. --player;

  189. if(choice_m == 2)

  190. {

  191. if(player==1)

  192. score1++;

  193. else

  194. score2++;

  195. }

  196. cout<<" ==>\a";show_name(player);cout<<" win !!! ";

  197. }

  198. else

  199. {

  200. cout<<" ==>\aGame draw";

  201. if(choice_m == 2)

  202. {

  203. score1++;score2++;

  204. }

  205. }

  206. getch();

  207. }

  208. /*********************************************

  209. start a new series

  210. **********************************************/

  211. void new_series()

  212. {

  213. clrscr();

  214. int choice_s;

  215. if(choice_vm != 1)

  216. {

  217. score1=score2=0;

  218. show();

  219. cout<<"\n\n How many games you want to play.";

  220. cin>>games;

  221. if(games>50)

  222. {

  223. cout<<"\n Error!@!?!@!";

  224. getch();

  225. return;

  226. }


  227. }


  228. do

  229. {

  230. show();

  231. cout<<"\n\n SERIES MENU";

  232. cout<<"\n\n 1.Start the new game.";

  233. cout<<"\n 2.Show the score.";

  234. cout<<"\n 3.Quit and save series.";

  235. cout<<"\n\n Enter your choice";

  236. cin>>choice_s;


  237. switch (choice_s)

  238. {

  239. case 1:

  240. clrscr();

  241. new_match();

  242. game++;

  243. if(game == games)

  244. {

  245. show();

  246. if(score1>score2)

  247. cout<<endl<<endl<<"\a ==>"<<plr1 <<" won the series ���!!";

  248. else if(score1<score2)

  249. cout<<endl<<endl<<"\a ==>"<<plr2 <<" won the series ���!!";

  250. else

  251. cout<<"\n\n\a ==>Series Draw !!!";

  252. getch();

  253. return;

  254. }

  255. break;


  256. case 2:

  257. show();

  258. cout<<"\n\n Total games - "<<games;

  259. cout<<"\n\n SCORES";

  260. cout<<"\n\n "<<plr1<<" - "<<score1<<endl;

  261. cout<<" "<<plr2<<" - "<<score2;

  262. getch();

  263. break;


  264. case 3:

  265. show();

  266. fstream fout;


  267. fout.open("player_info.dat",ios::binary|ios::out);

  268. if(fout)

  269. {

  270. char o;

  271. show();

  272. cout<<"\n\n Do you want to overwrite previous series?(Y/N)";

  273. cin>>o;

  274. if(o=='n' || o=='n')

  275. break;

  276. }


  277. strcpy(pl1.playername,plr1);

  278. pl1.score=score1;

  279. strcpy(pl2.playername,plr2);

  280. pl2.score=score2;

  281. pl1.game=game;

  282. pl1.games=games;


  283. fout.write((char*)&pl1,sizeof(pl1));

  284. fout.write((char*)&pl2,sizeof(pl1));


  285. show();

  286. cout<<"\n\n Series saved";

  287. getch();

  288. fout.close();

  289. break;


  290. default:

  291. cout<<"\n Wrong choice";

  292. getch();

  293. }

  294. }

  295. while(choice_s != 3);


  296. }

  297. /*********************************************

  298. FUNCTION TO RETURN GAME STATUS

  299. 1 FOR GAME IS OVER WITH RESULT

  300. -1 FOR GAME IS IN PROGRESS

  301. O GAME IS OVER AND NO RESULT

  302. **********************************************/


  303. int checkwin()

  304. {

  305. //*****************************//


  306. //to check if anybdy won the game...


  307. int l=0;

  308. for(int m=0;m<=39;m++)

  309. {

  310. if(square[check[m][l]] == square[check[m][l+1]] && square[check[m][l+1]] == square[check[m][l+2]] && square[check[m][l+2]] == square[check[m][l+3]] && ((square[check[m][l]] == 'X') || (square[check[m][l]] == 'O')))

  311. {

  312. square[check[m][l]] = square[check[m][l+1]] = square[check[m][l+2]] = square[check[m][l+3]] = '�';

  313. return 1;

  314. }

  315. }


  316. //*****************************//


  317. //*****************************//

  318. //for draw


  319. int count1=0;

  320. for(int draw1=1;draw1<=30;draw1++)

  321. {

  322. if( square[draw1] == 'X' || square[draw1] == 'O')

  323. count1++;

  324. }



  325. if(count1==30)

  326. return 0;


  327. /***********************************************/


  328. //nobody win till now


  329. else

  330. return -1;

  331. }

  332. /**************************************************/

  333. //FUNCTION TO INSERT NAME

  334. /**************************************************/

  335. void input_name()

  336. {

  337. clrscr();

  338. show();

  339. cout<<"\n\n Enter the first player name.";

  340. gets(plr1);

  341. cout<<"\n Enter the second player name.";

  342. gets(plr2);

  343. }

  344. /*******************************************************/

  345. //FUNCTION TO SHOW NAME

  346. /*******************************************************/

  347. void show_name(int z)

  348. {

  349. if(z == 1)

  350. cout<<plr1;

  351. else

  352. cout<<plr2;

  353. }

  354. /*******************************************************************

  355. //FUNCTION YO SHOW GAME CONNECT FOUR

  356. /*******************************************************************/

  357. void show()

  358. {

  359. clrscr();

  360. cout<<"\n\n\n\n GAME CONNECT FOUR";

  361. }

  362. /*******************************************************************

  363. FUNCTION TO DRAW BOARD OF SHAGGY's GAME WITH PLAYERS MARK

  364. ********************************************************************/



  365. void board()

  366. {

  367. clrscr();

  368. cout <<endl<<endl;

  369. cout <<"\n\n "<< " GAME CONNECT FOUR \n\n";

  370. cout <<" "<< plr1<<" (X)";if(choice_m == 2) cout<<" - "<<score1;cout<<" :: "<<plr2<<" (O)";if( choice_m == 2) cout<<" - "<<score2; cout<< endl << endl;

  371. cout <<"\n "<< "Press q to quit from the game."<<endl<<endl;

  372. cout <<" "<< " | | | | | " << endl;

  373. cout <<" "<< " ";cout<<square[25];cout <<" | "; cout<< square[26]; cout << " | " ; cout<< square[27]; cout <<" | "; cout<<square[28];cout<<" | "; cout<<square[29];cout<<" | "; cout<<square[30];cout<< endl;

  374. cout <<" "<< "_____|_____|_____|_____|_____|_____" << endl;

  375. cout <<" "<< " | | | | | " << endl;

  376. cout <<" "<< " ";cout<<square[19];cout <<" | "; cout<< square[20]; cout << " | " ; cout<< square[21]; cout <<" | "; cout<<square[22];cout<<" | "; cout<<square[23];cout<<" | "; cout<<square[24];cout<< endl;

  377. cout <<" "<< "_____|_____|_____|_____|_____|_____" << endl;

  378. cout <<" "<< " | | | | | " << endl;

  379. cout <<" "<< " ";cout<<square[13];cout <<" | "; cout<< square[14]; cout << " | " ; cout<< square[15]; cout <<" | "; cout<<square[16];cout<<" | "; cout<<square[17];cout<<" | "; cout<<square[18];cout<< endl;

  380. cout <<" "<< "_____|_____|_____|_____|_____|_____" << endl;

  381. cout <<" "<< " | | | | | " << endl;

  382. cout <<" "<< " ";cout<<square[7];cout <<" | "; cout<< square[8]; cout << " | " ; cout<< square[9]; cout <<" | "; cout<<square[10];cout<<" | "; cout<<square[11];cout<<" | "; cout<<square[12];cout<< endl;

  383. cout <<" "<< "_____|_____|_____|_____|_____|_____" << endl;

  384. cout <<" "<< " | | | | | " << endl;

  385. cout <<" "<< " " << square[1] <<" | " << square[2] << " | " << square[3] <<" | "<<square[4]<<" | "<<square[5]<<" | "<<square[6]<< endl;

  386. cout <<" "<< " | | | | | " << endl;


  387. }


  388. /*******************************************************************

  389. END OF CODE

  390. ********************************************************************/

  391.  



No comments:

Post a Comment