-
-
Save codecademydev/7a2c40f419cfe2ba57d596589eefea83 to your computer and use it in GitHub Desktop.
Codecademy export
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "functions.hpp" | |
void introduction() { | |
std::cout << "\n==============\nWelcome to the Tic-Tac-Toe game!\n==============\n"; | |
std::cout << "In this game, you will play a paper-and-pencil game for two players (represented by the X and O letter) in a 3x3 grid. The first to place their marks in a vertical/horizontal/diagonal row wins the game.\n==============\n"; | |
} | |
void end_game(char mark) { | |
std::cout << "\nWhoohoo! Player with the" << mark << " symbol has won the game! Congratulations!\n"; | |
} | |
void show_table(char positions[9]) { | |
std::cout << "\n=========\n"; | |
std::cout << positions[0] " | " << positions[1] << " | " << positions[2] << "\n"; | |
std::cout << "__ ___ __\n"; | |
std::cout << positions[3] << " | " << positions[4] << " | " << positions[5] << "\n"; | |
std::cout << "__ ___ __\n"; | |
std::cout << positions[6] << " | " << positions[7] << " | " << positions[8] << "\n"; | |
std::cout << "=========\n"; | |
} | |
void choose_mark(int move, char &mark) { | |
if((move%2) == 0) mark = "O"; | |
else mark = "X"; | |
} | |
void choose_position(char positions[9], char mark, int &move) { | |
int number; | |
std::cout << "\n==========\nWhich positions do you want to choose?\nValid positions are: \n"; | |
for(int i = 0; i <= 8; i++) { | |
if(positions[i] == ' ') { | |
std::cout << i+1 << " - "; | |
} | |
} | |
std::cout << "\nYour position is: "; | |
std::cin >> number; | |
while(positions[number-1] != ' ') { | |
std::cout << "Invalid position! Please choose again.\nYour position is: "; | |
std::cin >> number; | |
} | |
if(positions[number-1] == ' ') { | |
positions[number-1] = mark; | |
move++; | |
choose_mark(move, mark); | |
} | |
} | |
bool win_condition(char positions[9], char &mark) { | |
bool win = false; | |
// Horizontal directions | |
if((positions[0]==positions[1]) && (positions[1]==positions[2])) { | |
mark = positions[0]; | |
win = true; | |
} | |
if((positions[3]==positions[4]) && (positions[4]==positions[5])) { | |
mark = positions[3]; | |
win = true; | |
} | |
if((positions[6]==positions[7]) && (positions[7]==positions[8])) { | |
mark = positions[6]; | |
win = true; | |
} | |
// Vertical directions | |
if((positions[0]==positions[3]) && (positions[3]==positions[6])) { | |
mark = positions[0]; | |
win = true; | |
} | |
if((positions[1]==positions[4]) && (positions[4]==positions[7])) { | |
mark = positions[1]; | |
win = true; | |
} | |
if((positions[2]==positions[5]) && (positions[5]==positions[8])) { | |
mark = positions[2]; | |
win = true; | |
} | |
// Diagonal directions | |
if((positions[0]==positions[4]) && (positions[4]==positions[8])) { | |
mark = positions[0]; | |
win = true; | |
} | |
if((positions[2]==positions[4]) && (positions[4]==positions[7])) { | |
mark = positions[2]; | |
win = true | |
} | |
return win; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
void introduction(); | |
void end_game(char mark); | |
void show_table(char positions[9]); | |
void choose_position(char positions[9], char mark, int &move); | |
void choose_mark(int move, char &mark); | |
bool win_condition(char positions[9], char &mark); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include "functions.hpp" | |
int main() { | |
int move = 0; | |
char mark; | |
char positions[9] = {" ", " ", " ", " ", " ", " ", " ", " ", " "}; | |
introduction(); | |
show_table(positions); | |
while(win_condition(positions[9], mark) != true) { | |
choose_position(positions, mark, move); | |
show_table(positions); | |
win_condition(positions, mark); | |
} | |
end_game(mark); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment