Guest User

Untitled

a guest
Apr 20th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.34 KB | None | 0 0
  1. //--------------------------------------------------
  2. // INCLUDE SECTION
  3. //--------------------------------------------------
  4. #include "pal.h"
  5.  
  6. //--------------------------------------------------
  7. // gen_num
  8. //--------------------------------------------------
  9.  
  10. int gen_num(int lb, int ub) {
  11.  
  12. //1. We create the output variable with an initial value.
  13. int res = 0;
  14.  
  15. //2. We assign num to a random value in the desired range
  16. res = (rand() % (ub - lb)) + lb;
  17.  
  18. //3. We return the output variable
  19. return res;
  20. }
  21.  
  22. //--------------------------------------------------
  23. // my_getchar
  24. //--------------------------------------------------
  25.  
  26. char my_get_char() {
  27. //1. We create the output variable with the value the user has input by keyboard
  28. char res = getchar();
  29.  
  30. //2. We discard any extra character the user has input by keyboard
  31. boolean finish = False;
  32. char dummy_char = ' ';
  33.  
  34. while (finish == False) {
  35. dummy_char = getchar();
  36. if (dummy_char == '\n')
  37. finish = True;
  38. }
  39.  
  40. //3. We return the output variable
  41. return res;
  42. }
  43.  
  44. //--------------------------------------------------
  45. // initialise_array
  46. //--------------------------------------------------
  47. void initialise_array(int a[6], int num) {
  48.  
  49.  
  50. int n = num;
  51. int index = 5;
  52.  
  53. while(n > 0){
  54.  
  55. a[index] = n%10;
  56. n/=10;
  57. index--;
  58.  
  59. }
  60.  
  61.  
  62. }
  63.  
  64. //--------------------------------------------------
  65. // is_pal
  66. //--------------------------------------------------
  67. boolean is_pal(int a[6])
  68. {
  69. int first = 0;
  70. int last = 5;
  71. boolean flag = True; //flag apears if not palindrome
  72.  
  73. while(first < last){
  74.  
  75. if(a[first] != a[last]){ //if a[0]==a[5-0] do{ if 1 == 2-1 do{
  76. flag = False;
  77. }
  78. first++;
  79. last--;
  80. }
  81.  
  82. return flag;
  83. }
  84. /*
  85.  
  86. boolean is_pal(int a[6])
  87. {
  88. int reversedInteger = 0, remainder, originalInteger;
  89. boolean b;
  90. originalInteger = a;
  91.  
  92. // reversed integer is stored in variable
  93. while( a!=0 )
  94. {
  95. remainder = originalInteger%10;
  96. reversedInteger = reversedInteger*10 + remainder;
  97. originalInteger /= 10;
  98. }
  99.  
  100. // palindrome if orignalInteger and reversedInteger are equal
  101.  
  102. if (originalInteger == reversedInteger)
  103. b = True;
  104. else
  105. b = False;
  106.  
  107. return 0;
  108. }
  109. */
  110.  
  111. //--------------------------------------------------
  112. // ask_for_command
  113. //--------------------------------------------------
  114. char ask_for_command() {
  115.  
  116.  
  117. //captures the command entered by the user or player of the game
  118. char res = ' ';
  119.  
  120. //printf("\nValid Inputs are A, D, W & X");
  121. printf("\n\nEnter next command please\n");
  122. res = my_get_char();
  123.  
  124.  
  125. // printf("Your next move is %s",res);
  126.  
  127. return res;
  128.  
  129. }
  130.  
  131. //--------------------------------------------------
  132. // process_movement
  133. //--------------------------------------------------
  134. void process_movement(int a[6], int** p_p, int* p_nm, char c) {
  135.  
  136. //int** is a pointer to a pointer
  137. //p_nm increases everytime a command is selected as it tracks the amount of moves or plays it takes the player to win
  138. //a[6] is the array
  139. //
  140.  
  141. char command = c;
  142.  
  143. switch(command){
  144.  
  145. case 'a' :
  146. if(*p_p > a) {
  147.  
  148. *p_p = (*p_p) - 1;
  149. *p_nm += 1;
  150.  
  151. //break;
  152. }
  153.  
  154. break;
  155.  
  156. case 'd':
  157. if(*p_p < &a[5]) {
  158.  
  159. *p_p = (*p_p) + 1;
  160. *p_nm += 1;
  161.  
  162. //break;
  163. }
  164. break;
  165.  
  166. case 'w':
  167. if(**p_p < 9) {
  168.  
  169. **p_p = (**p_p) + 1;
  170. *p_nm += 1;
  171.  
  172. //break;
  173.  
  174. }
  175. break;
  176.  
  177. case 'x':
  178. if(**p_p > 0) {
  179.  
  180. **p_p = (**p_p) - 1;
  181. *p_nm += 1;
  182.  
  183. //break;
  184. }
  185. break;
  186.  
  187. default:
  188.  
  189. printf("\nerror\n");
  190.  
  191. }
  192.  
  193.  
  194. }
  195.  
  196. //--------------------------------------------------
  197. // print_status
  198. //--------------------------------------------------
  199.  
  200.  
  201. void print_status(int a[6], int* p, int nm) {
  202. int i;
  203.  
  204.  
  205. printf(" Number: ");
  206. printf("{");
  207.  
  208. for (i = 0; i < 6; i++) {
  209. printf("%d ", a[i]);
  210. }
  211.  
  212. printf("}");
  213. printf("\n");
  214. printf(" ");
  215.  
  216. int s = (p - &a[0]);
  217.  
  218. for (int i = 0; i < (2 * s); i++)
  219. printf(" ");
  220.  
  221. printf("^");
  222. printf("\n");
  223.  
  224. printf("Number of moves:");
  225. printf("%d", nm);
  226. printf("\n");
  227.  
  228. printf("---------------------------------------\n");
  229. }
  230.  
  231.  
  232.  
  233. //--------------------------------------------------
  234. // user_game_palindrome
  235. //--------------------------------------------------
  236.  
  237. void user_game_palindrome(int pal_num) {
  238.  
  239. //created all variables here
  240. char c;
  241. int a[6] = {0, 0, 0, 0, 0, 0};
  242. //int a[6] = {2, 4, 3, 3, 4, 2};
  243. int index = gen_num(0, 6);
  244. int nm = 0;
  245. int *p = index + &a[0];
  246. int *p_num_moves = &nm;
  247. int **p_p = &p;
  248. boolean x = False;
  249.  
  250. initialise_array(a, pal_num);
  251. p = a + index;
  252. print_status(a, p, *p_num_moves);
  253.  
  254. while (is_pal(a) == False) {
  255. //do {
  256.  
  257. // print_status(a, p, *p_num_moves);
  258.  
  259. c = ask_for_command();
  260.  
  261. process_movement(a, p_p, p_num_moves, c);
  262.  
  263. print_status(a, p, *p_num_moves);
  264.  
  265. //x = is_pal(a);
  266.  
  267. }
  268.  
  269. printf("\n---------\n");
  270. printf("\nYou win");
  271. printf("\n---------\n");
  272.  
  273.  
  274. }
  275.  
  276.  
  277. //
Add Comment
Please, Sign In to add comment