Created
July 13, 2014 21:14
-
-
Save krofna/f113eb6f37267a697591 to your computer and use it in GitHub Desktop.
This file contains 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 <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
int x,y,n; | |
char** ploca; | |
char** arraymalloc(size_t width, size_t height) | |
{ | |
int i; | |
/* Inicijaliziraj pokazivače na NULL! */ | |
char** array = calloc(height, sizeof(char*)); | |
if (array == NULL) | |
return NULL; | |
for (i = 0; i < height; ++i) | |
{ | |
array[i] = malloc(width); | |
if (array[i] == NULL) | |
goto error; | |
} | |
return array; | |
error: | |
for (i = 0; i < height; ++i) | |
free(array[i]); | |
free(array); | |
return NULL; | |
} | |
void arraymemset(char** array, int c, size_t width, size_t height) | |
{ | |
int i; | |
for (i = 0; i < height; ++i) | |
memset(array[i], c, width); | |
} | |
void init() | |
{ | |
scanf("%d%d", &x, &y); | |
ploca = arraymalloc(x, y); | |
arraymemset(ploca, '.', x, y); | |
scanf("%d", &n); | |
} | |
void arrayfree(char** array, size_t height) | |
{ | |
int i; | |
if (array == NULL) | |
return; | |
for (i = 0; i < height; ++i) | |
free(array[i]); | |
free(array); | |
} | |
void draw() | |
{ | |
int i,j; | |
for (i = 0; i<y; ++i) | |
{ | |
for (j = 0; j<x; ++j) | |
putchar(ploca[i][j]); | |
putchar('\n'); | |
} | |
} | |
int diagonal(char w) | |
{ | |
int i,j,h,s=0; | |
for (i=(x-1); i>=0; --i) | |
{ | |
for (h=i, j=0; h<x && j<y; ++h, ++j) | |
{ | |
if (ploca[j][h] == w) | |
++s; | |
} | |
if (s == n) | |
return 0; | |
s=0; | |
for (h=i, j=0; h>=0 && j<y; --h, ++j) | |
{ | |
if (ploca[j][h] == w) | |
++s; | |
} | |
if (s == n) | |
return 0; | |
s=0; | |
} | |
for (j=(y-1); j>=0; --j) | |
{ | |
for (h=j, i=0; h<y && i<x; ++h, ++i) | |
{ | |
if (ploca[h][i] == w) | |
++s; | |
} | |
if (s == n) | |
return 0; | |
s=0; | |
for (h=j, i=0; h>=0 && i<x; --h, ++i) | |
{ | |
if (ploca[h][i] == w) | |
++s; | |
} | |
if (s == n) | |
return 0; | |
s=0; | |
} | |
return 1; | |
} | |
int wincheck(char w) | |
{ | |
int i,j,s=0; | |
for (i=0; i<y; ++i) | |
{ | |
for(j=0; j<x; ++j) | |
{ | |
if(ploca[i][j] == w) | |
s++; | |
} | |
if(s == n) | |
return 0; | |
s=0; | |
} | |
for (i=0; i<x; ++i) | |
{ | |
for(j=0; j<y; ++j) | |
{ | |
if(ploca[j][i] == w) | |
s++; | |
} | |
if(s == n) | |
return 0; | |
s=0; | |
} | |
diagonal(w); | |
} | |
void play() | |
{ | |
int i,j,s=0; | |
char p; | |
while(1) | |
{ | |
scanf("%d%d", &i, &j); | |
if (i>=x || j>=y || i<0 || j<0 || ploca[j][i]!='.') | |
continue; | |
if (s++ % 2 == 0) | |
p='x'; | |
else | |
p='o'; | |
ploca[j][i]=p; | |
draw(); | |
if (wincheck(p) == 0) | |
{ | |
printf("Pobjedio je %c. \n", p); | |
return; | |
} | |
} | |
} | |
int main() | |
{ | |
init(); | |
draw(); | |
play(); | |
arrayfree(ploca, y); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment