Monday, 29 September 2014

TicTacToe Real-Time Game in Java

Tic-Tac-Toe REAL TIME GAME IN JAVA
                        by AGNIVA MUKHOPADHYAY
                        This is a simple  game of criss – cross which I brought out using Java.
You have to enter the row-number and column number of the grid. Well the computer does not have any intelligence. But still it never misses it’s turn.
  There is a very interesting logic behind it using Math.random() class. You can understand it by  seeing the coding . And  also be careful with  logic of decision making regarding the winner.

FINALLY,enjoy the game…..

Snapshots of the game





CODE……..
//coded by AGNIVA MUKHOPADHYAY
import java.io.*;
public class TicTacToe
{
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static String a[][] = new String [3][3];
   
    public void displayScreen(){
        
        System.out.println("   0 1 2  "); 
        System.out.println("  0_|_|_   ");
        System.out.println("  1_|_|_   ");
        System.out.println("  2 | |    ");
        System.out.println(" IMAGINE THIS AS A GRID OF TICTACTOE  ");
    }
    public static void main(String args[])throws IOException
    {
        TicTacToe ob = new TicTacToe();
        for( int t = 0;t<3;t++){
            for( int y = 0;y<3;y++){
                a[t][y] = " ";
            }
        }
        System.out.println(" ENTER THE RESPECTIVE POSITION U CHOOSE & WAIT FOR THE COMPUTER'S TURN   ");
       
        for( int i = 0;i<4;i++){
            ob.displayScreen();
            // user's turn
            System.out.print("ENTER THE ROW NUMBER :");
            int r = Integer.parseInt(br.readLine());
            System.out.println();
            System.out.print("ENTER THE COLUMN NUMBER :");
            int c = Integer.parseInt(br.readLine());
            System.out.println();
            a[r][c] = "x";
            // computer's turn
            int co = 0;
            do{
             int r1 = (int)(Math.random()*3);
             int c1 = (int)(Math.random()*3);
             if(a[r1][c1] == " "){
                 a[r1][c1] = "o";co = 1;
             }
             else{
                 co = 0;
             }
            }while(co == 0);
            // the result per step
             System.out.println("\f");
            for( int t1 = 0;t1<3;t1++){
              for( int y1 = 0;y1<3;y1++){
                System.out.print(a[t1][y1]);
               
              }
               System.out.println();
           }
           if(ob.validity() || ob.negativity()){
               break;
            }
           System.out.println("______________________________________________________");
        }
        if(ob.validity()){
            System.out.print("YOU WON");
        }
        else if(ob.negativity()){
            System.out.print("YOU LOSE");
        }
        else{
            System.out.print("IT's A TIE");
        }
    }
    public boolean validity(){
        String valid = "";boolean f = false;
        for(int i = 0;i<3;i++){
            for(int j = 0;j<3;j++){
                valid += a[j][i];
            }
            if(xcount(valid,'x') == 3){
                f = true;
            }
            valid = "";
            for(int j = 0;j<3;j++){
                valid += a[i][j];
            }
            if(xcount(valid,'x') == 3){
                f = true;
            }
            valid = "";
        }
        if((a[0][2].equals("x") && a[1][1].equals("x") && a[2][0].equals("x"))||(a[0][0].equals("x") && a[1][1].equals("x") &&  a[2][2].equals("x"))){
            f = true;
        }
        return f;
    }
    public boolean negativity(){
        String valid = "";boolean f = false;
        for(int i = 0;i<3;i++){
            for(int j = 0;j<3;j++){
                valid += a[j][i];
            }
            if(xcount(valid,'o') == 3){
                f = true;
            }
            valid = "";
            for(int j = 0;j<3;j++){
                valid += a[i][j];
            }
            if(xcount(valid,'o') == 3){
                f = true;
            }
            valid = "";
        }
        if((a[0][2].equals("o") && a[1][1].equals("o") && a[2][0].equals("o"))||(a[0][0].equals("o") && a[1][1].equals("o") &&  a[2][2].equals("o"))){
            f = true;
        }
        return f;
       
    }
    public int xcount(String s,char d){
        int n = 0;
        for(int i = 0;i<s.length();i++){
            char c = s.charAt(i);
            if(c == d){
                n++;
            }

        }
        return n;
    }
    public void input()throws IOException
    {
        for( int t = 0;t<3;t++){
            for( int y = 0;y<3;y++){
                System.out.println("Enter a String(x/o)");
                a[t][y] = br.readLine();
            }
        }
        for( int t = 0;t<3;t++){
            for( int y = 0;y<3;y++){
                System.out.print( a[t][y]+" ");
            }
            System.out.println();
        }
    }

}

1 comment :

  1. the pics may not be displayed but the program is absolutely perfect..

    ReplyDelete