Having problems getting my game to work... Any thoughts on what I might do? I have gone through most of the code and initialized it, but for some reason it is saying for most of the function that I don't have the proper points. Also that the drawBoard
doesn't have the void type. Please help
Thanks
#include <iostream>
using namespace std;
// initialize a 2d board (3,3) with asterisk
void drawBoard(char board[][]);
char check_Winner(char [][]);
int main(){
char board[3][3]={{'*','*','*'},{'*','*','*'},{'*','*','*'}};
bool win = false;
bool in_row = false;
bool in_col = false;
int row;
int column;
// run a loop:
while (!win) {
// Display the content of the board
drawBoard(char board[][]));
cout << "Player 1's turn:";
// Ask player one to chose a location (row, column)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
bool valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else {
valid = true;
}
}
board[row-1][column-1] = 'X';
drawBoard(char board[][]);
// Check if someone won or if there is a tie
check_Winner(char board[][]);
// Ask player two to chose a location (row, column)
cout << "Player 2's turn:";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
// check if move is valid
//check if location has astericks and its between 0 thru 2
bool valid = false;
while (!valid)
{
//check if the move is within range.
if(!(row <= 3 && row >= 1 && column <= 3 && column >= 1)){
cout << "Error: Either the row or column is not within range of the board.\n";
// ask again (error: row in column not within range)
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}
else if(board[row-1][column-1] != '*')
{
cout << "Error: The (row,column) is already occupied.\n";
cout << "What is the row:?\n";
cin >> row;
cout << "What is the column?\n";
cin >> column;
}else
{
valid = true;
}
board[row-1][column-1] = 'O';
}
drawBoard(char board[][])
// Check if someone won or if there is a tie
check_Winner(char board[][]);
}
system("pause");
return 0;
}
char check_Winner(char board[][]){
char winner = 'T';
// Checks for horizontal:
for (int i = 0; i < 3; i++)
if (board[i][0] == board[i][1] && board[i][1] == board[i][2])
winner = board[i][0];
// Checks for vertical:
for (int i = 0; i < 3; i++)
if (board[0][i] == board[1][i] && board[1][i] == board[2][i])
winner = board[0][1];
// Checks for diagnol:
if ((board[0][0] == board[1][1] && board[1][1] == board[2][2]) ||
(board[0][2] == board[1][1] && board[1][1] == board[2][0]))
winner = board[1][1];
// checking the result:
switch (winner) {
case 'T': cout << "IT'S A TIE";/* tie */ break;
case 'X': cout << "PLAYER 1 WON!";/* X won */ break;
case 'O': cout << "PLAYER 2 WON!";/* O won */ break;
default : cout << "NEXT PLAYER'S TURN...>"; continue;
}
}
void drawBoard(char board[][])
{
cout << " 1 2 3" << endl;
cout << " +---+---+---+" << endl;
cout << " 1" << " | " << board[0][0] << " | " << board[0][1] << " | " << board[0][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 2" << " | " << board[1][0] << " | " << board[1][1] << " | " << board[1][2] << " | " << endl;
cout << " +---+---+---+" << endl;
cout << " 3" << " | " << board[2][0] << " | " << board[2][1] << " | " << board[2][2] << " | " << endl;
cout << " +---+---+---+" << endl;
}