跳棋游戏... check_move功能?

时间:2015-03-11 19:58:18

标签: c++

尝试运行此程序,但要了解check_move函数。我相信这是为了确保那里没有一块板子......让移动变得非法。 它被声明为一个函数,但是我找不到定义/实现。程序尝试使用调用该函数     在void input()中“while(check_move()== false)”。有点失落......有人可以帮忙吗?

//This is a 2 player checker game
//
#include <iostream>

using namespace std;

char board[8][8] =
{
    {' ', 'b', ' ', 'b', ' ', 'b', ' ', 'b'},
    {'b', ' ', 'b', ' ', 'b', ' ', 'b', ' '},
    {' ', 'b', ' ', 'b', ' ', 'b', ' ', 'b'},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '},
    {'r', ' ', 'r', ' ', 'r', ' ', 'r', ' '},
    {' ', 'r', ' ', 'r', ' ', 'r', ' ', 'r'},
    {'r', ' ', 'r', ' ', 'r', ' ', 'r', ' '},
};

char turn = 'B';
bool leap;
bool game_running = true;
int row1, row2, column1, column2;

void display_board();// 
void input();//
bool check_move();//where is the 
void move();
void do_leap();
void king();
void game_over();

int main()
{
    cout << "***CHECKER GAME***\n";
    cout << "Player 1 [b]\n";
    cout << "Player 2 [r]\n\n";
    cout << "Multiple leaps are supported.\n";
    cout << "A capital letter represents a king piece.\n";
    cout << "NOTE: Rows and columns are counted starting from 0, not 1.\n";
    cout << "<------COLUMNS------>\n";
    cout << "^\n";
    cout << "|\n";
    cout << "|\n";
    cout << "|\n";
    cout << "ROWS\n";
    cout << "|\n";
    cout << "|\n";
    cout << "|\n";
    cout << "v\n\n";
    cout << "NOTE: Resize your console screen so that this line fits on the screen:\n";
    cout << "_____________________________________________________________________________________\n\n";
    cout << "Press enter to begin...";
    cin.get();//This waits for the user to press enter before continuing

    while (game_running)//game_running was initialzed to true so will run
    {
        display_board();//function is called to display board

        if (turn == 'B')//was initialzed to 'B'line 19
        {
            cout << "--Player 1 [B]--\n";//says player 1 turn
        }
        else if (turn == 'R')
        {
            cout << "--Player 2 [R]--\n";
        }
        //funcitons called
        input();
        move();
        king();
        game_over();
    }

    if (turn == 'B')
    {
        cout << endl << endl << "Player 2 [Red] wins!!!\n";
    }
    else if (turn == 'R')
    {
        cout << endl << endl << "Player 1 [Black] wins!!!\n";
    }

    cout << "GAME OVER!\n";
}

void display_board()//board displayed is similar to tic tac toe except its bigger, has empty spaces
{
    cout << "==================================================================================\n\n\n\n";
    cout << "       0         1         2         3         4         5         6         7     \n";
    cout << "  _________________________________________________________________________________\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "0 |    " << board[0][0] << "    |    " << board[0][1] << "    |    " << board[0][2] << "    |    "  << board[0][3] << "    |    " << board[0][4] << "    |    " << board[0][5] << "    |    " << board[0][6] << "    |    " << board[0][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "1 |    " << board[1][0] << "    |    " << board[1][1] << "    |    " << board[1][2] << "    |    "  << board[1][3] << "    |    " << board[1][4] << "    |    " << board[1][5] << "    |    " << board[1][6] << "    |    " << board[1][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "2 |    " << board[2][0] << "    |    " << board[2][1] << "    |    " << board[2][2] << "    |    "  << board[2][3] << "    |    " << board[2][4] << "    |    " << board[2][5] << "    |    " << board[2][6] << "    |    " << board[2][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "3 |    " << board[3][0] << "    |    " << board[3][1] << "    |    " << board[3][2] << "    |    "  << board[3][3] << "    |    " << board[3][4] << "    |    " << board[3][5] << "    |    " << board[3][6] << "    |    " << board[3][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "4 |    " << board[4][0] << "    |    " << board[4][1] << "    |    " << board[4][2] << "    |    "  << board[4][3] << "    |    " << board[4][4] << "    |    " << board[4][5] << "    |    " << board[4][6] << "    |    " << board[4][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "5 |    " << board[5][0] << "    |    " << board[5][1] << "    |    " << board[5][2] << "    |    "  << board[5][3] << "    |    " << board[5][4] << "    |    " << board[5][5] << "    |    " << board[5][6] << "    |    " << board[5][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "6 |    " << board[6][0] << "    |    " << board[6][1] << "    |    " << board[6][2] << "    |    "  << board[6][3] << "    |    " << board[6][4] << "    |    " << board[6][5] << "    |    " << board[6][6] << "    |    " << board[6][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "7 |    " << board[7][0] << "    |    " << board[7][1] << "    |    " << board[7][2] << "    |    "  << board[7][3] << "    |    " << board[7][4] << "    |    " << board[7][5] << "    |    " << board[7][6] << "    |    " << board[7][7] << "    |\n";
    cout << "  |         |         |         |         |         |         |         |         |\n";
    cout << "  |_________|_________|_________|_________|_________|_________|_________|_________|\n";
}

void input()//asks player first to CHOOSE their piece using row and column # for exact location, then to MOVE using same concept
{
    cout << "Move piece\n";
    cout << "Row: ";
    cin >> row1;//
    cout << "Column: ";
    cin >> column1;

    while (row1 < 0 || row1 > 7 || column1 < 0 || column1 > 7)//CHOOSE PIECE ...makes sure the input(Chosen piece) is within range
    {
        cout << "Incorrect input. Enter numbers between 0 and 7.\n";//runs if input out of range
        cout << "Move piece\n";
        cout << "Row: ";
        cin >> row1;//allows another chance to enter input 
        cout << "Column: ";
        cin >> column1;
    }

    cout << "To box\n";//allows to MOVE to another location based on row and column
    cout << "Row: ";
    cin >> row2;
    cout << "Column: ";
    cin >> column2;

    while (row2 < 0 || row2 > 7 || column2 < 0 || column2 > 7)//MOVE PIECE...makes sure the move is legal
    {
        cout << "Incorrect input. Enter numbers between 0 and 7.\n";
        cout << "To box\n";
        cout << "Row: ";
        cin >> row2;
        cout << "Column: ";
        cin >> column2;
    }

    while (check_move() == false)//check_move is called....and checks if it is false
    {
        cout << "ILLEGAL MOVE!!\n";
        input();//input function starts again at line 133
    }
}



void move()
{
    bool king_piece = false;

    if (board[row1][column1] == 'B' || board[row1][column1] == 'R')
    {
        king_piece = true;
    }

    board[row1][column1] = ' ';

    if (turn == 'B')
    {
        if (king_piece == false)
        {
            board[row2][column2] = 'b';
        }
        else if (king_piece == true)
        {
            board[row2][column2] = 'B';
        }

        turn = 'R';
    }
    else if (turn == 'R')
    {
        if (king_piece == false)
        {
            board[row2][column2] = 'r';
        }
        else if (king_piece == true)
        {
            board[row2][column2] = 'R';
        }

        turn = 'B';
    }

    if (leap == true)
    {
        do_leap();
    }
}

void do_leap()
{
    char answer;

    //It removes the checker piece after leap.
    if (row2 > row1 && column2 > column1)
    {
        board[row2 - 1][column2 - 1] = ' ';
    }
    else if (row2 > row1 && column2 < column1)
    {
        board[row2 - 1][column2 + 1] = ' ';
    }
    else if (row2 < row1 && column2 > column1)
    {
        board[row2 + 1][column2 - 1] = ' ';
    }
    else if (row2 < row1 && column2 < column1)
    {
        board[row2 + 1][column2 + 1] = ' ';
    }

    display_board();//It displays the board after the changes

    //It asks if the user wants to leap again.
    do
    {
        cout << "You just leaped once. Do you want to do a second leap IF YOU CAN? (y/n): ";
        cin >> answer;
    }
    while (answer != 'Y' && answer != 'y' && answer != 'N' && answer != 'n');

    if (answer == 'y' || answer == 'Y')
    {
        row1 = row2;
        column1 = column2;
        cout << "Leap piece: row: " << row1 << ", column: " << column1 << endl;
        cout << "To box\n";
        cout << "row: ";
        cin >> row2;
        cout << "column: ";
        cin >> column2;

        while (row2 < 0 || row2 > 7 || column2 < 0 || column2 > 7)
        {
            cout << "Incorrect input. Enter numbers between 0 and 7.\n";
            cout << "To box\n";
            cout << "Row: ";
            cin >> row2;
            cout << "Column: ";
            cin >> bool check_move()

            {
                //it checks if a non-king piece is moving backwards.
                if (board[row1][column1] != 'B' && board[row1][column1] != 'R')
                {
                    if ((turn == 'B' && row2 < row1) || (turn == 'R' && row2 > row1))
                    {
                        leap = false;
                        return false;
                    }
                }

                //It checks if the location the piece is moving to is already taken.
                if (board[row2][column2] != ' ')
                {
                    leap = false;
                    return false;
                }

                //It checks if location entered by the user contains a piece to be moved.
                if (board[row1][column1] == ' ')
                {
                    leap = false;
                    return false;
                }

                //It checks if the piece isn't moving diagonally.
                if (column1 == column2 || row1 == row2)
                {
                    leap = false;
                    return false;
                }

                //It checks if the piece is moving by more than 1 column and only 1 row
                if ((column2 > column1 + 1 || column2 < column1 - 1) && (row2 == row1 +1 || row2 == row1 - 1))
                {
                    leap = false;
                    return false;
                }

                //It checks if the piece is leaping.
                if (row2 > row1 + 1 || row2 < row1 - 1)
                {
                    //It checks if the piece is leaping too far.
                    if (row2 > row1 + 2 || row2 < row1 - 2)
                    {
                        leap = false;
                        return false;
                    }

                    //It checks if the piece isn't moving by exactly 2 columns
                    if (column2 != column1 + 2 && column2 != column1 - 2)
                    {
                        leap = false;
                        return false;
                    }

                    //It checks if the piece is leaping over another piece.
                    if (row2 > row1 && column2 > column1)
                    {
                        if (board[row2 - 1][column2 - 1] == ' ')
                        {
                            leap = false;
                            return false;
                        }
                    }
                    else if (row2 > row1 && column2 < column1)
                    {
                        if (board[row2 - 1][column2 + 1] == ' ')
                        {
                            leap = false;
                            return false;
                        }
                    }
                    else if (row2 < row1 && column2 > column1)
                    {
                        if (board[row2 + 1][column2 - 1] == ' ')
                        {
                            leap = false;
                            return false;
                        }
                    }
                    else if (row2 < row1 && column2 < column1)
                    {
                        if (board[row2 + 1][column2 + 1] == ' ')
                        {
                            leap = false;
                            return false;
                        }
                    }

                    leap = true;
                    return true;
                }

                leap = false;
                return true;
            } column2;
        }
    }

        if (turn == 'B')
        {
            turn = 'R';
        }
        else if (turn == 'R')
        {
            turn = 'B';
        }

        check_move();

        if (leap == false)
        {
            cout << "INVALID LEAP!!\n";

            if (turn == 'B')
            {
                turn = 'R';
            }
            else if (turn == 'R')
            {
                turn = 'B';
            }
        }
        else if (leap == true)
        {
            move();
        }
    }
}

void king()
{
    for (int i = 0; i < 8; i++)
    {
        if (board[0][i] == 'r')
        {
            board[0][i] = 'R';
        }

        if (board[7][i] == 'b')
        {
            board[7][i] = 'B';
        }
    }
}

void game_over()
{
    int counter = 0;

    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if (board[i][j] != ' ')
            {
                counter++;
            }
        }
    }

    if (counter > 1)
    {
        game_running = true;
    }
    else if (counter == 1)
    {
        game_running = false;
    }
}
}   



/*

 ISSUE:

 -check_move is declared as s function but has no def/implementation
 -Could it be intended to be declared as a variable??

4 个答案:

答案 0 :(得分:0)

bool check_move();

forward declaration该函数必须在其他文件中定义。 您应该知道代码的来源并要求完整的来源。

答案 1 :(得分:0)

do_leap函数的中间,我们看到:

 cout << "Column: ";
 cin >> bool check_move()
 {
        //it checks if a non-king piece is moving backwards.
        if (board[row1][column1] != 'B' && board[row1][column1] != 'R')
        {

这句话的第二行是语法错误;看起来有人将check_move函数复制粘贴到do_leap函数的中间(可能是带有鼠标复制粘贴功能的编辑器中的错误点击)。

错误放置的check_move函数在cin >>之后适合,没有什么明显的,所以看起来这个粘贴操作实际上“切掉”了一些原始代码。您需要从其他来源找到原始代码(或者如果您感到勇敢,精神错乱check_move并尝试重写do_leap的缺失部分。)

答案 2 :(得分:0)

此源代码显然已损坏。

check_move函数在另一个函数的中间声明...

        cout << "Column: ";
        cin >> bool check_move()
        {
           ...

这不是有效的C ++。

答案 3 :(得分:0)

不知道你的来源在哪里,但是可以在这里找到完整的程序源,并找到缺失的方法:

http://runnable.com/U-_XgNndbURbbgx-/c%2B%2B-checkers-game

bool check_move()
{
    //it checks if a non-king piece is moving backwards.
    if (board[row1][column1] != 'B' && board[row1][column1] != 'R')
    {
        if ((turn == 'B' && row2 < row1) || (turn == 'R' && row2 > row1))
        {
            leap = false;
            return false;
        }
    }

    //It checks if the location the piece is moving to is already taken.
    if (board[row2][column2] != ' ')
    {
        leap = false;
        return false;
    }

    //It checks if location entered by the user contains a piece to be moved.
    if (board[row1][column1] == ' ')
    {
        leap = false;
        return false;
    }

    //It checks if the piece isn't moving diagonally.
    if (column1 == column2 || row1 == row2)
    {
        leap = false;
        return false;
    }

    //It checks if the piece is moving by more than 1 column and only 1 row
    if ((column2 > column1 + 1 || column2 < column1 - 1) && (row2 == row1 +1 || row2 == row1 - 1))
    {
        leap = false;
        return false;
    }

    //It checks if the piece is leaping.
    if (row2 > row1 + 1 || row2 < row1 - 1)
    {
        //It checks if the piece is leaping too far.
        if (row2 > row1 + 2 || row2 < row1 - 2)
        {
            leap = false;
            return false;
        }

        //It checks if the piece isn't moving by exactly 2 columns
        if (column2 != column1 + 2 && column2 != column1 - 2)
        {
            leap = false;
            return false;
        }

        //It checks if the piece is leaping over another piece.
        if (row2 > row1 && column2 > column1)
        {
            if (board[row2 - 1][column2 - 1] == ' ')
            {
                leap = false;
                return false;
            }
        }
        else if (row2 > row1 && column2 < column1)
        {
            if (board[row2 - 1][column2 + 1] == ' ')
            {
                leap = false;
                return false;
            }
        }
        else if (row2 < row1 && column2 > column1)
        {
            if (board[row2 + 1][column2 - 1] == ' ')
            {
                leap = false;
                return false;
            }
        }
        else if (row2 < row1 && column2 < column1)
        {
            if (board[row2 + 1][column2 + 1] == ' ')
            {
                leap = false;
                return false;
            }
        }

        leap = true;
        return true;
    }

    leap = false;
    return true;
}