Back to project page Android-Games.
The source code is released under:
GNU General Public License
If you think the Android project Android-Games listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package game.basic.ttt; //w w w . j a v a2 s .c om import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class Main extends Activity { boolean flag=true; Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); reset(); } private void reset() { flag=true; ((Button)findViewById(R.id.button1)).setText(" "); ((Button)findViewById(R.id.button2)).setText(" "); ((Button)findViewById(R.id.button3)).setText(" "); ((Button)findViewById(R.id.button4)).setText(" "); ((Button)findViewById(R.id.button5)).setText(" "); ((Button)findViewById(R.id.button6)).setText(" "); ((Button)findViewById(R.id.button7)).setText(" "); ((Button)findViewById(R.id.button8)).setText(" "); ((Button)findViewById(R.id.button9)).setText(" "); } public void onClickMethod(View v) { btn=(Button)v; if(btn.getId()==R.id.button10){reset();} else if(!btn.getText().toString().contains("X")&&!btn.getText().toString().contains("O")){ if(flag){btn.setText("X");flag=!flag;} else{btn.setText("O");flag=!flag;} } chkWin(); } private void chkWin() { // Horizontal Check if(((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button2)).getText() && ((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button3)).getText() && (((Button)findViewById(R.id.button1)).getText().toString().contains("X") ||((Button)findViewById(R.id.button1)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button1)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} if(((Button)findViewById(R.id.button4)).getText()== ((Button)findViewById(R.id.button5)).getText() && ((Button)findViewById(R.id.button4)).getText()== ((Button)findViewById(R.id.button6)).getText() && (((Button)findViewById(R.id.button4)).getText().toString().contains("X") ||((Button)findViewById(R.id.button4)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button4)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} if(((Button)findViewById(R.id.button7)).getText()== ((Button)findViewById(R.id.button8)).getText() && ((Button)findViewById(R.id.button7)).getText()== ((Button)findViewById(R.id.button9)).getText() && (((Button)findViewById(R.id.button7)).getText().toString().contains("X") ||((Button)findViewById(R.id.button7)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button7)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} // Vertical Check if(((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button4)).getText() && ((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button7)).getText() && (((Button)findViewById(R.id.button1)).getText().toString().contains("X") ||((Button)findViewById(R.id.button1)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button1)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} if(((Button)findViewById(R.id.button2)).getText()== ((Button)findViewById(R.id.button5)).getText() && ((Button)findViewById(R.id.button2)).getText()== ((Button)findViewById(R.id.button8)).getText() && (((Button)findViewById(R.id.button2)).getText().toString().contains("X") ||((Button)findViewById(R.id.button2)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button2)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} if(((Button)findViewById(R.id.button3)).getText()== ((Button)findViewById(R.id.button6)).getText() && ((Button)findViewById(R.id.button3)).getText()== ((Button)findViewById(R.id.button9)).getText() && (((Button)findViewById(R.id.button3)).getText().toString().contains("X") ||((Button)findViewById(R.id.button3)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button3)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} // Diagonal Check if(((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button5)).getText() && ((Button)findViewById(R.id.button1)).getText()== ((Button)findViewById(R.id.button9)).getText() && (((Button)findViewById(R.id.button1)).getText().toString().contains("X") ||((Button)findViewById(R.id.button1)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button1)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} if(((Button)findViewById(R.id.button3)).getText()== ((Button)findViewById(R.id.button5)).getText() && ((Button)findViewById(R.id.button3)).getText()== ((Button)findViewById(R.id.button7)).getText() && (((Button)findViewById(R.id.button3)).getText().toString().contains("X") ||((Button)findViewById(R.id.button3)).getText().toString().contains("O"))) {Toast.makeText(getApplicationContext(),((Button)findViewById(R.id.button3)).getText()+" wins.",Toast.LENGTH_SHORT).show(); reset();} } }