'How to put what the user picks for the row and column in the board
So my question is how do I put what the user enters, for example, they put row : 1 and column: 2 in my board that I made. I'm working on this in the void playerChoice
function.
I've thought of some solutions to this problem like board[row][column] = 'X' in like an if statement with board[row][column] = 'O' as well. Is this alright?
Hopefully you guys can help figure this problem this out with me.
const int ROWS = 3; // For the rows
const int COLS = 3; // For the number of columns;
void showBoard(const char[][COLS], int); // Shows the board
void playerChoice(char[][COLS], char); // shows the player choice
int winner(const char[][COLS],
const char, string);
int main() {
char board[ROWS][COLS] = {
{
'*',
'*',
'*'
},
{
'*',
'*',
'*'
},
{
'*',
'*',
'*'
}
};
string winner = " ";
showBoard(board, 3); // displays the board
for (int i = 0; i < 3; i++) {
playerChoice(board, 'X');
showBoard(board, 3);
playerChoice(board, 'O');
showBoard(board, 3);
}
cout << "The winner is:" << winner;
}
void showBoard(const char arr[][COLS], int SIZE) {
for (int row = 0; row < SIZE; row++) {
for (int col = 0; col < SIZE; col++)
cout << '|' << arr[row][col];
cout << '|' << endl;
}
}
void playerChoice(char arr[][COLS], char name) {
int row, columns;
cout << "What row would you like?\n";
cout << "Row: ";
cin >> row;
while (row < 1 || row > 3) {
cout << "Error please pick a row in between 1 and 3.\n";
cout << "Row: ";
cin >> row;
cout << endl;
}
cout << "What column would you like?\n";
cout << "Column: ";
cin >> columns;
while (columns < 1 || columns > 3) {
cout << "Error,please pick a row in between 1 and 3.\n";
cout << "Column: ";
cin >> columns;
cout << endl;
}
}
int winner(const char[][COLS],
const char, string) {
// working on this.
return winner;
}
Solution 1:[1]
I've thought of some solutions to this problem like board[row][column] = 'X' in like an if statement with board[row][column] = 'O' as well.
Yes. You can try the code below:
if (arr[row - 1][columns - 1] == '*')
{
arr[row - 1][columns - 1] = name;
}
else
{
cout << "Retry" << endl;
playerChoice(arr, name);
}
Solution 2:[2]
Will this work for you?
unsigned int row = 0u;
unsigned int column = 0u;
char board[3][3] = {' '};
std::cout << "Enter row: ";
std::cin >> row;
std::cout << "Enter column: ";
std::cin >> column;
// Insert a character at the User's row and column
board[row][column] = 'X';
If you want to insert the User's coordinates into the board, you'll need a different type of board.
Edit 1: A different type of board
Let's call the items of the board a Cell.
In order to store the row and column, you'll need a cell that can store the row and column:
struct Cell
{
char c;
unsigned int row;
unsigned int column;
};
Cell board[3][3];
//...
board[row][column].c = 'O';
board[row][column].row = row;
board[row][column].column = column;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | CherryDT |
Solution 2 |