You are on page 1of 6

<?xml version="1.0" encoding="utf-8"?

>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/B2"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:background="#33b5e5"
android:minHeight="64dip"
android:text=""
android:textSize="32sp" />
<Button
android:id="@+id/B1"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignBaseline="@+id/B2"
android:layout_alignBottom="@+id/B2"
android:layout_margin="16dp"
android:layout_toLeftOf="@+id/B2"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/B3"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_centerVertical="true"
android:layout_margin="16dp"
android:layout_toRightOf="@+id/B2"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/A1"
android:layout_width="64dip"
android:layout_height="64dip"

android:layout_above="@+id/B1"
android:layout_alignLeft="@+id/B1"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/A2"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignBaseline="@+id/A1"
android:layout_alignBottom="@+id/A1"
android:layout_alignLeft="@+id/B2"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/A3"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignBaseline="@+id/A2"
android:layout_alignBottom="@+id/A2"
android:layout_alignLeft="@+id/B3"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/C3"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignLeft="@+id/B3"
android:layout_below="@+id/B3"
android:layout_marginTop="15dp"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/C2"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignBaseline="@+id/C3"

android:layout_alignBottom="@+id/C3"
android:layout_alignLeft="@+id/B2"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/C1"
android:layout_width="64dip"
android:layout_height="64dip"
android:layout_alignBaseline="@+id/C2"
android:layout_alignBottom="@+id/C2"
android:layout_alignLeft="@+id/B1"
android:background="#33b5e5"
android:minHeight="64dip"
android:textSize="32sp" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="New Game" />
</RelativeLayout>
-----------------------------------------------------------package com.abs192.tictactoe;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
boolean turn = true; // true = X & false = O
int turn_count = 0;
Button[] bArray = null;
Button a1, a2, a3, b1, b2, b3, c1, c2, c3;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a1 = (Button) findViewById(R.id.A1);
b1 = (Button) findViewById(R.id.B1);
c1 = (Button) findViewById(R.id.C1);
a2 = (Button) findViewById(R.id.A2);
b2 = (Button) findViewById(R.id.B2);
c2 = (Button) findViewById(R.id.C2);
a3 = (Button) findViewById(R.id.A3);
b3 = (Button) findViewById(R.id.B3);
c3 = (Button) findViewById(R.id.C3);
bArray = new Button[] { a1, a2, a3, b1, b2, b3, c1, c2, c3 };
for (Button b : bArray)
b.setOnClickListener(this);
Button bnew = (Button) findViewById(R.id.button1);
bnew.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
turn = true;
turn_count = 0;
enableOrDisable(true);
}
});
}
@Override
public void onClick(View v) {
// just to be clear. as no other views are registered YET,
// it is safe to assume that only
// the 9 buttons call this on click
buttonClicked(v);
}
private void buttonClicked(View v) {
Button b = (Button) v;
if (turn) {
// X's turn

b.setText("X");
} else {
// O's turn
b.setText("O");
}
turn_count++;
b.setClickable(false);
b.setBackgroundColor(Color.LTGRAY);
turn = !turn;
checkForWinner();
}
private void checkForWinner() {
// NOOB TECHNIQUE...
// if used repeatedly, causes several mental injuries
// dont try this at home
boolean there_is_a_winner = false;
// horizontal:
if (a1.getText() == a2.getText() && a2.getText() == a3.getText()
&& !a1.isClickable())
there_is_a_winner = true;
else if (b1.getText() == b2.getText() && b2.getText() == b3.getText()
&& !b1.isClickable())
there_is_a_winner = true;
else if (c1.getText() == c2.getText() && c2.getText() == c3.getText()
&& !c1.isClickable())
there_is_a_winner = true;
// vertical:
else if (a1.getText() == b1.getText() && b1.getText() == c1.getText()
&& !a1.isClickable())
there_is_a_winner = true;
else if (a2.getText() == b2.getText() && b2.getText() == c2.getText()
&& !b2.isClickable())
there_is_a_winner = true;
else if (a3.getText() == b3.getText() && b3.getText() == c3.getText()
&& !c3.isClickable())
there_is_a_winner = true;

// diagonal:
else if (a1.getText() == b2.getText() && b2.getText() == c3.getText()
&& !a1.isClickable())
there_is_a_winner = true;
else if (a3.getText() == b2.getText() && b2.getText() == c1.getText()
&& !b2.isClickable())
there_is_a_winner = true;
// i repeat.. DO NOT TRY THIS AT HOME
if (there_is_a_winner) {
if (!turn)
message("X wins");
else
message("O wins");
enableOrDisable(false);
} else if (turn_count == 9)
message("Draw!");
}
private void message(String text) {
Toast.makeText(getApplicationContext(), text, Toast.LENGTH_SHORT)
.show();
}
private void enableOrDisable(boolean enable) {
for (Button b : bArray) {
b.setText("");
b.setClickable(enable);
if (enable) {
b.setBackgroundColor(Color.parseColor("#33b5e5"));
} else {
b.setBackgroundColor(Color.LTGRAY);
}
}
}
}

You might also like