问题描述
对于像我这样的初学者,我有一个相当艰难的任务。我正在寻求帮助
这里是分配
在这项任务中,你将被引导完成提供的程序骨架你这样一种方式,允许用户以交互方式解决河内塔问题(即使用PegB作为临时挂钩将n个磁盘从PegA移动到PegC),而不违反游戏规则即:
一次只能移动一个磁盘(任何挂钩上的顶部磁盘),并且,
磁盘d只能放在空挂钉上或磁盘顶部如果d小于D,则D在另一个Peg上。简单地说,如同在课堂上讨论的那样,较大的磁盘不能放在较小的磁盘上。
一个可执行文件,显示你的程序应该如何与之交互还提供了用户(和样本输出),以及程序框架的软拷贝。
该程序使用如下两种结构:
struct PegType
{
char name [_NameSize]; //存储Peg的名称,例如pegA
char shortName; //为Peg存储一个字符的名称,例如''A''
int disks [_ArraySize]; //初始化为{1,2,3,4,5,6,7,8,9}为完整(pegA)
//和{_NoDisk}(pegB和pegC)
int TopDiskLocation; //存储位置(索引)
//最顶层的磁盘
//这应该是= towers.size(= n个要移动的磁盘数)没有
//磁盘在那里并且每次递减1
//一个磁盘被添加,直到当所有n个磁盘都在这个挂钩上时它达到0。 />
int size; // n或要移动的磁盘数量(通常为3到9)
};
struct ThreePegsType
{
int GraphicsLargestDiskSize; //最大磁盘底座的最大宽度
//(默认使用10);
int Space_Between_Pegs; //默认使用= 5;
int size; // n或要移动的磁盘数量(通常为3到9)
PegType pegA;
PegType pegB;
PegType pegC;
};
使用这种安排,单个变量,例如ThreePegsType类型的塔将具有您的程序所需的所有结构。请记住确保始终初始化结构中的值并通过引用函数传递它们,以便对结构进行的更改实际上是在结构上完成而不是结构的副本。
最后主要程序,初始化程序和显示(打印)三个Pegs配置的例程作为教学辅助工具提供,并为您提供一个良好的开端。您可以使用这些或自己编写,
任务:
以下任务的详细说明将允许您测试在进行下一个任务之前,每个阶段的程序都要确保它是正确的:
1)研究数据结构,初始化例程以及显示(打印)三个Pegs的例程配置,以确保您了解它们的工作原理。编译并运行程序框架。
2)编写函数:
void addDiskToPeg(int diskNumber,PegType& peg)
将会只需减少TopDiskLocation并将磁盘diskNumber添加到阵列磁盘中新的顶部磁盘位置---在给定的挂钩上。 (此时不进行检查)。
通过在主程序的底部添加以下行来测试你的功能:
addDiskToPeg(4,towers.PegB) ;
printConfig(塔);
并运行程序。尝试使用其他测试向Pegs B或C添加不同的磁盘编号。请注意,如果磁盘已满,我们的功能不应允许添加磁盘。接下来我们将解决这个问题。
注意:请务必记住在测试完成后为测试添加的任何代码行。
3)编写函数:
bool isFull(const PegType& peg)
如果Peg已满,将返回true,否则返回false。
您可以通过以下方式测试它在主程序中添加以下行:
If(isFull(towers.PegX))
cout<< ?Peg已满\ n?;
其他
cout<< ?Peg未满\ n?;
将X更改为A,然后将B更改为C,然后运行它以确保其正常工作。然后在继续下一个任务之前评论(或删除)测试代码。
4)现在修改第2部分的addDiskToPeg(?)函数以检查并显示消息Peg已满 ?不再允许磁盘\ n如果钉子已满。否则它应该像在步骤2中那样添加磁盘。
通过在主程序中添加以下行来测试你的函数:
addDiskToPeg(1,towers.PegA) ;
addDiskToPeg(1,towers.PegB);
printConfig(塔);
5)编写函数:
bool isEmpty(const PegType& peg)
如果Peg为空则返回true,否则返回false
你可以测试它将以下行添加到主程序:
If(isEmpty(towers.PegX))
cout<< ?Peg是空的\ n?;
else
cout<< ?Peg不为空\ n?;
将X更改为A,然后将B更改为C,然后运行它以确保其正常工作。然后在继续下一个任务之前评论(或删除)测试代码。
6)现在编写函数:
bool canAddDisk(int diskNumber,const PegType& peg)
如果peg为空,或者diskNumber小于(小于)peg上的顶部磁盘,则返回true。否则返回false。您当然可以使用上一步中编写的isEmpty函数。
7)同样,修改第2部分的addDiskToPeg(?)函数以调用canAddDisk(?)函数检查diskNumber是否可以在实际添加之前添加到peg中。否则,它应显示消息无法在较小的磁盘上添加较大的磁盘\ n。
通过在主程序中添加以下行来再次测试您的功能:
addDiskToPeg(1,towers.PegB);
printConfig(塔);
addDiskToPeg(2,towers.PegB);
printConfig(塔);
在继续下一步之前测试并修复你的程序。
8)编写函数:
int removeDiskFromPeg(PegType& peg)
从peg中删除顶部磁盘并返回从peg中删除的磁盘的磁盘编号。该功能应首先检查:如果挂钩是空的并显示消息没有更多磁盘可用于移动\ n并返回_NoDisk(即0);否则它继续从peg中移除顶部磁盘(并用_NoDisk替换它的值)然后递增TopDiskLocation,并返回被删除的磁盘的磁盘号。
通过在主程序中添加以下行来测试你的功能:
cout<< ?磁盘已删除? << removeDiskFromPeg(towers.PegA)<< endl;
printConfig(塔);
cout<< ?磁盘已删除? << removeDiskFromPeg(towers.PegA)<< endl;
printConfig(塔);
在继续下一步之前测试并修复你的程序。
9)写功能:
void moveOneDisk(PegType& fromPeg,PegType& toPeg)
从fromPeg中删除顶部磁盘并将其添加到toPeg
填写以下代码:
{
if(____________)// fromPeg上没有磁盘移动
____________ ; //显示消息?磁盘X上没有磁盘可以移动\ n?
//其中X将酌情为A,B或C.
else
{
int diskToMove = fromPeg.disks [fromPeg.TopDiskLocation]
if(canAddDisk(diskToMove,toPeg))
{//在这里写下将函数调用到实际
//从fromPeg中删除一个磁盘并将其添加到toPeg
??
??。
} ;
其他
____; //显示消息?不允许移动\ n?
}
};
10)通过在主程序中添加以下行来测试以前的函数:
moveOneDisk(towers.PegC,towers.PegB); printConfig(塔);
moveOneDisk(towers.PegA,towers.PegB); printConfig(塔);
moveOneDisk(towers.PegA,towers.PegB); printConfig(塔);
moveOneDisk(towers.PegA,towers.PegB);
printConfig(塔);
测试在进行下一步之前修复程序。
11)现在将aline放入主程序中调用函数:
void InterActive_TOH_Solve(ThreePegsType& ;塔);
请注意,它正确地要求挂钩字母,但没有执行任何移动。
完成上述功能,以便调用moveOneDisk(?)根据用户输入
请注意,允许(合法移动)动作是:
A到B,A到C,B到A,B到C,C到A对于所有其他组合你应该输出吗?非法移动?
12)最后,注意如果你玩并成功将所有磁盘从PegA移动到PegC(或PegB)该计划并不祝贺你获胜而且不会停止。那是因为函数:
bool gameIsSolved(PegType& peg);
总是返回false。如果所有磁盘都在给定的挂钉上,则返回true
13)恭喜!喜欢玩游戏。
I have a pretty tough assignment for beginner like me & i''m seeking help please
here is the assign
In this assignment, you will be guided to complete the program skeleton provided to you in such a way as to allow the user to interactively solve the Tower of Hanoi problem (i.e. Moving n disks from PegA to PegC using PegB as a temporary Peg), without violating the rules of the game namely that:
Only One disk (the top disk on any Peg) can be moved at a time, and,
A disk d can only be placed on top of an empty Peg or on top of a disk D on another Peg if d is smaller than D. Simply stated a larger disk can not be placed on a smaller disk as discussed in class.
An Executable showing how your program should interact with the user (and sample outputs) have also been provided, as well as soft copies of the program skeleton.
The program uses two structures as follows:
struct PegType
{
char name[_NameSize]; // Stores name of the Peg, e.g. pegA
char shortName; // Stores a one character name for the Peg, e.g. ''A''
int disks[_ArraySize]; // initialize to { 1, 2, 3, 4, 5, 6, 7, 8, 9} for full (pegA)
// and to { _NoDisk } (pegB and pegC)
int TopDiskLocation; // Store the location (index)
// of the top most disk
// This should be = towers.size (=n number of disks to move) if no
// disks are there and decremented by 1 each time
// a disk is added until it reaches 0 when all the n disks are on this peg.
int size; // n or number of disks to move (3 to 9 typically)
};
struct ThreePegsType
{
int GraphicsLargestDiskSize; // Maximum width of base of largest disk
// (default use 10);
int Space_Between_Pegs; // default use = 5;
int size; // n or number of disks to move (3 to 9 typically)
PegType pegA;
PegType pegB;
PegType pegC;
};
With this arrangement, a single variable, e.g. towers of type ThreePegsType will have all the structures you need for your program. Remember to make sure you always initialize the values in the structures and pass them by reference to functions so that changes made to them are actually done on the structures and not to copies of the structures.
Finally the main program, the initialization routines and the routine that displays (prints) the three Pegs configuration are provided as a teaching aid and to give you a head start. You may use these or write your own,
Tasks:
The following tasks are detailed in such a way that will allow to test your program at every stage to ensure that it is correct before proceeding to the next task:
1)Study the data structures, the initialization routines and the routine that displays (prints) the three Pegs configuration to make sure you understand how they work. Compile and Run the program skeleton.
2)Write the function:
void addDiskToPeg (int diskNumber, PegType & peg)
that will simply decrement the TopDiskLocation and adds the disk diskNumber at the new top disk location in the array disks--- on the given peg. (At this point no checking is done).
Test your function by adding the following lines to the bottom of your main program:
addDiskToPeg (4, towers.PegB);
printConfig (towers);
and running the program. Experiment with additional tests adding different disk numbers to the Pegs B or C. Notice that our function should not allow a disk to be added if the disk is full. We will fix that next.
Note: Always remember to comment any lines of code that you add for testing after the testing is complete.
3)Write the function:
bool isFull (const PegType & peg)
that will return true if the Peg is full, false otherwise.
You may test it by adding the following lines to your main program:
If (isFull (towers.PegX))
cout << ?Peg is full \n?;
else
cout << ?Peg is not full \n?;
Change the X to A, then B then C, running it to ensure it is working correctly. Then comment (or delete) the testing code before proceeding to the next task.
4)Now modify your addDiskToPeg (?)function of part 2 to check and display the message "Peg is full ? No more disks are allowed\n" if the peg is full. Otherwise it should add the disk as was done in step 2.
Test your function by adding the following lines to your main program:
addDiskToPeg (1, towers.PegA);
addDiskToPeg (1, towers.PegB);
printConfig (towers);
5)Write the function:
bool isEmpty (const PegType & peg)
that will return true if the Peg is empty, false otherwise.
You may test it by adding the following lines to your main program:
If (isEmpty (towers.PegX))
cout << ?Peg is empty \n?;
else
cout << ?Peg is not empty \n?;
Change the X to A, then B then C, running it to ensure it is working correctly. Then comment (or delete) the testing code before proceeding to the next task.
6)Now write the function:
bool canAddDisk (int diskNumber, const PegType & peg)
that will return true if the peg is empty, or if diskNumber is less (smaller) than the top disk on the peg. Returns false otherwise. You may of course use the isEmpty function written in the previous step.
7)Again, modify your addDiskToPeg (?) function of part 2 to call the canAddDisk ( ?) function to also check that diskNumber can be added to peg before actually adding it. Otherwise, it should display the message "Can not add a larger disk on a smaller disk \n".
Test again your function by adding the following lines to your main program:
addDiskToPeg (1, towers.PegB);
printConfig (towers);
addDiskToPeg (2, towers.PegB);
printConfig (towers);
Test and fix your program before proceeding to the next step.
8)Write the function:
int removeDiskFromPeg (PegType & peg)
that removes the top disk from the peg and returns the disk number of the disk removed from the peg. The function should check first: if the peg is empty and display the message "No more disks are available to move\n" and return _NoDisk (i.e. 0); Otherwise it proceeds to removes the top disk from the peg (and replace its value with _NoDisk) It then increments the TopDiskLocation, and returns the disk number of the disk removed.
Test your function by adding the following lines to your main program:
cout << ?Disk Removed ? << removeDiskFromPeg (towers.PegA) << endl;
printConfig (towers);
cout << ?Disk Removed ? << removeDiskFromPeg (towers.PegA) << endl;
printConfig (towers);
Test and fix your program before proceeding to the next step.
9)Write the function:
void moveOneDisk (PegType & fromPeg, PegType & toPeg)
which removes the top disk from fromPeg and adds it to the toPeg
by completing the following code:
{
if ( ____________) //there are NO disks on the fromPeg to move
____________; //display the message ?No disks on disk X to move\n?
//where X will be A, B or C as appropriate.
else
{
int diskToMove = fromPeg.disks[fromPeg.TopDiskLocation]
if (canAddDisk (diskToMove , toPeg))
{ //write here the code that will call the functions to actually
// remove one disk from the fromPeg and adds it to the toPeg
??
??.
};
else
____;//display the message ?Move not allowed\n?
}
};
10)Test your previous function by adding the following lines to your main program:
moveOneDisk (towers.PegC, towers.PegB); printConfig (towers);
moveOneDisk (towers.PegA, towers.PegB); printConfig (towers);
moveOneDisk (towers.PegA, towers.PegB); printConfig (towers);
moveOneDisk (towers.PegA, towers.PegB);
printConfig (towers);
Test and fix your program before proceeding to the next step.
11)Now put aline in your main program to call the function:
void InterActive_TOH_Solve (ThreePegsType & towers);
Notice that it is correctly asking for the peg letters but is not performing any moves.
Complete the above function so that it calls the moveOneDisk( ?) as per the user inputs
Notice that allowable (legal moves) moves are:
A to B, A to C, B to A, B to C, C to A, and C to B. For All other combinations you should output ?Illegal Move?.
12)Finally, notice that if you play and successfully move all the disks from PegA to PegC (or to PegB) the program does not congratulate you on winning and does not stop. That is because the function:
bool gameIsSolved (PegType & peg);
always returns false. Make it return true if all disks are on the given peg
13) Congratulations ! Enjoy playing the game.
There is some big piece missing here. Where is the code that you have written? See guidelines.
这篇关于河内的塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!