Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "primlib.h"
- #define NUMBER_OF_RODS 4
- #define HEIGHT_OF_GROUND (screenHeight() - WIDTH_GROUND)
- #define WIDTH_GROUND 50
- #define WIDTH_OF_ROD 5
- #define NUMBER_OF_DISKS 4
- #define STEP 2
- #define DISKS_WIDTH 10
- #define HEIGHT_OF_ROD 200
- #define DISKS_HEIGHT ((HEIGHT_OF_ROD - 15) / NUMBER_OF_DISKS)
- #define FRAME_COLOR BLACK
- #define DISKS_COLOR BLUE
- #define ROD_COLOR RED
- #define TOWERS_COLOR GREEN
- #define WIDTH_DIFFRENCE ((((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2 - DISKS_WIDTH) / NUMBER_OF_DISKS)
- int gameArray[NUMBER_OF_DISKS + 1][NUMBER_OF_RODS];
- int howManyDisksArr[NUMBER_OF_RODS];
- double DistanceBetweenRods();
- void DrawRods();
- void DrawDisks();
- void Draw();
- void InitialGame();
- int FindingTopDisc(int tower);
- void MovingConditions();
- int 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);
- int main()
- {
- if(initGraph())
- {
- exit(3);
- }
- InitialGame();
- while(1){
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
- DrawRods();
- DrawDisks();
- updateScreen();
- if(Win() == 1)
- {
- break;
- }
- MovingConditions();
- updateScreen();
- if (isKeyDown(SDLK_SPACE) == 1){
- break;
- }
- }
- SDL_Delay(40);
- return 0;
- }
- void Draw()
- {
- filledRect(0, 0, screenWidth() - 1, screenHeight() - 1, BLACK);
- filledRect(0, screenHeight(), screenWidth(), screenHeight() - 50 , YELLOW);
- DrawRods();
- DrawDisks();
- }
- double DistanceBetweenRods()
- {
- return (screenWidth()/(NUMBER_OF_RODS + 1));
- }
- 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, TOWERS_COLOR);
- /*filledRect((DistanceBetweenRods() * iterationNumber) - WIDTH_OF_ROD, HEIGHT_OF_GROUND - 1,
- (DistanceBetweenRods() * iterationNumber) + WIDTH_OF_ROD, HEIGHT_OF_GROUND - HEIGHT_OF_ROD , ROD_COLOR);
- */}
- }
- void DrawDisks()
- { //policzymy roznice szerokosci jaka maja krazki, podzielimy przez ich ilosc i to bedzie wartosc o jaką się bedą poszerzać
- 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){
- /*if(individualWidth > ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2){
- individualWidth = ((screenWidth() / NUMBER_OF_RODS) - WIDTH_OF_ROD) / 2 + increment;
- increment += 2;
- }*/
- filledRect(screenWidth() / (NUMBER_OF_RODS * 2) * ( 1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
- //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
- HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
- //screenHeight() - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT - WIDTH_GROUND,
- // screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
- screenWidth() / (NUMBER_OF_RODS * 2) * (1+2*column)+ individualWidth + WIDTH_OF_ROD/2,
- //(DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
- HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, DISKS_COLOR);
- // screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -1, DISKS_COLOR);
- rect(screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) - individualWidth - WIDTH_OF_ROD / 2,
- //(DistanceBetweenRods() * (column + 1)) - (DISKS_WIDTH * gameArray[row][column]),
- HEIGHT_OF_GROUND - (NUMBER_OF_DISKS - row) * DISKS_HEIGHT,
- // screenHeight()-(NUMBER_OF_DISKS-row)*DISKS_HEIGHT- WIDTH_GROUND,
- screenWidth() / (NUMBER_OF_RODS * 2) * (1 + 2 * column) + individualWidth + WIDTH_OF_ROD / 2,
- // (DistanceBetweenRods() * (column + 1)) + (DISKS_WIDTH * gameArray[row][column]),
- HEIGHT_OF_GROUND - ((NUMBER_OF_DISKS - row - 1) * DISKS_HEIGHT) - 1, FRAME_COLOR);
- // screenHeight()-((NUMBER_OF_DISKS-row-1)*DISKS_HEIGHT)- WIDTH_GROUND -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 );
- }
- }
- }
- int Win()
- {
- if(gameArray[0][NUMBER_OF_RODS - 1] == 1){
- textout((screenWidth()/2) - 100, (screenHeight()/2) - 50, "YOU WON", RED);
- updateScreen();
- getkey();
- return 1;
- }
- return 0;
- }
- 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 = (startTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2);
- printf("pozycja y zabierajac: %d\n", position_y);
- if(startTower < stopTower)
- {
- direction = 1;
- }
- if(startTower > stopTower)
- {
- direction = -1;
- }
- MovingUp( position_x, position_y, diskValue, stopTower, direction);
- HowManyDisks(stopTower);
- gameArray[stopRow][stopTower] = diskValue;
- }
- void MovingUp(int position_x, int position_y, int diskValue, int stopTower, int direction)
- {
- while((position_y + STEP) >= (screenHeight() - (WIDTH_GROUND + (NUMBER_OF_DISKS + 4) * DISKS_HEIGHT)))
- {
- Draw();
- filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
- position_y + DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y + DISKS_HEIGHT, FRAME_COLOR);
- position_y -= STEP;
- updateScreen();
- }
- 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 - (stopTower * 2 + 1) * screenWidth() / (NUMBER_OF_RODS * 2)) >= STEP)
- {
- Draw();
- filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),// (WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y + DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),//(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y + DISKS_HEIGHT, FRAME_COLOR);
- position_x = position_x + direction * STEP;
- updateScreen();
- }
- MovingDown(position_x, position_y, stopTower, diskValue);
- }
- void MovingDown(int position_x, int position_y, int stopTower, int diskValue)
- {
- printf("tyle jest dyskow na tej wiezy: %d\n", howManyDisksArr[stopTower]);
- while((position_y + DISKS_HEIGHT) <= (screenHeight() - (WIDTH_GROUND + (howManyDisksArr[stopTower] + 1) * DISKS_HEIGHT)))
- {
- Draw();
- filledRect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE), //(WIDTH_OF_ROD / 2 + diskValue * DISKS_WIDTH),
- position_y + DISKS_HEIGHT, DISKS_COLOR);
- rect(position_x - (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
- position_y,
- position_x + (WIDTH_OF_ROD / 2 + DISKS_WIDTH + diskValue * WIDTH_DIFFRENCE),
- position_y + DISKS_HEIGHT, FRAME_COLOR);
- position_y += STEP;
- updateScreen();
- }
- printf("upuszczajac: %d\n", position_y );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement