I NEED HELP TO HAVE A MAIN FUNCTION FOR TESTING // C++ code #include #include #i
ID: 3891070 • Letter: I
Question
I NEED HELP TO HAVE A MAIN FUNCTION FOR TESTING // C++ code
#include #include #include #include #include #include #include #include using namespace std; class ABoardGame { private: // private method int calcPieces(int idxi, int idxj, int boardSize) { // determine pieces for alex and bob int peiceAlex = 0; int pieceBob = 0; // if i and j are less than bpard size if (idxi <= boardSize - 1 && idxj <= boardSize - 1) { peiceAlex = boardSize - idxj; pieceBob = boardSize - idxi; } // if i is less an j is greater than boardsize else if (idxi < boardSize && idxj >= boardSize) { peiceAlex = idxj - boardSize + 1; pieceBob = boardSize - idxi; } // if j is greater and i is less than bpard size else if (idxi >= boardSize && idxj < boardSize) { peiceAlex = boardSize - idxj; pieceBob = idxi - boardSize + 1; } // otherwize else { peiceAlex = idxj - boardSize + 1; pieceBob = idxi - boardSize + 1; } // return the maximum of pieces for alex and bob if( peiceAlex > pieceBob) return peiceAlex; else return pieceBob; } public: // Method signature: string whoWins(vector board) string whoWins(vector boardRegion) { int pieces; // determine board size int boardSize = boardRegion.size() / 2; // initialize score array to alice and bob and assign them 0 int alice_Score[boardSize] = {0}; int bob_Score[boardSize]= {0}; // iterate over the size for (int i = 0; i < 2 * boardSize; i++) { for (int j = 0; j < 2 * boardSize; j++) { pieces = calcPieces(i, j, boardSize); if (boardRegion[i][j] == 'A') alice_Score[pieces - 1]++; else if (boardRegion[i][j] == 'B') bob_Score[pieces - 1]++; } } int flag = 0; // find the winner of both for (int i = 0; i < boardSize; i++) { cout << alice_Score[i] << ":" << bob_Score[i] << endl; if (alice_Score[i] > bob_Score[i]) { flag = 1; break; } else if (alice_Score[i] < bob_Score[i]) { flag = 2; break; } } if(flag == 1) return "Alice wins"; else if(flag == 2) return "Bob wins"; // if no one wins, retune a draw return "Draw"; } };
Explanation / Answer
class BoardGame
{ private:
// private method
int calcPieces(int idxi, int idxj, int boardSize)
{ // determine pieces for alex and bob
int peiceAlex = 0; int pieceBob = 0;
// if i and j are less than bpard size
if (idxi <= boardSize - 1 && idxj <= boardSize - 1)
{
peiceAlex = boardSize - idxj;
pieceBob = boardSize - idxi; }
// if i is less an j is greater than boardsize
else if (idxi < boardSize && idxj >= boardSize)
{
peiceAlex = idxj - boardSize + 1;
pieceBob = boardSize - idxi; }
// if j is greater and i is less than bpard size
else if (idxi >= boardSize && idxj < boardSize)
{
peiceAlex = boardSize - idxj;
pieceBob = idxi - boardSize + 1;
}
// otherwize
else
{
peiceAlex = idxj - boardSize + 1;
pieceBob = idxi - boardSize + 1;
}
// return the maximum of pieces for alex and bob
if( peiceAlex > pieceBob)
return peiceAlex;
else
return pieceBob;
}
public:
// Method signature: string whoWins(vector board)
string whoWins(vector boardRegion)
{
int pieces;
// determine board size
int boardSize = boardRegion.size() / 2;
// initialize score array to alice and bob and assign them 0
int alice_Score[boardSize] = {0};
int bob_Score[boardSize]= {0};
// iterate over the size
for (int i = 0; i < 2 * boardSize; i++)
{
for (int j = 0; j < 2 * boardSize; j++)
{
pieces = calcPieces(i, j, boardSize);
if (boardRegion[i][j] == 'A')
alice_Score[pieces - 1]++;
else if (boardRegion[i][j] == 'B')
bob_Score[pieces - 1]++;
}
}
int flag = 0;
// find the winner of both
for (int i = 0; i < boardSize; i++)
{
cout << alice_Score[i] << ":" << bob_Score[i] << endl;
if (alice_Score[i] > bob_Score[i])
{
flag = 1;
break;
}
else if (alice_Score[i] < bob_Score[i])
{
flag = 2; break;
}
}
if(flag == 1)
return "Alice wins";
else if(flag == 2)
return "Bob wins";
// if no one wins, retune a draw
return "Draw";
}
};
int main () {
ABoardGame bg;
vector<int> boardRegion;
cout << "Winner is : " << bg.whoWins(boardRegion);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.