Thread: logic error

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    5

    Post logic error

    Code:
    bool flip(char board[8][8], bool players, char player[2], int ck[8][8], int rows, int column)
    {
        int test, x, new_col, new_row;
        bool ok = false;
        char opponent = player[!players];
    
    
        for (i = 0; i < 8; i++)
        {
            for (j = 0; j < 8; j++)
            {
                ck[i][j] = 0;
            }
        }
    
    
    
    
        new_col = column - 1;
        test = 0;
        for (i = new_col; i >= 0; i--) //To left of selection
        {
    
    
            if (board[rows][i] == opponent)
            {
                test--;
            }
            else if (board[rows][i] == ' ')
            {
                i = -1;
            }
            else if (board[rows][i] == player[players])
            {
                ok = true;
                for (x = new_col + test; x <= new_col; x++)
                {
                    ck[rows][x] = 1;
    
    
                }
                i = -1;
            }
        }
        new_col = column + 1;
        test = 0;
        for (i = new_col; i < 8; i++) //To right of selection
        {
    
    
            if (board[rows][i] == opponent)
            {
                test++;
            }
            else if (board[rows][i] == ' ')
            {
                i = 8;
            }
            else if (board[rows][i] == player[players])
            {
                ok = true;
                for (x = new_col; x <= new_col + test; x++)
                {
                    ck[rows][x] = 1;
    
    
                }
                i = 8;
            }
        }
        new_row = rows - 1;
        test = 0;
        for (i = new_row; i > -1; i--)  //To top of selection
        {
            if (board[i][column] == opponent)
            {
                test--;
            }
            else if (board[i][column] == ' ')
            {
                i = -1;
            }
            else if (board[i][column] == player[players])
            {
                ok = true;
                for (x = new_row + test; x <= new_row; x++)
                {
                    ck[x][column] = 1;
    
    
                }
                i = -1;
            }
        }
    
    
        new_row = rows + 1;
        test = 0;
        for (i = new_row; i < 8; i++)  //To bottom of selection
        {
            if (board[i][column] == opponent)
            {
                test++;
            }
            else if (board[i][column] == ' ')
            {
                i = 8;
            }
            else if (board[i][column] == player[players])
            {
                ok = true;
                for (x = new_row; x <= new_row + test; x++)
                {
                    ck[x][column] = 1;
    
    
                }
                i = -1;
            }
        }
    
    
        new_col = column + 1;
        new_row = rows + 1;
        test = 0;
        for (i = new_row; i < 8; i++)  //To lower right
        {
            if (board[i][new_col] == opponent && new_col < 8)
            {
                test++;
            }
            else if (board[i][new_col] == ' ' && new_col < 8)
            {
                i = 9;
            }
            else if (board[i][new_col] == player[players])
            {
                ok = true;
                new_col = column + 1;
                for (x = new_row; x <= new_row + test; x++)
                {
                    ck[x][new_col] = 1;
                    new_col++;
                }
                i = 9;
            }
            new_col++;
        }
        new_col = column - 1;
        new_row = rows - 1;
        test = 0;
        for (i = new_row; i >= 0; i--)  //To upper left
        {
            if (board[i][new_col] == opponent && new_col > -1)
            {
                test--;
            }
            else if (board[i][new_col] == ' ' && new_col > -1)
            {
                i = -1;
            }
            else if (board[i][new_col] == player[players])
            {
                ok = true;
                new_col = column - 1;
                for (x = new_row ; x > new_row + test; x--)
                {
                    ck[x][new_col] = 1;
                    new_col--;
                }
                i = 9;
            }
            new_col--;
        }
    
    
        new_row = rows - 1;
        new_col = column + 1;
        test = 0;
        for (i = new_row; i >= 0; i--)  //To upper right
        {
            if (board[i][new_col] == opponent && new_col < 8)
            {
                test--;
            }
            else if (board[i][new_col] == ' ' && new_col < 8)
            {
                i = -1;
            }
            else if (board[i][new_col] == player[players])
            {
                ok = true;
                new_col = column + 1;
                for (x = 0; x >= test; x--)
                {
                    ck[new_row+x][new_col] = 1;
                    new_col++;
                }
                i = -1;
            }
            new_col++;
        }
    
    
        new_row = rows + 1;
        new_col = column - 1;
        test = 0;
        for (i = new_row; i < 8; i++)  //To lower left
        {
            if (board[i][new_col] == opponent && new_col >= 0)
            {
                test++;
            }
            else if (board[i][new_col] == ' ' && new_col >=0)
            {
                i = 8;
            }
            else if (board[i][new_col] == player[players])
            {
                ok = true;
                new_col = column - 1;
                for (x = new_row; x <= new_row + test; x++)
                {
                    ck[x][new_col] = 1;
                    new_col--;
                }
                i = 8;
            }
            new_col--;
        }
    
    
    
    
        return ok;
    }
    
    
    void playgame(char board[8][8], bool players, char player[2])
    {
        int x_quantity, o_quantity, rows, column;
        int ck[8][8];
        char cols;
        bool found, ok;
        do
        {
            x_quantity = 0, o_quantity = 0;
            for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                    if (board[i][j] == 'X')
                        x_quantity++;
                    else if (board[i][j] == 'O')
                        o_quantity++;
            }
    
    
            cout << "Score:             O = " << o_quantity << "    X = " << x_quantity << endl;
            cout << "Current Player:   "<< player[players] << endl;
            cout << "==>" << endl;
            cin >> cols;
            column = toupper(cols) - 65; // ASCII value of 'A' is 65, H is 72,
            //so column equals 0 to 7, corresponding to array
            cin >> rows;
            rows--; // Array starts at 0. Starts with 1 on screen. Subtract 1 to match array
            found = false;
            for (i = -1; i < 2; i++)
            {
                for (j = -1; j < 2; j++)
                {
                    if (board[rows + i][column + j] == player[!players])
                    {
                        found = true;
                    }
                }
            }
            if (found)
            {
                ok = flip(board, players, player, ck, rows, column);
                if (ok)
                {
                    board[8-rows-1][column] = player[players];
                    for (i = 0; i < 8; i++)
                    {
                        for (j = 0; j < 8; j++)
                        {
                            if (ck[i][j] == 1)
                            {
                                board[i][j] = player[players];
                            }
                        }
                    }
                    drawboard(board);// Redraw board after placement of piece
                    players = !players; // Switch players
                }
    
    
            }
            else
                cout << "Error 404" << endl;
            //drawboard(board);// Redraw board after placement of piece
    
    
        } while (true);
    }
    the major of program is there,it's doesnot flip well.
    anyone can help?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mike9407
    the major of program is there,it's doesnot flip well.
    anyone can help?
    Unfortunately, "does not flip well" is too vague. You need to clearly describe what you are trying to do and explain how does it not work. It might also help if you would provide a main function that is used to test the code, then tell us the test input, expected output and actual output.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    my flip function related to the if(found) then it's will flip,according to my flip function it's just flip top and button and another 6 corner do not flip,it's a my reversi game btw

  4. #4
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Let's slow it on down.

    What do you want to do?
    What do correct results looks like?
    How will you test for correct results?
    What will incorrect results look like?
    How will you test for incorrect results?

  5. #5
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    u try to download the codes and run u will know error in where...i can't fix the error that why i calling you all to help

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mike9407
    u try to download the codes and run u will know error in where...i can't fix the error that why i calling you all to help
    After I do that, perhaps I will know the error, and maybe I will be able to fix it, but you won't be able to get my fixes without paying me... but as this is probably an academic assignment, that will be cheating, so I would either be ethical and refuse, or if I want to be unethical, I might as well go the whole hog and charge you an exorbitant price for my fix.

    In other words, you need to do your own homework. We can help you along, but the essential work must be yours, and that includes the work of debugging. Being able to explain how does your program not work is part of the process of fixing it.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    i have settled my problem,how can i delete my thread?

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can't, and you shouldn't as others will benefit from the solution to your problem. Years in the future, this page may be indexed by google and poor people who have the same problem google for a solution might end up here.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Aug 2015
    Posts
    5
    but i dont want other to see my problem

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sorry, but that's the rule around here. In fact, this is a pretty common rule on public message boards. What you post is publically available for all to see. If you don't like that, then you shouldn't post.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Mike9407 View Post
    but i dont want other to see my problem
    So what you're saying is that simply succeeding is not enough. You want to see others struggle, and ultimately fail, because you are more important than them. If you intend to make programming your career, you will inevitably work with others in a group. That attitude will make you useless as a programmer, and ensure that you remain unemployed.
    What can this strange device be?
    When I touch it, it gives forth a sound
    It's got wires that vibrate and give music
    What can this thing be that I found?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compile error? Logic error? Syntax Error? Please help
    By Khody Afkhami in forum C Programming
    Replies: 4
    Last Post: 10-11-2014, 01:36 AM
  2. logic error
    By gameover6005 in forum C Programming
    Replies: 2
    Last Post: 03-30-2013, 12:47 PM
  3. Possible Logic Error
    By seanksg in forum C Programming
    Replies: 8
    Last Post: 03-14-2011, 06:42 PM
  4. logic Error
    By asmaa in forum C++ Programming
    Replies: 4
    Last Post: 02-13-2009, 10:58 PM
  5. Error in logic
    By mesmer in forum C Programming
    Replies: 2
    Last Post: 10-21-2008, 06:42 AM

Tags for this Thread