河内的塔 [英] towers of hanoi

查看:56
本文介绍了河内的塔的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于像我这样的初学者,我有一个相当艰难的任务。我正在寻求帮助


这里是分配


在这项任务中,你将被引导完成提供的程序骨架你这样一种方式,允许用户以交互方式解决河内塔问题(即使用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)恭喜!喜欢玩游戏。

解决方案

这里是un full的骨架


#include< iostream>

#include< iomanip>


使用命名空间std;


const int _ArraySize = 12; //保存磁盘的数组的大小(用作堆栈)

const int _NameSize = 15; //保存Peg名称的字符数组的大小

const int _NoDisk = 0; //初始化peg数组的元素为0表示

//没有磁盘,否则存储磁盘号(1个最小值

struct PegType

{

char name [_NameSize]; //存储Peg的名称,例如pegA

char shortName; //为Peg存储一个字符的名称,例如''A''

int disks [_ArraySize]; //初始化为{1,2,3, 4,5,6,7,8,9} for full(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;

};


void printSpaces(int HowMany)

{

for(int i = 0; i< HowMany; i ++)

cout<< " " ;;

}


void printBase(const ThreePegsType& towers)

{

for(int i = 0; i< 2 * towers.GraphicsLargestDiskSize - 1; i ++)

cout<< " =" ;;

printSpaces(towers.Space_Between_Pegs);


for(int j = 0; j< 2 * towers.GraphicsLargestDiskSize - 1 ; j ++)

cout<< " =";

printSpaces(towers.Space_Between_Pegs);


for(int k = 0; k< 2 * towers.GraphicsLargestDiskSize - 1 ; k ++)

cout<< " =";

cout<<结束;

}


void printPartLine(const PegType& peg,int level,const ThreePegsType& towers)// level为0对于顶行,arraySize为底部

{

int howManySpaces;

if(peg.disks [level] == _NoDisk)// print一个较少的对称空间

{

howManySpaces = 2 * towers.GraphicsLargestDiskSize - 1;

printSpaces(howManySpaces);

}

其他

{

howManySpaces = towers.GraphicsLargestDiskSize - peg.disks [level];


printSpaces(howManySpaces);

//打印磁盘号n,(2 * n - 1)次

for(int j = 0; j < 2 * peg.disks [level] -1; j ++)

cout<< peg.disks [等级];


printSpaces(howManySpaces);

}


printSpaces(towers.Space_Between_Pegs );

}


void printConfig(const ThreePegsType塔)

{

for(int level = 0; level< towers.size; level ++)

{

printPartLine(towers.pegA,level,towers);

printPartLine(towers.pegB,level,towers);

printPartLine(towers.pegC,level,towers);

cout<<结束;

}

printBase(塔);

}



/ /将磁盘初始化为{1,2,3,4,5,6,7,8,9,...};

void initializeToFull(PegType& peg,int numOfDisks)

{

for(int location = 0; location< numOfDisks; location ++)

{

int diskNumber = location + 1;

peg.disks [location] = diskNumber;

}

peg.TopDiskLocation = 0; //挂满了

peg.size = numOfDisks;

}


//将磁盘初始化为{_NoDisk};

void initializeToEmpty(PegType& peg,int numOfDisks)

{

for(int location = 0; location< numOfDisks; location ++)

{

peg.disks [location] = _NoDisk;

}

peg.TopDiskLocation = numOfDisks; // peg是空的

peg.size = numOfDisks;

}


char getPegLetter()

{

char diskLetter;

while(true)

{

cin>> diskLetter;

diskLetter = toupper(diskLetter);


if((diskLetter ==''A'')||(diskLetter ==''B '')||

(diskLetter ==''C'')||(diskLetter ==''Q''))

return diskLetter;

其他cout<< \ nPlease ...只键入A,B,C(a,b,c)播放,Q(或q)停止\ nn;

}

}


//如果所有磁盘都在Peg上,则将其更改为true

bool gameIsSolved(PegType& peg)

{

返回false;

}


无效InterActive_TOH_Solve(ThreePegsType&塔)

{

bool continePlaying = true;


while(continePlaying)

{

cout<< A,B,C要播放,Q要停止\ n;;

cout<< 从Peg移动磁盘:;

char fromPegLetter = getPegLetter();

if(fromPegLetter!=''Q'')

{

cout<< \ n从Peg移动磁盘: << fromPegLetter<< " to Peg:" ;;

char toPegLetter = getPegLetter();

if(toPegLetter!=''Q'')

{

//这里放置调用相应代码的代码

// .......

// ...... 。

// .......

// .......

// ..... ..

printConfig(塔); //移动后打印新配置

}

其他continePlaying = false;

}

其他continePlaying = false;

if(gameIsSolved(towers.pegB)|| gameIsSolved(towers.pegC))

continePlaying = false;

};

}



int main()

{

ThreePegsType塔;

towers.GraphicsLargestDiskSize = 10; //最大磁盘的最大基数宽度(默认值= 10);

towers.Space_Between_Pegs = 5; //默认使用= 5;


towers.size = 0;

while((towers.size< = 0)||(towers.size > 9))

{

cout<< 输入要移动的磁盘数量.n(1-9)\ n;

cin>> towers.size;

}


//将钉子的短名称初始化为''A'',''B''和'' C''分别

towers.pegA.shortName =''A'';

towers.pegB.shortName =''B'';

towers.pegC.shortName =''C'';


//初始化磁盘[]和每个挂钩中顶部磁盘的位置(索引)

initializeToFull(towers.pegA,towers.size); //peg.TopDiskLocation = 0;意味着peg已满

initializeToEmpty(towers.pegB,towers.size); // peg.TopDiskLocation = towers.size意味着peg为空

initializeToEmpty(towers.pegC,towers.size);


cout<< Pegs --- initial configuration\\\
\ n;

printConfig(塔);


cout<< endl;


返回0; //表示成功终止


} //结束主


/ *样本交互..输出

输入要移动的磁盘数量.. n(1- 9)

3

钉子---初始配置


PegA PegB PegC


1

222

33333

========= ========== =================== =================== />
输入A,B,C,Q停止

从Peg移动磁盘:b


从Peg移动磁盘:b to Peg:a

Peg上没有磁盘:B要移动。


这是真正的挑战因为我必须提交它tommorow所以专家们com'' on



这是一个真正的挑战因为我必须提交它tommorow所以专家们com''on


这里有一些很大的缺失。你写的代码在哪里?请参阅g uidelines


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.

解决方案

and here is the un full sekeleton

#include <iostream>
#include <iomanip>

using namespace std;


const int _ArraySize = 12; // size of array that holds the disks (used as a stack)
const int _NameSize = 15; // size of character array that holds the Peg name
const int _NoDisk = 0; // intialize an element of the peg array to 0 to indicate that
// no disk is present, otherwise store a disk number ( 1 smallest
// disk size,9 largest disk size
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 = 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;
};


void printSpaces(int HowMany)
{
for (int i= 0; i < HowMany; i++)
cout << " ";
}

void printBase(const ThreePegsType & towers)
{
for (int i= 0; i < 2 * towers.GraphicsLargestDiskSize - 1 ; i++)
cout << "=";
printSpaces(towers.Space_Between_Pegs);

for (int j= 0; j < 2 * towers.GraphicsLargestDiskSize - 1 ; j++)
cout << "=";
printSpaces(towers.Space_Between_Pegs);

for (int k= 0; k < 2 * towers.GraphicsLargestDiskSize - 1 ; k++)
cout << "=";
cout << endl;
}


void printPartLine(const PegType & peg, int level, const ThreePegsType & towers) // level is 0 for top line , arraySize for bottom
{
int howManySpaces;
if (peg.disks[level] == _NoDisk) // print one less space for symmetry
{
howManySpaces = 2 * towers.GraphicsLargestDiskSize - 1;
printSpaces(howManySpaces);
}
else
{
howManySpaces = towers.GraphicsLargestDiskSize - peg.disks[level];

printSpaces(howManySpaces);
// print the disk number n, (2*n - 1) times
for (int j= 0; j <2 * peg.disks[level] -1; j++)
cout << peg.disks[level];

printSpaces(howManySpaces);
}

printSpaces(towers.Space_Between_Pegs);
}


void printConfig (const ThreePegsType towers)
{
for (int level = 0; level < towers.size; level++)
{
printPartLine(towers.pegA, level, towers);
printPartLine(towers.pegB, level, towers);
printPartLine(towers.pegC, level, towers);
cout << endl;
}
printBase(towers);
}


// initialize disks to { 1, 2, 3, 4, 5, 6, 7, 8, 9, ...};
void initializeToFull(PegType & peg, int numOfDisks)
{
for (int location = 0; location < numOfDisks; location++)
{
int diskNumber = location + 1;
peg.disks[location] = diskNumber;
}
peg.TopDiskLocation = 0; //peg is full
peg.size = numOfDisks;
}

// initialize disks to { _NoDisk };
void initializeToEmpty(PegType & peg, int numOfDisks)
{
for (int location = 0; location < numOfDisks; location++)
{
peg.disks[location] = _NoDisk;
}
peg.TopDiskLocation = numOfDisks; //peg is empty
peg.size = numOfDisks;
}


char getPegLetter()
{
char diskLetter;
while (true )
{
cin >> diskLetter;
diskLetter = toupper(diskLetter);

if ((diskLetter == ''A'') || (diskLetter == ''B'') ||
(diskLetter == ''C'') || (diskLetter == ''Q''))
return diskLetter;
else cout << "\nPlease ... Type only A, B, C (a, b, c) to play, Q (or q) to stop\n";
}
}

// Change this to return true if all disks are on the Peg
bool gameIsSolved (PegType & peg)
{
return false;
}


void InterActive_TOH_Solve(ThreePegsType & towers)
{
bool continePlaying = true;

while ( continePlaying )
{
cout << "Type A, B, C to play, Q to stop\n";
cout << "Move disk from Peg: ";
char fromPegLetter = getPegLetter();
if (fromPegLetter != ''Q'')
{
cout << "\nMove disk from Peg: " << fromPegLetter << " to Peg: ";
char toPegLetter = getPegLetter();
if (toPegLetter != ''Q'')
{
//put here the code that calls the appropriate
// .......
// .......
// .......
// .......
// .......
printConfig (towers); // prints the new configuration after the move
}
else continePlaying = false;
}
else continePlaying = false;
if (gameIsSolved (towers.pegB) || gameIsSolved (towers.pegC))
continePlaying = false;
};
}



int main()
{
ThreePegsType towers;
towers.GraphicsLargestDiskSize = 10; // Maximum width of base of largest disk (default = 10);
towers.Space_Between_Pegs = 5; // default use = 5;

towers.size = 0;
while ((towers.size <= 0) || (towers.size > 9))
{
cout << "Enter the number of disks to move .. n ( 1- 9)\n";
cin >> towers.size;
}

// initialize the short names of the pegs to ''A'', ''B'', and ''C'' respectively
towers.pegA.shortName = ''A'';
towers.pegB.shortName = ''B'';
towers.pegC.shortName = ''C'';

// initialize disks[] and the location (index) of the top disk in each peg
initializeToFull(towers.pegA, towers.size); //peg.TopDiskLocation = 0; means peg is full
initializeToEmpty(towers.pegB, towers.size ); // peg.TopDiskLocation = towers.size means peg is empty
initializeToEmpty(towers.pegC, towers.size);

cout << "Pegs --- initial configuration\n\n";
printConfig (towers);

cout << endl;

return 0; // indicates successful termination

} // end main

/* Sample Interactions .. Outputs
Enter the number of disks to move .. n ( 1- 9)
3
Pegs --- initial configuration

PegA PegB PegC

1
222
33333
=================== =================== ===================
Type A, B, C to play, Q to stop
Move disk from Peg: b

Move disk from Peg: b to Peg: a
No disks on Peg: B to move .


It''s a real challange cuz i gotta submit it tommorow so expert guys com'' on


It''s a real challange cuz i gotta submit it tommorow so expert guys com'' on

There is some big piece missing here. Where is the code that you have written? See guidelines.


这篇关于河内的塔的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