Skip to content

Instantly share code, notes, and snippets.

@10maurycy10
Created November 30, 2020 16:52
Show Gist options
  • Save 10maurycy10/2d97c58f631abdf2422ed777f917c5cf to your computer and use it in GitHub Desktop.
Save 10maurycy10/2d97c58f631abdf2422ed777f917c5cf to your computer and use it in GitHub Desktop.
A tic tac toe ai.
#include <stdio.h>
#include <string.h>
int win(int board[9]) {
int wins[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(int i = 0; i < 8; ++i) {
if(board[wins[i][0]] != 0 &&
board[wins[i][0]] == board[wins[i][1]] &&
board[wins[i][0]] == board[wins[i][2]])
return board[wins[i][2]];
}
return 0;
}
char getHuman(int board[9], int i) {
switch (board[i]) {
case -1:
return 'X';
case 1:
return 'O';
case 0:
return '0' + i;
}
return 0;
}
void copyBoard(int board[9], int dest[9]) {
for (int i = 0; i < 9; i++)
dest[i] = board[i];
}
void printBoard(int board[9]) {
printf("\n%c %c %c\n",getHuman(board,0),getHuman(board,1),getHuman(board,2));
printf("%c %c %c\n",getHuman(board,3),getHuman(board,4),getHuman(board,5));
printf("%c %c %c\n\n",getHuman(board,6),getHuman(board,7),getHuman(board,8));
}
void playerMove(int board[9]) {
printf("move (0 - 8)? ");
int move;
while (!scanf("%d",&move)) {
scanf("%*[^\n]");
printBoard(board);
printf("huh?\n");
printf("move (0 - 8)? ");
}
if (move > 8 || move < 0) {
printBoard(board);
printf("nice try.\n");
playerMove(board);
} else if (board[move] != 0) {
printBoard(board);
printf("cheater.\n");
playerMove(board);
} else
board[move] = -1;
}
int minmax(int board[9], int from) {
if (win(board) != 0) return win(board)*from; //if return 1 if fails do -1
int move = -1;
int score = -2;
int temp[9];
for(int i = 0; i < 9; i++) { //for all moves
if(board[i] == 0) { //if legal
copyBoard(board,temp);
temp[i] = from; //try move
int tempScore = -minmax(temp, -from); //minmax from player
if(tempScore > score) {
score = tempScore;
move = i;
}
}
}
if (move == -1)
return 0;
return score;
}
void computerMove(int board[9]) {
int move = -1;
int score = -2;
int temp[9];
for(int i = 0; i < 9; i++) { //for all moves
if(board[i] == 0) { //if legal
copyBoard(board,temp);
temp[i] = 1; //try move
int tempScore = -minmax(temp, -1); //minmax from player
if(tempScore > score) {
score = tempScore;
move = i;
}
}
}
if (move != -1)
board[move] = 1; //move if found move
}
int main() {
int board[9] = {0,0,0,0,0,0,0,0}; //ai is 1 player is -1
printf("first(1) or second(2): ");
int player=0;
scanf("%d",&player);
for (int turn = 0; turn < 9; turn++) {
switch (win(board)) {
case 1:
printBoard(board);
printf("you loose.\n");
return 0;
break;
case -1:
printBoard(board);
printf("you win. :)\n");
return 0;
break;
}
if (player == 1) {
printBoard(board);
playerMove(board);
player = 2;
} else {
computerMove(board);
player = 1;
}
}
printf("draw.\n\
/|_____|\\ \n\
__| o o |__\n\
__| v |__\n\
\\_______/ \n\
");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment