Skip to content

Instantly share code, notes, and snippets.

@riemass
Created May 7, 2020 10:40
Show Gist options
  • Select an option

  • Save riemass/e91aac579f61050883c1e6d7b4b1fa93 to your computer and use it in GitHub Desktop.

Select an option

Save riemass/e91aac579f61050883c1e6d7b4b1fa93 to your computer and use it in GitHub Desktop.
#include <stdio.h>
/*
x | |
------------
| x |
------------
| o |
*/
void print_teren(char teren[][3]);
void human_player(char teren[][3], char sim);
void ai_player(char teren[][3], char sim);
int player_wins(char teren[][3], char sim);
// Unos se treba validirati prije poteza
// Omoguciti izbor igrac-igrac ili igrac-racunar
// AI igrac da bude malo pametniji
int main() {
char teren[3][3] = {
{' ', ' ', ' '},
{' ', ' ', ' '},
{' ', ' ', ' '},
};
print_teren(teren);
while (1) {
printf("Igrac 1\n");
human_player(teren, 'x');
print_teren(teren);
if(player_wins(teren, 'x')) {
printf("Player 1 WINS\n");
break;
}
printf("Igrac 2\n");
ai_player(teren, 'o');
print_teren(teren);
if(player_wins(teren, 'o')) {
printf("Player 2 WINS\n");
break;
}
}
return 0;
}
int player_wins(char teren[][3], char sim) {
int i;
for (i = 0; i < 2; i++) {
if (teren[0][i] == sim && teren[1][i] == sim && teren[2][i] == sim)
return 1;
if (teren[i][0] == sim && teren[i][1] == sim && teren[i][2] == sim)
return 1;
}
if (teren[0][0] == sim && teren[1][1] == sim && teren[2][2] == sim)
return 1;
if (teren[0][2] == sim && teren[1][1] == sim && teren[2][0] == sim)
return 1;
return 0;
}
void human_player(char teren[][3], char sim) {
int x, y;
printf("Unesite lokaciju: ");
scanf("%d %d", &x, &y);
teren[x][y] = sim;
}
void ai_player(char teren[][3], char sim) {
int i, j;
for (i = 0; i < 2; i++) {
for (j = 0; j < 2; j++) {
if (teren[i][j] == ' ') {
teren[i][j] = sim;
return;
}
}
}
}
void print_teren(char teren[][3]) {
int i, j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
printf(" %c ", teren[i][j]);
if (j != 2) putchar('|');
}
if (i != 2) printf("\n-----------\n");
}
putchar('\n');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment