Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "primlib.h" /*This is graphics library*/
- #define BACKGROUND_COLOR BLACK
- /*This variable represents the color ofthe background*/
- #define FLOOR_COLOR YELLOW
- /*This variable represents the color of the floor*/
- #define TOWERS_COLOR RED
- /*This variable represents the color of the towers*/
- #define DISKS_COLOR MAGENTA
- /*This variable represents the color of the disks*/
- #define FRAME_COLOR BLUE
- /*This variable represents the color of the frames of the disks*/
- #define TEXT_COLOR YELLOW
- /*This variable represents the color ofthe text used in the programm */
- #define TOWERS_NUMBER 5
- /*This variable represents the number of towers */
- #define DISKS_NUMBER 4
- /*This variable represents the number of disks */
- #define TOWERS_WIDTH 6
- /*This variable represents the width of the towers */
- #define FLOOR_HEIGHT 30
- /*This variable represents the height of the floor */
- #define DISKS_WIDTH 3
- /*This variable represents the width of the smallest disk minus tower's width and divided by two*/
- #define STEP 2
- /*This variable represents the step per iteration of the moving disks */
- #define DISKS_HEIGHT 10
- /*This variable represents the height of the disks */
- /******************************************************************************************************************************************************************************************/
- /* global variables */
- int how_many_disks_arr[TOWERS_NUMBER];
- /*This array stores information about how many disks are on the current tower*/
- int disks_arr[DISKS_NUMBER+1][TOWERS_NUMBER];
- /*In this array we store the sizes of individual disks which occupy current position on towers */
- float nr_towers=TOWERS_NUMBER; /*This variable represents the number of towers*/
- float nr_disks=DISKS_NUMBER; /*This variable represents the numbe*/
- void drawing(void) /*This function is drawing on the screen everything that is going on in the programm: backgroung, floor, disks, towers */
- {
- int row,column;
- /*row - counter in operations concerning number of rows
- column - counter in operations concerning number of columns*/
- filledRect(0, 0, screenWidth(), screenHeight(), BACKGROUND_COLOR);
- /*This function creates the background*/
- filledRect(0, screenHeight()-FLOOR_HEIGHT, screenWidth(), screenHeight(), FLOOR_COLOR);
- /*This function creates the floor*/
- for(column=0;column<TOWERS_NUMBER;column++) /*This for loop draws towers*/
- {
- filledRect((column*2+1)*screenWidth()/(nr_towers*2)-TOWERS_WIDTH/2, screenHeight()-((nr_disks+1)*DISKS_HEIGHT+FLOOR_HEIGHT), (column*2+1)*screenWidth()/(nr_towers*2)+TOWERS_WIDTH/2, screenHeight()-FLOOR_HEIGHT-1, TOWERS_COLOR);
- }
- for(column=0;column<TOWERS_NUMBER;column++) /*This two loops create disks and their frames according to the current position of disks written in the array*/
- {
- for(row=0;row<DISKS_NUMBER;row++)
- {
- if(disks_arr[row][column]!=0)
- {
- filledRect(screenWidth()/(nr_towers*2)*(1+2*column)-DISKS_WIDTH*disks_arr[row][column]-TOWERS_WIDTH/2, screenHeight()-(nr_disks-row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(nr_towers*2)*(1+2*column)+DISKS_WIDTH*disks_arr[row][column]+TOWERS_WIDTH/2, screenHeight()-((nr_disks-row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, DISKS_COLOR);
- rect(screenWidth()/(nr_towers*2)*(1+2*column)-DISKS_WIDTH*disks_arr[row][column]-TOWERS_WIDTH/2, screenHeight()-(nr_disks-row)*DISKS_HEIGHT-FLOOR_HEIGHT, screenWidth()/(nr_towers*2)*(1+2*column)+DISKS_WIDTH*disks_arr[row][column]+TOWERS_WIDTH/2, screenHeight()-((nr_disks-row-1)*DISKS_HEIGHT)-FLOOR_HEIGHT-1, FRAME_COLOR);
- }
- }
- }
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement