Skip to content

Instantly share code, notes, and snippets.

@addisaden
Last active August 29, 2015 14:01
Show Gist options
  • Save addisaden/b648ec64913db4233509 to your computer and use it in GitHub Desktop.
Save addisaden/b648ec64913db4233509 to your computer and use it in GitHub Desktop.
#include <stdio.h>
const char gamer[3] = "XO";
int same_size(char a[], char b[])
{
int i;
for(i = 0;; i++) {
if(a[i] == '\0' || b[i] == '\0') {
return a[i] == b[i];
}
}
}
void printer(char game[], char selector[])
{
if(!same_size(game, selector)) {
puts("TicTacToe printer input is not correct.");
return;
}
printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
puts("tic tac toe\n");
char charrow[] = " %c | %c | %c | %c %c %c\n";
char placer[] = "---+---+--- |";
int i;
for(i = 0; i < 3; i++) {
printf(charrow,
game[3*i], game[3*i+1], game[3*i+2],
selector[3*i], selector[3*i+1], selector[3*i+2]);
if(i < 2)
puts(placer);
}
puts("");
}
int select(char game[], char selector[], int runde)
{
if(!same_size(game, selector)) {
puts("select input is wrong.");
return -1;
}
char selected[2];
int selected_pos = -1;
int dead_game = 1;
int i;
for(i = 0; game[i] != '\0'; i++) {
if(game[i] == ' ') {
dead_game = 0;
break;
}
}
while(!dead_game) {
printf("\n%c ist am Zug\n\n", gamer[runde%2]);
printf("Bitte wählen Sie eine Position: ");
scanf("%1s", selected);
int match = 0;
for(i = 0; selector[i] != '\0'; i++) {
if(selector[i] == selected[0]) {
selected_pos = i;
match = 1;
break;
}
}
if(!match)
puts("\nDas Feld existiert nicht.");
else if(game[selected_pos] != ' ') {
puts("\nDas Feld ist belegt.");
match = 0;
}
if(match) break;
}
return selected_pos;
}
char winner(char game[])
{
if(!same_size(game, "123456789")) {
puts("winner input has not the correct size");
return '\0';
}
int winnerstrikes[8][3] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6}
};
int i;
for(i = 0; i < 8; i++) {
if(game[winnerstrikes[i][0]] == game[winnerstrikes[i][1]] &&
game[winnerstrikes[i][0]] == game[winnerstrikes[i][2]] &&
game[winnerstrikes[i][0]] != ' ') {
return game[winnerstrikes[i][0]];
}
}
for(i = 0; i < 9; i++) {
if(game[i] == ' ') {
return ' ';
}
}
return '\0';
}
int ki(char game[], int runde)
{
char my = runde%2;
int my_char = gamer[my];
int i, j;
int selecting[9] = {0, 0, 0, 0, 0, 0, 0, 0, 0};
int gamefield[9] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
for(i = 0; i < 9; i++) {
if(game[i] != ' ') {
if(game[i] == my_char)
gamefield[i] = 2;
else
gamefield[i] = 0;
}
}
int combi[8][3] = {
{0, 1, 2},
{3, 4, 5},
{6, 7, 8},
{0, 3, 6},
{1, 4, 7},
{2, 5, 8},
{0, 4, 8},
{2, 4, 6}
};
for(i = 0; i < 8; i++) {
for(j = 0; j < 3; j++) {
int s = combi[i][j];
if(gamefield[s] != 1) continue;
int s1 = gamefield[combi[i][(j + 1) % 3]];
int s2 = gamefield[combi[i][(j + 2) % 3]];
// X X 32
// O O 16
// X - 8
// - X 8
// - O 4
// O - 4
// - - 2
// O X 1
// X O 1
//
// 2 == X == MY
// 0 == O == Other
// 1 == - == Empty
if (s1 == 2 && s2 == 2) selecting[s] += 32;
if (s1 == 0 && s2 == 0) selecting[s] += 16;
if ((s1 == 2 && s2 == 1) || (s1 == 1 && s2 == 2)) selecting[s] += 8;
if ((s1 == 0 && s2 == 1) || (s1 == 1 && s2 == 0)) selecting[s] += 4;
if (s1 == 1 && s2 == 1) selecting[s] += 2;
if ((s1 == 0 && s2 == 2) || (s1 == 2 && s2 == 0)) selecting[s] += 1;
}
}
int choosed = -1;
for(i = 0; i < 9; i++) {
if(gamefield[i] == 1) {
if(choosed == -1 || selecting[i] > selecting[choosed])
choosed = i;
}
}
return choosed;
}
int main()
{
char game[10] = " ";
char selector[10] = "wersdfxcv";
int runde = 0;
while(winner(game) == ' ') {
printer(game, selector);
int selected;
if(runde%2 == 0)
selected = select(game, selector, runde);
else
selected = ki(game, runde);
if(selected >= 0 && game[selected] == ' ') {
game[selected] = gamer[runde++ % 2];
}
}
printer(game, selector);
switch(winner(game)) {
case 'X':
case 'O':
printf("Spieler %c hat gewonnen.\n", winner(game));
break;
case '\0':
puts("Es ist unentschieden.");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment