Ejemplo n.º 1
0
void shiftLeft(int start, int end, int diskCount, Queue<int> board[])
{
    int onePeg = start - 1;
    int otherPeg = end - 1;
    int steps = 0;
    double totalSteps = pow(2.0, (double)diskCount) - 1;
    Disk test;
    while(steps < totalSteps)
    {
        bool cases = (board[onePeg].isEmpty() || board[onePeg].peek() > board[otherPeg].peek()) ? true : false;
        makeValidMove(board[onePeg], board[otherPeg]);
        test.displayQueue(19, 20, board[0]);
        test.displayQueue(19, 40, board[1]);
        test.displayQueue(19, 60, board[2]);
        delay_output(DELAY);
        if(cases)
            board[onePeg].prioritize(board[onePeg].seek(board[onePeg].size()-1));   
        else
            board[otherPeg].prioritize(board[otherPeg].seek(board[otherPeg].size()-1));
        onePeg = (onePeg == 2) ? 0 : onePeg + 1;
        otherPeg = (otherPeg == 2) ? 0 : otherPeg + 1;
        steps++;
        test.displayQueue(19, 20, board[0]);
        test.displayQueue(19, 40, board[1]);
        test.displayQueue(19, 60, board[2]);
        delay_output(DELAY);
    }
}
Ejemplo n.º 2
0
int main(int argc, char *argv[])
{
    int diskCount = getDiskCount();
    int startPos = getStartPos();
    int endPos = getEndPos(startPos);

    Queue<int> peg1(diskCount);
    Queue<int> peg2(diskCount);
    Queue<int> peg3(diskCount);

    switch(startPos)
    {
    case 1:
        for(int i = 1; i <= diskCount; i++)
            peg1.enqueue(i);
        break;
    case 2:
        for(int i = 1; i <= diskCount; i++)
            peg2.enqueue(i);
        break;
    case 3:
        for(int i = 1; i <= diskCount; i++)
            peg3.enqueue(i);
        break;
    default:
        cout << "error in initializing startPeg. " << endl;
        break;
    }

    Queue<int> board[3];
    board[0] = peg1;
    board[1] = peg2;
    board[2] = peg3;

    initialize();
    cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
    
    Disk test;
    test.displayQueue(19, 20, board[0]);
    test.displayQueue(19, 40, board[1]);
    test.displayQueue(19, 60, board[2]);
    delay_output(DELAY);
    
    int findGoal = (startPos == 3) ? 1 : startPos + 1;
    cout << "                    1                   2                   3" << endl;
    
    if(diskCount%2) //case for odd number of disks
    {
        //shift in the same direction of peg
        if(findGoal == endPos) //if end peg is on right
            shiftRight(startPos, endPos, diskCount, board);
        else
            shiftLeft(startPos, endPos, diskCount, board);
    }
    else    //case for even number of disks
    {
        //shift in opposite direction of peg
        int otherPeg = findOtherPeg(startPos, endPos);
        if(findGoal == endPos) //if end peg is on right
            shiftLeft(startPos, otherPeg, diskCount, board);
        else
            shiftRight(startPos, otherPeg, diskCount, board);
    }
    system("PAUSE");
    return EXIT_SUCCESS;
}