'I'm having trouble putting who wins in my game
So everything in my code works so far, but I don't know why the checkWinner function doesn't work. In my Bool checkWinner
function, I used bool. When I call the function I'm not sure where to put it as well.
What I tried: one place where I have tried was in my for loop in the main function. Ex:
checkWinner(board, gwinner)
However it doesn't work, the game keeps on playing when someone even has three X's or O's.
Possible problems I see: I'm not sure what's wrong, could it be my checkwinner
function, or could it be my playerChoice
function?
The question is why isn't a winner being picked? Thank you for the help.
#include <iostream>
#include <string>
using namespace std;
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
bool checkwinner(const char[][COLS], string);
int main()
{
char board[ROWS][COLS] = { { '*', '*', '*' }, { '*', '*', '*' }, { '*', '*', '*' } };
string gwinner = " ";
showBoard(board, 3); // displays the board
for (int i = 0; i < 4; i++) {
playerChoice(board, 'X');
showBoard(board, 3);
playerChoice(board, 'O');
showBoard(board, 3);
}
cout << "The winner is: " << gwinner;
}
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;
char board[ROWS][COLS] = { { '*', '*', '*' }, { '*', '*', '*' }, { '*', '*', '*' } };
cout << "What row would you like?\n";
cout << "Row: ";
cin >> row;
while (row < 1 || row > 3) {
cout << "Error please pick a row 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 column between 1 and 3.\n";
cout << "Column: ";
cin >> columns;
cout << endl;
}
if (arr[row - 1][columns - 1] == '*') {
arr[row - 1][columns - 1] = name;
}
else {
cout << "This space is occupied.\n";
cout << "Please select again\n";
}
}
bool checkwinner(const char arr[][COLS], string gwinner)
{
bool winner = false;
if ((arr[0][0] == arr[0][1]) && (arr[0][1] == arr[0][2])) // checks row
{
winner = true;
}
else if ((arr[1][0] == arr[1][1]) && (arr[1][1] == arr[1][2])) // checks row
{
winner = true;
}
else if ((arr[2][0] == arr[2][1]) && (arr[2][1] == arr[2][2])) // checks row
{
winner = true;
}
else if ((arr[0][0] == arr[1][0]) && (arr[1][0] == arr[2][0])) // checks columns
{
winner = true;
}
else if ((arr[0][1] == arr[1][1]) && (arr[1][1] == arr[2][1])) // checks columns
{
winner = true;
}
else if ((arr[0][2] == arr[1][2]) && (arr[1][2] == arr[2][2])) // checks columns
{
winner = true;
}
else if ((arr[0][0] == arr[1][1]) && (arr[1][1] == arr[2][2])) // checks diagonal
{
winner = true;
}
else if ((arr[2][0] == arr[1][1]) && (arr[1][1] == arr[0][2])) // checks diagonal
{
winner = true;
}
else {
gwinner = "It's a tie! ";
winner = false;
}
return winner; // returns the winner
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|