Stuck at trying to put the user inputs into the array and displaying the board after each input, also need help with finding a winner.
/*
Designing the Board
*/
#include <iostream>
#include <string.h>
using namespace std;
const int MAXROW = 3;
const int MAXCOL = 3;
void initializeBoard(char initializeBoard[MAXROW][MAXCOL]);
void printBoard(char printBoard[MAXROW][MAXCOL]);
float player1(char initBoard[MAXROW][MAXCOL]);
float player2(char initBoard[MAXROW][MAXCOL]);
//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
// Loop through rows
for (int row = 0; row < MAXROW; row++)
{
// Loop through columns
for (int column = 0; column < MAXCOL; column++)
for (int i = 0; i < 7; i++)
initBoard[row][column] = i;
}
}
//Display the board
void printBoard(char theBoard[MAXROW][MAXCOL])
{
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << endl;
for (int row = 0; row < MAXROW; row++)
{
cout << " | ";
for (int column = 0; column < MAXCOL; column++)
cout << theBoard[row][column] << " | ";
cout << endl;
}
cout << endl;
cout << "Here is how to play:" << endl;
cout << "Step 1: Player 1 enter an X in a desired location" << endl;
cout << "Step 2: Player 2 enter an O in a desired location" << endl;
cout << "Step 3: Repeat until there is a winner, loser, or a draw" << endl;
cout << "Step 4: Have fun and play again!" << endl;
cout << endl;
}
float player1(char initBoard[MAXROW][MAXCOL])
{
int row = 0;
int column = 0;
cout << "Player 1 enter row number: ";
cin >> row;
while ((isdigit(row) == 0) || (int(row) < 48) || (int(row) > 51))
{
cout << "Invalid number \n";
cout << "Please enter 0, 1, 2 for tic tac toe row:";
cin >> initBoard[row][MAXCOL];
}
cout << "Player 1 enter column number: ";
cin >> column;
//input validation
while ((isdigit(column) == 0) || (int(column) < 48) || (int(column) > 51))
{
cout << "Invalid number \n";
cout << " Please enter 0, 1, 2 for tic tc toe column:";
cin >> initBoard[MAXROW][column];
}
Ok I have added to the and the only problem I am having is for the input to be between 1-9 because it is taking numbers like 10 and 111 as the first slot.
/*
Designing the Board
*/
#include <iostream>
#include <string.h>
#include <string>
using namespace std;
const int MAXROW = 3;
const int MAXCOL = 3;
void initializeBoard(char initializeBoard[MAXROW][MAXCOL]);
void display_board(char game1[MAXROW][MAXCOL]);
void printBoard(char theBoard[MAXROW][MAXCOL]);
int player1();
int player2();
int check(char);
char board[MAXROW][MAXCOL];
char game[MAXROW][MAXCOL] = { { '1','2','3' },{ '4','5','6' },{ '7','8','9' } };
void gameplay();
//Main function
int main(char initBoard[MAXROW][MAXCOL])
{
//TicTacToe
gameplay();
system("pause");
return 0;
}
//Checks for winner, loser or draw
int check(char input)
{
//Inititalize local variables
int i, j;
int draw = 1;
//Loops to enforce game rules
for (i = 0; i < 3; i++)
{
if ((board[i][0] == input) && (board[i][1] == input) && (board[i][2] == input))
return 1;
}
for (i = 0; i < 3; i++)
{
if ((board[0][i] == input) && (board[1][i] == input) && (board[2][i] == input))
return 1;
}
for (i = 0; i < 3; i++)
{
if ((board[0][0] == input) && (board[1][1] == input) && (board[2][2] == input))
return 1;
}
for (i = 0; i < 3; i++)
{
if ((board[0][2] == input) && (board[1][1] == input) && (board[2][0] == input))
return 1;
}
for (i = 0; i < 3; i++)
{
if ((board[0][0] == input) && (board[1][1] == input) && (board[2][2] == input))
return 1;
}
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (board[i][j] == '*')
draw = 0;
if (draw)
return 2;
else
return 0;
}
//Fills up the initial board with asterisks
void initializeBoard(char initBoard[MAXROW][MAXCOL])
{
// Loop through rows
for (int row = 0; row < MAXROW; row++)
{
// Loop through columns
for (int column = 0; column < MAXCOL; column++)
initBoard[row][column] = '*';
}
}
//Display the initialized board
void printBoard(char theBoard[MAXROW][MAXCOL])
{
cout << "Welcome to Tic-Tac-Toe!" << endl;
cout << endl;
for (int row = 0; row < MAXROW; row++)
{
cout << " | ";
for (int column = 0; column < MAXCOL; column++)
cout << theBoard[row][column] << " | ";
cout << endl;
}
cout << endl;
cout << "Here is how to play:" << endl;
cout << "Step 1: Player 1 choose a desired location" << endl;
cout << "Step 2: Player 2 choose a desired location" << endl;
cout << "Step 3: Repeat until there is a winner, loser, or a draw" << endl;
cout << "Step 4: Have fun and play again!" << endl;
cout << endl;
}
//Calculates a winner, loser, or draw and displays the result
void gameplay()
{
//Declare local variables
int Player1;
int Player2;
char enter = 0;
int p1_wins = 0; //p1_wins = 1 if p1 wins, and p1_wins = 2 if it is a draw
int p2_wins = 0; //p2_wins = 1 if p2 wins, and p2_wins = 2 if it is a draw
cout << "Enter Y or y to play TIC TAC TOE. Enter N or n to exit." << endl;
cin >> enter;
if ((enter == 'Y') || (enter == 'y'))
{
initializeBoard(board);
printBoard(board);
int player1()
{
int row = 0;
int column = 0;
char choice;
bool position_filled = 1;
bool valid = 1;
while (position_filled || valid)
{
cout << "Player 1 enter your choice: ";
cin >> choice;
if ((isdigit(choice) == 0) || (int(choice) < 48) || (int(choice) > 57))
{
valid = 1;
cout << "Invalid entry! \n";
cout << "Please enter a number between 1-9 for your choice: ";
}
else
{
valid = 0;
switch (choice)
{
case '1': row = 0; column = 0; break;
case '2': row = 0; column = 1; break;
case '3': row = 0; column = 2; break;
case '4': row = 1; column = 0; break;
case '5': row = 1; column = 1; break;
case '6': row = 1; column = 2; break;
case '7': row = 2; column = 0; break;
case '8': row = 2; column = 1; break;
case '9': row = 2; column = 2; break;
default:
cout << "You didn't enter a correct number! Try again\n";
valid = 1;
}