C++ Programming: Please, include comments so I can understand and review this ex
ID: 3887448 • Letter: C
Question
C++ Programming: Please, include comments so I can understand and review this exercise. Thanks in advance.
ABoardGame
Problem Statement
Your friends Alice and Bob are playing a board game. They have asked you to help them to determine the winner. The game is played on a square board with 2N rows and 2N columns. The exact rules of the game itself are not important for this problem. Once the game is over, each cell of the board is either empty or contains a single piece that belongs to either Alice or Bob. You are given board, where the j-th character in i-th element (0-based indices) describes the contents of the cell in row i, column j: '.' represents an empty cell, 'A' a cell with Alice's piece and 'B' a cell with Bob's piece.
The entire board is divided into N regions. Region 1 occupies the 4 central cells of the board. Each next region contains all cells that are horizontally, vertically or diagonally adjacent to cells of the immediately previous region and do not belong to any of the previous regions. For example, when N = 4, here is how the regions look:
44444444
43333334
43222234
43211234
43211234
43222234
43333334
44444444
The winner is determined as follows. Consider the lowest numbered region that contains a different number of Alice's and Bob's pieces. The player who has more pieces in this region is the winner. If all regions contain the same number of Alice's and Bob's pieces, the game ends in a draw.
Return "Alice" if Alice wins the given game, "Bob" if Bob wins and "Draw" if the game ends in a draw. Note that return values are case-sensitive.
Definition
Class: ABoardGame
Method: whoWins
Parameters: vector <string>
Returns: string
Method signature: string whoWins(vector <string> board)
(be sure your method is public)
Limits
Time limit (s): 2.000
Memory limit (MB): 256
Constraints
- board will contain between 2 and 50 elements, inclusive.
- The number of elements in board will be even.
- Each element of board will contain the same number of characters as the number of elements in board.
- Each character in board will be 'A', 'B' or '.'.
Examples
0)
{".....A", "......", "..A...", "...B..", "......", "......"}
Returns: "Alice"
Both Alice and Bob have 1 piece in region 1, so they are tied there. In region 2, they have no pieces at all, so a tie again. Finally, in region 3 Alice has 1 piece, while Bob has none. So Alice is the winner of this game.
1)
{"AAAA", "A.BA", "A..A", "AAAA"}
Returns: "Bob"
Even though Alice has 12 pieces and Bob just one, this one piece is enough for him to win.
2)
{"..", ".."}
Returns: "Draw"
The board can be entirely empty.
3)
{"BBB..BAB...B.B", ".AAAAAAAAAAAA.", "AA.AA.AB..A.AB", "..........B.AB", ".A..BBAB.A.BAB", ".AB.B.......A.", ".A..A.AB.A..AB", ".ABAA.BA...BA.", "BAAAB.....ABA.", ".A....B..A..B.", "B...B....B..A.", "BA.B..A.ABA.A.", "BAAAA.AAAAA.A.", "B.B.B.BB.B...."}
Returns: "Alice"
4)
{"..A..AAA..AA", "ABABB..AAAAA", "ABBBBBBBBBA.", "AABBBABABBAA", "...BABABABBA", "B.BA..A.BBA.", "AA.A..B.AB.B", "..BA.B.AABAA", "..ABABBBABA.", ".ABB.BBBBBAA", "ABAAA.AA.A.A", "A..AAA.AAA.A"}
Returns: "Bob"
5)
{"B..ABAABBB", "B.........", "A..A.AA..B", "A.BBBAA..A", "B.AAAAB...", "A..BBBBB.A", "B..ABAABBA", "A......B.B", "B......A.A", "BA.AABBB.A"}
Returns: "Draw"
Explanation / Answer
// C++ code
#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include <string>
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 <string> board)
string whoWins(vector<string> 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";
}
};
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.