Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need my program to do 2 more things I need two more functions this is a checkers game in C++ using visual studions 2017

I need my program to do 2 more things I need two more functions

this is a checkers game in C++ using visual studions 2017

*functions one should play a sound when the prgram begins and also when it ends

*function two should be when a time funtion thats possibly writes to a data file for example

0:10 . players one move

0:35 plyer two jumped etc etc.

source code :

#include //basic input-output #include //header for current time when game is won

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

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

//functions void display_board(); void input(); bool check_move(); void move(); void do_leap(); void king(); void game_over();

int main() {

cout <<" The Checkers Game " << endl;

cout << "Instructions: "; cout << "Player 1 is [b] on the checker board "; cout << "Player 2 is [r] on the checker board "; cout << "Multiple leaps are supported. "; cout << "A capital letter represents a king piece. "; cout << "King Pieces can be utilised when a piece reaches the end of the other side of the board. "; cout << "NOTE: Rows and columns are counted starting from 0, not 1. " < "; cout << "^ "; cout << "| "; cout << "| "; cout << "| "; cout << "ROWS "; cout << "| "; cout << "| "; cout << "| "; cout << "v "; cout << "NOTE: Resize your comand line console so that this line fits on the screen: "; cout << "_____________________________________________________________________________________ "; cout <<"Remember to input position with rows first then columns. "; cout <<"Good luck and may the best player win! "; cout << "Press enter to begin..."; cin.get(); //This waits for the user to press enter before continuing

while (game_running) { display_board();

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

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

if (turn == 'B') { cout << endl << endl << "Player 2 [Red] wins!!! ";

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

}

cout << "GAME OVER! "; }

void display_board() { cout << "================================================================================== "; cout << " 0 1 2 3 4 5 6 7 "; cout << " _________________________________________________________________________________ "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; cout << " | | | | | | | | | "; cout << " | | | | | | | | | "; 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] << " | "; cout << " | | | | | | | | | "; cout << " |_________|_________|_________|_________|_________|_________|_________|_________| "; }

void input() { cout << "Move piece "; cout << "Row: "; cin >> row1; cout << "Column: "; cin >> column1;

while (row1 < 0 || row1 > 7 || column1 < 0 || column1 > 7) //handles errors in program while users enter data { cout << "Incorrect input. Enter numbers between 0 and 7. "; cout << "Move piece "; cout << "Row: "; cin >> row1; cout << "Column: "; cin >> column1; }

cout << "To box "; 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. "; cout << "To box "; cout << "Row: "; cin >> row2; cout << "Column: "; cin >> column2; }

while (check_move() == false) { cout << "Illegal Move. Please try again. Remember, rows first then columns (left hand side then the top) "; input(); } }

bool check_move() //constructs restrictions based on rules of checkers and handles errors associated with 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; } }

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

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

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

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

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

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

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

//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();//displays/ refreshes the board after the changes

//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 "; 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. "; cout << "To box "; 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!! ";

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

void king() //capitalises the checker letter so it becomes a 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; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago

Question

1. Describe the types of power that effective leaders employ

Answered: 1 week ago