Tic-Tac-Toe Challenge Project (C++)

Thanks codecademy team. This was a great challenge!. So I want to share my solution to you. I like feedbacks!
:slight_smile:

Hello, I had a question about my code.

I am trying to validate the user input and it works if I leave it as an if statement but the second I convert it to a while loop Codecademy loses connection and it won’t compile at all. Any input is greatly appreciated.

I have this at the top of my functions file:
std::vector<std::string> board = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
[…]
// Checks to see if there is a letter on that spot

bool checkSpace(int choice){
  if(board[choice - 1] != "X" && board[choice - 1] != "O"){
    return true;
  } else {
    return false;
  }
}

// Validates user input

int userChoice(std::string player) {
  int choice;
  std::cout << player << " choose your spot (1-9):\n";
  std::cin >> choice;

  if(choice < 1 || choice > 9){
    std::cin.clear();
    std::cout << "Please choose a valid number (1-9):\n";
    std::cin >> choice;
  }
  if(board[choice - 1] != "X" && board[choice - 1] != "O"){
    std::cout << "That spot is unavailable. Please choose an open space:\n";
    std::cin.clear();
    std::cin >> choice;
    std::cout << choice;
  } 
  return choice;
}

ttt.cpp

#include <iostream>
#include <vector>
#include <string>
#include "func.cpp"

int main() {
	std::cout << "lets game, gamers\n";
	int choice;
	turn1 = false;
	turn2 = false;

	std::cout << "\nthe board:\n";
	board();
	std::cout << "\n";

	while (over == false) {
		while (turn1 == false){
			choice = 0;
			std::cout << "\nplayer one (X), please select which tile you want to choose!\n\n";
			std::cin >> choice;
			std::cout << "\n";
				if ((choice > 0) && (choice < 10)) {
					if (moves[choice-1] == " ") {
						moves[choice-1] = "X";
						turns++;
						turn1 = true;
					}
					else {
						std::cout << "\nplease pick a valid tile.\n";
					}
					
				}
				else {
					std::cout << "\nplease pick a valid tile.\n";
				}

		}

		if (turns != 9) {
			board();
		}

		while (turn2 == false) {
			if (turns == 9) {
				break;
			}
			choice = 0;
			std::cout << "\nplayer two (O), please select which tile you want to choose!\n\n";
			std::cin >> choice;
			std::cout << "\n";
			if ((choice > 0) && (choice < 10)) {
				if (moves[choice - 1] == " ") {
					moves[choice - 1] = "O";
					turns++;
					turn2 = true;
				}
				else {
					std::cout << "\nplease pick a valid tile.\n";
				}
			}
			else {
				std::cout << "\nplease pick a valid tile.\n";
			}
		}

		board();
		wincheck();

		turn1 = false;
		turn2 = false;
		
	}



}

func.cpp

#include <iostream>
#include <vector>
#include <string>

std::vector<std::string> moves = { " ", " ", " ", " ", " ", " ", " ", " ", " " };
bool over = false;
int turns = 0;
bool turn1 = false;
bool turn2 = false;

void board() {

	std::cout << "-------------\n";
	std::cout << "| " << moves[0] << " | " << moves[1] << " | " << moves[2] << " |" << " possible choices: 1, 2, 3";
	std::cout << "\n-------------";
	std::cout << "\n| " << moves[3] << " | " << moves[4] << " | " << moves[5] << " |" << " possible choices: 4, 5, 6";
	std::cout << "\n-------------";
	std::cout << "\n| " << moves[6] << " | " << moves[7] << " | " << moves[8] << " |" << " possible choices: 7, 8, 9";
	std::cout << "\n-------------\n";
}

void wince(int check) {
	if (moves[check] == "X") {
		std::cout << "\nplayer 1 wins!\n";
		over = true;
		turn2 = true;
	}
	else if (moves[check] == "O") {
		std::cout << "\nplayer 2 wins!\n";
		over = true;
		turn1 = true;
	}
}

void wincheck() {

	//across the top
	if ((moves[0] == moves[1]) && (moves[0] == moves[2])) {
		wince(0);
	}
	
	//down from left
	else if ((moves[0] == moves[3]) && (moves[0] == moves[6])){
		wince(0);
	}
	
	//diagonal through the centre 1
	else if ((moves[0] == moves[4]) && (moves[0] == moves[8])) {
		wince(0);
	}

	//across the bottom
	else if ((moves[6] == moves[7]) && (moves[6] == moves[8])) {
		wince(6);
	}

	//diagonal through the centre 2
	else if ((moves[6] == moves[4]) && (moves[6] == moves[2])) {
		wince(6);
	}

	//down the middle
	else if ((moves[1] == moves[4]) && (moves[1] == moves[7])) {
		wince(1);
	}

	//down from right
	else if ((moves[2] == moves[5]) && (moves[2] == moves[8])) {
		wince(2);
	}

	//across the middle
	else if ((moves[3] == moves[4]) && (moves[3] == moves[5])) {
		wince(3);
	}

	else if (turns == 9) {
		std::cout << "\ndraw!\n";
		over = true;
		turn2 = true;
	}
}

Check out my code

Here is my solution to the tic-tac-toe challenge. I utilized a single vector to store board values and a draw function that draws the board. Winner is determined by comparing values within the vector.

Hello fellow Coders! Here is my solution to the Classic TicTacToe Game written in C++. Would really love anybody peer review and feedback regarding my solution. Peace and love. Thank you.

1 Like

Here is my solution to this project, I have added the ability to change the number of games easier by having to only change one variable.
Tick Tac Toe

I have encountered one bug that I am in the process of fixing, and that’s when both players have not won and have filled up the board, it justs goes straight to Player 1 Winning when the last spot on the board is filled.

Truffle-Files/NewWheel at main · TroubleTruffle/Truffle-Files (github.com)

Kind of went overboard with the ascii art stuff. Also ended up binging a c++ series so I put in some of the later topics in.

Hello! I’ve been on Codecademy for sometime and this is my first post!

I would love if someone can review my tic-tac-toe project → Tic-tac-toe

Thank you all!

Hello! I just finished the project! Feel free to compare it with yours!

You will notice I didn’t use vectors, I only used arrays because of an issue.
I found this issue when I tried to make an idea to put a vector as the type for a vector.
e.g. (std::vector<std::vector> something = {vector1, vector2}