Skip to content

Instantly share code, notes, and snippets.

@Smilin-Dominator
Last active July 30, 2021 17:58
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Smilin-Dominator/4973cb496c5c7d55cf7c81580b01a7f8 to your computer and use it in GitHub Desktop.
Codecademy Tic-Tac-Toe Challenge
#include <iostream>
#include "vector"
void draw(std::vector<char> board) {
std::cout << "\n\n0|1|2\n3|4|5\n6|7|8\n\n";
std::cout << " | | \n";
std::cout << " " << board[0] << " | " << board[1] << " | " << board[2] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << board[3] << " | " << board[4] << " | " << board[5] << "\n";
std::cout << "_____|_____|_____ \n";
std::cout << " | | \n";
std::cout << " " << board[6] << " | " << board[7] << " | " << board[8] << "\n";
std::cout << " | | \n\n\n";
std::cout << "\n";
}
bool checkpos(char pos1, char pos2, char pos3, char pos4, char pos5, char pos6, char pos7, char pos8, char pos9, char t) {
bool p123 = (pos1 == t) && (pos2 == t) && (pos3 == t);
bool p456 = (pos4 == t) && (pos5 == t) && (pos6 == t);
bool p789 = (pos7 == t) && (pos8 == t) && (pos9 == t);
bool p147 = (pos1 == t) && (pos4 == t) && (pos7 == t);
bool p258 = (pos2 == t) && (pos5 == t) && (pos8 == t);
bool p369 = (pos3 == t) && (pos6 == t) && (pos9 == t);
bool p357 = (pos3 == t) && (pos5 == t) && (pos7 == t);
bool p159 = (pos1 == t) && (pos5 == t) && (pos9 == t);
if (p123 || p456 || p789 || p147 || p258 || p369 || p357 || p159) {
return true;
} else {
return false;
}
}
bool endgame(std::vector<char> board) {
// Declaring vars for pos
char pos1 = board[0];
char pos2 = board[1];
char pos3 = board[2];
char pos4 = board[3];
char pos5 = board[4];
char pos6 = board[5];
char pos7 = board[6];
char pos8 = board[7];
char pos9 = board[8];
// x and o
char x = 'X';
char o = 'O';
bool xWon = checkpos(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, x);
bool oWon = checkpos(pos1, pos2, pos3, pos4, pos5, pos6, pos7, pos8, pos9, o);
if (xWon) {
std::cout << "X Won!";
draw(board);
return true;
} else if (oWon) {
std::cout << "O Won!";
draw(board);
return true;
} else {
return false;
}
}
std::vector<char> playerOne(std::vector<char> board) {
int pos;
char letter = 'X';
bool there = false;
while (!there) {
std::cout << "Player (X) Where Would You Like To Place X? (0-8): ";
std::cin >> pos;
if (board[pos] == '-') {
board[pos] = letter;
there = true;
} else {
std::cout << "Invalid Position, Try Again.." << std::endl;
there = false;
}
}
return board;
}
std::vector<char> playerTwo(std::vector<char> board) {
int pos;
bool there = false;
char letter = 'O';
while (!there) {
std::cout << "Player (O) Where Would You Like To Place O? (0-8): ";
std::cin >> pos;
if (board[pos] == '-') {
board[pos] = letter;
there = true;
} else {
std::cout << "Invalid Position, Try Again.." << std::endl;
there = false;
}
}
return board;
}
int main() {
std::cout << "\n[ Devisha's Tic-Tac-Toe Program! ]\n\n";
bool won = false;
std::vector<char> board = {'-','-','-','-','-','-','-','-','-'};
while (!won) {
draw(board);
board = playerOne(board);
won = endgame(board);
if (won) {
won = true;
} else {
draw(board);
board = playerTwo(board);
won = endgame(board);
if (won) {
won = true;
} else {
won = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment