Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help implementing a condition in my program (C++) when a player is not ab

ID: 3790504 • Letter: I

Question

I need help implementing a condition in my program (C++) when a player is not able to move any of his/her pieces, that player loses that turn. But at later turns, moves may become available. I hope somone can show me how to implement this condition as I have been stuck the last two days.

Code:
#include
#include
using namespace std;

const char BLANK = '.';
const char RED = 'R';
const char BLUE = 'B';
const char GREEN = 'G';
const char YELLOW = 'Y';
const char ROWS = 11;
const char COLUMNS = 4;
const char PLAYER1 = 0;
const char PLAYER2 = 1;
const int PIECE = 1;

void initBoard(char board[][ROWS]); //initializes board
void displayBoard(char board[][ROWS], char playerToColor[][2]); //prints out board
int colorToPlayer(char color, char playerToColor[][2]); //checks if the piece belongs to player 1 or 2
int score(int player, char board[][ROWS], char playerToColor[][2]); //calculates score for both player 1 and player 2
bool whoseTurn(int player); //true if it is player 1's turn, false for player 2
void changePlayer(int &player); //changes player turn
void move(int x, int y, char board[][ROWS], char playerToColor[][2], int player, bool &moved); //the move function


int main()
{
   int x = 0,
       y = 0,
       player = 0;
   bool validTurn = true; //just a placeholder for now...
   bool moved = false;
   bool chosen = false;

   char choice = ' ';
   char board[COLUMNS][ROWS];
   char tempPlayer;
   char playerToColor[2][2]; // first index = player number, second index = slot for colors

   cout << "Player 1, which color do you choose to be? Red/Blue or Yellow/Green." << endl
       << "(Input as R for Red/Blue or Y for Yellow/Green)" << endl;
  

   while (chosen == false) {
       cin >> choice;
       if (choice == 'R' || choice == 'r') {
           playerToColor[PLAYER1][0] = RED; // P1 = RED, BLUE
           playerToColor[PLAYER1][1] = BLUE;
           playerToColor[PLAYER2][0] = YELLOW; // P2 = YELLOW, GREEN
           playerToColor[PLAYER2][1] = GREEN;
           chosen = true;
       }
       else if (choice == 'Y' || choice == 'y') {
           playerToColor[PLAYER2][0] = RED; // P2 = RED, BLUE
           playerToColor[PLAYER2][1] = BLUE;
           playerToColor[PLAYER1][0] = YELLOW; // P1 = YELLOW, GREEN
           playerToColor[PLAYER1][1] = GREEN;
           chosen = true;
       }
       else
           cout << "You did not enter a valid entry. Please choose again." << endl;
   }

   initBoard(board);

   do {
       displayBoard(board, playerToColor);

       cout << "Player 1 " << score(PLAYER1, board, playerToColor) << endl;
       cout << "Player 2 " << score(PLAYER2, board, playerToColor) << endl;

       if (whoseTurn(player) == true) {
           while (moved == false) {
               cout << "Player 1: What piece do you want to move ? (Input: x y)" << endl;
               cin >> x >> y;
               move(x, y, board, playerToColor, player, moved);
           }
           changePlayer(player);
           moved = false;
       }

       else {
           while (moved == false) {
               cout << "Player 2: What piece do you want to move ? (Intput: x y)" << endl;
               cin >> x >> y;
               move(x, y, board, playerToColor, player, moved);
           }
           changePlayer(player);
           moved = false;
       }
   } while (validTurn == true);
}

void initBoard(char board[][ROWS])
{
   for (int x = 0; x < COLUMNS; x++)
       for (int y = 0; y < ROWS; y++)
           board[x][y] = BLANK;
   board[0][0] = YELLOW; board[1][0] = GREEN; board[2][0] = RED; board[3][0] = BLUE;
   board[0][1] = BLUE; board[1][1] = YELLOW; board[2][1] = GREEN; board[3][1] = RED;
   board[0][2] = RED; board[1][2] = BLUE; board[2][2] = YELLOW; board[3][2] = GREEN;
   board[0][3] = GREEN; board[1][3] = RED; board[2][3] = BLUE; board[3][3] = YELLOW;
   return;
}

void displayBoard(char board[][ROWS], char playerToColor[][2])
{
   for (int y = ROWS - 1; y >= 0; y--)
   {
       cout << setw(2) << y << ":" << " ";
       for (int x = 0; x < COLUMNS; x++)
       {
           cout << " ";
           if (colorToPlayer(board[x][y], playerToColor) == PLAYER1)
               cout << static_cast(tolower(board[x][y]));
           else
               cout << board[x][y];
           cout << " ";
       }
       if (y == 10) cout << " 60";
       if (y == 9) cout << " 40";
       if (y == 8) cout << " 30";
       if (y == 7) cout << " 20";
       if (y == 6) cout << " 10";
       cout << endl;
   }
   cout << " 0 1 2 3" << endl;
}

int colorToPlayer(char color, char playerToColor[][2])
{
   if (playerToColor[PLAYER1][0] == color)
       return PLAYER1;
   if (playerToColor[PLAYER1][1] == color)
       return PLAYER1;
   // If it wasn't player 1 then it must belong to player 2
   return PLAYER2;
}

int score(int player, char board[][ROWS], char playerToColor[][2])
{
   int total = 0;
   int rowValue = 10;
   for (int y = 6; y <= 10; y++)
   {
       for (int x = 0; x < COLUMNS; x++)
       {
           if (board[x][y] != BLANK)
               if (colorToPlayer(board[x][y], playerToColor) == player)
                   total += rowValue;
       }
       rowValue += 10;
       if (y == 9)
           rowValue += 10;
   }
   return total;
}

bool whoseTurn(int player) {
   if (player == 0)
       return true;
   else
       return false;
}

void changePlayer(int &player) {
   if (player == 0)
       player = 1;
   else
       player = 0;
}

void move(int x, int y, char board[][ROWS], char playerToColor[][2], int player, bool &moved) {
   char tempPlayer = ' '; //stores the color of the piece that you are moving
   int temp = 0; //stores the number of how many spaces you are to move forward
   int check = 0; //stores the number of pieces of the same color in the row you are moving to
   int top = 0; //stores the number of pieces that are on the same row as the one you are moving to and above

   if (colorToPlayer(board[x][y], playerToColor) == player && board[x][y] != BLANK) { //checks if the piece belongs to the correct player and what you are choosing is not blank
       for (int i = y; i < ROWS; i++) {
           for (int j = 0; j < COLUMNS; j++) {
               if (board[j][i] != BLANK) //checks if the piece you are trying to move the highest piece of the game
                   top++;
           }
       }
       for (int i = 0; i < COLUMNS; i++)
           if (board[i][y] != BLANK) //checks the number of pieces on that row
               temp++;
       if (y + temp < ROWS) {
           if (board[x][y + temp] == BLANK) { //checks if the space you are moving to is occupied
               tempPlayer = board[x][y];
               if (y + temp < 6)
                   for (int i = 0; i < COLUMNS; i++)
                           if (board[i][y + temp] == tempPlayer) //checks if there is a piece of the same color on that row
                               check++;
               if (check == 0) {
                       if (top > 1 && y > 5) {
                           board[x][y] = BLANK;
                           board[x][y + temp] = tempPlayer;
                           moved = true;
                       }
                       else if (y < 6) {
                           board[x][y] = BLANK;
                           board[x][y + temp] = tempPlayer;
                           moved = true;
                       }
                       else {
                           cout << "This piece is at the very top! you can not move this piece. Try again." << endl;
                           temp = 0;
                           check = 0;
                           top = 0;
                       }
                   }

                   else {
                       cout << "There is another piece of the same color in the row. Please try again." << endl;
                       temp = 0;
                       check = 0;
                       top = 0;
                   }
               }
               else {
                   cout << "There is another piece at the spot you are trying to move to. Try again." << endl;
                   temp = 0;
                   check = 0;
                   top = 0;
               }
       }
       else {
           cout << "You are attempting to move outside of the board space! Please try again." << endl;
           temp = 0;
           check = 0;
           top = 0;
       }
   }
   else {
       cout << "That is not your piece! Try again." << endl;
       temp = 0;
       check = 0;
       top = 0;
   }
}

Explanation / Answer

#include
#include
using namespace std;

const char BLANK = '.';
const char RED = 'R';
const char BLUE = 'B';
const char GREEN = 'G';
const char YELLOW = 'Y';
const char ROWS = 11;
const char COLUMNS = 4;
const char PLAYER1 = 0;
const char PLAYER2 = 1;
const int PIECE = 1;

void initBoard(char board[][ROWS]); //initializes board
void displayBoard(char board[][ROWS], char playerToColor[][2]); //prints out board
int colorToPlayer(char color, char playerToColor[][2]); //checks if the piece belongs to player 1 or 2
int score(int player, char board[][ROWS], char playerToColor[][2]); //calculates score for both player 1 and player 2
bool whoseTurn(int player); //true if it is player 1's turn, false for player 2
void changePlayer(int &player); //changes player turn
void move(int x, int y, char board[][ROWS], char playerToColor[][2], int player, bool &moved); //the move function


int main()
{
   int x = 0,
       y = 0,
       player = 0;
   bool validTurn = true; //just a placeholder for now...
   bool moved = false;
   bool chosen = false;

   char choice = ' ';
   char board[COLUMNS][ROWS];
   char tempPlayer;
   char playerToColor[2][2]; // first index = player number, second index = slot for colors

   cout << "Player 1, which color do you choose to be? Red/Blue or Yellow/Green." << endl
       << "(Input as R for Red/Blue or Y for Yellow/Green)" << endl;
  

   while (chosen == false) {
       cin >> choice;
       if (choice == 'R' || choice == 'r') {
           playerToColor[PLAYER1][0] = RED; // P1 = RED, BLUE
           playerToColor[PLAYER1][1] = BLUE;
           playerToColor[PLAYER2][0] = YELLOW; // P2 = YELLOW, GREEN
           playerToColor[PLAYER2][1] = GREEN;
           chosen = true;
       }
       else if (choice == 'Y' || choice == 'y') {
           playerToColor[PLAYER2][0] = RED; // P2 = RED, BLUE
           playerToColor[PLAYER2][1] = BLUE;
           playerToColor[PLAYER1][0] = YELLOW; // P1 = YELLOW, GREEN
           playerToColor[PLAYER1][1] = GREEN;
           chosen = true;
       }
       else
           cout << "You did not enter a valid entry. Please choose again." << endl;
   }

   initBoard(board);

   do {
       displayBoard(board, playerToColor);

       cout << "Player 1 " << score(PLAYER1, board, playerToColor) << endl;
       cout << "Player 2 " << score(PLAYER2, board, playerToColor) << endl;

       if (whoseTurn(player) == true) {
           while (moved == false) {
               cout << "Player 1: What piece do you want to move ? (Input: x y)" << endl;
               cin >> x >> y;
               move(x, y, board, playerToColor, player, moved);
           }
           changePlayer(player);
           moved = false;
       }

       else {
           while (moved == false) {
               cout << "Player 2: What piece do you want to move ? (Intput: x y)" << endl;
               cin >> x >> y;
               move(x, y, board, playerToColor, player, moved);
           }
           changePlayer(player);
           moved = false;
       }
   } while (validTurn == true);
}

void initBoard(char board[][ROWS])
{
   for (int x = 0; x < COLUMNS; x++)
       for (int y = 0; y < ROWS; y++)
           board[x][y] = BLANK;
   board[0][0] = YELLOW; board[1][0] = GREEN; board[2][0] = RED; board[3][0] = BLUE;
   board[0][1] = BLUE; board[1][1] = YELLOW; board[2][1] = GREEN; board[3][1] = RED;
   board[0][2] = RED; board[1][2] = BLUE; board[2][2] = YELLOW; board[3][2] = GREEN;
   board[0][3] = GREEN; board[1][3] = RED; board[2][3] = BLUE; board[3][3] = YELLOW;
   return;
}

