Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "primlib.h"
- #define BACKGROUND_COLOR BLACK
- #define FLOOR_COLOR YELLOW
- #define TOWERS_COLOR RED
- #define DISKS_COLOR MAGENTA
- #define FRAME_COLOR BLACK
- #define TEXT_COLOR YELLOW
- #define TOWERS_NUMBER 9
- #define TOWERS_WIDTH 5
- #define TOWERS_HIGHT 300
- #define DISKS_NUMBER 5
- #define DISKS_WIDTH 15
- #define STEP_PER_ITERATION_OF_MOVING_DISC 1
- #define DISKS_HEIGHT 10
- #define FLOOR_HEIGHT 30
- //-----------------------global variables----------------------------------
- int how_many_disks_on_the_current_tower_tab[TOWERS_NUMBER];
- int disks_tab[DISKS_NUMBER + 1][TOWERS_NUMBER];
- float number_of_towers = TOWERS_NUMBER;
- float number_of_disks = DISKS_NUMBER;
- double disks_height = TOWERS_HIGHT / (DISKS_NUMBER + 3);
- double disks_width = DISKS_WIDTH / TOWERS_NUMBER ;
- void initialingPositionOfDisks();
- void checkingBoundry();
- void drawingAll();
- int winning();
- void array_transport();
- void howManyDisks(int);
- void moving(int,int,int,int);
- void drawingBackgraund();
- void drawingTowers();
- void drawingDisks();
- int main(int argc, char* argv[])
- {
- initialingPositionOfDisks();
- if(initGraph())
- {
- exit(3);
- }
- do
- {
- checkingBoundry();
- drawingAll();
- updateScreen();
- winning();
- if(winning() == 1)
- {
- break;
- }
- array_transport();
- updateScreen();
- }while(isKeyDown(SDLK_ESCAPE) != 1);
- return 0;
- }
- void initialingPositionOfDisks(void)
- {
- int row_counter, number_of_column;
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
- {
- for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
- {
- if(number_of_column == 0)
- {
- disks_tab[row_counter][number_of_column] = row_counter + 1;
- }
- else
- {
- disks_tab[row_counter][number_of_column] = 0;
- }
- }
- }
- }
- void checkingBoundry()
- {
- if(DISKS_NUMBER < 3 || TOWERS_NUMBER > 10)
- {
- printf("Invalid number of Towers \n");
- exit(3);
- }
- if(DISKS_NUMBER < 3 || DISKS_NUMBER >15)
- {
- printf("Invalid number of Disks.\n");
- exit(3);
- }
- }
- void drawingAll(void)
- {
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- }
- void drawingBackgraund(void)
- {
- int row_counter, number_of_column;
- filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
- filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
- }
- void drawingTowers(void)
- {
- int row_counter, number_of_column;
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
- {
- filledRect((number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2)-TOWERS_WIDTH / 2,
- screenHeight() - TOWERS_HIGHT + FLOOR_HEIGHT ,
- (number_of_column * 2 + 1) * screenWidth() / (number_of_towers * 2) + TOWERS_WIDTH / 2, screenHeight() - FLOOR_HEIGHT - 1, TOWERS_COLOR);
- }
- }
- void drawingDisks(void)
- {
- int row_counter, number_of_column;
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
- {
- for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)
- {
- if(disks_tab[row_counter][number_of_column] != 0)
- {
- filledRect(screenWidth()/(number_of_towers * 2)*(1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column]-TOWERS_WIDTH / 2, screenHeight() - (number_of_disks-row_counter) * disks_height-FLOOR_HEIGHT,
- screenWidth()/(number_of_towers * 2) * ( 1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2, screenHeight()-((number_of_disks-row_counter - 1) * disks_height)-FLOOR_HEIGHT - 1, DISKS_COLOR);
- rect(screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column) - disks_width * disks_tab[row_counter][number_of_column]-TOWERS_WIDTH / 2,screenHeight() - (number_of_disks-row_counter) * disks_height-FLOOR_HEIGHT, screenWidth() / (number_of_towers * 2) * (1 + 2 * number_of_column) + disks_width * disks_tab[row_counter][number_of_column] + TOWERS_WIDTH / 2, screenHeight() -((number_of_disks-row_counter - 1) * disks_height) - FLOOR_HEIGHT - 1, FRAME_COLOR);
- }
- }
- }
- }
- int winning(void)
- {
- int exit;
- int number_column;
- exit = 0;
- for(number_column = 1; number_column < TOWERS_NUMBER; number_column ++)
- {
- howManyDisks(number_column);
- if (how_many_disks_on_the_current_tower_tab[number_column] == DISKS_NUMBER)
- {
- textout(screenWidth() / 2 - 30, screenHeight() / 2 - 50, "YOU WON !!!", MAGENTA);
- updateScreen();
- getkey();
- exit = 1;
- }
- }
- return exit;
- }
- void array_transport(void)
- {
- int start_tower, end_tower;
- int start_row;
- int occupied_row, row_counter;
- start_tower = getkey() - '1';
- end_tower = getkey() - '1';
- if(start_tower == ('0' - '1'))
- {
- start_tower = 9;
- }
- if(end_tower == ('0' - '1'))
- {
- end_tower = 9;
- }
- if((start_tower != end_tower) && (start_tower >= 0) && (start_tower < TOWERS_NUMBER) && (end_tower >= 0) && (end_tower < TOWERS_NUMBER))
- {
- start_row = DISKS_NUMBER;
- occupied_row = DISKS_NUMBER;
- disks_tab[DISKS_NUMBER][start_tower] = DISKS_NUMBER;
- disks_tab[DISKS_NUMBER][end_tower] = DISKS_NUMBER;
- for(row_counter=DISKS_NUMBER - 1; row_counter >=0; row_counter --)//looks for last disk on initial tower
- {
- if(disks_tab[row_counter][start_tower]!=0)
- {
- start_row = row_counter;
- }
- }
- for(row_counter = DISKS_NUMBER - 1; row_counter >= 0; row_counter --)//looks for last disk on last tower
- {
- if(disks_tab[row_counter][end_tower] != 0)
- {
- occupied_row = row_counter;
- }
- }
- if((start_row != DISKS_NUMBER) && (disks_tab[start_row][start_tower] <= disks_tab[occupied_row][end_tower]))
- {
- moving(start_tower, end_tower, start_row, occupied_row - 1);
- }
- }
- }
- void howManyDisks(int number_of_towers)
- {
- int row_counter;
- how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
- for(row_counter = 0; row_counter < DISKS_NUMBER; row_counter ++)//check if current tower is occupied
- {
- if(disks_tab[row_counter][number_of_towers]!= 0)
- {
- how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
- }
- }
- }
- void moving(start_tower,destination_tower,start_row,destination_row)
- {
- int vertical_position_of_the_bottom_of_moving_disk;
- int horizontal_position_of_the_centre_of_moving_disk ;
- int valu_for_the_disk_to_move;
- int direction_on_horizontal_axes;
- howManyDisks(start_tower);
- valu_for_the_disk_to_move = disks_tab[start_row][start_tower];
- disks_tab[start_row][start_tower] = 0;
- vertical_position_of_the_bottom_of_moving_disk = screenHeight()-(FLOOR_HEIGHT+DISKS_HEIGHT*how_many_disks_on_the_current_tower_tab[start_tower]);
- horizontal_position_of_the_centre_of_moving_disk = (start_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2);
- if(start_tower < destination_tower)
- direction_on_horizontal_axes = 1;
- if(start_tower > destination_tower)
- direction_on_horizontal_axes = -1;
- while((vertical_position_of_the_bottom_of_moving_disk+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
- {
- drawingAll();
- filledRect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, DISKS_COLOR);
- rect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, FRAME_COLOR);
- vertical_position_of_the_bottom_of_moving_disk-= STEP_PER_ITERATION_OF_MOVING_DISC;
- updateScreen();
- }
- /*This while loop is responsible for the horizontal move */
- while(abs(horizontal_position_of_the_centre_of_moving_disk -(destination_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2))>=STEP_PER_ITERATION_OF_MOVING_DISC)
- {
- drawingAll();
- filledRect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, DISKS_COLOR);
- rect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, FRAME_COLOR);
- horizontal_position_of_the_centre_of_moving_disk =horizontal_position_of_the_centre_of_moving_disk +direction_on_horizontal_axes*STEP_PER_ITERATION_OF_MOVING_DISC;
- updateScreen();
- }
- howManyDisks(destination_tower); /*Calculation of how many disks are on the destination tower*/
- /*This while loop is responsible for the second part of the vertical move of the disks*/
- while(vertical_position_of_the_bottom_of_moving_disk<(screenHeight()-(FLOOR_HEIGHT+(how_many_disks_on_the_current_tower_tab[destination_tower]+1)*DISKS_HEIGHT)))
- {
- drawingAll();
- filledRect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, DISKS_COLOR);
- rect(horizontal_position_of_the_centre_of_moving_disk -(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk, horizontal_position_of_the_centre_of_moving_disk +(TOWERS_WIDTH/2+valu_for_the_disk_to_move*DISKS_WIDTH), vertical_position_of_the_bottom_of_moving_disk+DISKS_HEIGHT, FRAME_COLOR);
- vertical_position_of_the_bottom_of_moving_disk += STEP_PER_ITERATION_OF_MOVING_DISC;
- updateScreen();
- }
- disks_tab[destination_row][destination_tower]=valu_for_the_disk_to_move;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement