Skip to content

Instantly share code, notes, and snippets.

@codecademydev
Created May 17, 2020 21:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codecademydev/408a0617611975785662984e9496e98b to your computer and use it in GitHub Desktop.
Save codecademydev/408a0617611975785662984e9496e98b to your computer and use it in GitHub Desktop.
Codecademy export
#include <iostream>
#include "ttt.hpp"
#include <vector>
#include <array>
#include <ctime>
#include <cstdlib>
int main() {
bool won_or_lose;
int selection;
int selection_2;
int board[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
bool winner = false;
bool board_filled = false;
int x = 0;
draw(board);
srand(time(0));
int coin_flip = (rand () % 2) + 1;
if (coin_flip = 1) {
std::cout << "Youngest player goes first. What square would you like to claim?\n";
} else {
std::cout << "Oldest player goes first. Type 1 to be player 1. What square would you like to claim?\n";
}
while (winner == false && board_filled == false) {
turn1(selection);
draw(board);
wincheck(winner, x);
filledcheck(board_filled);
if (winner == true) {
endwin();
return 0;
}
if (board_filled == true) {
endtie();
return 0;
}
x++;
question();
turn2(selection);
draw(board);
wincheck(winner, x);
filledcheck(board_filled);
if (winner == true) {
endwin();
return 0;
}
if (board_filled == true) {
endtie();
return 0;
}
x++;
question();
}
}
#include <array>
void draw(int board[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9});
void question();
void turn1(int selection);
void turn2(int selection);
void endwin();
void endtie();
void wincheck(bool winner, int x);
void filledcheck(bool board_filled);
#include <iostream>
#include <vector>
#include <array>
void draw(int board[9] = {1, 2, 3, 4, 5, 6, 7, 8, 9}) {
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";
std::cout << "\n";
}
void question() {
std::cout << "What square would you like to claim?\n";
}
void turn1(int selection) {
std::cin >> selection;
board[selection - 1] = 0;
}
void turn2(int selection;) {
std::cin >> selection;
board[selection - 1] = 11;
}
void endwin() {
std::cout << "YOU WIN!\n";
}
void endtie() {
std::cout << "TIE GAME\n";
}
void wincheck(bool winner, int x) {
// This is a monstrosity. I am sorry for anyone who has to look at this.
if ((board[0] == 0 && board[1] == 0 && board[2] == 0 || board[3] == 0 && board[4] == 0 && board[5] == 0 || board[6] == 0 && board[7] == 0 && board[8] == 0 || board[0] == 0 && board[3] == 0 && board[6] == 0 || board[1] == 0 && board[4] == 0 && board[7] == 0 || board[2] == 0 && board[5] == 0 && board[8] == 0 || board[0] == 0 && board[4] == 0 && board[8] == 0 || board[2] == 0 && board[4] == 0 && board[6] == 0) || (board[0] == 11 && board[1] == 11 && board[2] == 11 || board[3] == 11 && board[4] == 11 && board[5] == 11 || board[6] == 11 && board[7] == 11 && board[8] == 11 || board[0] == 11 && board[3] == 11 && board[6] == 11 || board[1] == 11 && board[4] == 11 && board[7] == 11 || board[2] == 11 && board[5] == 11 && board[8] == 11 || board[0] == 11 && board[4] == 11 && board[8] == 11 || board[2] == 11 && board[4] == 11 && board[6] == 11)) {
winner = true;
} else {
winner = false;
}
}
x = 0;
void filledcheck(bool board_filled) {
if (x == 9) {
board_filled = true;
} else {
board_filled = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment