Skip to content

Instantly share code, notes, and snippets.

@krofna
Created July 5, 2014 16:50
Show Gist options
  • Save krofna/80054cedfb5f10cac700 to your computer and use it in GitHub Desktop.
Save krofna/80054cedfb5f10cac700 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
char map[3][3];
void* memset(void* s, int c, size_t n);
int p=1;
int win=0;
int input;
char turn;
void init()
{
memset(map, '.', 3 * 3 * sizeof(char));
}
void draw()
{
int x,y;
for (y=0;y<3;y++){
for (x=0;x<3;x++){
putchar(map[x][y]);
}
putchar('\n');
}
}
int provjera()
{
if(((map[0][0]==turn)&&(map[1][1]==turn)&&(map[2][2]==turn))||((map[0][2]==turn)&&(map[1][1]==turn)&&(map[2][0]==turn)))
return 0;
return 1;
}
void play()
{
do{
if (p==1) turn = 'X'; else turn = 'O';
draw();
scanf("%d",&input);
switch(input)
{
case 1: map[0][2]=turn; break;
case 2: map[1][2]=turn; break;
case 3: map[2][2]=turn; break;
case 4: map[0][1]=turn; break;
case 5: map[1][1]=turn; break;
case 6: map[2][1]=turn; break;
case 7: map[0][0]=turn; break;
case 8: map[1][0]=turn; break;
case 9: map[2][0]=turn; break;
}
if (provjera()==0) {
printf("Igrac %c je pobijedio",turn);
win=1;
}
if (p==1) p=0; else p=1;
}while(win!=1);
}
int main()
{
init();
play();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment