Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "primlib.h"
- #include <stdbool.h>
- #define NUMBER_OF_RODS 3
- #define NUMBER_OF_DISKS 3
- #define HEIGHT_OF_ROD 200
- #define WIDTH_GROUND 50
- #define WIDTH_OF_ROD 5
- #define DISKS_WIDTH 10
- #define HEIGHT_OF_GROUND (screenHeight() - WIDTH_GROUND)
- #define DISKS_HEIGHT ((HEIGHT_OF_ROD - 15) / NUMBER_OF_DISKS)
- #define WIDTH_DIFFRENCE ((((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2 - DISKS_WIDTH) / NUMBER_OF_DISKS)
- #define STEP 2
- #define FRAME_COLOR BLACK
- #define DISKS_COLOR BLUE
- #define DELAY 40
- int gameArray[NUMBER_OF_DISKS + 1][NUMBER_OF_RODS];
- int howManyDisksArr[NUMBER_OF_RODS];
- void DrawRods();
- void DrawDisks();
- void Draw();
- void InitialGame();
- int FindingTopDisc(int tower);
- void MovingConditions();
- bool Win();
- void HowManyDisks(int tower);
- void Animation(int startTower, int stopTower, int startRow, int stopRow);
- void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction);
- void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction);
- void MovingDown(int position_x, int position_y, int stopTower, int diskValue);
- void DrawingMovedDisc(int position_x, int position_y, int diskValue);
- int main()
- {
- if(initGraph())
- {
- exit(3);
- }
- InitialGame();
- while(1)
- {
- Draw();
- updateScreen();
- if(Win() == true)
- {
- break;
- }
- MovingConditions();
- updateScreen();
- if(isKeyDown(SDLK_SPACE) == 1)
- {
- break;
- }
- }
- SDL_Delay(DELAY);
- return 0;
- }
- void Draw()
- {
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
- DrawRods();
- DrawDisks();
- }
- void DrawRods()
- {
- int column;
- for(column = 0; column < NUMBER_OF_RODS; column++)
- {
- filledRect((column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) - WIDTH_OF_ROD / 2,
- screenHeight() - ((NUMBER_OF_DISKS + 1) * DISKS_HEIGHT + WIDTH_GROUND),
- (column * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2) + WIDTH_OF_ROD / 2,
- screenHeight() - WIDTH_GROUND - 1, GREEN);
- }
- }
- void DrawDisks()
- {
- int row, column, individualWidth;
- for(column = 0; column < NUMBER_OF_RODS; column++)
- {
- for(row = 0; row < NUMBER_OF_DISKS; row++)
- {
- individualWidth = DISKS_WIDTH + (WIDTH_DIFFRENCE * gameArray[row][column]);
- if(gameArray[row][column] != 0)
- {
- filledRect(screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
- HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
- screenWidth() / (NUMBER_OF_RODS * 2) * (1+2*column)+ individualWidth + WIDTH_OF_ROD/2,
- HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, DISKS_COLOR);
- rect(screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
- HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
- screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) + individualWidth + WIDTH_OF_ROD / 2,
- HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, FRAME_COLOR);
- }
- }
- }
- }
- void InitialGame()
- {
- int i, j;
- for(i = 0; i < NUMBER_OF_DISKS; i++)
- {
- for(j = 0; j < NUMBER_OF_RODS; j++)
- {
- if(j == 0){
- gameArray[i][j] = i + 1;
- } else {
- gameArray[i][j] = 0;
- }
- }
- }
- }
- int FindingTopDisc(int tower)
- {
- int row, topDisc;
- for(row = NUMBER_OF_DISKS; row >= 0; row--)
- {
- if(gameArray[row][tower] != 0)
- {
- topDisc = row;
- }
- }
- return topDisc;
- }
- void MovingConditions()
- {
- int startTower, stopTower, startRow, stopRow;
- startTower = getkey() - '1';
- stopTower = getkey() - '1';
- if(startTower == ('0'-'1'))
- {
- startTower = 9;
- }
- if(stopTower == ('0'-'1'))
- {
- stopTower = 9;
- }
- if((startTower < NUMBER_OF_RODS) && (startTower >= 0) && (startTower != stopTower) && (stopTower >= 0) && (stopTower < NUMBER_OF_RODS))
- {
- startRow = NUMBER_OF_DISKS;
- stopRow = NUMBER_OF_DISKS;
- gameArray[NUMBER_OF_DISKS][startTower] = NUMBER_OF_DISKS;
- gameArray[NUMBER_OF_DISKS][stopTower] = NUMBER_OF_DISKS;
- startRow = FindingTopDisc(startTower);
- stopRow = FindingTopDisc(stopTower);
- if((startRow != NUMBER_OF_DISKS) && (gameArray[startRow][startTower] <= gameArray[stopRow][stopTower]))
- {
- Animation(startTower, stopTower, startRow, stopRow - 1 );
- }
- }
- }
- bool Win()
- {
- if(gameArray[0][NUMBER_OF_RODS - 1] == 1)
- {
- textout(screenWidth() / 2 - 25, 50, "YOU WON", RED);
- updateScreen();
- getkey();
- return true;
- }
- return false;
- }
- void HowManyDisks(int tower)
- {
- int row;
- howManyDisksArr[tower] = 0;
- for(row = 0; row < NUMBER_OF_DISKS; row++)
- {
- if(gameArray[row][tower] != 0)
- {
- howManyDisksArr[tower] += 1;
- }
- }
- }
- void Animation(int startTower, int stopTower, int startRow, int stopRow)
- {
- int position_y, position_x, diskValue, direction;
- HowManyDisks(startTower);
- diskValue = gameArray[startRow][startTower];
- gameArray[startRow][startTower] = 0;
- position_y = screenHeight() - (WIDTH_GROUND + DISKS_HEIGHT * howManyDisksArr[startTower]);
- position_x = screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * startTower);
- //(startTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2);
- if(startTower < stopTower)
- {
- direction = 1;
- }
- if(startTower > stopTower)
- {
- direction = -1;
- }
- MovingUp( position_x, position_y, diskValue, stopTower, direction);
- gameArray[stopRow][stopTower] = diskValue;
- }
- void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction)
- {
- while((position_y + STEP) >= (HEIGHT_OF_GROUND - HEIGHT_OF_ROD - (2 * DISKS_HEIGHT)))
- {
- Draw();
- DrawingMovedDisc(position_x, position_y, diskValue);
- position_y -= STEP;
- updateScreen();
- SDL_Delay(DELAY);
- }
- MovingHorizontally(position_x, position_y, stopTower, diskValue, direction);
- }
- void MovingHorizontally(int position_x, int position_y, int stopTower, int diskValue, int direction)
- {
- while(abs(position_x - (screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * stopTower))/*(stopTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2)*/) >= STEP)
- {
- Draw();
- DrawingMovedDisc(position_x, position_y, diskValue);
- position_x = position_x + direction * STEP;
- updateScreen();
- SDL_Delay(DELAY/4);
- }
- MovingDown(position_x, position_y, stopTower, diskValue);
- }
- void MovingDown(int position_x, int position_y, int stopTower, int diskValue)
- {
- HowManyDisks(stopTower);
- while(position_y < (screenHeight() - (WIDTH_GROUND + (howManyDisksArr[stopTower] + 1) * DISKS_HEIGHT)))
- {
- Draw();
- DrawingMovedDisc(position_x, position_y, diskValue);
- position_y += STEP;
- updateScreen();
- SDL_Delay(DELAY);
- }
- }
- void DrawingMovedDisc(int position_x, int position_y, int diskValue)
- {
- int individualWidth = WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE;
- filledRect(position_x - individualWidth,
- position_y,
- position_x + individualWidth,
- position_y + DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x - individualWidth,
- position_y,
- position_x + individualWidth,
- position_y + DISKS_HEIGHT, FRAME_COLOR);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement