Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct {
- int* pegA;
- int* pegB;
- int* pegC;
- unsigned int blocks;
- } state;
- void initState();
- void printState();
- int hasWon();
- void play();
- int main() {
- printf("Towers of Hanoi\t~By Arc\n\n");
- printf("Enter number of layers to play with:\t");
- scanf("%d", &state.blocks);
- initState();
- while(!hasWon()) {
- system("clear||cls");
- printf("Towers of Hanoi\t~By Arc\n\n");
- printState();
- play();
- }
- system("clear||cls");
- printf("Towers of Hanoi\t~By Arc\n\n");
- printState();
- printf("Congratulations! You solved the puzzle!");
- //clean
- free(state.pegA);
- free(state.pegB);
- free(state.pegC);
- return 0;
- }
- void initState() {
- int i;
- state.pegA = (int*)calloc(state.blocks, sizeof(int));
- state.pegB = (int*)calloc(state.blocks, sizeof(int));
- state.pegC = (int*)calloc(state.blocks, sizeof(int));
- //populate pegA
- for(i = 0; (unsigned int)i < state.blocks; i++) {
- *(state.pegA + i) = i + 1;
- }
- }
- void printState() {
- int i;
- for(i = 0; (unsigned int)i < state.blocks; i++) {
- printf("%d\t%d\t%d\n", *(state.pegA + i), *(state.pegB + i), *(state.pegC + i));
- }
- printf("Peg A Peg B Peg C\n\n");
- }
- int hasWon() {
- int i;
- //check in pegB
- for(i = 0; (unsigned int)i < state.blocks; i++) {
- if (*(state.pegB + i) != i + 1) break;
- }
- if ((unsigned int)i == state.blocks) {
- return 1;
- }
- //check in pegC
- for(i = 0; (unsigned int)i < state.blocks; i++) {
- if (*(state.pegC + i) != i + 1) break;
- }
- if ((unsigned int)i == state.blocks) {
- return 1;
- }
- return 0;
- }
- void play() {
- int ch;
- int source, target;
- int *sourcePeg, *targetPeg;
- int i;
- int block = 0;
- int blockIndex = 0;
- //clean input stream
- while((ch = getchar()) != '\n' && ch != EOF);
- printf("Select a peg to move a block from (A/B/C):\nPeg ");
- source = getchar();
- if(source != 'A' && source != 'B' && source != 'C' && source != 'a' && source != 'b' && source != 'c') {
- printf("Invalid input. Please try again.\n");
- play();
- }
- //clean input stream
- while((ch = getchar()) != '\n' && ch != EOF);
- printf("Select a peg to move the block to (A/B/C):\nPeg ");
- target = getchar();
- if(target != 'A' && target != 'B' && target != 'C' && target != 'a' && target != 'b' && target != 'c') {
- printf("Invalid input. Please try again.\n");
- play();
- }
- else if (source == target) {
- printf("You cannot move to the same peg!\n");
- play();
- }
- if (source == 'A' || source == 'a') sourcePeg = state.pegA;
- else if (source == 'B' || source == 'b') sourcePeg = state.pegB;
- else sourcePeg = state.pegC;
- if (target == 'A' || target == 'a') targetPeg = state.pegA;
- else if (target == 'B' || target == 'b') targetPeg = state.pegB;
- else targetPeg = state.pegC;
- //remove from source
- for(i = 0; (unsigned int)i < state.blocks; i++) {
- if (*(sourcePeg + i) != 0) {
- block = *(sourcePeg + i);
- //*(sourcePeg + i) = 0;
- blockIndex = i;
- break;
- }
- }
- if (block == 0) {
- printf("Peg %c has no blocks!\n", source);
- play();
- }
- //stack on target
- if (*(targetPeg + state.blocks - 1) == 0) {
- *(targetPeg + state.blocks - 1) = block;
- *(sourcePeg + blockIndex) = 0;
- }
- else for(i = 0; (unsigned int)i < state.blocks; i++) {
- if(*(targetPeg + i) != 0 && block < *(targetPeg + i)) {
- *(targetPeg + i - 1) = block;
- //remove from source
- *(sourcePeg + blockIndex) = 0;
- break;
- }
- else if (*(targetPeg + i) != 0 && block > *(targetPeg + i)) {
- printf("Cannot place a bigger block on top of a smaller block!\n");
- play();
- }
- }
- //clean
- sourcePeg = NULL;
- targetPeg = NULL;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement