I am just beginning to learn coding and decided to create a text based tic tac toe board as my first solo C++ project. My goals were as follows:
- be able to test if there is a winning state on any given board
- be able to call the board state
- have the computer play O's for any possible position
- cleanly close out the game and announce the winner
Code is tested and bug free!
Some feedback on what I could improve on,what I did poorly, as well as any other general thoughts would be much appreciated!
#include <iostream>
#include <string>
// initialize some variables, give them each a different name so the player can choose a space
std::string a1{ "1" };
std::string a2{ "2" };
std::string a3{ "3" };
std::string b1{ "4" };
std::string b2{ "5" };
std::string b3{ "6" };
std::string c1{ "7" };
std::string c2{ "8" };
std::string c3{ "9" };
void boardState()
{
//print the state of the board
std::cout << " |" << " |" << '\n';
std::cout << " " << a1 << " |" << " " << b1 << " |" << " " << c1 << '\n';
std::cout << "__________________" << '\n';
std::cout << " |" << " |" << '\n';
std::cout << " " << a2 << " |" << " " << b2 << " |" << " " << c2 << '\n';
std::cout << "__________________" << '\n';
std::cout << " |" << " |" << '\n';
std::cout << " " << a3 << " |" << " " << b3 << " |" << " " << c3 << '\n';
}
void getMove()
{
bool over{ false };
while(over==false)
{
// ask user for their move
std::cout << "Type your move(1-9): ";
std::string temp{};
std::cin >> temp;
// take their move and if it is valid, place it on the board and exit
if (temp == "1" && a1 == "1")
{
a1 = "X";
over = true;
}else if (temp == "2" && a2 == "2"){
a2 = "X";
over = true;
}else if (temp == "3" && a3 == "3"){
a3 = "X";
over = true;
}else if (temp == "4" && b1 == "4"){
b1 = "X";
over = true;
}else if (temp == "5" && b2 == "5"){
b2 = "X";
over = true;
}else if (temp == "6" && b3 == "6"){
b3 = "X";
over = true;
}else if (temp == "7" && c1 == "7"){
c1 = "X";
over = true;
}else if (temp == "8" && c2 == "8"){
c2 = "X";
over = true;
}else if (temp == "9" && c3 == "9"){
c3 = "X";
over = true;
}
}
}
bool winCheck()
{
// there are 8 total combinations of 3 placements that constitiute a win. manually check if any of these 8 occur and if one does return true
if (a1 == a3 && a1 == a2)
{
std::cout << a1 << "'s WIN!";
return true;
}else if (a1 == b1 && a1 == c1){
std::cout << a1 << "'s WIN!";
return true;
}else if (a1 == b1 && a1 == c1){
std::cout << a1 << "'s WIN!";
return true;
}else if (a1 == b2 && a1 == c3){
std::cout << a1 << "'s WIN!";
return true;
}else if (b1 == b2 && b1 == b3){
std::cout << b1 << "'s WIN!";
return true;
}else if (c1 == b2 && c1 == a3){
std::cout << c1 << "'s WIN!";
return true;
}else if (c1 == c2 && c1 == c3){
std::cout << c1 << "'s WIN!";
return true;
}else if (a2 == b2 && a2 == c2){
std::cout << a2 << "'s WIN!";
return true;
}else if (a3 == b3 && a3 == c3){
std::cout << a3 << "'s WIN!";
return true;
}else{
return false;
}
}
void Oresponse()
{
// the loop will check each space in this order; 5, 1, 9, 7, 3, 4, 6, 8, 2. and will place an O in the first available spot
bool temp{ false };
while (temp == false)
{
if (b2 == "5")
{
b2 = "O";
temp = true;
}else if(a1 == "1"){
a1 = "O";
temp = true;
}else if(c3 == "9"){
c3 = "O";
temp = true;
}else if(c1 == "7"){
c1 = "O";
temp = true;
}else if(a3 == "3"){
a3 = "O";
temp = true;
}else if(b1 == "4"){
b1 = "O";
temp = true;
}else if(b3 == "6"){
b3 = "O";
temp = true;
}else if(c2 == "8"){
c2 = "O";
temp = true;
}else if(a2 == "2"){
a2 = "O";
temp = true;
}else{
temp = true;
}
}
}
int main()
{
// until the gamestate is considered finished, loop printing the boardstate and prompting the user to make a move
bool end{ false };
while (end == false)
{
bool finished = winCheck();
if (finished == true)
{
end = true;
// if all possible board spots are filled end the game
}else if(a1 != "1" && a2 != "2" && a3 != "3" && b1 != "4" && b2 != "5" && b3 != "6" && c1 != "7" && c2 != "8" && c3 != "9"){
end = true;
}else{
boardState();
getMove();
Oresponse();
}
}
boardState();
winCheck();
std::cout << std::endl;
return 0;
}