Add to Favorites    Make Home Page 16812 Online  
 Language Categories  
 Our Services  

Home » C Home » Beginners / Lab Assignments Home » Tower of Hanoi

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Tower of Hanoi
Author james
Author Email james_miguel [at] yahoo.com
Description 1. For this program, we will assume that there are 5 disks
to
be used.
2. When the program starts, draw the starting position of the game.
3. Ask the user to choose the peg where a disk will be removed. The
user
should input 'A', 'B' or 'C'.
Check whether the user made a valid choice and inform the user of any
error. Of course, you should
also check whether that peg is empty or not (you cannot remove a disk
from
an empty peg). Your
program should also work for lowercase inputs.
4. After the user has correctly chosen a peg where a disk will be
removed
from, ask the user to which
peg will that disk be moved. Remember that you may not place a disk on
top
of a smaller one.
5. When a disk has been successfully moved in #4, you should draw the
new
disk positions. For
A B C
example, if we move a disk from peg A to peg B, using the starting
position show in #2, your program
should display the updated drawing:
5. Repeat steps #3, #4 and #5. The program should end when all the
disks
have been correctly placed
on peg C.
6. Alternatively, the user may also prematurely end the game by
inputting
'Q' in either #3 or #4.
7. Global variables are NOT allowed in your program.
8. Follow the EXACT specifications given for asking input from the user
(steps #3 and #4). When
testing your program, your instructor will be using input redirection
so
the input sequence is important.
Category C » Beginners / Lab Assignments
Hits 370122
Code Select and Copy the Code
#include<stdio.h> void initialize (int disks, int peg_A[8], int peg_B[8], int peg_C[8]); void get_number_of_disks(int *disks); void print_status(int disks, int peg_A[8], int peg_B[8], int peg_C[8]); void print_no_disk(char *choice_from); int validate_move_from(char *choice_from, int peg_A[8], int peg_B[8], int peg_C[8]); int validate_move_to(int disks, char choice_from, char *choice_to, int *index_from, int peg_A[8], int peg_B[8], int peg_C[8]); void disk_on_top(int disks, int *top_of_stack_to, char choice_to, int peg_A[8], int peg_B[8], int peg_C[8]); void move_disk(int disks, char choice_from, char choice_to, int index_from, int peg_A[8], int peg_B[8], int peg_C[8]); int main (void) { int peg_A[8]; /*initial number of asterisks in peg_A*/ int peg_B[8]; /*initial number of asterisks in peg_B*/ int peg_C[8]; /*initial number of asterisks in peg_C*/ int disks; /*desired number of disks*/ int index_from; /*position of the disk in a peg*/ char choice_from; /*the peg where a disk will be removed*/ char choice_to; /*the peg where a disk will be transferred*/ printf(" Instructions on playing TOWERS OF HANOI: There are three pegs, labeled A, B, and C. Initially, peg A contains a certain amount of disks, each one with a different size. The disks are stacked in increasing size so that a disk is always on top of a larger one, forming a tower. The goal of the game is to move all the disks from peg A to peg C. You may move only one disk at a time. You may move the top disk from any peg to the top of the stack at another peg. The only limitation is that you may not place a disk on top of one which is smaller. YOU MAY STOP THE GAME ANYTIME BY ENTERING Q (THEN PRESSING THE ENTER KEY) "); get_number_of_disks(&disks); initialize(disks, peg_A, peg_B, peg_C); print_status(disks, peg_A, peg_B, peg_C); while (peg_C[disks-1]!=3) { validate_move_from(&choice_from, peg_A, peg_B, peg_C); if(choice_from=='Q' || choice_from=='q') break; validate_move_to(disks, choice_from, &choice_to, &index_from, peg_A, peg_B, peg_C); if(choice_to=='Q' || choice_to=='q') break; move_disk(disks, choice_from, choice_to, index_from, peg_A, peg_B, peg_C); print_status(disks, peg_A, peg_B, peg_C); } if(peg_C[disks-1]==3) printf("Congratulations! You wonà "); return 0; } /*initializes the contents of the pegs*/ void initialize (int disks, int peg_A[8], int peg_B[8], int peg_C[8]) { int i; for(i=0; i<disks; i++) { peg_A[i]=(disks-i)*2 + 1; peg_B[i]=1; peg_C[i]=1;}} /*gets the desired number of disks(min of 3, max of 8)*/ void get_number_of_disks(int *disks) { printf("Enter the desired number of disks(min of 3, max of 8): "); scanf("%d", disks); while(*disks>8 || *disks<3) { printf("Invalid input! Enter the desired number of disks(min of 3, max of 8): "); scanf("%d", disks); }} /*displays the present position of the disks*/ void print_status(int disks, int peg_A[8], int peg_B[8], int peg_C[8]) { int number_spaces; /*number of spaces*/ int number_asterisk; /*number of spaces*/ int i=1; /*counter*/ printf(" "); for(i=1; i<=disks; i++) { for(number_spaces=1; number_spaces<= (disks+3)-(peg_A[disks-i]/2); number_spaces++) printf(" "); for(number_asterisk=1; number_asterisk <= peg_A[disks-i]; number_asterisk++) printf("*"); for(number_spaces=1; number_spaces<= (disks+3)-(peg_A[disks-i]/2); number_spaces++) printf(" "); for(number_spaces=1; number_spaces<= (disks+3)-(peg_B[disks-i]/2); number_spaces++) printf(" "); for(number_asterisk=1; number_asterisk <= peg_B[disks-i]; number_asterisk++) printf("*"); for(number_spaces=1; number_spaces<= (disks+3)-(peg_B[disks-i]/2); number_spaces++) printf(" "); for(number_spaces=1; number_spaces<= (disks+3)-(peg_C[disks-i]/2); number_spaces++) printf(" "); for(number_asterisk=1; number_asterisk <= peg_C[disks-i]; number_asterisk++) printf("*"); printf(" ");} for(i=1; i<=(disks*2 + 7)*3; i++) printf("*"); printf(" "); for(i=1; i<=disks+3; i++) printf(" "); printf("A"); for(i=1; i<=(disks + 3)*2; i++) printf(" "); printf("B"); for(i=1; i<=(disks + 3)*2; i++) printf(" "); printf("C "); } /*prints that there's no disk and asks the user to enter another input*/ void print_no_disk(char *choice_from) { printf("There's no disk! Choose a peg with disk(s). Move a disk from what peg?: "); scanf("%s", choice_from); } /*asks the user to enter the name of the peg from where a disk will be removed*/ /*validates input*/ int validate_move_from(char *choice_from, int peg_A[8], int peg_B[8], int peg_C[8]) { printf("Move a disk FROM what peg?: "); scanf("%c", choice_from); if (*choice_from=='Q' || *choice_from=='q') return 0; while(peg_A[0] == 1 && (*choice_from == 'A' || *choice_from =='a')) print_no_disk(choice_from); while(peg_B[0] == 1 && (*choice_from == 'B' || *choice_from =='b')) print_no_disk(choice_from); while(peg_C[0] == 1 && (*choice_from == 'C' || *choice_from =='c')) print_no_disk(choice_from); while(*choice_from<'A' || (*choice_from>'C' && *choice_from<'a') || *choice_from>'c') {printf("(A, B, C or Q only): "); scanf("%c", choice_from); if (*choice_from=='Q' || *choice_from=='q') return 0; while(peg_A[0] == 1 && (*choice_from == 'A' || *choice_from =='a')) print_no_disk(choice_from); while(peg_B[0] == 1 && (*choice_from == 'B' || *choice_from =='b')) print_no_disk(choice_from); while(peg_C[0] == 1 && (*choice_from == 'C' || *choice_from =='c')) print_no_disk(choice_from); } return 0;} /*asks the user to enter the name of the peg to which a disk will be placed*/ /*also validates input*/ int validate_move_to(int disks, char choice_from, char *choice_to, int *index_from, int peg_A[8], int peg_B[8], int peg_C[8]) { int i; int top_of_stack_from, /*the disk to be removed*/ top_of_stack_to; /*the smallest disk in a certain peg*/ for (i=disks-1; i>=0; i--) { if ((choice_from=='A' || choice_from=='a') && peg_A[i]!=1) { top_of_stack_from = peg_A[i]; *index_from=i; break; } else if ((choice_from=='B' || choice_from=='b') && peg_B[i]!=1) { top_of_stack_from = peg_B[i]; *index_from=i; break; } else if ((choice_from=='C' || choice_from=='c') && peg_C[i]!=1) { top_of_stack_from = peg_C[i]; *index_from=i; break; }} printf("Move the disk TO: "); scanf("%c", choice_to); if (*choice_to=='Q' || *choice_to=='q') return 0; disk_on_top(disks, &top_of_stack_to, *choice_to, peg_A, peg_B, peg_C); while(*choice_to<'A' || (*choice_to>'C' && *choice_to<'a') || *choice_to>'c') { printf("(A, B, or C only): "); scanf("%c", choice_to); if (*choice_to=='Q' || *choice_to=='q') return 0; disk_on_top(disks, &top_of_stack_to, *choice_to, peg_A, peg_B, peg_C); } while(top_of_stack_to-top_of_stack_from<0) { printf("That is an INVALID move! Choose a peg with a larger disk. Move the disk TO: "); scanf("%c", choice_to); if (*choice_to=='Q' || *choice_to=='q') return 0; while(*choice_to<'A' || (*choice_to>'C' && *choice_to<'a') || *choice_to>'c') { printf("(A, B, or C only): "); scanf("%c", choice_to); if (*choice_to=='Q' || *choice_to=='q') return 0; disk_on_top(disks, &top_of_stack_to, *choice_to, peg_A, peg_B, peg_C); }} return 0; } /*determines the top of stack of a certain peg where a disk will be placed*/ void disk_on_top(int disks, int *top_of_stack_to, char choice_to, int peg_A[8], int peg_B[8], int peg_C[8]) { int i; for (i=disks-1; i>=0; i--) { if ((choice_to=='A' || choice_to=='a') && peg_A[i]!=1) { *top_of_stack_to = peg_A[i]; break; } else if ((choice_to=='B' || choice_to=='b') && peg_B[i]!=1) { *top_of_stack_to = peg_B[i]; break; } else if ((choice_to=='C' || choice_to=='c') && peg_C[i]!=1) { *top_of_stack_to = peg_C[i]; break; } else *top_of_stack_to=17; }} /*moves the chosen disk to the desired peg*/ void move_disk(int disks, char choice_from, char choice_to, int index_from, int peg_A[8], int peg_B[8], int peg_C[8]) { int i, temp, index_to; for (i=0; i<disks; i++) { if ((choice_to=='A' || choice_to=='a') && peg_A[i]==1) { index_to=i; break; } else if ((choice_to=='B' || choice_to=='b') && peg_B[i]==1) { index_to=i; break; } else if ((choice_to=='C' || choice_to=='c') && peg_C[i]==1) { index_to=i; break; }} if ((choice_from=='A' || choice_from=='a') && (choice_to=='B' || choice_to=='b')) { temp = peg_B[index_to]; peg_B[index_to] = peg_A[index_from]; peg_A[index_from] = temp; } else if ((choice_from=='A' || choice_from=='a') && (choice_to=='C' || choice_to=='c')) { temp = peg_C[index_to]; peg_C[index_to] = peg_A[index_from]; peg_A[index_from] = temp; } else if ((choice_from=='B' || choice_from=='b') && (choice_to=='C' || choice_to=='c')) { temp = peg_C[index_to]; peg_C[index_to] = peg_B[index_from]; peg_B[index_from] = temp; } else if ((choice_from=='B' || choice_from=='b') && (choice_to=='A' || choice_to=='a')) { temp = peg_A[index_to]; peg_A[index_to] = peg_B[index_from]; peg_B[index_from] = temp; } else if ((choice_from=='C' || choice_from=='c') && (choice_to=='A' || choice_to=='a')) { temp = peg_A[index_to]; peg_A[index_to] = peg_C[index_from]; peg_C[index_from] = temp; } else if ((choice_from=='C' || choice_from=='c') && (choice_to=='B' ||choice_to=='b')) { temp = peg_B[index_to]; peg_B[index_to] = peg_C[index_from]; peg_C[index_from] = temp; }}

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
RECURSIVE BALANCED QUICK SORT ashish
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi ¦Õ¤ ?¤Ð Ãß??ÝÐÝ
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=1147


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review