Advertisement
Guest User

Untitled

a guest
Nov 21st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.83 KB | None | 0 0
  1.  
  2. #include<stdio.h>
  3. #include <stdlib.h>
  4. #include <time.h>
  5.  
  6. int main()
  7. {
  8.  
  9. int main_board[9]={1,2,3,4,5,6,7,8,9}; // It defines the main board to store all the values from 1-9
  10. //int missing_array[9]; // This line generates an array of size 9 to keep track of missing values
  11. int game_board[9]; // It defines an array to form the actual game board with random numbers and hidden values
  12. int temp_board[9]; // It defines an array which is a copy of the game_board and will be displayed to the user and will be updated in each iteration
  13. int row_results[3]; // this is an array that stores the results of calculation of each row
  14. int column_results[3]; // this is an array that stores the results of calculation of each column
  15. char operators_array[12]; // this is an array that stores random operations
  16. int i,j; // these variables will be used for loop indexes
  17. int rand_num,temp1; // rand_num will be used to store the result of random number generations, temp1 will be used as a temporary variable
  18. int level; // this variable stores the level of difficulty
  19. char check_game; // this is a flag variable to determine if the user solves the game correctly
  20. int play_again; // this is a flag variable to determine if the user wants to play again or not
  21. int missing_count; // this is a variable that keep track of the number of missing values in the game board.
  22.  
  23. srand(time(NULL)); // It generates a seed, using current time, for the random generator
  24.  
  25. do{
  26. check_game=1;
  27. level=0;
  28. printf("Welcome to Nine-Gaps game!...\n");
  29. printf("*****************************\n");
  30. printf(" ***************** \n");
  31. printf(" ***** \n");
  32. printf(" * \n");
  33.  
  34. // get the level of difficulty from the user
  35. do{
  36. puts("Choose the level of difficulty (1-Beginner, 2-Intermediate, 3-Advanced, 4-Expert):");
  37. scanf("%d",&level);
  38. if ((level < 1) || (level > 4))
  39. puts("The entered value is not valid");
  40. else{
  41. switch (level){
  42. case 1:
  43. puts("You have chosen the Beginner level");
  44. break;
  45. case 2:
  46. puts("You have chosen the Intermediate level");
  47. break;
  48. case 3:
  49. puts("You have chosen the Advanced level");
  50. break;
  51. case 4:
  52. puts("You have chosen the Expert level");
  53. break;
  54. }
  55. break;
  56. }
  57. } while (1);
  58. //////////////////////////////////////////////////
  59.  
  60. // 1- generate a random main board by shuffling the main_board (COMPLETE THIS PART)
  61. int temp;
  62. for (int i=0;i<8;i++){
  63. int j=(rand()%9);
  64. temp=main_board[i];
  65. main_board[i]=main_board[j];
  66. main_board[j]=temp;
  67. }
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77. /////////////////////////////////////////////////
  78.  
  79.  
  80. // 2- generate random operators ( + , - , x ) (COMPLETE THIS PART)
  81. for(int i=0;i<12;i++){
  82. int op=(rand()%3);
  83. switch(op){
  84. case 0:
  85. operators_array[i]='+';
  86. break;
  87. case 1:
  88. operators_array[i]='-';
  89. break;
  90. case 2:
  91. operators_array[i]='*';
  92. break;
  93. }
  94. }
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /////////////////////////////////////////////////
  106.  
  107. // 3- Calculate the results in columns and rows (COMPLETE THIS PART)
  108. for(int i=0;i<3;i++){
  109. if(operators_array[2*i]=='+' && operators_array[2*i+1]=='+')
  110. row_results[i]=main_board[3*i]+main_board[3*i+1]+main_board[3*i+2];
  111. else if(operators_array[2*i]=='-' && operators_array[2*i+1]=='+')
  112. row_results[i]=main_board[3*i]-main_board[3*i+1]+main_board[3*i+2];
  113. else if(operators_array[2*i]=='+' && operators_array[2*i+1]=='-')
  114. row_results[i]=main_board[3*i]+main_board[3*i+1]-main_board[3*i+2];
  115. else if(operators_array[2*i]=='-' && operators_array[2*i+1]=='-')
  116. row_results[i]=main_board[3*i]-main_board[3*i+1]-main_board[3*i+2];
  117.  
  118.  
  119. else if(operators_array[2*i]=='*' && operators_array[2*i+1]=='*')
  120. row_results[i]=main_board[3*i] * main_board[3*i+1] * main_board[3*i+2];
  121. else if(operators_array[2*i]=='-' && operators_array[2*i+1]=='*')
  122. row_results[i]=(main_board[3*i]-main_board[3*i+1]) * main_board[3*i+2];
  123. else if(operators_array[2*i]=='*' && operators_array[2*i+1]=='-')
  124. row_results[i]=main_board[3*i] * main_board[3*i+1]-main_board[3*i+2];
  125.  
  126.  
  127. else if(operators_array[2*i]=='*' && operators_array[2*i+1]=='+')
  128. row_results[i]=main_board[3*i] * main_board[3*i+1]+main_board[i+2];
  129. else if(operators_array[2*i]=='+' && operators_array[2*i+1]=='*')
  130. row_results[i]=(main_board[3*i]+main_board[3*i+1]) * main_board[i+2];
  131.  
  132. }
  133. for(int i=0;i<3;i++){
  134. if(operators_array[2*i+6]=='+' && operators_array[2*i+7]=='+')
  135. column_results[i]=main_board[i]+main_board[i+3]+main_board[i+6];
  136. else if(operators_array[2*i+6]=='-' && operators_array[2*i+7]=='+')
  137. column_results[i]=main_board[i]-main_board[i+3]+main_board[i+6];
  138. else if(operators_array[2*i+6]=='+' && operators_array[2*i+7]=='-')
  139. column_results[i]=main_board[i]+main_board[i+3]-main_board[i+6];
  140. else if(operators_array[2*i+6]=='-' && operators_array[2*i+7]=='-')
  141. column_results[i]=main_board[i]-main_board[i+3]-main_board[i+6];
  142.  
  143.  
  144. else if(operators_array[2*i+6]=='*' && operators_array[2*i+7]=='*')
  145. column_results[i]=main_board[i] * main_board[i+3] * main_board[i+6];
  146. else if(operators_array[2*i+6]=='-' && operators_array[2*i+7]=='*')
  147. column_results[i]=(main_board[i]-main_board[i+3]) * main_board[i+6];
  148. else if(operators_array[2*i+6]=='*' && operators_array[2*i+7]=='-')
  149. column_results[i]=main_board[i] * main_board[i+3] - main_board[i+6];
  150.  
  151.  
  152. else if(operators_array[2*i+6]=='*' && operators_array[2*i+7]=='+')
  153. column_results[i]=main_board[i] * main_board[i+3]+main_board[i+6];
  154. else if(operators_array[2*i+6]=='+' && operators_array[2*i+7]=='*')
  155. column_results[i]=(main_board[i]+main_board[i+3]) * main_board[i+6];
  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. // 4- initializing missing array to 0 (COMPLETE THIS PART)
  187.  
  188. int missing_array[9] = {0};
  189.  
  190.  
  191. ////////////////////////////////////////////////////////
  192.  
  193.  
  194. // 5- copy the main_board into game_board (COMPLETE THIS PART)
  195. for(int i=0;i<9;i++)
  196. game_board[i]=main_board[i];
  197.  
  198.  
  199.  
  200. /////////////////////////////////////////////////////////
  201.  
  202.  
  203. // Based on the selected level, 3 or 5 or 7 board values will be hidden.
  204.  
  205. i=0; // initializing i to 0
  206.  
  207. while (i < level*2+1){
  208. int r = rand() % 3;
  209. int c = rand() % 3;
  210. if (game_board[3*r+c]!='?'){
  211. missing_array[game_board[3*r+c]-1]= game_board[3*r+c]; // the hidden values will be added to the set of missing values
  212. game_board[3*r+c] = '?';
  213. i++;
  214. } // end of if
  215. } // end of while
  216. ////////////////////////////////////////////////////////
  217.  
  218. // 6- Copy the game board into a temporary board (COMPLETE THIS PART)
  219. for(int i=0;i<9;i++)
  220. temp_board[i]=game_board[i];
  221.  
  222.  
  223. ///////////////////////////////////////////////////////
  224.  
  225. // the inner loop to get values from the user
  226. do{
  227.  
  228. // Display the game board after each update
  229.  
  230. printf("Game Board:\n");
  231. printf("---------------------------------------------------\n");
  232. for (i=0;i<3;i++){
  233. if (i>0){ // show the column operators
  234. for (j=0;j<3;j++){
  235. printf(" %c\t\t", operators_array[3*(i+1)+ j]);
  236. }
  237. printf("\n");
  238. }
  239.  
  240. for (j=0;j<3;j++){ // show the rows
  241. if (game_board[(i*3)+j]!='?')
  242. printf(" %d\t ", temp_board[(i*3)+j]);
  243. else if (game_board[(i*3)+j]!=temp_board[(i*3)+j]){
  244. printf(" %d?\t ", temp_board[(i*3)+j]);
  245. }
  246. else
  247. printf(" ?\t ");
  248.  
  249. if (j<2)
  250. printf(" %c\t", operators_array[(i*2) + j]);
  251. else
  252. printf("= %d", row_results[i]); // show the rows results
  253. }
  254. printf("\n");
  255. if (i==2)
  256. printf (" =\t\t =\t\t =\n");
  257. }
  258. for (i=0;i<3;i++) // show the columnar results
  259. printf(" %d\t\t", column_results[i]);
  260. printf("\n---------------------------------------------------\n");
  261.  
  262. ////////////////////////////////////////////////////////
  263.  
  264. // 7- Display the missing values and update the missing_count variable (COMPLETE THIS PART)
  265. missing_count=0;
  266. int num=0;
  267. printf("The missing numbers are: ");
  268. for(int i=0;i<9;i++){
  269. if(missing_array[i]!=0){
  270. printf("%d ",missing_array[i]);
  271. missing_count++;
  272. }
  273.  
  274. }
  275. printf("\n");
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283. /////////////////////////////////////////////////////////
  284.  
  285. // 8- break if the missing values are empty (COMPLETE THIS PART)
  286.  
  287. if(missing_count==0)
  288. break;
  289.  
  290. /////////////////////////////////////////////////////
  291.  
  292. // Get user's guess (location and value) and check inputs' validity
  293.  
  294. printf("Enter a row number (1-3), column(1-3), value(One of the missing values):\n");
  295. int r, c, v;
  296. scanf("%d %d %d", &r, &c, &v);
  297. if (r==0 || c==0 || v==0)
  298. break;
  299.  
  300. if (r < 1 || r > 3 || c < 1 || c > 3){
  301. puts("Invalid row and/or column numbers. Try again.");
  302. continue;
  303. }
  304.  
  305. if (v < 1 || v > 9) {
  306. puts("Invalid cell value. Try again.");
  307. continue;
  308. }
  309.  
  310. if (missing_array[v-1]<1) {
  311. puts("This value is already there. Try again.");
  312. continue;
  313. }
  314.  
  315. // 9- if the selected cell is changeable, add the value into the cell and remove it from the missing values, in other case show an error and repeat the inner loop.
  316. // (COMPLETE THIS PART)
  317.  
  318. if (game_board[3*(r-1)+c-1]=='?'){
  319. temp_board[3*(r-1)+c-1]=v;
  320. missing_array[3*(r-1)+c-1]=0;
  321. }
  322. else{
  323. printf("This cell is already filled! Try again.\n");
  324. continue;
  325. }
  326.  
  327.  
  328.  
  329.  
  330.  
  331.  
  332.  
  333. //////////////////////////////////////////////////////
  334.  
  335. } while (1); // end of the inner loop
  336.  
  337. //////////////////////////////////////////////////////////
  338.  
  339. // 10- check the results (COMPLETE THIS PART)
  340. temp1=0;
  341. for(int i=0;i<9;i++){
  342. if(temp_board[i]==main_board[i])
  343. temp1++;
  344. else
  345. continue;
  346. }
  347. if(temp1<9)
  348. check_game='\0';
  349. else if(temp1==9)
  350. check_game='a';
  351.  
  352.  
  353.  
  354.  
  355.  
  356. //////////////////////////////////////////////////////
  357.  
  358. if (check_game) // display Wining/Losing Message
  359. printf ("**** Congratulations!!!****\n ***You Won!!!!*** \n");
  360. else
  361. printf (":( Sorry it is not correct :( \n");
  362.  
  363. printf("#######################################\n");
  364. printf (" Do you want to play again? (Yes:1, No:0)\n"); // ask the user to play again
  365. printf("#######################################\n");
  366. scanf ("%d", &play_again);
  367.  
  368. if (!play_again){
  369. printf("Bye!");
  370. break;
  371. }
  372. } while (1); // end of the outer loop
  373. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement