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 4
- #define DISKS_NUMBER 3
- #define TOWERS_WIDTH 6
- #define FLOOR_HEIGHT 30
- #define TOWERS_HIGHT 200
- #define DISKS_WIDTH 3
- #define STEP_PER_ITERATION_OF_MOVING_DISC 2
- #define DISKS_HEIGHT 10
- #define SPACE_BTW_TOWERS screenWidth()/TOWERS_NUMBER
- #define GROUND_HEIGHT screenHeight() - 30
- 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 nuber_of_disks = DISKS_NUMBER;
- double makingDistanceBetweenTowers();
- void checkingBoundry();
- void drawingBackgraund();
- void drawingTowers();
- void drawingDisks();
- void initialingPositionOfDisks();
- void howManyDisks(int);
- void drawingAll(void);
- void moving(int,int,int,int);
- int winning();
- void array_transport(void);
- int main(int argc, char* argv[])
- {
- /*
- if(initGraph())
- {
- exit(3);
- }
- do
- {
- checkingBoundry();
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- initialingPositionOfDisks();
- updateScreen();
- winning();
- }while(isKeyDown(SDLK_ESCAPE) != 1);
- return 0;*/
- initialingPositionOfDisks();
- if(initGraph())
- exit(3);
- do
- {
- checkingBoundry();
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- updateScreen();
- winning();
- if(winning()==1)
- break;
- array_transport();
- updateScreen();
- }while(pollkey()!=SDLK_ESCAPE);
- return 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 drawingBackgraund(void)
- {
- int number_of_row, number_of_column;
- filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
- filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
- }
- double makingDistanceBetweenTowers()
- {
- double distance_between_towers = screenWidth() / (TOWERS_NUMBER + 1);
- return distance_between_towers;
- }
- void drawingTowers(void)
- {
- int number_of_row, 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()-((nuber_of_disks + 1)*DISKS_HEIGHT+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 number_of_row, number_of_column;
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
- {
- for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row++)
- {
- if(disks_tab[number_of_row][number_of_column]!=0)
- {
- filledRect(screenWidth()/(number_of_towers * 2)*(1 + 2 * number_of_column) - DISKS_WIDTH * disks_tab[number_of_row][number_of_column]-TOWERS_WIDTH/2, screenHeight()-(nuber_of_disks-number_of_row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers * 2) * ( 1 + 2 * number_of_column) + DISKS_WIDTH*disks_tab[number_of_row][number_of_column]+TOWERS_WIDTH / 2, screenHeight()-((nuber_of_disks-number_of_row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, DISKS_COLOR);
- rect(screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column)-DISKS_WIDTH*disks_tab[number_of_row][number_of_column]-TOWERS_WIDTH/2,screenHeight()-(nuber_of_disks-number_of_row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers * 2) * (1 + 2 * number_of_column)+DISKS_WIDTH*disks_tab[number_of_row][number_of_column]+TOWERS_WIDTH / 2, screenHeight()-((nuber_of_disks-number_of_row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, FRAME_COLOR);
- }
- }
- }
- }
- void initialingPositionOfDisks(void)
- {
- int number_of_row, number_of_column;
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column ++)
- {
- for(number_of_row = 0; number_of_row < DISKS_NUMBER; number_of_row ++)
- {
- if(number_of_column == 0)
- {
- disks_tab[number_of_row][number_of_column] = number_of_row + 1;
- }
- else
- {
- disks_tab[number_of_row][number_of_column]= 0;
- }
- }
- }
- }
- void howManyDisks(int number_of_towers)
- {
- int number_of_row;
- how_many_disks_on_the_current_tower_tab[number_of_towers] = 0;
- for(number_of_row = 0;number_of_row < DISKS_NUMBER; number_of_row++)
- {
- if(disks_tab[number_of_row][number_of_towers]!= 0)
- {
- how_many_disks_on_the_current_tower_tab[number_of_towers] += 1;
- }
- }
- }
- void moving(initial_tower,destination_tower,initial_row,destination_row)
- {
- int position_y, position_x, disk_value, direction;
- howManyDisks(initial_tower);
- disk_value = disks_tab[initial_row][initial_tower];
- disks_tab[initial_row][initial_tower] = 0;
- position_y = screenHeight()-(FLOOR_HEIGHT+DISKS_HEIGHT*how_many_disks_on_the_current_tower_tab[initial_tower]);
- position_x = (initial_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2);
- if(initial_tower < destination_tower)
- direction = 1;
- if(initial_tower > destination_tower)
- direction = -1;
- while((position_y+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
- {
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
- position_y-= STEP_PER_ITERATION_OF_MOVING_DISC;
- updateScreen();
- }
- /*This while loop is responsible for the horizontal move */
- while(abs(position_x-(destination_tower*2+1)*screenWidth()/(TOWERS_NUMBER*2))>=STEP_PER_ITERATION_OF_MOVING_DISC)
- {
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
- position_x=position_x+direction*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(position_y<(screenHeight()-(FLOOR_HEIGHT+(how_many_disks_on_the_current_tower_tab[destination_tower]+1)*DISKS_HEIGHT)))
- {
- drawingBackgraund();
- drawingTowers();
- drawingDisks();
- filledRect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x-(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y, position_x+(TOWERS_WIDTH/2+disk_value*DISKS_WIDTH), position_y+DISKS_HEIGHT, FRAME_COLOR);
- position_y += STEP_PER_ITERATION_OF_MOVING_DISC;
- updateScreen();
- }
- disks_tab[destination_row][destination_tower]=disk_value;
- }
- int winning(void)
- {
- int exit,column;
- exit = 0;
- for(column=1;column<TOWERS_NUMBER;column++)
- {
- howManyDisks(column);
- if (how_many_disks_on_the_current_tower_tab[column]==DISKS_NUMBER)
- {
- textout(screenWidth()/2-30, screenHeight()/2-50, "YOU WON !!!", TEXT_COLOR);
- updateScreen();
- getkey();
- exit = 1;
- }
- }
- return exit;
- }
- void array_transport(void) /*This function is responsible for operations on disks_array. If the conditions to move a disk from one towers to the other are satisfied then it launches the moving() function*/
- {
- int initial_tower, destination_tower, initial_row, destination_row, occupied_row, row_counter;
- /*initial_row - the first row from above occupied by disk on the initial tower
- occupied_row - the first occupied row(from above) on the destination tower
- destination_row - row on the destination tower where the disk will be moved
- row - counter when operating on rows*/
- initial_tower=getkey()-'1'; /*This variable represents the initial tower on which was located the disk that will be moved*/
- destination_tower=getkey()-'1'; /*This variable represents the tower on which the disk will be placed after the movement*/
- if(initial_tower==('0'-'1'))
- initial_tower=9;
- if(destination_tower==('0'-'1'))
- destination_tower=9;
- /*These if functions make the programm to work on the 10th tower when the user presses '0'*/
- if((initial_tower!=destination_tower)&&(initial_tower>=0)&&(initial_tower<TOWERS_NUMBER)&&(destination_tower>=0)&&(destination_tower<TOWERS_NUMBER))
- /*This if function ckecks whether the initial and destination towers aren't the same and belong to the interval between 0 and TOWERS_NUMBER*/
- {
- initial_row=DISKS_NUMBER;
- occupied_row=DISKS_NUMBER;
- how_many_disks_on_the_current_tower_tab[DISKS_NUMBER][initial_tower]=DISKS_NUMBER;
- how_many_disks_on_the_current_tower_tab[DISKS_NUMBER][destination_tower]=DISKS_NUMBER;
- for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)/*This loop looks for the last (from below) disk on the initial tower and writes its row coordinate as initial_row variable*/
- {
- if(how_many_disks_on_the_current_tower_tab[rrow_counterow][initial_tower]!=0)
- {
- initial_row=rrow_counterow;
- }
- }
- for(row_counter=DISKS_NUMBER-1;row_counter>=0;row_counter--)/*This loop looks for the last (from below) disk on the destination tower and writes destination_row as the first (from below) free space on the tower*/
- {
- if(how_many_disks_on_the_current_tower_tab[row_counter][destination_tower]!=0)
- {
- occupied_row=row;
- }
- }
- if((initial_row!=DISKS_NUMBER)&&(how_many_disks_on_the_current_tower_tab[initial_row][initial_tower]<=how_many_disks_on_the_current_tower_tab[occupied_row][destination_tower])) /*This if function checks if the conditions of movement are satisfied - if the disk which we want to move is smaller than the disk on which we want to place it*/
- moving(initial_tower,destination_tower, initial_row, occupied_row-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement