Saturday, December 15, 2012

Tic Tac Toe - Unbeatable Algorithm

This post is part of the "First Few Old Blog Posts" archive.
You could expect a certain lack of coherency/maturity from these posts.

For my Computer Project at school , I had to make a simple Java Program. The rules although simple, were very restricting since my class hadn't learnt as much in programming, The program had to be console based (i.e. no Graphics), I could not use any Library Classes apart from the ones that came along with java.lang and Console Input classes. Also, my class hadn't learnt about inheritance and other OOP stuff so I had to stay clear from those too.

The trouble now was to make a good enough project that fit the criteria.
My classmates had settled on making games such as hangman, crossword, calculators etc, but I had already made most of them before while I was getting into programming, and they didn't have much appeal to me.

In the end , I concluded that the project would have to be small anyway, so I decided to make Tic Tac Toe Singleplayer. I'd already made Tic Tac Toe mutliplayer last year as part of a larger package , so I thought that it would be a nice addition to have this as well.

The 3x3 grid was constructed using a 2 dimensional array.
You had to play against the computer in 3 modes - Easy , Medium and Hard respectively.

Easy mode was simple. I used Math.random() to decide the X and Y co-ordinates of the resulting move.

Medium randomly chose between playing easy or hard for that move.

And hard used an algorithm that was impossible to beat. Of course , tic tac toe isn't such a full blown game , and any person smart enough could play any number of matches and always make a tie. But anyway, here is the code for all 3 modes.

Note: This is a function that simply calculates the position that the computer plays in, in a given circumstance. It takes an integer value (int c) that contains the difficulty value (1,2 or 3 for easy,medium or hard). It also takes a 2 dimensional character array of length 3 and 3 (char[][] u) which contains 'X' (player value) , 'O' (computer value) or '-' (empty value). The function returns an integer array of size 2 which contains the x and y co-ordinates of the computed value.

To see the whole program , click here.

     
    public static int[] compute(int c,char[][] u){
        int ar[]=new int[2];
        if (c==1){
            int x=(int)(Math.random()*3);
            int y=(int)(Math.random()*3);
            if (u[x][y]=='-'){
                ar[0]=x;
                ar[1]=y;
                return ar;
            }
            else{
                return compute(1,u);
            }
        }
        if (c==2){
            return compute(((int)(Math.random()*3)+1),u);
        }
        if (c==3){
            boolean mark=false;
            int x=0,y=0;
            int count=0;
            for (int i=0;i<3;i++){
                for (int j=0;j<3;j++){
                    if (u[i][j]=='-'){
                        count++;
                        u[i][j]='X';
                        if (check(u)==2){
                            mark=true;
                            x=i;
                            y=j;
                        }
                        u[i][j]='-';
                    }
                }
            }
            for (int i=0;i<3;i++){
                for (int j=0;j<3;j++){
                    if (u[i][j]=='-'){
                        u[i][j]='O';
                        if (check(u)==1){
                            mark=true;
                            x=i;
                            y=j;
                        }
                        u[i][j]='-';
                    }
                }
            }
            if ((!mark)&&(predict(u,0,0)>1||predict(u,0,2)>1||predict(u,1,1)>1||predict(u,2,0)>1||predict(u,2,2)>1)){
                for (int i=0;i<3;i++){
                    for (int j=0;j<3;j++){
                        if (u[i][j]=='-'&&((i==0&&j==1)||(i==1&&j==0)||(i==1&&j==2)||(i==2&&j==1))){
                            u[i][j]='O';
                            for (int k=0;k<3;k++){
                                for (int l=0;l<3;l++){
                                    if (u[k][l]=='-'){
                                        u[k][l]='O';
                                        if (check(u)==1){
                                            mark=true;
                                            x=k;
                                            y=l;
                                        }
                                        u[k][l]='-';
                                    }
                                }
                            }
                            u[i][j]='-';
                        }
                    }
                }
            }
            if (!mark){
                if (count==9){
                    int ran=(int)(Math.random()*5);
                    switch(ran){
                        case 0 : x=0;
                                 y=0;
                                 break;
                        case 1 : x=1;
                                 y=1;
                                 break;
                        case 2 : x=2;
                                 y=2;
                                 break;
                        case 3 : x=0;
                                 y=2;
                                 break;
                        case 4 : x=2;
                                 y=0;
                                 break;
                    }
                }
                else if (count==8){
                    if (u[1][1]=='-'){
                        x=1;
                        y=1;
                    }
                    else{
                        int ran=(int)(Math.random()*4);
                        switch(ran){
                            case 0 : x=0;
                                     y=0;
                                     break;
                            case 1 : x=2;
                                     y=0;
                                     break;
                            case 2 : x=2;
                                     y=2;
                                     break;
                            case 3 : x=0;
                                     y=2;
                                     break;
                        }
                    }
                }
                else{
                    if (u[0][0]=='-'&&u[2][2]=='O'){
                        x=0;
                        y=0;
                    }
                    else if (u[2][2]=='-'&&u[0][0]=='O'){
                        x=2;
                        y=2;
                    }
                    else if (u[0][2]=='-'&&u[2][0]=='O'){
                        x=0;
                        y=2;
                    }
                    else if (u[2][0]=='-'&&u[0][2]=='O'){
                        x=2;
                        y=0;
                    }
                    else{
                        if (u[0][0]=='-'){
                            x=0;
                            y=0;
                        }
                        else if (u[2][2]=='-'){
                            x=2;
                            y=2;
                        }
                        else if (u[0][2]=='-'){
                            x=0;
                            y=2;
                        }
                        else if (u[2][0]=='-'){
                            x=2;
                            y=0;
                        }
                        else{
                            for (int i=0;i<3;i++){
                                for (int j=0;j<3;j++){
                                    if (u[i][j]=='-'){
                                        x=i;
                                        y=j;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (u[x][y]=='-'){
                ar[0]=x;
                ar[1]=y;
                return ar;
            }
            else{
                return compute(3,u);
            }
        }
        return ar;
    }


Well, I admit that this algorithm isn't the most efficient one out there, but I had functionality in mind, instead of efficiency, so I rolled with a makeshift solution.
But what's more fun is to tell everyone that you couldn't beat my program the next day at the computer lab. They enthusiastically take up the challenge at once, and their faces were worth being looked at when most of them lost to the computer at the second game.

It was great fun.

No comments:

Post a Comment