Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <limits.h>
- #define TRUE 0
- #define FALSE 1
- #define PLAYER_1 -1
- #define PLAYER_2 1
- #define UNASSIGNED 0
- #define NULL -100
- #define SIZE 3
- #define table(index) table[index/SIZE][index%SIZE]
- #define MAXIMIZE_PLAYER
- void clrscr() {
- printf("\n\n\n");
- }
- int checkWin(int table[SIZE][SIZE]);
- int minLose(int table[SIZE][SIZE], int player, int depth) ;
- int shouldContinue() {
- int in;
- do {
- printf("Do you want to continue? (%d: yes, %d: no)", TRUE, FALSE);
- scanf("%d", &in);
- } while(in != TRUE && in != FALSE);
- return in;
- }
- char getDisplay(int player, char def) {
- switch (player) {
- case PLAYER_1:
- return 'X';
- case PLAYER_2:
- return 'O';
- default:
- return def;
- }
- }
- void printTable(int table[SIZE][SIZE]) {
- int i, j;
- for (i = 0; i < SIZE; i++) {
- for (j = 0; j < SIZE; j++)
- printf("[%c]", getDisplay(table[i][j], (char) ('0' + (i * SIZE + j))));
- printf("\n");
- }
- }
- void resetTable(int table[SIZE][SIZE]) {
- int i, j;
- for(i = 0; i < SIZE; i++)
- for(j = 0; j < SIZE; j++)
- table[i][j] = UNASSIGNED;
- }
- int defPlayerMove(int table[SIZE][SIZE], int player) {
- int in;
- printf("Player %d: it's your turn!\n", player);
- do {
- printf("->");
- scanf("%d", &in);
- if(in < 0 && in >= SIZE * SIZE)
- printf("Please write a proper index\n");
- else if (table(in) != UNASSIGNED)
- printf("The index is already used by %c\n", getDisplay(table[in/SIZE][in%SIZE], '?'));
- else break;
- } while (1);
- return in;
- }
- int max(int table[SIZE][SIZE], int player, int depth) {
- int i, j, tmp, max = -INT_MAX;
- int winner = checkWin(table);
- if(winner == -player)
- return 10 - depth;
- for(j = 0; j < 9; j++) {
- if(table(j) == UNASSIGNED) {
- table(j) = -player;
- tmp = minLose(table, player, depth + 1);
- if(tmp > max)
- max = tmp;
- table(j) = 0;
- }
- }
- return max;
- }
- int minLose(int table[SIZE][SIZE], int player, int depth) {
- int i, j;
- int move = -1, min = INT_MAX, tmp;
- int winner = checkWin(table);
- if(winner != NULL) {
- if (winner == -player)
- return 10 - depth;
- return 0;
- }
- for(i = 0; i < 9; i++) {
- if(table(i) != UNASSIGNED)
- continue;
- table(i) = player;
- tmp = max(table, player, depth + 1);
- if(depth == 0) {
- printf("%d -> %d\n", i, tmp);
- }
- if(tmp < min) {
- min = tmp;
- move = i;
- }
- table(i) = 0;
- }
- return depth == 0 ? move : min;
- }
- int minmax(int table[SIZE][SIZE], int player) {
- return minLose(table, player, 0);
- }
- int movePlayer1(int table[SIZE][SIZE]) {
- return defPlayerMove(table, 1);
- }
- int movePlayer2(int table[SIZE][SIZE]) {
- int choice = minmax(table, PLAYER_2);
- printf("Cpu: %d\n", choice);
- return choice;
- }
- void move(int round, int table[SIZE][SIZE]) {
- int move;
- if(round % 2 == 0) {
- move = movePlayer1(table);
- table(move) = PLAYER_1;
- } else {
- move = movePlayer2(table);
- table(move) = PLAYER_2;
- }
- }
- int checkWin(int table[SIZE][SIZE]) {
- int i;
- //Check rows
- for(i = 0; i < SIZE; i++) {
- if(table[i][0] != UNASSIGNED &&
- (table[i][0] == table[i][1] && table[i][1] == table[i][2]))
- return table[i][0];
- }
- //Check cols
- for(i = 0; i < SIZE; i++) {
- if(table[0][i] != UNASSIGNED &&
- (table[0][i] == table[1][i] && table[1][i] == table[2][i]))
- return table[0][i];
- }
- if(
- table[1][1] != UNASSIGNED &&
- ((table[0][0] == table[1][1] && table[1][1] == table[2][2]) ||
- (table[0][2] == table[1][1] && table[1][1] == table[2][0]))
- )
- return table[1][1];
- for(i = 0; i < SIZE*SIZE; i++)
- if(table(i) == UNASSIGNED) break;
- if(i >= SIZE*SIZE)
- return UNASSIGNED;//Tie
- return NULL;
- }
- void match() {
- int table[SIZE][SIZE], round = 0, winner;
- resetTable(table);
- while ((winner = checkWin(table)) == NULL) {
- clrscr();
- printTable(table);
- move(round++, table);
- }
- printTable(table);
- switch (winner) {
- case PLAYER_1:
- printf("Player 1 win!\n");
- break;
- case PLAYER_2:
- printf("Player 2 win!\n");
- break;
- default:
- printf("Draw!\n");
- }
- printf("Match ended!\n");
- }
- int main() {
- do {
- match();
- } while (shouldContinue() == TRUE);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement