Created
November 17, 2017 16:21
-
-
Save winhtut/d3ce339df11f430bda5c617840f4fe73 to your computer and use it in GitHub Desktop.
Tic Tac Toe V2.0
This file contains hidden or 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<conio.h> | |
#include<stdio.h> | |
#include<Windows.h> | |
#include<stdlib.h> | |
void draw(); | |
void input(); | |
char turnplayer(char player1); | |
char win(); | |
using namespace std; | |
char matrix[3][3] = { '1','2','3','4','5','6','7','8','9' }; | |
char player = 'x'; | |
int n = 0; | |
void draw() { | |
cout << "Tic Tac Toe V2.0>>>" << endl; | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
cout << matrix[i] [j]<<" "; | |
} | |
cout << endl; | |
} | |
} | |
int main() { | |
n = 0; | |
draw(); | |
while (1) | |
{ | |
n++; | |
input(); | |
system("cls"); | |
draw(); | |
if (win() == 'x') { | |
cout << "x win!" << endl; | |
system("color 31"); | |
break; | |
} | |
else if (win() == '#') { | |
cout << " # win "<<endl; | |
system("color 31"); | |
break; | |
} | |
else if (win() == '/'&& n == 9) { | |
cout << "It is a draw !" << endl; | |
break; | |
} | |
player=turnplayer(player); | |
} | |
system("pause"); | |
return 0; | |
} | |
void input() { | |
int a = 0; | |
cout << "It's " << player << "turn" << endl; | |
cout << "Press the number you want to field :"; | |
cin >> a; | |
if (a == 1) { | |
if (matrix[0][0] == '1') | |
matrix[0][0] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 2) { | |
if (matrix[0][1] == '2') | |
matrix[0][1] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 3) { | |
if (matrix[0][3] == '3') | |
matrix[0][3] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 4) { | |
if (matrix[1][0] == '4') | |
matrix[1][0] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 5) { | |
if (matrix[1][1] == '5') | |
matrix[1][1] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 6) { | |
if (matrix[1][2] == '6') | |
matrix[1][2] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 7) { | |
if (matrix[2][0] == '7') | |
matrix[2][0] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 8) { | |
if (matrix[2][1] == '8') | |
matrix[2][1] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
else if (a == 9) { | |
if (matrix[2][2] == '9') | |
matrix[2][2] = player; | |
else { | |
cout << "The place is busy" << endl; | |
input(); | |
} | |
} | |
} | |
char turnplayer(char player1) { | |
if (player1 =='x') | |
{ | |
player1 = '#'; | |
return player1; | |
} | |
if (player1=='#') | |
{ | |
player1 = 'x'; | |
return player1; | |
} | |
} | |
char win() { | |
//first winner | |
if (matrix[0][0]=='x' && matrix[0][1] == 'x' && matrix[0][2] == 'x') | |
return 'x'; | |
else if (matrix[1][0] == 'x' && matrix[1][1] == 'x' && matrix[1][2] == 'x') | |
return 'x'; | |
else if (matrix[2][0] == 'x' && matrix[2][1] == 'x' && matrix[2][2] == 'x') | |
return 'x'; | |
else if (matrix[0][0] == 'x' && matrix[1][1] == 'x' && matrix[2][2] == 'x') | |
return 'x'; | |
else if (matrix[0][0] == 'x' && matrix[1][0] == 'x' && matrix[2][0] == 'x') | |
return 'x'; | |
else if (matrix[0][1] == 'x' && matrix[1][1] == 'x' && matrix[2][1] == 'x') | |
return 'x'; | |
else if (matrix[0][2] == 'x' && matrix[1][2] == 'x' && matrix[2][2] == 'x') | |
return 'x'; | |
else if (matrix[2][0] == 'x' && matrix[1][1] == 'x' && matrix[0][2] == 'x') | |
return 'x'; | |
//second winner | |
else if (matrix[0][0] == '#' && matrix[0][1] == '#' && matrix[0][2] == '#') | |
return '#'; | |
else if (matrix[1][0] == '#' && matrix[1][1] == '#' && matrix[1][2] == '#') | |
return '#'; | |
else if (matrix[2][0] == '#' && matrix[2][1] == '#' && matrix[2][2] == '#') | |
return '#'; | |
else if (matrix[0][0] == '#' && matrix[1][0] == '#' && matrix[2][0] == '#') | |
return '#'; | |
else if (matrix[0][1] == '#' && matrix[1][1] == '#' && matrix[2][1] == '#') | |
return '#'; | |
else if (matrix[0][2] == '#' && matrix[1][2] == '#' && matrix[2][2] == '#') | |
return '#'; | |
else if (matrix[0][0] == '#' && matrix[1][1] == '#' && matrix[2][2] == '#') | |
return '#'; | |
else if(matrix[2][0] == '#' && matrix[1][1] == '#' && matrix[0][2] == '#') | |
return '#'; | |
return '/'; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment