Skip to content

Instantly share code, notes, and snippets.

@winhtut
Created November 14, 2017 05:06
Show Gist options
  • Save winhtut/933d503facb5dba3677d4cd931230f6d to your computer and use it in GitHub Desktop.
Save winhtut/933d503facb5dba3677d4cd931230f6d to your computer and use it in GitHub Desktop.
tic tac toe
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<Windows.h>
void draw();
void input();
void turnplayer();
char win();
using namespace std;
char matrix[3][3] = { '1','2','3','4','5','6','7','8','9' };
char player = 'x';
void draw() {
cout << "Tic Tac Toe V1.0>>>" << endl;
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
cout << matrix[i] [j]<<" ";
}
cout << endl;
}
}
int main() {
system("cls");
draw();
while (1)
{
input();
draw();
if (win() == 'x') {
cout << "x win!" << endl;
system("color 31");
break;
}
else if (win() == '#') {
cout << " # win "<<endl;
system("color 31");
break;
}
turnplayer();
}
system("pause");
return 0;
}
void input() {
int a = 0;
cout << "Press the number you want to field :";
cin >> a;
if (a == 1)
matrix[0][0] = player;
else if (a == 2)
matrix[0][1] = player;
else if (a == 3)
matrix[0][2] = player;
else if (a == 4)
matrix[1][0] = player;
else if (a == 5)
matrix[1][1] = player;
else if (a == 6)
matrix[1][2] = player;
else if (a == 7)
matrix[2][0] = player;
else if (a == 8)
matrix[2][1]=player;
else if (a == 9)
matrix[2][2] = player;
}
void turnplayer() {
if (player = 'x')
player = '#';
else
player = 'x';
}
char win() {
//first winner
if (matrix[0][0]=='x' && matrix[0][1] == 'x' && matrix[0][2] == 'x')
return 'x';
if (matrix[1][0] == 'x' && matrix[1][1] == 'x' && matrix[1][2] == 'x')
return 'x';
if (matrix[2][0] == 'x' && matrix[2][1] == 'x' && matrix[2][2] == 'x')
return 'x';
if (matrix[0][0] == 'x' && matrix[1][0] == 'x' && matrix[2][2] == 'x')
return 'x';
if (matrix[0][0] == 'x' && matrix[1][0] == 'x' && matrix[2][2] == 'x')
return 'x';
if (matrix[0][1] == 'x' && matrix[1][1] == 'x' && matrix[2][1] == 'x')
return 'x';
if (matrix[0][2] == 'x' && matrix[1][2] == 'x' && matrix[2][2] == 'x')
return 'x';
if (matrix[0][0] == 'x' && matrix[1][1] == 'x' && matrix[2][2] == 'x')
return 'x';
if (matrix[2][0] == 'x' && matrix[1][1] == 'x' && matrix[0][2] == 'x')
return 'x';
//second winner
if (matrix[0][0] == '#' && matrix[0][1] == '#' && matrix[0][2] == '#')
return 'x';
if (matrix[1][0] == '#' && matrix[1][1] == '#' && matrix[1][0] == '#')
return 'x';
if (matrix[2][0] == '#' && matrix[2][1] == '#' && matrix[2][0] == '#')
return 'x';
if (matrix[0][0] == '#' && matrix[1][0] == '#' && matrix[2][0] == '#')
return 'x';
if (matrix[0][0] == '#' && matrix[1][0] == '#' && matrix[2][0] == '#')
return 'x';
if (matrix[0][1] == '#' && matrix[1][1] == '#' && matrix[2][1] == '#')
return 'x';
if (matrix[0][2] == '#' && matrix[1][2] == '#' && matrix[2][2] == '#')
return 'x';
if (matrix[0][0] == '#' && matrix[1][1] == '#' && matrix[2][2] == '#')
return 'x';
if (matrix[2][0] == '#' && matrix[1][1] == '#' && matrix[0][2] == '#')
return 'x';
return '/';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment