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 BLUE
- #define TEXT_COLOR MAGENTA
- #define TOWERS_NUMBER 5
- #define DISKS_NUMBER 4
- #define TOWERS_WIDTH 6
- #define FLOOR_HEIGHT 30
- #define DISKS_WIDTH 3
- /*The width of the smallest disk-tower's width/2 */
- #define STEP_PER_ITERATION_OF_MOVING_DISC 2
- #define DISKS_HEIGHT 10
- /* global variables */
- int how_many_disks_on_the_current_tower_tab[TOWERS_NUMBER];
- int disks_sizes_on_current_position_tab[DISKS_NUMBER + 1][TOWERS_NUMBER];
- float number_of_towers = TOWERS_NUMBER;
- float number_of_disks = DISKS_NUMBER;
- void filledRect(int x1, int y1, int x2, int y2, enum color c);
- void drawing(void) /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- {
- int number_of_rows;
- int number_of_column;
- filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
- filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
- for(number_of_column = 0; number_of_column < TOWERS_NUMBER; number_of_column++) /* drawing towers*/
- {
- filledRect((number_of_column*2+1)*screenWidth()/(number_of_towers*2)-TOWERS_WIDTH/2, screenHeight()-((number_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);
- }
- for(number_of_column=0;number_of_column<TOWERS_NUMBER;number_of_column++) /*This two loops create disks and their frames according to the current position of disks written in the array*/
- {
- for(number_of_rows=0;number_of_rows<DISKS_NUMBER;number_of_rows++)
- {
- if(disks_sizes_on_current_position_tab[number_of_rows][number_of_column]!=0)
- {
- filledRect(screenWidth()/(number_of_towers*2)*(1+2*number_of_column)-DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows[number_of_column]-TOWERS_WIDTH/2 screenHeight()-(number_of_disks-number_of_rows)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(number_of_towers*2)*(1+2*number_of_column)+DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]+TOWERS_WIDTH/2, screenHeight()-((number_of_disks-number_of_rows-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, DISKS_COLOR);
- rect(screenWidth()/(number_of_towers*2)*(1+2*number_of_column)-DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]-TOWERS_WIDTH/2, screenHeight()-(number_of_disks - number_of_rows)*DISKS_HEIGHT - FLOOR_HEIGHT, screenWidth()/(number_of_towers*2)*(1+2*number_of_column)+DISKS_WIDTH*disks_sizes_on_current_position_tab[number_of_rows][number_of_column]+TOWERS_WIDTH/2, screenHeight()-((number_of_disks-number_of_rows-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, FRAME_COLOR);
- }
- }
- }
- }
- /* functions declarations */
- //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*/
- //void moving(int,int,int,int); /*This function is responsible for the movement of the disks*/
- //oid how_many_disks_on_the_current_tower(int);
- /*This function calculates how many disks are on the current tower and writes it into the array how_many_disks[]*/
- //void initial_state(void); /*This function writes the initial position of the disks into the array*/
- //int winning(void); /* This function prints the text when the user wins the game*/
- int main(int argc, char* argv[])
- {
- //initial_state(); /*This function writes the initial position of the disks into the array*/
- if(initGraph()) /*Initialization of graphics. Exit on error*/
- exit(3);
- do
- {
- drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- updateScreen();
- //winning(); /* This function prints the text when the user wins the game*/
- //if(winning()==1) /*Function 'winning()' returns value 1 if the user won the game so the main loop of the game is terminated by the 'break' expression*/
- // break;
- //array_transport(); /*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*/
- //updateScreen();
- }while(pollkey()!=SDLK_ESCAPE); /*This loop makes the programm to work until the user will close the window*/
- return 0;
- }
- /*
- void initial_state(void) /*This function writes the initial position of the disks into the array*/
- //{
- //int number_of_rows, column;
- /*row - counter in operations concerning number of rows
- column - counter in operations concerning number of columns*/
- //for(column=0;column<TOWERS_NUMBER;column++)
- // {
- // for(number_of_rows=0;number_of_rows<DISKS_NUMBER;number_of_rows++) /*This loop writes the initial state of disks on towers into the array 'disks_arr'*/
- // {
- // if(column==0)
- // disks_sizes_on_current_position_tab[number_of_rows][column]=number_of_rows+1;
- ///else
- // disks_sizes_on_current_position_tab[number_of_rows][column]=0;
- // }
- // }
- //}*/
- //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, number_of_rows;
- /*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;
- //disks_sizes_on_current_position_tab[DISKS_NUMBER][initial_tower]=DISKS_NUMBER;
- //disks_sizes_on_current_position_tab[DISKS_NUMBER][destination_tower]=DISKS_NUMBER;
- //for(number_of_rows=DISKS_NUMBER-1;number_of_rows>=0;number_of_rows--)/*This loop looks for the last (from below) disk on the initial tower and writes its row coordinate as initial_row variable*/
- //{
- //if(disks_sizes_on_current_position_tab[number_of_rows][initial_tower]!=0)
- //{
- //initial_row=number_of_rows;
- //}
- //}
- //for(number_of_rows=DISKS_NUMBER-1;number_of_rows>=0;number_of_rows--)/*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(disks_sizes_on_current_position_tab[number_of_rows][destination_tower]!=0)
- // {
- // occupied_row=number_of_rows;
- // }
- // }
- // if((initial_row!=DISKS_NUMBER)&&(disks_sizes_on_current_position_tab[initial_row][initial_tower]<=disks_sizes_on_current_position_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 w//hich we want to place it*/
- // moving(initial_tower,destination_tower, initial_row, occupied_row-1);
- //
- //}
- //}
- //void NumberOfDisksOnTheCurrentTower(int tower)/*This function calculates how many disks are on the current tower*/
- //{
- //int number_of_rows
- //how_many_disks_on_the_current_tower_tab[tower]=0;
- // for(number_of_rows = 0; number_of_rows < DISKS_NUMBER; number_of_rows++)
- // {
- // if(disks_sizes_on_current_position_tab[number_of_rows][tower]!=0)
- // {
- // how_many_disks_on_the_current_tower_tab[tower]+=1;
- // }
- // }
- //}
- //int winning(void) /* This function prints the text when the user wins the game*/
- //{
- ///int exit,column;
- //exit=0;
- /*column - counter when operating on columns
- exit - this variable is equal to 1 if the user has won the game and this value is returned by the function*/
- //for(column=1;column<TOWERS_NUMBER;column++) /*This loop calls the fucntion that calculates how many disks are on a current tower for every tower except the first one. If the number of disks on a tower is equal to the number of all disks then it prints on the screen 'YOU WON'*/
- //{
- ///how_many_disks(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 moving(initial_tower,destination_tower,initial_row,destination_row)
- /*This function is responsible for the movement of the disks*/
- //{
- //int position_y, position_x, disk_value, direction;
- /*position_y - This variable represents the vertical position of the bottom of moving disk
- position_x - This variable represents the horizontal position of the centre of moving disk
- disk_value - This variable represents the value stored in 'disks_arr' for the disk we want to move
- direction - this variable is responsible for determining the direction in which the disk will be moving on the horizontal axes. It acquires only two values: '1' - for movement from left to right, '-1' - for movement from right to left*/
- /*how_many_disks_on_the_current_tower(initial_tower); /*Calculation of how many disks are on the initial tower */
- /*disk_value=disks_sizes_on_current_position_tab[initial_row][initial_tower];
- disks_sizes_on_current_position_tab[initial_row][initial_tower]=0; /*The disk which will be moved is ereased from the array so it would not appear on its initial static position during screen updating*/
- /*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;*/
- /*This while loop is responsible for the first part of the vertical move of the disks*/
- /*while((position_y+STEP_PER_ITERATION_OF_MOVING_DISC)>=(screenHeight()-(FLOOR_HEIGHT+(DISKS_NUMBER+4)*DISKS_HEIGHT)))
- {
- drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- /*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)
- {
- drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- /*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();
- }
- how_many_disks(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)))
- {
- drawing(); /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- /*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_sizes_on_current_position_tab[destination_row][destination_tower]=disk_value; */
- /*This line changes the values of the disks_arr after the movement is done so that the disks that has been moved are also printed on the screen on their new static positions by drawing function*/
- //}
Add Comment
Please, Sign In to add comment