Question
Code needs to be in c++ here is Thea a starter code don't access the array board directly, use get_element, set_element- #include #include #include #include
Code needs to be in c++ here is Thea a starter code don't access the array board directly, use get_element, set_element-
#include
/* Everything below this comment is starter code, and you are not allowed to modify */
using namespace std;
// Function that displays the instructions to the user
void display_instructions() { cout << "Class: CS 141" << endl << "Program 2: Great 13" << endl << endl << "Make a series of jumps until there is a single piece left" << endl << "anywhere on board. On each move you must jump an adjacent" << endl << "piece into an empty square, jumping horizontally," << endl << "vertically, or diagonally." << endl << endl << "Input of 'R' resets the board back to the beginning, and " << endl << "'X' exits the game." << endl; }
// The internal representation of the board as an array. Do not modify directly.
char board[13]{};
/* These two functions are how you will get and set elements on the board. Only use these two when changing, or reading the board. */
char get_element(char position) { if ((position < 'A') || (position > 'M')) { cout << "Failed to get element at position " << position << endl << "Make sure the given position is an uppercase letter " << endl << "between A-M." << endl; exit(1); } return board[position - 'A']; }
void set_element(char position, char value) { if ((position < 'A') || (position > 'M')) { cout << "Failed to set element at postion " << position << endl << "Make sure the given position is an uppercase letter " << endl << "between A-M." << endl; exit(1); } board[position - 'A'] = value; }
// print out the board with the legend on the right
void display_board() { cout << endl; cout << setw(7) << "Board" << setw(12) << "Position" << endl; cout << setw(5) << board[0] << setw(11) << 'A' << endl << setw(3) << board[1] << ' ' << board[2] << ' ' << board[3] << setw(11) << "B C D" << endl << board[4] << ' ' << board[5] << ' ' << board[6] << ' ' << board[7] << ' ' << board[8] << ' ' << " E F G H I" << endl << setw(3) << board[9] << ' ' << board[10] << ' ' << board[11] << setw(11) << "J K L" << endl << setw(5) << board[12] << setw(11) << 'M' << endl; cout << endl; }
/* This is the end of the starter code, and below is where you can add your own code. Feel free to add helper functions or variables. */
/* forward declarations for the functions you will write later. This is so the order of the function definitions, in this file, does not matter. */
void make_move(); void initialize_board(); bool game_over();
void make_move() { /* TODO: Write a function that reads in a command from the user and updates the board, if the move is valid. ONLY use the functions get_element and set_element to update the board, do not access the array 'board' directly. */ }
bool game_over() { /* TODO: Write a function that returns true only if there is one one peg left, or if the user gives up by entering 'X' (or 'x')*/ return true; }
void initialize_board() { /* TODO: Initialize the board to have all pegs, besides the very center hole. ONLY use the functions get_element and set_element to update the board, do not access the array 'board' directly. */
}
int main() { display_instructions(); do { display_board(); make_move(); } while(!game_over() && !cin.eof()); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started