Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "primlib.h"
- #include <stdlib.h>
- #include <math.h>
- #define PEGS 3
- #define DISCS 3
- #define DISTANCE_BETWEEN_PEGS (screenWidth()/PEGS+1)
- #define SDLKTODECIMAL -49
- #define ZEROTOTEN 10
- //Global Variables
- float MULTIPLIER = 0.9;
- int POWEROFDISCS = DISCS - 1;
- void STARTING_GAME (int GAME[][DISCS]);
- void MOVE_DISC (int GAME[][DISCS]);
- int CHECK_KEY (int key, int GAME[][DISCS]);
- void check (int GAME[][DISCS]);
- int main(int argc, char* argv[])
- {
- int GAME[PEGS][DISCS];
- if(initGraph())
- {
- exit(3);
- }
- STARTING_GAME (GAME);
- check (GAME);
- do
- {
- MOVE_DISC(GAME);
- check (GAME);
- }while (GAME[PEGS-1][0] != 0);
- return 0;
- }
- void STARTING_GAME (int GAME[][DISCS])
- {
- int i, j;
- float changer = DISTANCE_BETWEEN_PEGS;
- for (i=0;i<PEGS;i++)
- {
- for (j=0;j<DISCS;j++)
- {
- if(j==0)
- {
- GAME[i][j] = changer*pow(MULTIPLIER, POWEROFDISCS)*1.0;
- POWEROFDISCS--;
- }
- else
- {
- GAME[i][j] = 0;
- }
- }
- }
- }
- void MOVE_DISC (int GAME[][DISCS])
- {
- int from, to, i, j;
- do
- {
- do
- {
- from=getkey();
- from=CHECK_KEY(from, GAME);
- printf ("from = %d\n", from);
- }while(from<0 || from>PEGS-1 || from != SDLK_RETURN);
- do
- {
- to=getkey();
- to=CHECK_KEY(from, GAME);
- printf ("to = %d\n", to);
- }while(to<0 || to>PEGS-1 || to != SDLK_RETURN);
- }while (from != to);
- for (i=0;i<DISCS;i++)
- {
- if(GAME[from][i] > 0)
- {
- for (j=0;j<DISCS;j++)
- {
- if ((GAME[to][j+1] != 0 && GAME[to][j+1] > GAME[from][i]) || GAME[to][DISCS-1] == 0)
- {
- GAME[to][j] = GAME[from][i];
- GAME[from][i] = 0;
- break;
- }
- }
- break;
- }
- }
- }
- int CHECK_KEY (int key, int GAME[][DISCS])
- {
- if (key == SDLK_RETURN)
- {
- STARTING_GAME(GAME);
- }
- else if (key == SDLK_ESCAPE)
- {
- filledRect(0,0,screenWidth(),screenHeight(),BLACK);
- textout(screenWidth()/2, screenHeight()/2, "Try again", RED);
- updateScreen();
- getkey();
- exit(3);
- }
- else if (key >= SDLK_1 && key <= SDLK_9)
- {
- return key + SDLKTODECIMAL;
- }
- else if (key == SDLK_0)
- {
- key = key + ZEROTOTEN;
- return key + SDLKTODECIMAL;
- }
- else
- {
- return key;
- }
- }
- void check (int GAME[][DISCS])
- {
- int i, j;
- for (i=0;i<PEGS;i++)
- {
- for (j=0;j<DISCS;j++)
- {
- printf ("[%d]\t", GAME[i][j]);
- }
- printf ("\n");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement