Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //--------------------------------------------------
- // INCLUDE SECTION
- //--------------------------------------------------
- #include "pal.h"
- //--------------------------------------------------
- // gen_num
- //--------------------------------------------------
- int gen_num(int lb, int ub) {
- //1. We create the output variable with an initial value.
- int res = 0;
- //2. We assign num to a random value in the desired range
- res = (rand() % (ub - lb)) + lb;
- //3. We return the output variable
- return res;
- }
- //--------------------------------------------------
- // my_getchar
- //--------------------------------------------------
- char my_get_char() {
- //1. We create the output variable with the value the user has input by keyboard
- char res = getchar();
- //2. We discard any extra character the user has input by keyboard
- boolean finish = False;
- char dummy_char = ' ';
- while (finish == False) {
- dummy_char = getchar();
- if (dummy_char == '\n')
- finish = True;
- }
- //3. We return the output variable
- return res;
- }
- //--------------------------------------------------
- // initialise_array
- //--------------------------------------------------
- void initialise_array(int a[6], int num) {
- int n = num;
- int index = 5;
- while(n > 0){
- a[index] = n%10;
- n/=10;
- index--;
- }
- }
- //--------------------------------------------------
- // is_pal
- //--------------------------------------------------
- boolean is_pal(int a[6])
- {
- int first = 0;
- int last = 5;
- boolean flag = True; //flag apears if not palindrome
- while(first < last){
- if(a[first] != a[last]){ //if a[0]==a[5-0] do{ if 1 == 2-1 do{
- flag = False;
- }
- first++;
- last--;
- }
- return flag;
- }
- /*
- boolean is_pal(int a[6])
- {
- int reversedInteger = 0, remainder, originalInteger;
- boolean b;
- originalInteger = a;
- // reversed integer is stored in variable
- while( a!=0 )
- {
- remainder = originalInteger%10;
- reversedInteger = reversedInteger*10 + remainder;
- originalInteger /= 10;
- }
- // palindrome if orignalInteger and reversedInteger are equal
- if (originalInteger == reversedInteger)
- b = True;
- else
- b = False;
- return 0;
- }
- */
- //--------------------------------------------------
- // ask_for_command
- //--------------------------------------------------
- char ask_for_command() {
- //captures the command entered by the user or player of the game
- char res = ' ';
- //printf("\nValid Inputs are A, D, W & X");
- printf("\n\nEnter next command please\n");
- res = my_get_char();
- // printf("Your next move is %s",res);
- return res;
- }
- //--------------------------------------------------
- // process_movement
- //--------------------------------------------------
- void process_movement(int a[6], int** p_p, int* p_nm, char c) {
- //int** is a pointer to a pointer
- //p_nm increases everytime a command is selected as it tracks the amount of moves or plays it takes the player to win
- //a[6] is the array
- //
- char command = c;
- switch(command){
- case 'a' :
- if(*p_p > a) {
- *p_p = (*p_p) - 1;
- *p_nm += 1;
- //break;
- }
- break;
- case 'd':
- if(*p_p < &a[5]) {
- *p_p = (*p_p) + 1;
- *p_nm += 1;
- //break;
- }
- break;
- case 'w':
- if(**p_p < 9) {
- **p_p = (**p_p) + 1;
- *p_nm += 1;
- //break;
- }
- break;
- case 'x':
- if(**p_p > 0) {
- **p_p = (**p_p) - 1;
- *p_nm += 1;
- //break;
- }
- break;
- default:
- printf("\nerror\n");
- }
- }
- //--------------------------------------------------
- // print_status
- //--------------------------------------------------
- void print_status(int a[6], int* p, int nm) {
- int i;
- printf(" Number: ");
- printf("{");
- for (i = 0; i < 6; i++) {
- printf("%d ", a[i]);
- }
- printf("}");
- printf("\n");
- printf(" ");
- int s = (p - &a[0]);
- for (int i = 0; i < (2 * s); i++)
- printf(" ");
- printf("^");
- printf("\n");
- printf("Number of moves:");
- printf("%d", nm);
- printf("\n");
- printf("---------------------------------------\n");
- }
- //--------------------------------------------------
- // user_game_palindrome
- //--------------------------------------------------
- void user_game_palindrome(int pal_num) {
- //created all variables here
- char c;
- int a[6] = {0, 0, 0, 0, 0, 0};
- //int a[6] = {2, 4, 3, 3, 4, 2};
- int index = gen_num(0, 6);
- int nm = 0;
- int *p = index + &a[0];
- int *p_num_moves = &nm;
- int **p_p = &p;
- boolean x = False;
- initialise_array(a, pal_num);
- p = a + index;
- print_status(a, p, *p_num_moves);
- while (is_pal(a) == False) {
- //do {
- // print_status(a, p, *p_num_moves);
- c = ask_for_command();
- process_movement(a, p_p, p_num_moves, c);
- print_status(a, p, *p_num_moves);
- //x = is_pal(a);
- }
- printf("\n---------\n");
- printf("\nYou win");
- printf("\n---------\n");
- }
- //
Add Comment
Please, Sign In to add comment