01/10/2018, 13:26
Minimax - TikTakToe
Ai có thể check hộ em cái này sai chỗ nào được k ạ @@~
#define _CRT_SECURE_NO_WARNINGS // Để dùng được scanf trong VS2017
// KHAI BÁO CÁC THƯ VIỆN
#include <stdio.h>
#include <stdlib.h>
#define N_ROWS 3
#define N_COLS 3
//KHAI BÁO CÁC PROTOTYPE
void print_board(int a[][N_COLS]);
int check_winner(int a[][N_COLS]);
int check_draw(int a[][N_COLS]);
void AI_turn(int a[][N_COLS]);
int minimax(int a[][N_COLS], int player);
void player_turn(int a[][N_COLS]);
int main()
{
int a[N_ROWS][N_COLS];
int result;
while (true)
{
AI_turn(a);
result = check_winner(a);
if (result != 0)
{
print_board(a);
if (result == -2)
{
printf("
Draw
");
return 0;
}
else
{
printf("
AI win
");
return 0;
}
}
player_turn(a);
result = check_winner(a);
if (result != 0)
{
print_board(a);
if (result == -2)
{
printf("
Draw
");
return 0;
}
else
{
printf("
You win
");
return 0;
}
}
}
return 0;
}
void print_board(int a[][N_COLS])
{
// In chỉ số cột của bàn cờ
printf("
");
for (int j = 0; j < N_COLS; j++)
printf(" %i ", j);
printf("
");
// In đường kẻ ngang của bàn cờ
printf(" ");
for (int k = 0; k < N_COLS; k++)
printf("+---");
printf("+
");
for (int i = 0; i < N_ROWS; i++) // Duyệt dòng
{
printf(" %i ", i); // In chỉ số dòng của bàn cờ
for (int j = 0; j < N_COLS; j++) // Duyệt cột
{
if (a[i][j] > 0)
printf("| x ");
else if (a[i][j] < 0)
printf("| o ");
else
printf("| ");
}
printf("|
");
// In đường kẻ ngang của bàn cờ
printf(" ");
for (int k = 0; k < N_COLS; k++)
printf("+---");
printf("+
");
}
printf("
");
}
void player_turn(int a[][N_COLS])
{
print_board(a);
int x, y;
do
{
printf("O Player Turn! Please enter the value of rows and cols: ");
scanf("%d %d", &x, &y);
}
while ((x > 2) || (y > 2) || (x < 0) || (y < 0) || (a[x][y] != 0));
a[x][y] = -1;
}
void AI_turn(int a[][N_COLS])
{
int moveX = -1, moveY = -1;
int score = -99;
int i, j;
for (i = 0; i < N_COLS; i++)
for (j = 0; j < N_ROWS; j++)
{
if (a[i][j] == 0)
{
a[i][j] = 1;
int tempScore = -minimax(a, -1);
a[i][j] = 0;
if (tempScore > score)
{
score = tempScore;
moveX = i;
moveY = j;
}
}
}
a[moveX][moveY] = 1;
}
int minimax(int a[][N_COLS], int player)
{
int result = check_winner(a);
if ((result != 0) && (result != -2))
return result*player;
int move = -1;
int score = -99;
int i, j;
for (i = 0; i < N_COLS; i++)
for (j = 0; j < N_ROWS; j++)
{
if (a[i][j] != 0)
{
a[i][j] = player;
int tempScore = -minimax(a, player*-1);
if (tempScore > score)
{
score = tempScore;
move = 1;
}
a[i][j] = 0;
}
}
if (move == -1) return 0;
return score;
}
int check_winner(int a[][N_COLS])
{
for (int i = 0; i < N_ROWS; i++)
if ((a[i][0] == a[i][1]) && (a[i][1] == a[i][2]) && (a[i][1] != 0))
return a[i][0];
for (int j = 0; j < N_COLS; j++)
if ((a[0][j] == a[1][j]) && (a[1][j] == a[2][j]) && (a[1][j] != 0))
return a[0][j];
if ((a[0][0] == a[1][1]) && (a[1][1] == a[2][2]) && (a[1][1] != 0))
return a[0][0];
else if ((a[2][0] == a[1][1]) && (a[1][1] == a[0][2]) && (a[1][1] != 0))
return a[2][0];
else if (check_draw(a) == 1) return -2;
else return 0;
}
int check_draw(int a[][N_COLS])
{
for (int i = 0; i < N_COLS; i++)
for (int j = 0; j < N_ROWS; j++)
{
if (a[i][j] != 0) return 1;
}
return 0;
}
Bài liên quan
Hi Trí Nguyễn.
Bạn thử debug xem.
P/S
Vứt đống code lên đây rồi chờ người sửa tận miệng. @_@!