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

First of all, I would like to say that the programing language i am using is C++

ID: 3790237 • Letter: F

Question

First of all, I would like to say that the programing language i am using is C++. Anyways, we are making a now obsolete game called Upthrust from scratch and these are the rules:

1. A piece must move exactly as many spaces up as there are pieces in the horizontal row from which it departs. Thus, if there are two pieces in a row, either piece may move up exactly two spaces. After one piece is moved, the other may only move up one space since it has become the solitary piece in the row.

2. Only one piece may occupy a space. Pieces may jump over other pieces, as long as they land on empty spaces.

3. The most advanced piece of a color may not make a single space move. Therefore, a piece that is alone in a row cannot move if the other three pieces of the same color are below it on the board.

4. On any of the bottom six rows of the board (the non-scoring rows) two pieces of the same color may NEVER be in the same row at the same time. This restriction does not apply to the five scoring rows.

5. If 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.

6.Even when a piece has reached the scoring zone, it can continue to move up the board with regard to rules 1-3.

The game ends when:

1. No player is able to make a legal move

2. Only two pieces remain outside the scoring zone

The winner is the player whose pieces total the highest.

I would like to point out that I completed the majority of the rules. However, I am not able to come up for a solution for rule #5 and how to end the game.

Here is my current code...(Sorry for the mess, I tried to comment to the best I can):

#include <iostream>
#include <iomanip>
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<char>(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;
   }
}

So should i just try to somehow implement a boolean function with all of my previous checks in place and do a nested for loop of the whole board? I was thinking so, but that wouldnt be very efficent. I hope I provided you guys with enough information!

Thanks ahead of time
-A Struggling Computer Science Student

Explanation / Answer

Inside your code, there is a function called move with some parameters in it. You can put a condition at the last after checking all the rules from 1 to 4. This condition will ensure that if the player is not able to make any moves in that turn he/she loses the turn and later on he can try to move during the next iteration.

This will ensure that inside the same function the logic is implemented and also no need of creating a different function.