Skip to main content

X-O game using JAVA



Here, you can find a basic java code to implement X-O game.
Follow me @ facebook


//*********************************Coded-By : SOMES KUMAR K.*******************************//
 
//*********************************LEARN CODE WITH FUN*************************************// 
 
import java.util.Scanner;

public class X_O_Game {
    int xi,xj;
    static Scanner s =new Scanner(System.in);
    char board [][];
      public void make_board(int n){
         if(n%2!=0){
             setBoard(n);
         }
         else{
             System.out.println("Enter valid Board Size");
              n = s.nextInt();
              make_board(n);
         }
     }
    public void setBoard(int size) {
        board=new char[size][size];
        for(int i=0;i<size;i++){
            for (int j=0;j<size;j++){
                board[i][j]=' ';
            }
        }
    }
    boolean check_board_empty(int size){
        boolean c=true;
          for(int i=0;i<size;i++){
            for (int j=0;j<size;j++){
                if(board[i][j]==' '){
                    c=false;
                    break;
                }
            }
        }
        return !c;
    }
    public void display_board(int size){
        print_line(size);
         for(int i=0;i<size;i++){
            for (int j=0;j<size;j++){
               System.out.print(board[i][j]+" ");
            }
            System.out.println();
            print_line(size);
       }
    }
    void print_line(int n){
       int x = (n*4)-1;
        for(int i=0;i<x;i++){
            System.out.print("-");
         }
       System.out.println();
      
     } 
 boolean check_win(char op,int size){
         boolean col=true,row=true,dig1=true,dig2=true;
         int di=size-1;
         for(int i=0;i<size;i++){
              if(board[xi][i]!=op){
                  col=false;
              }
              if(board[i][xj]!=op){
                  row=false;
              }
              if(board[i][i]!=op){
                  dig1=false;
              }
              if(board[i][di]!=op){
                  dig2=false;
              }
              di--;
          }
          return col || row || dig1 || dig2 ;
    }
    public static void main(String[] args) {
         boolean win=false;
        X_O_Game xo = new X_O_Game();
        System.out.println("Get Board Size");
        int n = s.nextInt();
        xo.make_board(n);
        System.out.println("Intial Board");
        xo.display_board(n);
        while(xo.check_board_empty(n)){
            System.out.println("Player X");
            if(xo.get_input_x(n)){
                if(xo.check_win('X',n)){
                    win=true;
                    System.out.println("X wins....");
                    xo.display_board(n);
                    break;
                }
            }
            System.out.println("Player O");
            if(xo.get_input_y(n)){
                if(xo.check_win('O',n)){
                    win=true;
                    System.out.println("O wins....");
                    xo.display_board(n);
                    break;
                }
            }
        }
        if(!win){

            System.out.println("Match ties...");
            xo.display_board(n);
        }
    }
    boolean get_input_x(int size){
          if(check_board_empty(size))
          {
              System.out.println("i pos and j pos");
              xi=s.nextInt();
              xj=s.nextInt();
              if(xi<size && xj<size && board[xi][xj]==' '){
              board[xi][xj]='X';
              display_board(size);
              return  true;
          }
          else {
              System.out.println("Re enter your input");
              return get_input_x(size);
          }}
          else              return false;
    }
    boolean get_input_y(int size){
        if(check_board_empty(size))
        {
            System.out.println("i pos and j pos");
            xi=s.nextInt();
            xj=s.nextInt();
            if(xi<size && xj<size && board[xi][xj]==' '){
            board[xi][xj]='O';
            display_board(size);
            return  true;
        }
        else {
            System.out.println("Re enter your input");
            return get_input_y(size);
        }
        }
        else{
            return false;
        }
    }

}
 
OUTPUT:
 
Get Board Size
3
Intial Board
-----------
  |   |   | 
-----------
  |   |   | 
-----------
  |   |   | 
-----------
Player X
i pos and j pos
 0 0
-----------
X |   |   | 
-----------
  |   |   | 
-----------
  |   |   | 
-----------
Player O
i pos and j pos
 1 1
-----------
X |   |   | 
-----------
  | O |   | 
-----------
  |   |   | 
-----------
Player X
i pos and j pos
1 2
-----------
X |   |   | 
-----------
  | O | X | 
-----------
  |   |   | 
-----------
Player O
i pos and j pos
1 1
Re enter your input
i pos and j pos
1 0
-----------
X |   |   | 
-----------
O | O | X | 
-----------
  |   |   | 
-----------
Player X
i pos and j pos
0 1
-----------
X | X |   | 
-----------
O | O | X | 
-----------
  |   |   | 
