Skip to main content

Tic-Tac-Toe Game Design


Tic - Tac - Toe Game Design

Algorithm:

  1. Take a 2 D Array of size 3X3 initialized to NULL value.
  2. Take position for input. Input should be alter automatically as input taken one by one.
  3. count number of X and O in the Array
  4. if any count of X or O is equal to 3 then go to step 5
  5. check continuity of X or O row-wise, if found go to step 10
  6.  check continuity of X or O column-wise, if found go to step 10
  7. check continuity of X or O in Diagonal or Counter Diagonal wise, if found go to step 10
  8. if total input is less than 9 go to step 2
  9. Conclude:: Totally messed up..... go to Step 11
  10. Display the Owner.....
  11. The End

Program Design (in JAVA):

import java.io.*;

class Game
{
    public static void main (String args[]) throws IOException
    {
        BufferedReader inp=new BufferedReader (new InputStreamReader (System.in));
    int z=0;
    int flag=0;
    int xcount=0, ocount=0;
    char game[][]=new char[3][3];
    char ch;
    int i,j;   
       
       
        // Initialization of game Array
            for(i=0;i<=2;i++)
            {
                for(j=0;j<=2;j++)
                {
                    game[i][j]=' ';
                }
            }

    do{
       
   
       

        // Display Player's Board
            System.out.println(game[0][0]+" | "+game[0][1]+" | "+game[0][2]);
            System.out.println("- + - + -");
            System.out.println(game[1][0]+" | "+game[1][1]+" | "+game[1][2]);
            System.out.println("- + - + -");
            System.out.println(game[2][0]+" | "+game[2][1]+" | "+game[2][2]);
           
        // Take Input
       
        if(z%2==0)
        {
                System.out.println("Position for O:: ");
                ch='O';
        }
        else
        {
            System.out.println("Position for X:: ");
            ch='X';
        }
       
            System.out.println("Input row::");
            int r=Integer.parseInt(inp.readLine ());
            System.out.println("Input column::");
            int c=Integer.parseInt(inp.readLine ());
            if(game[r][c]!=' ')
            {
                z--;
                System.out.println("Don't Cheat!!!");
            }
            else
                game[r][c]=ch;       
           
        // Counting No. of Same input
            xcount=0;
            ocount=0;
            for(i=0;i<=2;i++)
                for(j=0; j<=2; j++)
                {
                    if(game[i][j]=='O')
                        ocount=ocount+1;
                    if(game[i][j]=='X')
                        xcount=xcount+1;
                }
       
            if(xcount>2 || ocount>2)
            {   
               
            // Row-wise checking
                for(i=0;i<=2; i++)   
                {    xcount=0;
                    ocount=0;
                    for(j=0; j<=2; j++)
                    {
                        if(game[i][j]=='O')
                            ocount+=1;
                        if(game[i][j]=='X')
                            xcount+=1;
                    }
                    if(xcount==3)
                    {
                        System.out.println("X has won");
                        flag=1;
                    }
                    if(ocount==3)
                    {
                        System.out.println("O has won");
                        flag=1;
                    }
                    if(flag==1)
                        break;
                }
                   
            // Column-wise checking
                for(i=0;i<=2; i++)   
                {    xcount=0;
                    ocount=0;
                    for(j=0; j<=2; j++)
                    {
                        if(game[j][i]=='O')
                            ocount+=1;
                        if(game[j][i]=='X')
                            xcount+=1;
                    }
                    if(xcount==3)
                    {
                        System.out.println("X has won");
                        flag=1;
                    }
                    if(ocount==3)
                    {
                        System.out.println("O has won");
                        flag=1;
                    }
                        if(flag==1)
                        break;
                }
                   
            // Fwd Diagon checking
                xcount=0;
                ocount=0;
                for(i=0;i<=2; i++)   
                {
                        if(game[i][i]=='O')
                            ocount+=1;
                        if(game[i][i]=='X')
                            xcount+=1;
                }
                    if(xcount==3)
                    {
                        System.out.println("X has won");
                        flag=1;
                    }
                    if(ocount==3)
                    {
                        System.out.println("O has won");
                        flag=1;
                    }
                        if(flag==1)
                        break;
               
                   
            // Rvs Diagon checking
                xcount=0;
                ocount=0;
                for(i=0;i<=2; i++)   
                {
                        if(game[i][2-i]=='O')
                            ocount+=1;
                        if(game[i][2-i]=='X')
                            xcount+=1;
                }
                    if(xcount==3)
                    {
                        System.out.println("X has won");
                        flag=1;
                        break;
                    }
                    if(ocount==3)
                    {
                        System.out.println("O has won");
                        flag=1;
                        break;
                    }   
                       
                }                               
                if(xcount==3 || ocount==3)
                    break;
                z=z+1;
                } while(z<9);
               
                if(flag==0)
                    System.out.println("Sorry !!! None has won!!!");
           

 // Display Player's Board
            System.out.println(game[0][0]+" | "+game[0][1]+" | "+game[0][2]);
            System.out.println("- + - + -");
            System.out.println(game[1][0]+" | "+game[1][1]+" | "+game[1][2]);
            System.out.println("- + - + -");
            System.out.println(game[2][0]+" | "+game[2][1]+" | "+game[2][2]);
                }
            }
   

It is a simple and basic logic design of the game named "Tic-Tac-Toe".

Comments

Popular posts from this blog

Class Point:: It holds co-ordinates of a point in format (x,y)

A class Named "Point" which stores the Co-ordinates of a point in (x,y) Format. This Class Contains:: O Constructor with default arguments O Copy Constructor O Function to display which Co-ordinate a Point belongs to O Function to get value of x of a point O Function to get value of y of a point O Function to get the Radious Vector (r) of a point O Function to get the Theta of a point O Overloaded Operator - to find out distance between two points O Overloaded Operator >> to take input O Overloaded operator O Destructor #include<iostream.h> #include<math.h> class point{ //Data Members int x,y; //They hold the value of X and Y co-ordinate of a Point. public: //Member Functions point(int t1=0, int t2=0) //Constructor with default arguments. { x=t1; y=t2; } point(point &t) //Copy Constructor { x=t.x; y=t.y; } int coordinate(void); //It returns the point's Co-ordinate, e.g., 1, 2, 3, or 4th. int getx(...

0s and 1s Count in 1 Byte Memory Word

This program will count all the 1s and 0s individualy presents in a 1 byte word. And store the result in individual places in Memory. Suppose, argument byte is stored in memory location 2050H and store the result of 1's count at 2060H memory location and 0's at 2061H Memory Location Hex Code Label Mnemonics Comment 3050 3051 3052 3A 50 20 START: LDA 2050H ;Load Accumulator with the number stored at location 2050H 3053 3054 06 00 MVI B, 00H ;Initiate 0's Counter 3055 3056 0E 00 MVI C, 00H ;Initiate 1's Counter 3057 3058 16 08 MVI D, 08H ;Initiate Bit Counter 3059 07 LOOP: RLC ;Rotate Accumulator Left with Carry 305A 305B 305C DA 61 30 JC ONE ;If 1 is found count 1 305D 04 INR B ;If 1 is not found it must be 0. So increase 0's Counter 305E 305F 3060 D2 62 30 JNC NEXT ;Go to next Bit 3061 0C ONE: INR C ;Increase 1's Counter 3062 15 NEXT: DCR D ;Decrease the Bit Counter 3063 3064 3065 C2 ...