Created
October 5, 2018 17:25
-
-
Save mastrobirraio/9174f9bbf79f4dafe11431f5c7d3f81b to your computer and use it in GitHub Desktop.
A simple tic tac toe game
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 <cstdlib> | |
#include <iostream> | |
using namespace std; | |
void board(); | |
void player_choose(int player); | |
bool check_winner(int player); | |
void show_game(int player_winner); | |
char get_value(char mark); | |
char game_matrix[3][3] = { | |
'1', '2', '3', | |
'4', '5', '6', | |
'7', '8', '9' | |
}; | |
int main() { | |
int player = 1; | |
int old_player; | |
int turns = 1; | |
do { | |
board(); | |
player_choose(player); | |
old_player = player; | |
player = (player == 1)?2:1; | |
turns++; | |
if(turns == 10) { | |
old_player = 0; | |
} | |
} while(!check_winner(old_player) && turns < 10); | |
show_game(old_player); | |
return 0; | |
} | |
void board() { | |
system("clear"); | |
cout << " | | " << endl; | |
for(size_t r=0; r<3; r++) { | |
cout << " " << game_matrix[r][0] << " | " << game_matrix[r][1] << " | " << game_matrix[r][2] << endl; | |
cout << "_____|_____|_____" << endl; | |
cout << " | | " << endl; | |
} | |
} | |
void player_choose(int player) { | |
char choose; | |
char mark = (player == 1)?'X':'O'; | |
cout << "Inserisci " << mark << ": "; | |
cin >> choose; | |
for(size_t r=0; r<3; r++) { | |
for(size_t c=0; c<3; c++) { | |
if(game_matrix[r][c] == choose) { | |
game_matrix[r][c] = mark; | |
} | |
} | |
} | |
} | |
bool check_winner(int player) { | |
bool winner = false; | |
char mark = (player == 1)?'X':'O'; | |
for(size_t r=0; r<3; r++) { | |
int count = 0; | |
for(size_t c=0; c<3; c++) { | |
if(game_matrix[r][c] == mark) { | |
count++; | |
} | |
} | |
if(count == 3) { | |
winner = true; | |
break; | |
} | |
} | |
if(!winner) { | |
for(size_t r=0; r<3; r++) { | |
int count = 0; | |
for(size_t c=0; c<3; c++) { | |
if(game_matrix[c][r] == mark) { | |
count++; | |
} | |
} | |
if(count == 3) { | |
winner = true; | |
break; | |
} | |
} | |
} | |
if(!winner) { | |
int count = 0; | |
for(size_t i=0; i<3; i++) { | |
if(game_matrix[i][i] == mark) { | |
count++; | |
} | |
if(count == 3) { | |
winner = true; | |
break; | |
} | |
} | |
} | |
if(!winner) { | |
int count = 0; | |
for(size_t c=2, r=0; c>0, r<3; c--, r++) { | |
if(game_matrix[r][c] == mark) { | |
count++; | |
} | |
if(count == 3) { | |
winner = true; | |
break; | |
} | |
} | |
} | |
return winner; | |
} | |
void show_game(int winner) { | |
system("clear"); | |
if(winner == 0) { | |
cout << "TIE!!!!" << endl; | |
} else { | |
cout << "Winner player " << winner << endl; | |
} | |
cout << " | | " << endl; | |
for(size_t r=0; r<3; r++) { | |
char cell1 = get_value(game_matrix[r][0]); | |
char cell2 = get_value(game_matrix[r][1]); | |
char cell3 = get_value(game_matrix[r][2]); | |
cout << " " << cell1 << " | " << cell2 << " | " << cell3 << endl; | |
cout << "_____|_____|_____" << endl; | |
cout << " | | " << endl; | |
} | |
} | |
char get_value(char mark) { | |
if(mark == 'X' || mark == 'O') { | |
return mark; | |
} else { | |
return ' '; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment