Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am including my code that I have already written, I just cannot figure out what to write in the boolean function winner ( )

I am including my code that I have already written, I just cannot figure out what to write in the boolean function winner() in order for my game to announce that it is over and show number of moves.
Game::Game(){
numMoves =0;
int num =1;
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
gameBoard[i][j]= num++;
}
}
}
void Game::displayGame(){
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
cout gameBoard[i][j]"";
}
cout endl;
}
}
bool Game::winner(){
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
if (gameBoard[i][j]!=15){
return false;
}
}
}
}
void Game::swapPositions(int num1, int num2){
int row1, row2;
int col1, col2;
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
if (gameBoard[i][j]== num1){
row1= i;
col1= j;
}
if (gameBoard[i][j]== num2){
row2= i;
col2= j;
}
}
}
int storeNum = gameBoard[row1][col1];
gameBoard[row1][col1]= gameBoard[row2][col2];
gameBoard[row2][col2]= storeNum;
displayGame();
cout endl;
numMoves++;
}
int Game::getMoves(){
return numMoves;
}
void Game::addRows(){
int sum =0;
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
sum += gameBoard[i][j];
}
cout "Row " i+1": " sum endl;
sum =0;
}
}
void Game::addColumns(){
int sum =0;
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
sum += gameBoard[j][i];
}
cout "Column " i+1": " sum endl;
sum =0;
}
}
void Game::addDiagonals(){
int sum1=0;
int sum2=0;
for (int i =0; i 3; ++i){
for (int j =0; j 3; ++j){
if (i == j){
sum1+= gameBoard[i][j];
}
if ((i + j)==(3-1)){
sum2+= gameBoard[i][j];
}
}
}
cout "Diagonal Left: " sum1 endl;
cout
"Diagonal Right: " sum2 endl;
}
int main()
{
Game all15;
cout "Let's Play All15!" endl;
cout "To win, swap numbers until each line equals 15!" endl;
cout endl;
all15.displayGame();
cout endl;
while (! all15.winner()){
int num;
int swapNum;
cout "Enter a digit to move: "; cin >> num;
cout "Enter a digit to swap with: "; cin >> swapNum;
all15.swapPositions(num, swapNum);
cout "Board not complete!" endl;
all15.addRows();
all15.addColumns();
all15.addDiagonals();
}
cout "Board complete!" endl;
cout "Number of moves: " all15.getMoves();
return 0;
}
I need assistance with making the boolean function show that each line's sum is now equal to 15 in order to end the program. Output should look like picture.
image text in transcribed

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

Logidata+ Deductive Databases With Complex Objects Lncs 701

Authors: Paolo Atzeni

1st Edition

354056974X, 978-3540569749

Students also viewed these Databases questions