-----------
Player O
i pos and j pos
0 2
-----------
X | X | O | 
-----------
O | O | X | 
-----------
  |   |   | 
-----------
Player X
i pos and j pos
2 0
-----------
X | X | O | 
-----------
O | O | X | 
-----------
X |   |   | 
-----------
Player O
i pos and j pos
2 2
-----------
X | X | O | 
-----------
O | O | X | 
-----------
X |   | O | 
-----------
Player X
i pos and j pos
2 1
-----------
X | X | O | 
-----------
O | O | X | 
-----------
X | X | O | 
-----------
Player O
Match ties...
-----------
X | X | O | 
-----------
O | O | X | 
-----------
X | X | O | 
-----------
  
 
 //****************************Any doubts??____drop a comment****************//
 
 
 
Follow me @ facebook

Comments

Popular posts from this blog

Failed even in Love..........

Hi, Once again sorry for my long gap. I actually had no idea about what to blog. Guys from now I'll post minimum one blog per week. thank you for all who asked me to continue my blog they are my motivators. So, I wish this could be a inspirational blog. Do you failed in anything and hidden those failure ?, I like to start my blog with this question. Mostly I hope the from you is yes. Because we are living in culture where the failed peoples are looked in awkward manner.  Actually all people has failed in something. But I am not writing this blog to talk about failures but about way you handle it. Myself, As a friend I like to share my views on my failure. I faced many failure I failed when learn something new. I never succeeded in any new skills at the very first attempt even in my love but not in my exam's. Nothing in the world is born with skills. They develop their skills after the birth. Even the new born of fish learns to swim after few seconds. What made it to acquire it...

My First Blog !

Hi, this is my first blog, I had planned to write a blog on every week from this year first of all, I wish you a very happy new year 2017 let 16 have some sorrow’s, joy’s, up’s and down’s On this new try to recollect it all and figure the reason for it Today's blog I'll say some interesting facts about Jan 01, The Romans celebrated the 1 st new year at Jan 1 on 153 b c, January is derived from Janus who is god for Gateways and beginnings, Jan 01 is accepted as a 1st day of the year in Julian and Gregorian calendar's but most of calendar’s  have different different days Tamil people celebrate their New at Chithirai 01 (April 14-15), when the Sun enter in to the zodiac Aries Chinese celebrate their new year when Lunar(moon) calendar so they have new year between (Jan 20 - Feb 20) Thailand celebrates its new year at april-13 or april-14, where it called as Songkran where the people splash water on other That’s all about today thank you for reading it.........

What is Fog Computing?

Hi, this is my second blog and it is the  First technical blog. here I’ll share some of my views about Fog computing, a morden concept for data storage.                       In this modern world, TIME become the most important factor which leads us to develop technologies to compute faster even though It is costly we prefer it, Similarly the online storage infrastructure cloud has some disadvantages like slow response, less security, unavailability, high bandwidth to overcome this the CISCO coined a new tech called FOG, can it overcome all the disadvantages of the Cloud? Well let’s see that in this blog…………….                   Fog computing also known as fogging or edge computing is a globally distributed computing  infrastructure in which application services are handled by the edge networking Smart devices like r...

OOP Encapsulation with Modifiers..

                  In this blog we can know about why we go for JAVA?. Java is a language known for object oriented features and its network support. Here object oriented is achieved by three concepts Encapsulation Inheritance Polymorphism                 but Java is not pure object oriented  because of its support to primitive data types(explained later..) like int, float,...to overcome this JAVA has loaded with wrapper class( know more ).          Encapsulation,   we had come across the word "capsule" many times in our life. capsule is nothing but a outer protection(cover) used to protect the something from external factors. example the medicines usually come with a capsule to protect the chemicals inside it. Similarly the encapsulation is used to wrap the code or data from intruders in order to ensure its security. It can be achieved from mod...

Object in Java..

         Object is a thing used to access a member in class. (i.e) the class members which are non static can be accessed in other class using object it is also referred as Instance of a Class. It is created during run time so its a run-time entity. Object can be used access only one class. But class can have multiple objects unless the constructor is declared as static. For static constructor only one object can be created. During run time object gets created and only when object created the memory of the class variables gets created and the object's memory gets stored in heap. Object can't be created for abstract class and interfaces.   Any object should posses  Identity State Behavior  To create a object Declare a reference variable Construct object with "new" keyword. new is used for dynamic memory allocation.  Assign the reference variable to new keyword. and construct the object. Example: class A{ ...