السلام عليكم
أنا مبتدأه في البرمجة بلغة السي
أحاول عمل كود لبرنامج tic tac toe أو لعبة 'O' , 'X'
المشكلة ان الكود لازم يكون بيستخدم minimax و heuristic
معي برنامج بحاول أستفيد منو .. بحاجة لشرح أفضل الو .. وبيان لو كان بيستخدم heuristic أو لأ
/*
Tic Tac Toe
game play with min and max computer always play on min level
while human always play with max level
*/
#include <stdio.h>
#include <dos.h>
/* the functions */
void theboard(void);
void playgame(int play, char who);
int boardfull(void);
int win(int who);
void computermove(int who);
void humanmove(int who);
void output(void);
int minimax(int who);
int bestminimaxmove(int a[9],int who);
int board[3][3];
/* Main function */
int main(void)
{
int play;
char c, who;
clrscr();
printf("Tic Tac Toe !\n");
do
{
/* the board */
theboard();
/* to play */
printf("typ 1 to play or 2 to exit game !");
while ((play=getchar()-48)<0 || play>2);
if (play==1)
{
printf("Who goes first (Computer or Human)?");
while((who=toupper(getchar()))!='C' && who!='H');
}
clrscr();
playgame(play,who);
clrscr();
/* final output */
output();
/* Determine who won or if it was a game over and output */
if (win(-1))
{
if(who=='C')printf("\nThe Computer won\n");
else if(who=='H')printf("\nPlayer won\n");
}
else if (win(1))
{
if(who=='C')printf("\nPlayer won\n");
else if(who=='H')printf("\nThe Computer won\n");
}
else
printf("\nIt game over.\n");
/* asking for another game */
printf("\nWould you like to play again?\n");
while ((c=toupper(getchar()))!='N' && c!='Y');
}
while(toupper©=='Y');
}
/* theboard sets all of the values in the board to 0 */
void theboard()
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
board[j]=0;
}
void playgame(int play, char who)
{
switch (play)
{
case 1: /* computer vs. human */
if (who=='H') humanmove(-1);
else computermove(-1);
while(!boardfull() && !win(-1))
{
if (who=='H') computermove(1);
else humanmove(1);
if (win(1))
break;
if (who=='H') humanmove(-1);
else computermove(-1);
}
break;
case 2:
exit();
}
}
/* Boardfull tests to see if the board is full or not */
int boardfull()
{
int i,j;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
if(board[j]==0)
return 0;
return -1;
}
/* Win test to see if a win has occured by who */
int win(int who)
{
/* Test for one in middle for most prossible wins */
if (board[1][1]==who)
{
/* Test all wins that include middle */
if (board[0][0]==who && board[2][2]==who)
return 1;
if (board[1][0]==who && board[1][2]==who)
return 1;
if (board[0][2]==who && board[2][0]==who)
return 1;
if (board[0][1]==who && board[2][1]==who)
return 1;
}
/* Test top left for top row and left column */
if (board[0][0]==who)
{
/* Test top row and left column */
if (board[0][1]==who && board[0][2]==who)
return 1;
if (board[1][0]==who && board[2][0]==who)
return 1;
}
/* Test bottom right for bottom row and right column */
if (board[2][2]==who)
{
/* Test bottom row and right column */
if (board[2][0]==who && board[2][1]==who)
return 1;
if (board[0][2]==who && board[1][2]==who)
return 1;
}
/* Otherwise no win has occured */
return 0;
}
/* makes the move of human */
void humanmove(int who)
{
int move;
clrscr();
output();
printf("\nWhere do you want to move?");
while((move=getchar()-49)<0 || move>8);
while(board[move/3][move%3]!=0)
{
printf("\nInvalid Move.\n");
printf("Where do you want to move?");
while((move=getchar()-49)<0 || move>8);
}
board[move/3][move%3]=who;
}
/* Outputs the board to the screen */
void output(void)
{
int i,j;
char out[12][24]={" | | ",
" | | ",
" | | ",
"-----------------------",
" | | ",
" | | ",
" | | ",
"-----------------------",
" | | ",
" | | ",
" | | "};
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
/* Put the square number in upper right corner */
out[4*i][8*j+6]=i*3+j+49;
/* Put 'X' or 'O' in the out array */
if(board[j]==-1)
out[4*i+1][8*j+3]='X';
else if(board[j]==1)
out[4*i+1][8*j+3]='O';
}
/* Output the array to screen */
for(i=0;i<12;i++)
printf("%s\n",out);
}
/* Finds the best computer move using the minimax function */
void computermove(int who)
{
int i,j,a[9];
for(i=0;i<9;i++)
a=-who;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
/* Check for invalid move */
if (board[j]!=0)
continue;
/* Try a move */
board[j]=who;
/* Score this board with the trial move */
a[i*3+j]=minimax(-who);
/* Undo the move */
board[j]=0;
}
/* Find the best move in the array */
i=bestminimaxmove(a,who);
/* Make the move */
board[i/3][i%3]=who;
}
/* minimax function of the move tree */
int minimax(int who)
{
/* gover = boolean test to see if no winner
Best = The best score */
int gover=0,i,j,best;
/* Test for a loss (win for one level up) */
if(win(-who))
return -who;
/* Test for gover */
if(boardfull())
return 0;
for(i=0;i<3;i++)
for(j=0;j<3;j++)
{
/* Skip invalid moves */
if (board[j]!=0)
continue;
/* Try move */
board[j]=who;
/* Score this move */
best=minimax(-who);
/* Undo move */
board[j]=0;
/* Return the best move if it's a win */
if(best==who)
return who;
/* At least one gover has been found */
else if (best==0)
gover=-1;
}
/* If a gover return gover */
if(gover)
return 0;
else
return -who;
}
/* Finds the best move for who in a[] */
int bestminimaxmove(int a[9],int who)
{
int i,best,index=0;
best=-who;
for(i=0;i<9;i++)
{
if(a==who)
return i;
if(a==0 && best==-who)
{
best=0;
index=i;
}
}
/* Returns the location of the best move */
return index;
}
............................................
يا ريت تقدروا تساعدوني مشكورين :)