Dec 2, 2014 at 12:06am UTC
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;
}
}