atom_1993 0 Newbie Poster

hi all,
ive been trying to do a program on tic tac toe in java
i dunno how to use graphics..nor want to use it
the problem is..i ve done everything with the program,a small segment of the
code called computer_move () is left blank..it is used to get the move frm the compter ie whenever the user enters a co-ordinate.i am not able to figure out how to counter the opponent.
please help!
here's me code :

import java.io.*;
class game
    {
        static String matrix[][]=new String [3][3];
        static int cod;
        public static void matrix_initialization()
            {
                int i,j;
                for (i=0;i<3;i++)
                    {
                        for (j=0;j<3;j++)
                        matrix[i][j]=" ";
                    }
                }                     
                                  
        public static void player_move()throws IOException
            {
                try
                {
                    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
                    System.out.print("Your Number : ");
                    cod=Integer.parseInt(br.readLine());
                }
                catch (Exception e)
                {
                    System.out.println("Invalid Input");
                    player_move();
                    //cod=Integer.parseInt(br.readLine());
                }
                
                     
                if (cod>0 & cod<=9)
                    {
                        if (cod==1)
                            {
                                if (matrix[0][0]!="O")
                                {
                                    matrix[0][0]="X";
                                }
                                else 
                                {
                                    System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                          else if (cod==2)
                            {
                                if (matrix[0][1]!="O")
                                {
                                matrix[0][1]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==3)
                            {
                                if (matrix[0][2]!="O")
                                {
                                matrix[0][2]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==4)
                            {
                                if (matrix[1][0]!="O")
                                {
                                matrix[1][0]="X";
                                } 
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==5)
                            {
                                if (matrix[1][1]!="O")
                                {
                                matrix[1][1]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==6)
                            {
                                if (matrix[1][2]!="O")
                                {
                                matrix[1][2]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==7)
                            {
                                if (matrix[2][0]!="O")
                                {
                                matrix[2][0]="X";
                                }   
                                else
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==8)
                            {
                                if (matrix[2][1]!="O")
                                {
                                matrix[2][1]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            }
                           else if (cod==9)
                            {
                                if (matrix[2][2]!="O")
                                {
                                matrix[2][2]="X";
                                }
                                else 
                                {
                                System.out.println("You are not allowed to assign O's allocation");
                                player_move();
                                }
                            } 
                        }
                else 
                    {
                        System.out.println("Invalid move");
                        player_move();
                    }
                }
                        
              
        
        public static void computer_move()
            {
                
               
            }
                    
    public static void matrix_display()
            {
                    int r;
                    for (r=0;r<3;r++)
                        {
                            System.out.println(" "+matrix[r][0]+" | "+matrix[r][1]+" | "+matrix[r][2]+" ");
                            if(r!=2)
                                {
                                    System.out.println("---+---+---");
                                }
                        }
                        
            }
         public static String check_winner()
            {
              int i;
              for(i=0; i<3; i++)
                
                    if(matrix[i][0]=="X" & matrix[i][1] == "X" &  matrix[i][2] == "X") 
                    return "X";
                
              for(i=0; i<3; i++) 
                
                     if(matrix[0][i]=="X" & matrix[1][i]=="X" & matrix[2][i]=="X") 
                     return "X";
                
              if(matrix[0][0]=="X" & matrix[1][1] == "X" &  matrix[2][2]=="X")
                
                    return "X";
                
             else if(matrix[0][2] == "X" & matrix[1][1] == "X" & matrix[2][0] == "X")
                
                    return "X";
                
               for(i=0; i<3; i++)
                
                    if(matrix[i][0]=="O" & matrix[i][1] == "O" &  matrix[i][2] == "O") 
                    return "O";
                
              for(i=0; i<3; i++) 
                
                     if(matrix[0][i]=="O" & matrix[1][i]=="O" & matrix[2][i]=="O") 
                     return "O";
                
              if(matrix[0][0]=="O" & matrix[1][1] == "O" &  matrix[2][2]=="O")
                
                    return "O";
                
             else if(matrix[0][2] == "O" & matrix[1][1] == "O" & matrix[2][0] == "O")
                
                    return "O";
                
             else
             return " ";
             }
        }
         
        
       
public class tic_tac_toe
    {
        public static void main()throws IOException
            {
                game tic=new game();
                System.out.println("The matrix co-ordinates are as follows : ");
                System.out.println(" 1 | 2 | 3 ");
                System.out.println("---+---+---");
                System.out.println(" 4 | 5 | 6 ");
                System.out.println("---+---+---");
                System.out.println(" 7 | 8 | 9 ");
                String x;
                BufferedReader q=new BufferedReader(new InputStreamReader(System.in));
                tic.matrix_initialization();
                do
                    {
                        tic.matrix_display();
                        tic.player_move();
                        x=tic.check_winner();
                        if (x!=" ")
                        {
                        break;
                        }
                        tic.computer_move();
                        x=tic.check_winner();
                     }
                    while (x == " ");
                    tic.matrix_display();
                    if (x=="X")
                    { System.out.print("X wins");}
                    else if (x =="O")
                    {System.out.print("Y wins");}
                    else if (x==" ")
                    {System.out.print("Draw");}
                }
                }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.