Homework problem from class: I have to generate a tic tac toe game which uses a coin toss to determine who goes first. I've generated most of my code, but there are still several things/bugs I can't figure out.
There is no message when you just press enter without entering any value.
#include "pch.h"
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();
char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int n = 0;
int main()
{
srand(time(0));
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
Flip_Coin();
while (true)
{
n++;
Display_Board();
Ask_Turn();
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
break;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
break;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
break;
}
Computer_Player_Turn();
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
break;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
break;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
break;
}
}
}
void Flip_Coin()
{
int Flip;
cout << "Welcome to Tic - Tac - toe.\n\n"
<< "Wait I am flipping a coin to see who goes first . . .\n\n";
Flip = rand() % 2;
if (Flip == 0)
{
cout << "Computer wins coin toss.\n\n";
}
else
{
cout << "Human wins coin toss.\n\n";
}
cout << "The board is laid out like this:\n\n";
}
void Display_Board()
{
for (int Row = 0; Row < 3; Row++)
{
for (int Column = 0; Column < 3; Column++)
{
cout << Board[Row][Column] << "\t";
}
cout << "\n\n";
}
}
void Ask_Turn()
{
string input;
while (true)
{
cout << "Enter the position to place your X: ";
cin >> input;
cout << endl;
if (input != "")
{
char entered = input.c_str()[0]; // sets char character to act in place of string
if (entered >= '1' && entered <= '9')
{
cout << "The human places a X-token at position: " << entered << "\n\n";
cout << "Current board:\n\n";
int entered_number = entered - '0'; // changes char to int form
int index = entered_number - 1;
int row = index / 3;
int col = index % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
cout << "That position is already taken. ";
}
else {
Board[row][col] = 'X';
break;
}
}
else {
cout << "You must entered in the the range of 1-9.\n";
}
}
else {
cout << "You must enter something!"; // doesnt work
}
}
}
char Check_Winner()
{
//first player
if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
return 'X';
if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
return 'X';
if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
return 'X';
if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
return 'X';
//second player
if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
return 'O';
if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
return 'O';
if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
return 'O';
if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
return 'O';
return 'T';
}
void Computer_Player_Turn()
{
while (true)
{
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
continue;
}
else {
cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
Board[row][col] = 'O';
break;
}
}
}
Here is the solution.
1) Right now you have hard-coded to call Ask_Turn
first and then call Computer_Player_Turn
. Instead of calling them in this way, call them first from coin flip and then let the two functions call each other when they finish.
2) Call Display_Board()
before checking for winner.
3) Fixed here. You were only adding n by 1 for every 2 turns.
4) Ask for the input in a while loop, and as long as the input is empty be in the loop
So making all those changes, the code should look something similar to below.
I have tested the code here, https://onlinegdb.com/S1Qdcr63X
#include <iostream>
#include <string>
#include <cstdlib>
#include <time.h>
using namespace std;
void Flip_Coin();
void Do_Exercise();
void Display_Board();
void Ask_Turn();
char Check_Winner();
void Computer_Player_Turn();
char Board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
int n = 0;
void Computer_Player_Turn();
void Ask_Turn();
int main()
{
srand(time(0));
Do_Exercise();
cin.get();
return 0;
}
void Do_Exercise()
{
Flip_Coin();
}
void Flip_Coin()
{
int Flip;
cout << "Welcome to Tic - Tac - toe.\n\n"
<< "Wait I am flipping a coin to see who goes first . . .\n\n";
Flip = rand() % 2;
if (Flip == 0)
{
cout << "Computer wins coin toss.\n\n";
Computer_Player_Turn();
}
else
{
cout << "Human wins coin toss.\n\n";
Ask_Turn();
}
}
void Display_Board()
{
for (int Row = 0; Row < 3; Row++)
{
for (int Column = 0; Column < 3; Column++)
{
cout << Board[Row][Column] << "\t";
}
cout << "\n\n";
}
}
char Check_Winner()
{
//first player
if (Board[0][0] == 'X' && Board[0][1] == 'X' && Board[0][2] == 'X')
return 'X';
if (Board[1][0] == 'X' && Board[1][1] == 'X' && Board[1][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[2][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][0] == 'X' && Board[2][0] == 'X')
return 'X';
if (Board[0][1] == 'X' && Board[1][1] == 'X' && Board[2][1] == 'X')
return 'X';
if (Board[0][2] == 'X' && Board[1][2] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[0][0] == 'X' && Board[1][1] == 'X' && Board[2][2] == 'X')
return 'X';
if (Board[2][0] == 'X' && Board[1][1] == 'X' && Board[0][2] == 'X')
return 'X';
//second player
if (Board[0][0] == 'O' && Board[0][1] == 'O' && Board[0][2] == 'O')
return 'O';
if (Board[1][0] == 'O' && Board[1][1] == 'O' && Board[1][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[2][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][0] == 'O' && Board[2][0] == 'O')
return 'O';
if (Board[0][1] == 'O' && Board[1][1] == 'O' && Board[2][1] == 'O')
return 'O';
if (Board[0][2] == 'O' && Board[1][2] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[0][0] == 'O' && Board[1][1] == 'O' && Board[2][2] == 'O')
return 'O';
if (Board[2][0] == 'O' && Board[1][1] == 'O' && Board[0][2] == 'O')
return 'O';
return 'T';
}
bool winner()
{
if (Check_Winner() == 'X')
{
cout << "Human Wins!" << endl;
return true;
}
else if (Check_Winner() == 'O')
{
cout << "Computer Wins!" << endl;
return true;
}
else if (Check_Winner() == 'T' && n == 9)
{
cout << "It's a draw." << endl;
return true;
}
return false;
}
void Ask_Turn()
{
n++;
string input = "";
while (true)
{
do
{
cout << "Enter the position to place your X: ";
cin >> input;
cout << endl;
}
while(input == "" || (input.c_str()[0] < '1' || input.c_str()[0] > '9'));
char entered = input.c_str()[0]; // sets char character to act in place of string
cout << "The human places a X-token at position: " << entered << "\n\n";
cout << "Current board:\n\n";
int entered_number = entered - '0'; // changes char to int form
int index = entered_number - 1;
int row = index / 3;
int col = index % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
cout << "That position is already taken. ";
n--;
Ask_Turn();
}
else {
Board[row][col] = 'X';
break;
}
}
Display_Board();
if(winner())
return;
else
Computer_Player_Turn();
}
void Computer_Player_Turn()
{
n++;
while (true)
{
int computer_choice = (rand() % 9) + 1;
int row = (computer_choice - 1) / 3;
int col = (computer_choice - 1) % 3;
char grid_position = Board[row][col];
if (grid_position == 'X' || grid_position == 'O')
{
continue;
}
else {
cout << "The Computer places a O-token at position: " << computer_choice << "\n\n";
Board[row][col] = 'O';
break;
}
}
Display_Board();
if(winner())
return;
else
Ask_Turn();
}