void displayBoard(char board[][ROWS], char playerToColor[][2])
{
   for (int y = ROWS - 1; y >= 0; y--)
   {
       cout << setw(2) << y << ":" << " ";
       for (int x = 0; x < COLUMNS; x++)
       {
           cout << " ";
           if (colorToPlayer(board[x][y], playerToColor) == PLAYER1)
               cout << static_cast(tolower(board[x][y]));
           else
               cout << board[x][y];
           cout << " ";
       }
       if (y == 10) cout << " 60";
       if (y == 9) cout << " 40";
       if (y == 8) cout << " 30";
       if (y == 7) cout << " 20";
       if (y == 6) cout << " 10";
       cout << endl;
   }
   cout << " 0 1 2 3" << endl;
}

int colorToPlayer(char color, char playerToColor[][2])
{
   if (playerToColor[PLAYER1][0] == color)
       return PLAYER1;
   if (playerToColor[PLAYER1][1] == color)
       return PLAYER1;
   // If it wasn't player 1 then it must belong to player 2
   return PLAYER2;
}

int score(int player, char board[][ROWS], char playerToColor[][2])
{
   int total = 0;
   int rowValue = 10;
   for (int y = 6; y <= 10; y++)
   {
       for (int x = 0; x < COLUMNS; x++)
       {
           if (board[x][y] != BLANK)
               if (colorToPlayer(board[x][y], playerToColor) == player)
                   total += rowValue;
       }
       rowValue += 10;
       if (y == 9)
           rowValue += 10;
   }
   return total;
}

bool whoseTurn(int player) {
   if (player == 0)
       return true;
   else
       return false;
}

void changePlayer(int &player) {
   if (player == 0)
       player = 1;
   else
       player = 0;
}

void move(int x, int y, char board[][ROWS], char playerToColor[][2], int player, bool &moved) {
   char tempPlayer = ' '; //stores the color of the piece that you are moving
   int temp = 0; //stores the number of how many spaces you are to move forward
   int check = 0; //stores the number of pieces of the same color in the row you are moving to
   int top = 0; //stores the number of pieces that are on the same row as the one you are moving to and above

   if (colorToPlayer(board[x][y], playerToColor) == player && board[x][y] != BLANK) { //checks if the piece belongs to the correct player and what you are choosing is not blank
       for (int i = y; i < ROWS; i++) {
           for (int j = 0; j < COLUMNS; j++) {
               if (board[j][i] != BLANK) //checks if the piece you are trying to move the highest piece of the game
                   top++;
           }
       }
       for (int i = 0; i < COLUMNS; i++)
           if (board[i][y] != BLANK) //checks the number of pieces on that row
               temp++;
       if (y + temp < ROWS) {
           if (board[x][y + temp] == BLANK) { //checks if the space you are moving to is occupied
               tempPlayer = board[x][y];
               if (y + temp < 6)
                   for (int i = 0; i < COLUMNS; i++)
                           if (board[i][y + temp] == tempPlayer) //checks if there is a piece of the same color on that row
                               check++;
               if (check == 0) {
                       if (top > 1 && y > 5) {
                           board[x][y] = BLANK;
                           board[x][y + temp] = tempPlayer;
                           moved = true;
                       }
                       else if (y < 6) {
                           board[x][y] = BLANK;
                           board[x][y + temp] = tempPlayer;
                           moved = true;
                       }
                       else {
                           cout << "This piece is at the very top! you can not move this piece. Try again." << endl;
                           temp = 0;
                           check = 0;
                           top = 0;
                       }
                   }

                   else {
                       cout << "There is another piece of the same color in the row. Please try again." << endl;
                       temp = 0;
                       check = 0;
                       top = 0;
                   }
               }
               else {
                   cout << "There is another piece at the spot you are trying to move to. Try again." << endl;
                   temp = 0;
                   check = 0;
                   top = 0;
               }
       }
       else {
           cout << "You are attempting to move outside of the board space! Please try again." << endl;
           temp = 0;
           check = 0;
           top = 0;
       }
   }
   else {
       cout << "That is not your piece! Try again." << endl;
       temp = 0;
       check = 0;
       top = 0;
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote