Sunday, April 19, 2015

Simple TicTacToe android game with source code


TicTacToe android game
TicTacToe is a very simple and fun game and very easy to develop.You can get source code of this game from link below or Click read-more to copy code from below..

Download






Splash screen code

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;

public class Start extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
}
public void startclass(View v)
       {
Intent i=new Intent();
i.setClass(Start.this,MainActivity.class);
startActivity(i);
}

public void Exit(View v){
Start.this.finish();
}
}


MainActivity Class

import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.graphics.Color;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
Button one, two, three, four, five, six, seven, eight, nine, start;
Button yes, no;
Dialog d;
MediaPlayer bsound1, bsound2;
int a = 1, counter;
String s;
TextView txt1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

bsound1 = MediaPlayer.create(this, R.raw.sound1);
bsound2 = MediaPlayer.create(this, R.raw.sound2);
one = (Button) findViewById(R.id.one);
two = (Button) findViewById(R.id.two);
three = (Button) findViewById(R.id.three);
four = (Button) findViewById(R.id.four);
five = (Button) findViewById(R.id.five);
six = (Button) findViewById(R.id.six);
seven = (Button) findViewById(R.id.seven);
eight = (Button) findViewById(R.id.eight);
nine = (Button) findViewById(R.id.nine);
txt1 = (TextView) findViewById(R.id.textView1);

}

public void winX() {

if (one.getText().equals("X") && two.getText().equals("X")
&& three.getText().equals("X")) {

txt1.setText("PLAYER X WINS");

dialog();

} else if (four.getText().equals("X") && five.getText().equals("X")
&& six.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();
} else if (seven.getText().equals("X") && eight.getText().equals("X")
&& nine.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();

} else if (one.getText().equals("X") && four.getText().equals("X")
&& seven.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();

} else if (two.getText().equals("X") && five.getText().equals("X")
&& eight.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();
}

else if (three.getText().equals("X") && six.getText().equals("X")
&& nine.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();
}

else if (one.getText().equals("X") && five.getText().equals("X")
&& nine.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();
} else if (three.getText().equals("X") && five.getText().equals("X")
&& seven.getText().equals("X")) {

txt1.setText("PLAYER X WINS");
dialog();
}

}

public void winO() {
if (one.getText().equals("O") && two.getText().equals("O")
&& three.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();

} else if (four.getText().equals("O") && five.getText().equals("O")
&& six.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();
} else if (seven.getText().equals("O") && eight.getText().equals("O")
&& nine.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();

} else if (one.getText().equals("O") && four.getText().equals("O")
&& seven.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();

} else if (two.getText().equals("O") && five.getText().equals("O")
&& eight.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();
}

else if (three.getText().equals("O") && six.getText().equals("O")
&& nine.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();
}

else if (one.getText().equals("O") && five.getText().equals("O")
&& nine.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();
} else if (three.getText().equals("O") && five.getText().equals("O")
&& seven.getText().equals("O")) {

txt1.setText("PLAYER O WINS");
dialog();
}
}

public void draw() {

if (counter == 8) {

txt1.setText("GAME DRAW");

dialog();
}
}

public void dialog() {
d = new Dialog(MainActivity.this);
d.setTitle("------ PLAY AGAIN? ------");
d.setContentView(R.layout.dialog);
d.show();
d.setCancelable(false);

yes = (Button) d.findViewById(R.id.yes);
no = (Button) d.findViewById(R.id.no);

yes.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

MainActivity.this.recreate();
d.cancel();

}
});

no.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

MainActivity.this.finish();

}
});
}

public void start(View v) {

switch (v.getId()) {
case R.id.one:

if (a == 1) {
one.setText("X");
one.setTextColor(Color.parseColor("#fd482f"));
one.setEnabled(false);
a = 2;
bsound1.start();
counter++;

}

else if (a == 2) {
one.setText("O");
bsound2.start();
one.setTextColor(Color.parseColor("#4099ff"));
a = 1;
one.setEnabled(false);
counter++;
 }
winX();
winO();
draw();
break;

case R.id.two:
if (a == 1) {
two.setText("X");
two.setTextColor(Color.parseColor("#fd482f"));
two.setEnabled(false);
counter++;
bsound1.start();
a = 2;
}

else if (a == 2) {
two.setText("O");
bsound2.start();
two.setTextColor(Color.parseColor("#4099ff"));
a = 1;
two.setEnabled(false);
counter++;
 }
winX();
winO();
draw();

break;

case R.id.three:
if (a == 1) {
three.setText("X");
three.setTextColor(Color.parseColor("#fd482f"));
three.setTextColor(Color.parseColor("#fd482f"));
counter++;
bsound1.start();
three.setEnabled(false);
a = 2;
  }

else if (a == 2) {
three.setText("O");
bsound2.start();
three.setTextColor(Color.parseColor("#4099ff"));
counter++;
three.setEnabled(false);
a = 1;
 }
winX();
winO();
draw();

break;

case R.id.four:
if (a == 1) {
four.setText("X");
bsound1.start();
four.setTextColor(Color.parseColor("#fd482f"));
counter++;
four.setEnabled(false);
a = 2;
  }

else if (a == 2) {
four.setText("O");
bsound2.start();
four.setTextColor(Color.parseColor("#4099ff"));
counter++;
four.setEnabled(false);
a = 1;
  }
winX();
winO();
draw();
break;

case R.id.five:
if (a == 1) {
five.setText("X");
bsound1.start();
five.setTextColor(Color.parseColor("#fd482f"));
counter++;
five.setEnabled(false);
a = 2;

}

else if (a == 2) {
five.setText("O");
bsound2.start();
counter++;
five.setTextColor(Color.parseColor("#4099ff"));
five.setEnabled(false);
a = 1;
}
winX();
winO();
draw();
break;

case R.id.six:
if (a == 1) {
six.setText("X");
bsound1.start();
six.setTextColor(Color.parseColor("#fd482f"));
counter++;
six.setEnabled(false);
a = 2;
  }
else if (a == 2) {
six.setText("O");
bsound2.start();
six.setTextColor(Color.parseColor("#4099ff"));
counter++;
six.setEnabled(false);
a = 1;
 }
winX();
winO();
draw();
break;

case R.id.seven:
if (a == 1) {
seven.setText("X");
bsound1.start();
seven.setTextColor(Color.parseColor("#fd482f"));
counter++;
seven.setEnabled(false);
a = 2;
   }

else if (a == 2) {
seven.setText("O");
bsound2.start();
seven.setTextColor(Color.parseColor("#4099ff"));
counter++;
seven.setEnabled(false);
a = 1; 
  }
winX();
winO();
draw();
break;

case R.id.eight:
if (a == 1) {
eight.setText("X");
bsound1.start();
eight.setTextColor(Color.parseColor("#fd482f"));
counter++;
eight.setEnabled(false);
a = 2;
  }

else if (a == 2) {
eight.setText("O");
bsound2.start();
eight.setTextColor(Color.parseColor("#4099ff"));
counter++;
eight.setEnabled(false);
a = 1;
   }
winX();
winO();
draw();
break;

case R.id.nine:
if (a == 1) {
nine.setText("X");
bsound1.start();
nine.setTextColor(Color.parseColor("#fd482f"));
counter++;
nine.setEnabled(false);
a = 2;
      }
    else if (a == 2) {
nine.setText("O");
bsound2.start();
nine.setTextColor(Color.parseColor("#4099ff"));
counter++;
nine.setEnabled(false);
a = 1;
   }
winX();
winO();
draw();
break;
}
}
}

No comments:

Post a Comment