Thread: Checkers game

  1. #1
    Registered User
    Join Date
    Apr 2013
    Posts
    34

    Checkers game

    Hi all, i'm writing checkers game and problem is that i can't get a right code to do a valid move. I mean i can move piece to any place. It should work only with two upper left and right sides. Any ideas how to do it?

    Code:
    #include <iostream> 
    #include <cstdlib>
    using namespace std;
    
    
    void drawBoard();
    
    
    char Board[8][8];
    char cPlayerMark = 'X';
    int iPlayerMove;
    int x, y;
    
    
    int main()
    {
        drawBoard();
        system("cls");
        while(true)
        {
            drawBoard();
    
    
            cout << "Enter right side:" << endl;
            cin >> y;
            cout << "Enter up side:" << endl;
            cin >> x;
    
    
            if(Board[y][x] == cPlayerMark)
            {
                cout << "Where you want to put it? Enter right side:" << endl;
                cin >> y;
                cout << "Enter up side: " << endl;
                cin >> x;
                if(Board[y][x] != cPlayerMark)
                {
                    Board[y][x] = cPlayerMark;
                }
                else
                {
                    cout << "Wrong Move" << endl;
                }
    
    
            }
            else
            {
                cout << "There is nothing in here" << endl;
            }
    
    
    
    
        }
    
    
    }
    
    
    void drawBoard()
    {
        cout << "0 1 2 3 4 5 6 7 " << endl;
        for(int x=0; x<8; x++)
        {
            cout << endl;
            for(int y=0; y<8; y++)
                {
                    cout << Board[x][y] << "|";
    
    
                }
                cout << x << endl;
    
    
        }
    
    
        cout << " " << endl;
        Board[5][0] = cPlayerMark; // Later on i will improve this code
        Board[7][0] = cPlayerMark;
        Board[6][1] = cPlayerMark;
        Board[5][2] = cPlayerMark;
        Board[7][2] = cPlayerMark;
        Board[6][3] = cPlayerMark;
        Board[5][4] = cPlayerMark;
        Board[7][4] = cPlayerMark;
        Board[6][5] = cPlayerMark;
        Board[5][6] = cPlayerMark;
        Board[7][6] = cPlayerMark;
        Board[6][7] = cPlayerMark;
        //Board[0][0] = '@';
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > // Later on i will improve this code
    Perhaps you should move this code into a function called initBoard(), which is only called ONCE.
    Otherwise, you're just resetting the board every time you draw it.

    > Hi all, i'm writing checkers game and problem is that i can't get a right code to do a valid move
    Well I would start with something like
    Code:
    bool isValidMove(char Board[8][8], int x, int y, char player ) {
        bool result = true;
        // improve this
        return result;
    }
    One thing you should do however, is start using parameters rather than relying on global variables.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    One thing you should do however, is start using parameters rather than relying on global variables.
    Ok, but i think i need to study a little more with this, i find it a bit difficult to understand how it works. (Im kinda stupid lol). But anyway, how i could use this bool isValidMove? How im supposed to make it valid only for upper left and right?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You have to have an idea why a move is valid before you do validation, because it either is ok or not.

  5. #5
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Big thanks to Salem, i have improved placing pieces and piece movement. But still don't know how to check if piece is out of board.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    int drawBoard();
    int placePiece();
    
    
    char Board[8][8];
    char cPlayerMark = 'X';
    int iPlayerMove;
    int x, y, lr;
    
    
    int main()
    {
        drawBoard();
        placePiece();
        system("cls");
        while(true)
        {
            drawBoard();
            cout << "Enter right side:" << endl;
            cin >> y;
            cout << "Enter up side:" << endl;
            cin >> x;
    
    
            if(Board[y][x] == cPlayerMark)
            {
                cout << "Where you want to put it? Left or Right? Left -1 Right-2" << endl;
                cin >> lr;
                if(lr == 1) // don't know how to make condition to check if piece is out of board
                {
                    if(Board[y-1][x-1] != cPlayerMark)
                    {
                        Board[y-1][x-1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                    else
                    {
                        cout << "Wrong move. There is a piece or out of bounds" << endl;
                    }
                }
                else if(lr == 2)
                {
                    if(Board[y-1][x+1] != cPlayerMark)
                    {
                        Board[y-1][x+1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                    else
                    {
                        cout << "Wrong move. There is a piece or out of bounds" << endl;
                    }
                }
                else
                {
                    cout << "Wrong Move" << endl;
                }
    
    
            }
            else
            {
                cout << "There is nothing in here" << endl;
            }
    
    
    
    
        }
    
    
    }
    
    
    int drawBoard()
    {
        cout << "0 1 2 3 4 5 6 7 " << endl;
        for(int x=0; x<8; x++)
        {
            cout << endl;
            for(int y=0; y<8; y++)
                {
                    cout << Board[x][y] << "|";
    
    
                }
                cout << x << endl;
    
    
        }
    
    
        cout << " " << endl;
    }
    
    
    int placePiece()
    {
        Board[5][0] = cPlayerMark;
        Board[7][0] = cPlayerMark;
        Board[6][1] = cPlayerMark;
        Board[5][2] = cPlayerMark;
        Board[7][2] = cPlayerMark;
        Board[6][3] = cPlayerMark;
        Board[5][4] = cPlayerMark;
        Board[7][4] = cPlayerMark;
        Board[6][5] = cPlayerMark;
        Board[5][6] = cPlayerMark;
        Board[7][6] = cPlayerMark;
        Board[6][7] = cPlayerMark;
    }

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    still don't know how to check if piece is out of board
    There are several ways you can deal with that, one is to increase the size of the board to include a border - which is filled with flag values like 1000 - then you can just check if the part of the board you are looking at does not contain the flag. Additionaly you can print out a nice character to show the border as a graphics touch.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Ok, so i did all you said guys, thanks for help. Now im facing problem how to insert function forceStrike (at the end) into main function. That function should check for piece if it is nearby and force to strike it.

    Code:
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    int drawBoard();
    int placePiece();
    
    
    char Board[9][9];
    char cPlayerMark, cPlayerMark2;
    int iPlayerMove = 2;
    int x, y, lr;
    int pO=12, pX=12;
    bool endGame = false;
    
    
    int main()
    {
        drawBoard();
        placePiece();
        system("cls");
        while(endGame == false)
        {
            cout << "Player O pieces left: " << pO << endl;
            cout << "Player X pieces left: " << pX << endl;
            drawBoard();
    
    
            if(iPlayerMove == 1)
            {
                iPlayerMove = 2;
            }
            else
            {
                iPlayerMove = 1;
            }
    
    
            cout << "Player " << iPlayerMove <<  " enter right side:" << endl;
            cin >> y;
            cout << "Player " << iPlayerMove << " enter up side:" << endl;
            cin >> x;
    
    
            if(iPlayerMove == 1)
            {
                cPlayerMark = 'X';
                cPlayerMark2 = 'O';
            }
            else
            {
                cPlayerMark = 'O';
                cPlayerMark2 = 'X';
            }
            if(cPlayerMark == 'X' && Board[y][x] == cPlayerMark)
            {
                cout << "Where you want to put it? Left or Right? Left -1 Right-2" << endl;
                cin >> lr;
                if(lr == 1 && Board[y-1][x-1] != '@' && Board[y-1][x-1] != cPlayerMark)
                {
                    if(Board[y-1][x-1] == cPlayerMark2)
                    {
                        Board[y-1][x-1] = ' ';
                        Board[y-2][x-2] = cPlayerMark;
                        Board[y][x] = ' ';
                        pO--;
                    }
                    else
                    {
                        Board[y-1][x-1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                }
                else if(lr == 2 && Board[y-1][x+1] != '@' && Board[y-1][x+1] != cPlayerMark)
                {
                    if(Board[y-1][x+1] == cPlayerMark2)
                    {
                        Board[y-1][x+1] = ' ';
                        Board[y-2][x+2] = cPlayerMark;
                        Board[y][x] = ' ';
                        pO--;
                    }
                    else
                    {
                        Board[y-1][x+1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                }
                else
                {
                    cout << "Wrong move " << endl;
                    iPlayerMove = 1;
                }
            }
            else if(cPlayerMark == 'O' && Board[y][x] == cPlayerMark)
            {
                cout << "Where you want to put it? Left or Right? Left -1 Right-2" << endl;
                cin >> lr;
                if(lr == 1 && Board[y+1][x-1] != cPlayerMark && Board[y+1][x-1] != '@')
                {
                    if(Board[y+1][x-1] == cPlayerMark2)
                    {
                        Board[y+1][x-1] = ' ';
                        Board[y+2][x-2] = cPlayerMark;
                        Board[y][x] = ' ';
                        pX--;
                    }
                    else
                    {
                        Board[y+1][x-1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                }
                else if(lr == 2 && Board[y+1][x+1] != cPlayerMark && Board[y+1][x+1] != '@')
                {
                    if(Board[y+1][x+1] == cPlayerMark2)
                    {
                        Board[y+1][x+1] = ' ';
                        Board[y+2][x+2] = cPlayerMark;
                        Board[y][x] = ' ';
                        pX--;
                    }
                    else
                    {
                        Board[y+1][x+1] = cPlayerMark;
                        Board[y][x] = ' ';
                    }
                }
                else
                {
                    cout << "Wrong Move" << endl;
                    iPlayerMove = 2;
                }
            }
            else
            {
                cout << "There is nothing in here" << endl;
                if(iPlayerMove == 1)
                    {
                        iPlayerMove = 2;
                    }
                else
                    {
                        iPlayerMove = 1;
                    }
            }
    
    
            if(pO == 0 || pX == 0)
            {
                endGame = true;
            }
            system("cls");
        }
    
    
        cout << "Game over, player " << iPlayerMove << " wins!" << endl;
    
    
    }
    
    
    int drawBoard()
    {
        cout << "0 1 2 3 4 5 6 7 " << endl;
        for(int x=0; x<8; x++)
        {
            cout << endl;
            for(int y=0; y<8; y++)
                {
                    cout << Board[x][y] << "|";
                }
                cout << x << endl;
    
    
        }
        cout << " " << endl;
    }
    
    
    int placePiece()
    {
        Board[5][0] = 'X';
        Board[7][0] = 'X';
        Board[6][1] = 'X';
        Board[5][2] = 'X';
        Board[7][2] = 'X';
        Board[6][3] = 'X';
        Board[5][4] = 'X';
        Board[7][4] = 'X';
        Board[6][5] = 'X';
        Board[5][6] = 'X';
        Board[7][6] = 'X';
        Board[6][7] = 'X';
    
    
        Board[1][0] = 'O';
        Board[0][1] = 'O';
        Board[2][1] = 'O';
        Board[1][2] = 'O';
        Board[0][3] = 'O';
        Board[2][3] = 'O';
        Board[1][4] = 'O';
        Board[0][5] = 'O';
        Board[2][5] = 'O';
        Board[1][6] = 'O';
        Board[0][7] = 'O';
        Board[2][7] = 'O';
    
    
        for(int i=0; i<9; i++) // Place '@' to check if move is out of board
        {
            Board[i][8] = '@';  // vertical
        }
        for(int i=0; i<9; i++)
        {
            Board[-1][i] = '@'; // horizontal
        }
    }
    
    
    bool forceStrike(int y, int x)
    {
    
    
    }
    Last edited by Aeoskype; 05-08-2013 at 04:00 AM.

  8. #8
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    I would rename your placePiece function to InitBoard() - And it is broken anyway - you cannot access array elements with index -1
    Even without this you would also only have the flag values @ placed at one column and one row - you need them all around the board. Apart from this you
    do not init the empty squares of the board either.

    You need to be using constant values like BOARD_W instead of magic numbers.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  9. #9
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Quote Originally Posted by rogster001 View Post
    I would rename your placePiece function to InitBoard() - And it is broken anyway - you cannot access array elements with index -1
    Even without this you would also only have the flag values @ placed at one column and one row - you need them all around the board. Apart from this you
    do not init the empty squares of the board either.

    You need to be using constant values like BOARD_W instead of magic numbers.
    What's the difference between placePiece and InitBoard? It's just a function name

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Aeoskype
    What's the difference between placePiece and InitBoard? It's just a function name
    "(...) a computer language is not just a way of getting a computer to perform operations but rather that it is a novel formal medium for expressing ideas about methodology. Thus, programs must be written for people to read, and only incidentally for machines to execute" - Abelson & Sussman, preface to the first edition of Structure and Interpretation of Computer Programs

    Why not name the function f? It is a shorter name. Why not name it ashjyixbasdgjcbasdghcvi? Then you don't need to think of a name.

    The function's name expresses something about its purpose. So, it is not "just" a function name. It conveys something to the reader. A misleading name misleads the reader, and that reader could well be yourself.
    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

  11. #11
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    What's the difference between placePiece and InitBoard? It's just a function name
    Laserlight has explained this well already , but to continue with an example, just read your placePiece() function body - Myself, and every other reader on seeing a declaration of placePiece, in the context of your game would easily assume that this function is for writing a move to the board.
    However you actually use it to try and initialise the starting board layout, and that is it. So do you still think the name fits?

    Your actual 'move drawing' is stuffed into the general code of main() - you would be advised to make a correct InitBoard() function and additionally change placePiece() to do what it says on the tin, or maybe call the function UpdateBoard() instead, then you have a function also called CheckMove() where you can test if the move was a valid one (of course incorporating the out of bounds checking here) and return a bool if it was successful or not.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  12. #12
    Registered User
    Join Date
    Apr 2013
    Posts
    34
    Ok, that code wasn't very good so i started from scratch. I think i have improved something, but still function forceStrike doen't work. After any move it returns true. Or it is true before return?

    Code:
    #include <iostream>
    #include <cstdlib>
    
    
    using namespace std;
    
    
    int drawBoard();
    int initBoard();
    int getMove();
    bool forceStrike(int x, int y, char cPlayerMark, char cPlayerMark2);
    
    
    char Board[9][9];
    bool endGame = false;
    int iPlayerMove, x, y, lr;
    char cPlayerMark, cPlayerMark2, emptyPlace = ' ';
    
    
    int main()
    {
        initBoard();
        drawBoard();
    
    
        while(endGame == false)
        {
            getMove();
            drawBoard();
            //system("cls");
        }
    }
    
    
    int drawBoard()
    {
    
    
        cout << " 0  1  2  3  4  5  6  7 " << endl;
        for(int x=0; x<8; x++)
        {
            for(int y=0; y<8; y++)
    
    
                cout << char(218) << char(196) << char(191);
                cout << x;
                cout<<'\n';
    
    
            for(int y=0; y<8; y++)
    
    
                cout << char(179) << Board[x][y] <<char(179);
                cout<<'\n';
    
    
            for(int y=0; y<8; y++)
    
    
                cout << char(192) << char(196) <<char(217);
                cout<<'\n';
        }
    }
    
    
    int initBoard()
    {
        for(int x=0; x<8; x++)
        {
            for(int y=0; y<8; y++)
            {
                Board[x][y] = emptyPlace;
            }
        }
    
    
        Board[5][0] = 'X';
        Board[7][0] = 'X';
        Board[6][1] = 'X';
        Board[5][2] = 'X';
        Board[7][2] = 'X';
        Board[6][3] = 'X';
        Board[5][4] = 'X';
        Board[7][4] = 'X';
        Board[6][5] = 'X';
        Board[5][6] = 'X';
        Board[7][6] = 'X';
        Board[6][7] = 'X';
    
    
        Board[1][0] = 'O';
        Board[0][1] = 'O';
        Board[2][1] = 'O';
        Board[1][2] = 'O';
        Board[0][3] = 'O';
        Board[2][3] = 'O';
        Board[1][4] = 'O';
        Board[0][5] = 'O';
        Board[2][5] = 'O';
        Board[1][6] = 'O';
        Board[0][7] = 'O';
        Board[2][7] = 'O';
    }
    
    
    int getMove()
    {
            if(iPlayerMove == 1)
            {
                iPlayerMove = 2;
            }
            else
            {
                iPlayerMove = 1;
            }
    
    
    
    
            if(iPlayerMove == 1)
            {
                cPlayerMark = 'X';
                cPlayerMark2 = 'O';
            }
            else
            {
                cPlayerMark = 'O';
                cPlayerMark2 = 'X';
            }
    
    
        cout << "Player " << iPlayerMove << " turn:" << endl;
        cout << "Enter right side" << endl;
        cin >> x;
        cout << "Enter top side" << endl;
        cin >> y;
    
    
    
    
        if(forceStrike(x,y,cPlayerMark,cPlayerMark2) == true)
            {
                cout << "You must strike" << endl;
            }
    
    
        if(cPlayerMark == 'X' && Board[x][y] == cPlayerMark)
        {
            cout << "Where you want to move it? Left-1, Right-2" << endl;
            cin >> lr;
    
    
            if(lr == 1 && Board[x-1][y-1] == emptyPlace)
            {
                Board[x][y] = emptyPlace;
                Board[x-1][y-1] = cPlayerMark;
            }
            else if(lr == 2 && Board[x-1][y+1] == emptyPlace)
            {
                Board[x][y] = emptyPlace;
                Board[x-1][y+1] = cPlayerMark;
            }
            else
            {
                cout << "Wrong move" << endl;
                iPlayerMove = 2;
            }
        }
        else if(cPlayerMark == 'O' && Board[x][y] == cPlayerMark)
        {
            cout << "Where you want to move it? Left-1, Right-2" << endl;
            cin >> lr;
    
    
            if(lr == 1 && Board[x+1][y-1] == emptyPlace)
            {
                Board[x][y] = emptyPlace;
                Board[x+1][y-1] = cPlayerMark;
            }
            else if(lr == 2 && Board[x+1][y+1] == emptyPlace)
            {
                Board[x][y] = emptyPlace;
                Board[x+1][y+1] = cPlayerMark;
            }
            else
            {
                cout << "Wrong move" << endl;
                iPlayerMove = 1;
            }
        }
        else
        {
            cout << "There is no piece, try again" << endl;
            if(iPlayerMove == 1)
            {
                iPlayerMove = 2;
            }
            else
            {
                iPlayerMove = 1;
            }
        }
    }
    
    
    bool forceStrike(int x, int y, char cPlayerMark, char cPlayerMark2) // How do i know if it is true or false now?
    {
        if(cPlayerMark == 'X')
        {
            if(Board[x-1][y-1] == cPlayerMark2 || Board[x-1][y+1] == cPlayerMark2)
            {
                return true;
            }
        }
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-30-2011, 07:18 PM
  2. Trying to make a checkers game..
    By Luminous Lizard in forum C++ Programming
    Replies: 3
    Last Post: 11-14-2011, 12:04 PM
  3. C++ Checkers Game
    By SnS CEO in forum C++ Programming
    Replies: 9
    Last Post: 09-07-2005, 01:21 AM
  4. Jump moves in Checkers Game
    By Reb0270 in forum C++ Programming
    Replies: 2
    Last Post: 04-08-2004, 01:06 PM
  5. IDEA: Checkers game
    By confuted in forum Contests Board
    Replies: 0
    Last Post: 06-28-2003, 03:25 PM