Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- /*
- 2.0
- Minimax working
- Multiple matches without having to close the program
- */
- int clearboard(int board[3][3])//Clears the board
- {
- int x,y;
- for(x=0;x<3;x++)
- {
- for(y=0;y<3;y++)
- {
- board[x][y] = 0;
- }
- }
- }
- int max(int board[3][3],int player)//Best sum for the player passed as parameter
- {
- int x,y,minscore,maxscore,tempscore;
- y = 0;
- minscore = -4;
- maxscore = 4;
- for(x=0;x<3;x++)
- {
- tempscore = board[x][0] + board[x][1] + board[x][2];
- if(player==1)
- {
- if(tempscore>minscore)
- {
- minscore = tempscore;
- }
- }else
- {
- if(tempscore<maxscore)
- {
- maxscore = tempscore;
- }
- }
- tempscore = board[0][x] + board[1][x] + board[2][x];
- if(player==1)
- {
- if(tempscore>minscore)
- {
- minscore = tempscore;
- }
- }else
- {
- if(tempscore<maxscore)
- {
- maxscore = tempscore;
- }
- }
- }
- for(x=0;x<2;x++)
- {
- tempscore = board[0][y-0]+board[1][1]+board[2][2-y];
- if(player==1)
- {
- if(tempscore>minscore)
- {
- minscore = tempscore;
- }
- }else
- {
- if(tempscore<maxscore)
- {
- maxscore = tempscore;
- }
- }
- y+=2;
- }
- if(player == 1)
- {
- return minscore;
- }else
- {
- return maxscore;
- }
- }
- int drawboard(int board[3][3])//Displays the current state of the board
- {
- int x,y;
- char c[3] = {'X',' ','O'};
- printf("---------------\n");
- for(x=0;x<3;x++)
- {
- for(y=0;y<3;y++)
- {
- printf("| %c |",c[board[x][y]+1]);
- }
- printf("\n---------------\n");
- }
- }
- int pcplay(int board[3][3])
- {
- int x,y,score,tempscore,movex,movey,player;
- player = 1;
- score = -4;
- for(x=0;x<3;x++)
- {
- for(y=0;y<3;y++)
- {
- if(board[x][y]==0)
- {
- board[x][y]=1;
- }else
- {
- continue;
- }
- tempscore = -minimax(board,player*-1);
- board[x][y] = 0;
- if(tempscore>score)
- {
- score = tempscore;
- movex = x;
- movey = y;
- }
- }
- }
- board[movex][movey] = 1;
- }
- int minimax(int board[3][3],int player)
- {
- int x,y,tempscore,score,move = 0;
- score = -4;
- if(max(board,player*-1)== 3 || max(board,player*-1)==-3)
- {
- return -1;
- }else
- {
- for(x=0;x<3;x++)
- {
- for(y=0;y<3;y++)
- {
- if(board[x][y]==0)
- {
- board[x][y]=player;
- move = 1;
- }else
- {
- continue;
- }
- tempscore = -minimax(board,player*-1);
- board[x][y] = 0;
- if(tempscore>score)
- {
- score = tempscore;
- }
- }
- }
- if(move == 0)
- {
- return 0;
- }else
- {
- return score;
- }
- }
- }
- int main()
- {
- int round,player,x,y;
- int board[3][3] = {{0,0,0},{0,0,0},{0,0,0}};
- player = -1;
- round = 1;
- while(1)
- {
- if(round %2 == 0)
- {
- pcplay(board);
- if(max(board,player*-1)==3)
- {
- drawboard(board);
- printf("Player 2 wins\n");
- system("pause");
- system("cls");
- clearboard(board);
- round = 1;
- }else
- {
- round++;
- }
- }else
- {
- drawboard(board);
- printf("Column : ");
- scanf("%i",&x);
- printf("Line : ");
- scanf("%i",&y);
- if(board[x][y]==0)
- {
- board[x][y] = player;
- if(max(board,player)==-3)
- {
- drawboard(board);
- printf("Player 1 wins\n");
- system("pause");
- system("cls");
- clearboard(board);
- round = 1;
- }else
- {
- round++;
- }
- }
- }
- if(round == 10)
- {
- drawboard(board);
- printf("It's a tie\n");
- system("pause");
- system("cls");
- clearboard(board);
- round = 1;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement