Skip to content

Instantly share code, notes, and snippets.

@winhtut
Created November 14, 2017 08:59
Show Gist options
  • Save winhtut/9cbf62d4c3ac1b5f680cb944b29cc4d7 to your computer and use it in GitHub Desktop.
Save winhtut/9cbf62d4c3ac1b5f680cb944b29cc4d7 to your computer and use it in GitHub Desktop.
tic tac toe
#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';
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() {
draw();
while (1)
{
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;
}
player=turnplayer(player);
}
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;
}
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