Can't Find Problem In Coding
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Can't Find Problem In Coding

Hybrid View

  1. #1
    Join Date
    Jan 2012
    Posts
    1

    Exclamation Can't Find Problem In Coding

    I can't find the problem with this coding. I'm trying to make the tower of hanoi. I know I don't have the stop movement if a higher # is put on a lower #. (console Application) Please help.


    #include <iostream>
    #include <iomanip>
    //#include <SFML/Graphics.hpp>

    using namespace std;

    void move(int movefrom, int moveto, int movefromspot, int movetospot);
    bool Checkwin();
    void Draw();
    int MoveSpotFrom(int move);
    int MoveSpotTo(int move);

    int disks;
    int Left[100000];
    int Middle[100000];
    int Right[100000];

    int main()
    {
    int movefrom, moveto, movefromspot, movetospot;
    cout << "How many Disks?" << endl;
    cin >> disks;
    for (int a=disks-1; a>-1; a--)
    {
    Left[a]=disks-a;
    Middle[a]=0;
    Right[a]=0;
    }
    while(!Checkwin())
    {
    Draw();
    cout << "Move from what tower to what tower?" << endl;
    cin >> movefrom >> moveto;
    movefromspot=MoveSpotFrom(movefrom);
    movetospot=MoveSpotTo(moveto);
    cout << movefromspot << " " << movetospot << " " << Left[movefromspot] << " " << Middle[movetospot] << endl;
    if((movefrom != moveto) && (movefromspot != -1) && (movetospot != -1))
    move(movefrom, moveto, movefromspot, movetospot);
    }
    }

    void move(int movefrom, int moveto, int movefromspot, int movetospot)
    {
    switch(movefrom)
    {
    case 1:
    {
    switch(moveto)
    {
    case 2:
    {
    Middle[movetospot] = Left[movefromspot];
    Left[movefromspot] = 0;
    }
    case 3:
    {
    Right[movetospot] = Left[movefromspot];
    Left[movefromspot] = 0;
    }
    }
    }
    case 2:
    {
    switch(moveto)
    {
    case 1:
    {
    Left[movetospot] = Middle[movefromspot];
    Middle[movefromspot] = 0;
    }
    case 3:
    {
    Right[movetospot] = Middle[movefromspot];
    Middle[movefromspot] = 0;
    }
    }
    }
    case 3:
    {
    switch(moveto)
    {
    case 1:
    {
    Left[movetospot] = Right[movefromspot];
    Right[movefromspot] = 0;
    }
    case 2:
    {
    Middle[movetospot] = Right[movefromspot];
    Right[movefromspot] = 0;
    }
    }
    }
    }
    }

    bool Checkwin()
    {
    for (int a=disks-1; a>-1; a--)
    {
    if(Right[a]!=disks-a+1)
    return false;
    }
    return true;
    }

    void Draw()
    {
    cout << endl;
    for (int a=disks; a>-1; a--)
    {
    cout << setw(20) << Left[a] << setw(10) << Middle[a] << setw(10) << Right[a] << endl;
    }
    cout << setw(20) << "-" << setw(10) << "-" << setw(10) << "-" << endl << setw(20) << 1 << setw(10) << 2 << setw(10) << 3 << endl;
    }

    int MoveSpotFrom(int move)
    {
    switch(move)
    {
    case 1:
    {
    for (int a=disks; a>-1; a--)
    {
    if(Left[a]!=0)
    return a;
    }
    return disks;
    }
    case 2:
    {
    for (int a=disks; a>-1; a--)
    {
    if(Middle[a]!=0)
    return a;
    }
    return disks;
    }
    case 3:
    {
    for (int a=disks; a>-1; a--)
    {
    if(Right[a]!=0)
    return a;
    }
    return disks;
    }
    default:
    return -1;
    }
    }

    int MoveSpotTo(int move)
    {
    switch(move)
    {
    case 1:
    {
    for (int a=0; a<disks; a++)
    {
    if(Left[a]==0)
    return a;
    }
    }
    case 2:
    {
    for (int a=0; a<disks; a++)
    {
    if(Middle[a]==0)
    return a;
    }
    }
    case 3:
    {
    for (int a=0; a<disks; a++)
    {
    if(Right[a]==0)
    return a;
    }
    }
    default:
    return -1;
    }
    }

  2. #2
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,317

    Re: Can't Find Problem In Coding

    Quote Originally Posted by ulvak View Post
    I can't find the problem with this coding
    Welcome to the forum. You can help others help you by using [CODE][/CODE] tags.

    Code:
    #include <iostream>
    #include <iomanip>
    //#include <SFML/Graphics.hpp>
    
    using namespace std;
    
    void move(int movefrom, int moveto, int movefromspot, int movetospot);
    bool Checkwin();
    void Draw();
    int MoveSpotFrom(int move);
    int MoveSpotTo(int move);
    
    int disks;
    int Left[100000];
    int Middle[100000];
    int Right[100000];
    
    int main()
    {
        int movefrom, moveto, movefromspot, movetospot;
        cout << "How many Disks?" << endl;
        cin >> disks;
        for (int a=disks-1; a>-1; a--)
        {
            Left[a]=disks-a;
            Middle[a]=0;
            Right[a]=0;
        }
        while(!Checkwin())
        {
            Draw();
            cout << "Move from what tower to what tower?" << endl;
            cin >> movefrom >> moveto;
            movefromspot=MoveSpotFrom(movefrom);
            movetospot=MoveSpotTo(moveto);
            cout << movefromspot << "  " << movetospot << "  " << Left[movefromspot] << "  " << Middle[movetospot] << endl;
            if((movefrom != moveto) && (movefromspot != -1) && (movetospot != -1))
            move(movefrom, moveto, movefromspot, movetospot);
        }
    }
    
    void move(int movefrom, int moveto, int movefromspot, int movetospot)
    {
        switch(movefrom)
        {
            case 1:
            {
                switch(moveto)
                {
                    case 2:
                    {
                        Middle[movetospot] = Left[movefromspot];
                        Left[movefromspot] = 0;
                    }
                    case 3:
                    {
                        Right[movetospot] = Left[movefromspot];
                        Left[movefromspot] = 0;
                    }
                }
            }
            case 2:
            {
                switch(moveto)
                {
                    case 1:
                    {
                        Left[movetospot] = Middle[movefromspot];
                        Middle[movefromspot] = 0;
                    }
                    case 3:
                    {
                        Right[movetospot] = Middle[movefromspot];
                        Middle[movefromspot] = 0;
                    }
                }
            }
            case 3:
            {
                switch(moveto)
                {
                    case 1:
                    {
                        Left[movetospot] = Right[movefromspot];
                        Right[movefromspot] = 0;
                    }
                    case 2:
                    {
                        Middle[movetospot] = Right[movefromspot];
                        Right[movefromspot] = 0;
                    }
                }
            }
        }
    }
    
    bool Checkwin()
    {
        for (int a=disks-1; a>-1; a--)
        {
            if(Right[a]!=disks-a+1)
            return false;
        }
        return true;
    }
    
    void Draw()
    {
        cout << endl;
        for (int a=disks; a>-1; a--)
        {
            cout << setw(20) << Left[a] << setw(10) << Middle[a] << setw(10) << Right[a] << endl;
        }
        cout << setw(20) << "-" << setw(10) << "-" << setw(10) << "-" << endl << setw(20) << 1 << setw(10) << 2 << setw(10) << 3 << endl;
    }
    
    int MoveSpotFrom(int move)
    {
        switch(move)
        {
            case 1:
            {
                for (int a=disks; a>-1; a--)
                {
                    if(Left[a]!=0)
                    return a;
                }
                return disks;
            }
            case 2:
            {
                for (int a=disks; a>-1; a--)
                {
                    if(Middle[a]!=0)
                    return a;
                }
                return disks;
            }
            case 3:
            {
                for (int a=disks; a>-1; a--)
                {
                    if(Right[a]!=0)
                    return a;
                }
                return disks;
            }
            default:
            return -1;
        }
    }
    
    int MoveSpotTo(int move)
    {
        switch(move)
        {
            case 1:
            {
                for (int a=0; a<disks; a++)
                {
                    if(Left[a]==0)
                    return a;
                }
            }
            case 2:
            {
                for (int a=0; a<disks; a++)
                {
                    if(Middle[a]==0)
                    return a;
                }
            }
            case 3:
            {
                for (int a=0; a<disks; a++)
                {
                    if(Right[a]==0)
                    return a;
                }
            }
            default:
            return -1;
        }
    }
    Is your question related to IO?
    Read this C++ FAQ LITE article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center