Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #define N 3
- struct Pos
- {
- int row,col;
- };
- struct Pos bestPos(int arr[N][N],int who);
- int scoreArr(int arr[N][N],int whostarted);
- int optimalMove(int arr[N][N],int level,int who,int whostarted);
- int posLeft(int arr[N][N]);
- int gameEnd(int nopos,int who);
- void printArr(int arr[N][N]);
- int main() {
- int i,who,tymczasowe;
- struct Pos bestpos;
- int arr[N][N]={
- {-1,-1,1},
- {1,1,1},
- {1,-1,-1}
- };;
- printArr(arr);
- //* for(i=0;i<N*N;i++)
- //* who=(i%2==0)?(-1):(1);
- bestpos=bestPos(arr,1);
- tymczasowe=scoreArr(arr,1);
- printf("The Optimal Move is :\n");
- printf("ROW: %d COL: %d\n\n", bestpos.row,bestpos.col);
- return 0;
- }
- int posLeft(int arr[N][N]){
- int i,j;
- for (i = 0; i<N; i++)
- for (j = 0; j<N; j++)
- if (arr[i][j]=='0') return 1;
- return 0;
- }
- int scoreArr(int arr[N][N],int whostarted) {
- int i, j;
- int me = whostarted;
- int enemy = (whostarted > 0) ? (-1) : (1);
- int towin = 0, tolose = 0;
- for (i = 0; i < N; i++) { //SPRAWDZANIE CZY JAKIŚ WIERSZ WYGRAŁ
- towin=0,tolose=0;
- for (j = 0; j < N - 1; j++){
- if (arr[i][j] == arr[i][j + 1]){
- printf("arr[i][j] == arr[i][j + 1]");
- printf(" %d %d",arr[i][j],arr[i][j+1]);
- if (arr[i][j] == me) {
- towin += 1;
- printf("\nscoraArr i j =%d %d\n",i,j);
- printf("\nscoraArr towin =%d\n",towin);
- if (towin == N-1){
- printf("win"); return 50;
- }
- }
- else if (arr[i][j] == enemy) {
- tolose += 1;
- printf("\nscoraArr i j =%d %d\n",i,j);
- printf("\nscoraArr tolose =%d\n",tolose);
- if (tolose == N-1){
- printf("lose"); return -50;
- }
- }
- }
- }
- }
- towin = 0, tolose = 0;
- for (i = 0; i < N; i++){ //SPRAWDZANIE CZY JAKAŚ KOLUMNA WYGRAŁA
- for (j = 0; j < N - 1; j++){
- towin=0,tolose=0;
- if (arr[j][i] == arr[j+1][i]){
- if (arr[j][i] == me) {
- towin += 1;
- if (towin == N-1)printf("win"); return 50;
- }
- else if (arr[j][i] == enemy) {
- tolose += 1;
- if (tolose == N-1)printf("lose"); return -50;
- }
- }
- }
- }
- return 0;
- towin = 0, tolose = 0;
- }
- int optimalMove(int arr[N][N],int level,int who,int whostarted){ // SPRAWDZANIE WSZYSTKICH MOŻLIWOŚCI DOKOŃCZENIA ROZRYWKI
- int me=who;
- int best,i,j,checkedmove;
- int enemy=(who>0)?(-1):(1);
- int score=scoreArr(arr,whostarted);
- printf("\nscore= %d\n",score);
- if(score==50) return score-level;
- if(score==-50) return score+level;
- if(posLeft==0) return 0;
- printArr(arr);
- if(me){
- best=-30;
- for(i=0;i<N;i++)
- for (j = 0; j < N; j++)
- if(arr[i][j]==0)
- {
- arr[i][j]=me;
- checkedmove=optimalMove(arr,level+1,enemy,whostarted);
- best = (best>checkedmove)?best:checkedmove;
- arr[i][j]=0;
- }
- printf("\n%d\n",best);
- return best;
- }
- else{
- best=30;
- for(i=0;i<N;i++)
- for (j = 0; j < N; j++)
- if (arr[i][j] == 0) {
- arr[i][j] = me;
- checkedmove = optimalMove(arr, level + 1, enemy,whostarted);
- best = (best < checkedmove) ? best : checkedmove;
- arr[i][j] = 0;
- }
- printf("\n%d\n",best);
- return best;
- }
- }
- struct Pos bestPos(int arr[N][N],int who){ // SZUKANIE NAJLEPSZEGO RUCHU ZA POMOCĄ SCORE`A
- int posval;
- int bestposval=-50;
- int me=who;
- int whostarted=who;
- int enemy=(who>0)?(-1):(1);
- int i,j;
- struct Pos bestpos;
- for(i=0;i<N;i++)
- for (j = 0; j < N; j++)
- if (arr[i][j] == 0) {
- arr[i][j] = me;
- posval = optimalMove(arr, 0, enemy, whostarted);
- arr[i][j] = 0;
- if (posval > bestposval) {
- bestpos.row = i;
- bestpos.col = j;
- bestposval = posval;
- }
- }
- return bestpos;
- }
- void printArr(int arr[N][N]){
- int i,j;
- for(i=0;i<N;i++)
- for(j=0;j<N;j++){
- printf("%d%s",arr[i][j],(j!=N-1)?(" "):("\n"));
- }
- printf("\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement