Sunday, 4 May 2014

TIC TAC TOE IN PROCESSING UNDER ANDROID MODE

Hello everyone!!!
I had a thought of developing an Android application for a long time. I created an application successfully and became little familiar now on how to create an android app using processing software.

Professional android apps are developed using eclipse, but it is very tedious for me to learn because of lack of knowledge in Java. My friend gave me an idea that we can create android apps using processing under android mode easily which uses basic C syntax. I tried it and became successful within a short period.

Refer the link below to learn android processing. I recommend it for developing simple to medium complexity android apps.

All you have to do is to download Processing software from the following link.

Then open processing, initially on the top right corner Java mode will be seen. We need to change to android mode to create android apps. I will show you the shortest way to create unsigned android app for the program written. Signed app can be created by following the steps available in the internet to distribute the developed app in Google Play (which I haven’t tried).

Click the Java mode on top right corner, from the drop down select add mode. Then install android mode from the newly opened window.


Now the top right corner will change to android mode. Write the code and select sketch à run in emulator. To run in emulator you have to install latest JDK, the steps to do it is given in processing website.


After selecting run in emulator, you can see some texts running on the console. After the “debug” text appear on the console, move few lines back to see the link which ends with .apk extension.




Copy the link and paste it in Run from start menu. Then a window opens where the apk file for the program exist. We can copy and install in android phone to run the app.

 












The code running in emulator




The unsigned .apk file to install in android phone is given below. 
https://docs.google.com/file/d/0BzTE0x13LEHYWUxUdmtmX2xHMzQ/edit

  

Code for the Tic Tac Toe application is given below.

PFont f;
int g=1,i,j,wid,hei;
float x=0.0;
Boolean win=false; 
Boolean breaker=true;
int[][] mat;
char[] s={'O','X'};
void setup()
{
   mat=new int[3][3];
   for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
        mat[i][j]=0;   
      }    
  if(displayWidth>displayHeight)    // For scaling different screen sizes
  {
    wid=displayHeight;
    hei=displayHeight;
  }
  else
  {
    wid=displayWidth;
    hei=displayWidth;
  }
  
  background(0);
  while(700*x<wid)
  x=x+0.01;
  scale(x);
  template();  
}

void draw()
{
 scale(x);
  if(mousePressed && breaker==true)
  {    
    f=createFont("Georgia",100);
    textFont(f);
    fill(255);
    
    if(mouseX<2*wid/7&&mouseY<2*hei/7&&win==false)
    {
      text(s[g],70,130); //1st box
      mat[0][0]=g+2;
    }
    else if(mouseX<4*wid/7&&mouseY<2*hei/7&&win==false)
    {
      text(s[g],270,130);//2nd box
      mat[0][1]=g+2;
    }
    else if(mouseX<6*wid/7&&mouseY<2*hei/7&&win==false)
    {
      text(s[g],470,130);//3rd box
      mat[0][2]=g+2;
    }
    else if(mouseX<2*wid/7&&mouseY<4*hei/7&&win==false)
    {
      text(s[g],70,330);//4th box
      mat[1][0]=g+2;
    }
    else if(mouseX<4*wid/7&&mouseY<4*hei/7&&win==false)
    {
      text(s[g],270,330);//5th box 
      mat[1][1]=g+2;    
    }
    else if(mouseX<6*wid/7&&mouseY<4*hei/7&&win==false)
    {
      text(s[g],470,330);//6th box
      mat[1][2]=g+2;
    }
    else if(mouseX<2*wid/7&&mouseY<6*hei/7&&win==false)
    {
      text(s[g],70,530);//7th box
      mat[2][0]=g+2; 
    }
    else if(mouseX<4*wid/7&&mouseY<6*hei/7&&win==false)
    {       
      text(s[g],270,530);//8th box
      mat[2][1]=g+2;
    }
    else if(mouseX<6*wid/7&&mouseY<6*hei/7&&win==false)
    {        
      text(s[g],470,530);//9th box
      mat[2][2]=g+2;
    }
    else if(mouseX>4*wid/7&&mouseY>6*hei/7)//reset
    {
      for(i=0;i<3;i++)
      {
        for(j=0;j<3;j++)
        mat[i][j]=0;
      }
      win=false; 
      background(0);      
    }
    for(i=0;i<3;i++)
    {
      stroke(255,0,0); 
      if((mat[0][0]==g+2&&mat[1][1]==g+2&&mat[2][2]==g+2))
      {
        line(0,0,600,600);
        win=true;
      } 
      else if((mat[0][2]==g+2&&mat[1][1]==g+2&&mat[2][0]==g+2))
      {
        line(0,600,600,0);
        win=true;
      } 
      else if((mat[0][i]==g+2&&mat[1][i]==g+2&&mat[2][i]==g+2))
      {
        line(100+(i*200),0,100+(i*200),600);
        win=true;
      }
      else if((mat[i][0]==g+2&&mat[i][1]==g+2&&mat[i][2]==g+2))
      {
        line(0,100+(i*200),600,100+(i*200));
        win=true;
      }
    }
    
    if(win!=true)
    {
    g=g+1;
    if(g==2)
    g=0;
    }
    template();
    breaker=false;
  }
  if(breaker==false && mousePressed==false)
  breaker=true;
}

void template()
{
  stroke(255);
  strokeWeight(15);
  fill(255);
  line(200,0,200,700);
  line(400,0,400,700);
  line(0,200,600,200);
  line(0,400,600,400);
  line(0,600,600,600);
  f=createFont("Georgia",30);
  textFont(f);
  text("Tic Tac Toe",225,660);
  fill(255,0,0);
  text("Reset",460,660);
  fill(0);
  noStroke();
  rect(20,625,125,50);  
  f=createFont("Georgia",30);
  textFont(f);
  fill(255);  
  if(win==true)
  {
    text(s[g],25,660);
    text("WINS",50,660); 
  }
  else
  {
    text(s[g],25,660);
    text("'s Turn",50,660);
  }  
}



1 comment: