Checkerboard Game

hi, im having trouble with my checkerboard game. i need it to work for 2 players and vs computer.
i need to use parameters.
[code]

#include <iostream>
#include <fstream>

using namespace std;
const int ROWS = 8;
const int COLS = 8;

char turn = 'B';
bool leap;
bool game_running = true;

int row1, row2, column1, column2;
void display_board();
void input();
bool check_move();
void move();
void do_leap();
void king();
void game_over();

void initialize(char board[][COLS]);
void display(char board[][COLS]);
void save(char board[][COLS]);
void readSaved(char board[][COLS]);

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 << "_____________________________________________________________________________________\n\n" << endl;
cout << "Press enter to begin...";
cin.get();

while (game_running)
{
display_board();

if (turn == 'B')
{
cout << "--Player 1 [B]--\n";
}
else if (turn == 'R')
{
cout << "--Player 2 [R]--\n";
}

input();
move();
king();
game_over();
}


char board[ROWS][COLS];
initialize(board);
display(board);
board[7][0] = 'r';
board[7][2] = 'r';
board[7][4] = 'r';
board[7][6] = 'r';
board[6][1] = 'r';
board[6][3] = 'r';
board[6][5] = 'r';
board[6][7] = 'r';
board[5][0] = 'r';
board[5][2] = 'r';
board[5][4] = 'r';
board[5][6] = 'r';
save(board);
readSaved(board);
display(board);
return 0;
}
void initialize(char board[][COLS])
{
int i, j;
for (i = 0; i < ROWS; i++) //row to row
for (j = 0; j < COLS; j++) // col elem
board[i][j] = '-';
board[0][1] = 'b';
board[0][3] = 'b';
board[0][5] = 'b';
board[0][7] = 'b';
board[1][0] = 'b';
board[1][2] = 'b';
board[1][4] = 'b';
board[1][6] = 'b';
board[2][1] = 'b';
board[2][3] = 'b';
board[2][5] = 'b';
board[2][7] = 'b';

return;
}
void display(char board[][COLS])
{
int i, j;
// column labels
cout << " ";
for (i = 0; i < COLS; i++)
cout << ' ' << i << ' ';

for (i = 0; i < ROWS; i++)
{
cout << endl;
//row label
cout << i << " ";
for (j = 0; j < COLS; j++)
//don't display the -
if (board[i][j] == '-')
cout << " ";
else
cout << ' ' << board[i][j] << ' ';
cout << endl;
}
return;
}
void save(char board[][COLS])
{
ofstream fout;
int i, j;
fout.open("gameBoard.txt");
for (i = 0; i < ROWS; i++)
{
for (j = 0; j < COLS; j++)
fout << board[i][j] << ' ';
fout << endl;
}
fout.close();
return;
}

void readSaved(char board[][COLS])
{
int i, j;
ifstream fin;
fin.open("gameBoard.txt");
for (i = 0; i < ROWS; i++)
for (j = 0; j < COLS; j++)
fin >> board[i][j];
fin.close();
return;
}
void input()
{
cout << "Move piece\n";
cout << "Row: ";
cin >> row1;
cout << "Column: ";
cin >> column1;

while (row1 < 0 || row1 > 7 || column1 < 0 || column1 > 7)
{
cout << "Incorrect input. Enter numbers between 0 and 7.\n";
cout << "Move piece\n";
cout << "Row: ";
cin >> row1;
cout << "Column: ";
cin >> column1;
}

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 >> column2;
}

while (check_move() == false)
{
cout << "ILLEGAL MOVE!!\n";
input();
}
}

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;
}

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 >> 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;
}
}
These functions don't know what board is since it isn't passed to the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
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;
}
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 >> 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;
    }
} 
Topic archived. No new replies allowed.