Skip to content

Instantly share code, notes, and snippets.

@egcodes
Created August 15, 2013 21:53
Show Gist options
  • Save egcodes/6245323 to your computer and use it in GitHub Desktop.
Save egcodes/6245323 to your computer and use it in GitHub Desktop.
/*************************************/
// Program Name : Tic Tac Toe
// Version : 1.0.5
// Start : 17.06.2010
// Update : 20.01.2011
// Author : egcodes (www.egcodes.blogspot.com)
/*************************************/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void print_game(char *a, int size);
void print_result(int result);
int check_status(char *a, int size);
void AI_Machine(char *a, int size);
int main()
{
char *a;
int k, ch;
int size = 3;
srand((unsigned int)time(0));
if ((a = (char *)malloc(size * size * sizeof(char))) == NULL) {
printf("Cannot allocate memory...\n");
exit(EXIT_FAILURE);
}
for (k = 0; k < size * size; k++)
a[k] = '.';
while (1) {
//Oyuncu oynuyor
print_game(a, size);
while (1) {
printf("Secim : ");
scanf("%d", &ch);
getchar();
if (a[ch - 1] == 'X' || a[ch - 1] == 'O')
printf("Girdiğiniz alan dolu...\n");
else {
a[ch - 1] = 'X';
break;
}
}
print_game(a, size);
print_result(check_status(a, size));
//Bilgisayar oynuyor
AI_Machine(a, 3);
print_game(a, size);
print_result(check_status(a, size));
}
return 0;
}
void print_game(char *a, int size)
{
int k;
system("clear");
printf("Tic Tac Toe\n");
printf("===========\n\n");
printf("-------------------------------------\n");
for (k = 0; k < size * size; k++) {
if (k % size == 0)
printf("\n\n");
printf("%-10c ", a[k]);
}
printf("\n\n-------------------------------------\n");
}
void print_result(int result)
{
if (result == 1) {
printf("Tebrikler kazandınız...\n");
getchar();
exit(EXIT_SUCCESS);
}
else if (result == 2) {
printf("Uzgunum kaybettiniz...\n");
getchar();
exit(EXIT_SUCCESS);
}
else if (result == 3) {
printf("Oyun berabere bitti...\n");
getchar();
exit(EXIT_SUCCESS);
}
else
;
}
int check_status(char *a, int size)
{
int line, col;
int counterX, counterO;
int n_time;
int n2_time;
int k;
//Yatay kontrol
n_time = size;
for (line = 1 ; n_time-- ; line += size) {
counterX = 0, counterO = 0;
n2_time = size;
for (col = line; n2_time-- ; col++) {
switch (a[col - 1]) {
case 'X' : counterX++;
if (counterX == size)
return 1;
break;
case 'O' : counterO++;
if (counterO == size)
return 2;
break;
}
}
}
//Dikey kontrol
n_time = size;
for (col = 1 ; n_time-- ; col++) {
counterX = 0, counterO = 0;
n2_time = size;
for (line = col; n2_time-- ; line += size) {
switch (a[line - 1]) {
case 'X' : counterX++;
if (counterX == size)
return 1;
break;
case 'O' : counterO++;
if (counterO == size)
return 2;
break;
}
}
}
//Capraz kontrol (Soldan saga)
n_time = size;
counterX = 0, counterO = 0;
for (line = 1; n_time-- ; line += size + 1) {
switch (a[line - 1]) {
case 'X' : counterX++;
if (counterX == size)
return 1;
break;
case 'O' : counterO++;
if (counterO == size)
return 2;
break;
}
}
//Capraz kontrol (Sagdan sola)
n_time = size;
counterX = 0, counterO = 0;
for (line = size; n_time-- ; line += size - 1) {
switch (a[line - 1]) {
case 'X' : counterX++;
if (counterX == size)
return 1;
break;
case 'O' : counterO++;
if (counterO == size)
return 2;
break;
}
}
//Oyun bitti mi
for (k = 0; k < size * size; k++)
if (a[k] == '.')
return 0;
//Beraberlik kontrol
return 3;
}
void AI_Machine(char *a, int size)
{
int line, col;
int n_time, n2_time;
int counter;
int random;
//Ozel Stratejiler
if (size == 3 && a[size + 1] == '.') {
a[size + 1] = 'O';
return ;
}
//Atak Stratejileri
//Yatay kontrol
n_time = size;
for (line = 1 ; n_time--; line += size) {
counter = 0, n2_time = size;
for (col = line; n2_time--; col++) {
if (a[col - 1] == 'O') {
counter++;
continue;
}
if (a[col - 1] == 'X')
counter--;
}
if (counter == size - 1) {
col = line - 1;
while (a[col] != '.')
col++;
a[col] = 'O';
return ;
}
}
//Dikey kontrol
n_time = size;
for (col = 1 ; n_time-- ; col++) {
counter = 0, n2_time = size;
for (line = col; n2_time-- ; line += size) {
if (a[line - 1] == 'O') {
counter++;
continue;
}
if (a[line - 1] == 'X')
counter--;
}
if (counter == size - 1) {
line = col - 1;
while (a[line] != '.')
line += size;
a[line] = 'O';
return ;
}
}
//Capraz kontrol - Soldan saga
counter = 0, n2_time = size;
for (col = 1; n2_time--; col += size + 1) {
if (a[col - 1] == 'O') {
counter++;
continue;
}
if (a[col - 1] == 'X')
counter--;
}
if (counter == size - 1) {
col = 1;
while (a[col - 1] != '.')
col += size + 1;
a[col - 1] = 'O';
return ;
}
//Capraz kontrol - Sagdan sola
counter = 0, n2_time = size;
for (col = size; n2_time--; col += size - 1) {
if (a[col - 1] == 'O') {
counter++;
continue;
}
if (a[col - 1] == 'X')
counter--;
}
if (counter == size - 1) {
col = size ;
while (a[col - 1] != '.')
col += size - 1;
a[col - 1] = 'O';
return ;
}
//Savunma Stratejileri
//Yatay kontrol
n_time = size;
for (line = 1 ; n_time--; line += size) {
counter = 0, n2_time = size;
for (col = line; n2_time--; col++) {
if (a[col - 1] == 'X') {
counter++;
continue;
}
if (a[col - 1] == 'O')
counter--;
}
if (counter == size - 1) {
col = line - 1;
while (a[col] != '.')
col++;
a[col] = 'O';
return ;
}
}
//Dikey kontrol
n_time = size;
for (col = 1 ; n_time-- ; col++) {
counter = 0, n2_time = size;
for (line = col; n2_time-- ; line += size) {
if (a[line - 1] == 'X') {
counter++;
continue;
}
if (a[line - 1] == 'O')
counter--;
}
if (counter == size - 1) {
line = col - 1;
while (a[line] != '.')
line += size;
a[line] = 'O';
return ;
}
}
//Capraz kontrol - Soldan saga
counter = 0, n2_time = size;
for (col = 1; n2_time--; col += size + 1) {
if (a[col - 1] == 'X') {
counter++;
continue;
}
if (a[col - 1] == 'O')
counter--;
}
if (counter == size - 1) {
col = 1;
while (a[col - 1] != '.')
col += size + 1;
a[col - 1] = 'O';
return ;
}
//Capraz kontrol - Sagdan sola
counter = 0, n2_time = size;
for (col = size; n2_time--; col += size - 1) {
if (a[col - 1] == 'X') {
counter++;
continue;
}
if (a[col - 1] == 'O')
counter--;
}
if (counter == size - 1) {
col = size ;
while (a[col - 1] != '.')
col += size - 1;
a[col - 1] = 'O';
return ;
}
//Atak ve savunma disinda rastgele secim
while (a[random = rand() % (size * size)] != '.')
;
a[random] = 'O';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment