Advertisement
Guest User

Untitled

a guest
Dec 8th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #define N 3
  4.  
  5. struct Pos
  6. {
  7.     int row,col;
  8. };
  9. struct Pos bestPos(int arr[N][N],int who);
  10. int scoreArr(int arr[N][N],int whostarted);
  11. int optimalMove(int arr[N][N],int level,int who,int whostarted);
  12. int posLeft(int arr[N][N]);
  13. int gameEnd(int nopos,int who);
  14. void printArr(int arr[N][N]);
  15.  
  16. int main() {
  17.     int i,who,tymczasowe;
  18.     struct Pos bestpos;
  19.     int arr[N][N]={
  20.             {-1,-1,1},
  21.             {1,1,1},
  22.             {1,-1,-1}
  23.     };;
  24.     printArr(arr);
  25.     //* for(i=0;i<N*N;i++)
  26.     //* who=(i%2==0)?(-1):(1);
  27.     bestpos=bestPos(arr,1);
  28.     tymczasowe=scoreArr(arr,1);
  29.     printf("The Optimal Move is :\n");
  30.     printf("ROW: %d COL: %d\n\n", bestpos.row,bestpos.col);
  31.     return 0;
  32.     }
  33. int posLeft(int arr[N][N]){
  34.     int i,j;
  35.     for (i = 0; i<N; i++)
  36.         for (j = 0; j<N; j++)
  37.             if (arr[i][j]=='0') return 1;
  38.     return 0;
  39. }
  40.  
  41. int scoreArr(int arr[N][N],int whostarted) {
  42.     int i, j;
  43.     int me = whostarted;
  44.     int enemy = (whostarted > 0) ? (-1) : (1);
  45.     int towin = 0, tolose = 0;
  46.     for (i = 0; i < N; i++)  {  //SPRAWDZANIE CZY JAKIŚ WIERSZ WYGRAŁ
  47.         towin=0,tolose=0;
  48.         for (j = 0; j < N - 1; j++){
  49.             if (arr[i][j] == arr[i][j + 1]){
  50.                 printf("arr[i][j] == arr[i][j + 1]");
  51.                 printf("  %d %d",arr[i][j],arr[i][j+1]);
  52.                 if (arr[i][j] == me) {
  53.                     towin += 1;
  54.                     printf("\nscoraArr i j  =%d %d\n",i,j);
  55.                     printf("\nscoraArr towin =%d\n",towin);
  56.                     if (towin == N-1){
  57.                         printf("win"); return 50;
  58.                     }
  59.                 }
  60.                 else if (arr[i][j] == enemy) {
  61.                     tolose += 1;
  62.                     printf("\nscoraArr i j  =%d %d\n",i,j);
  63.                     printf("\nscoraArr tolose =%d\n",tolose);
  64.                     if (tolose == N-1){
  65.                         printf("lose"); return -50;
  66.                     }
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     towin = 0, tolose = 0;
  72.     for (i = 0; i < N; i++){    //SPRAWDZANIE CZY JAKAŚ KOLUMNA WYGRAŁA
  73.         for (j = 0; j < N - 1; j++){
  74.             towin=0,tolose=0;
  75.             if (arr[j][i] == arr[j+1][i]){
  76.                 if (arr[j][i] == me) {
  77.                     towin += 1;
  78.                     if (towin == N-1)printf("win"); return 50;
  79.                 }
  80.                 else if (arr[j][i] == enemy) {
  81.                     tolose += 1;
  82.                     if (tolose == N-1)printf("lose"); return -50;
  83.                 }
  84.             }
  85.         }
  86.     }
  87.     return 0;
  88.     towin = 0, tolose = 0;
  89. }
  90.  
  91.  
  92. int optimalMove(int arr[N][N],int level,int who,int whostarted){   // SPRAWDZANIE WSZYSTKICH MOŻLIWOŚCI DOKOŃCZENIA ROZRYWKI
  93.     int me=who;
  94.     int best,i,j,checkedmove;
  95.     int enemy=(who>0)?(-1):(1);
  96.     int score=scoreArr(arr,whostarted);
  97.     printf("\nscore= %d\n",score);
  98.     if(score==50) return score-level;
  99.     if(score==-50) return score+level;
  100.     if(posLeft==0) return 0;
  101.     printArr(arr);
  102.     if(me){
  103.         best=-30;
  104.         for(i=0;i<N;i++)
  105.             for (j = 0; j < N; j++)
  106.                 if(arr[i][j]==0)
  107.                 {
  108.                     arr[i][j]=me;
  109.                     checkedmove=optimalMove(arr,level+1,enemy,whostarted);
  110.                     best = (best>checkedmove)?best:checkedmove;
  111.                     arr[i][j]=0;
  112.                 }
  113.         printf("\n%d\n",best);
  114.         return best;
  115.     }
  116.     else{
  117.         best=30;
  118.         for(i=0;i<N;i++)
  119.             for (j = 0; j < N; j++)
  120.                 if (arr[i][j] == 0) {
  121.                     arr[i][j] = me;
  122.                     checkedmove = optimalMove(arr, level + 1, enemy,whostarted);
  123.                     best = (best < checkedmove) ? best : checkedmove;
  124.                     arr[i][j] = 0;
  125.                 }
  126.         printf("\n%d\n",best);
  127.                 return best;
  128.     }
  129. }
  130.  
  131. struct Pos bestPos(int arr[N][N],int who){  // SZUKANIE NAJLEPSZEGO RUCHU ZA POMOCĄ SCORE`A
  132.     int posval;
  133.     int bestposval=-50;
  134.     int me=who;
  135.     int whostarted=who;
  136.     int enemy=(who>0)?(-1):(1);
  137.     int i,j;
  138.     struct Pos bestpos;
  139.     for(i=0;i<N;i++)
  140.         for (j = 0; j < N; j++)
  141.             if (arr[i][j] == 0) {
  142.                 arr[i][j] = me;
  143.                 posval = optimalMove(arr, 0, enemy, whostarted);
  144.                 arr[i][j] = 0;
  145.                 if (posval > bestposval) {
  146.                     bestpos.row = i;
  147.                     bestpos.col = j;
  148.                     bestposval = posval;
  149.                 }
  150.             }
  151.     return bestpos;
  152. }
  153.  
  154. void printArr(int arr[N][N]){
  155.     int i,j;
  156.     for(i=0;i<N;i++)
  157.         for(j=0;j<N;j++){
  158.             printf("%d%s",arr[i][j],(j!=N-1)?(" "):("\n"));
  159.         }
  160.     printf("\n");
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement