Anda di halaman 1dari 15

private void checkResult() {

boolean empty = false;


AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
for (int i = 0; i != 3; ++i)
{
for (int j = 0; j != 3; ++j)
{
if (table[i][j]==0)
{
empty = true;
break;
}
}
}
if (!empty)
{
dlgAlert.setMessage("Draw!");
dlgAlert.setTitle("Draw");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
dialog, int which) {
23.
finish();
24.
25.
}
26.
});
27.
dlgAlert.create().show();
28.
29.
}
30.
//check horizontal lines
31.
for (int i = 0; i != 3; ++i)
32.
{
33.
if (table[i][0] == 1 && table[i][1] == 1 && table[i][2]
== 1)
34.
{
35.
dlgAlert.setMessage("O Player wins!");
36.
dlgAlert.setTitle("congratulations");
37.
dlgAlert.setCancelable(true);
38.
dlgAlert.setPositiveButton("Ok",
39.
new DialogInterface.OnClickListener() {
40.
public void
onClick(DialogInterface dialog, int which) {
41.
finish();
42.
}
43.
});
44.
dlgAlert.create().show();
45.
46.
}
47.
if (table[i][0] == 2 && table[i][1] == 2 && table[i][2]
== 2)
48.
{
49.
dlgAlert.setMessage("X Player wins!");
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.

50.
51.
52.
53.
54.

dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void
onClick(DialogInterface dialog, int which) {
55.
finish();
56.
}
57.
});
58.
dlgAlert.create().show();
59.
60.
}
61.
}
62.
//check vertical lines
63.
for (int i = 0; i != 3; ++i)
64.
{
65.
if (table[0][i] == 1 && table[1][i] == 1 && table[2][i]
== 1)
66.
{
67.
dlgAlert.setMessage("O Player wins!");
68.
dlgAlert.setTitle("congratulations");
69.
dlgAlert.setCancelable(true);
70.
dlgAlert.setPositiveButton("Ok",
71.
new DialogInterface.OnClickListener() {
72.
public void
onClick(DialogInterface dialog, int which) {
73.
finish();
74.
}
75.
});
76.
dlgAlert.create().show();
77.
78.
}
79.
if (table[0][i] == 2 && table[1][i] == 2 && table[2][i]
== 2)
80.
{
81.
dlgAlert.setMessage("X Player wins!");
82.
dlgAlert.setTitle("congratulations");
83.
dlgAlert.setCancelable(true);
84.
dlgAlert.setPositiveButton("Ok",
85.
new DialogInterface.OnClickListener() {
86.
public void
onClick(DialogInterface dialog, int which) {
87.
finish();
88.
}
89.
});
90.
dlgAlert.create().show();
91.
92.
}
93.
}
94.
//check diagonals
95.
if (table[0][0] == 1 && table[1][1] == 1 && table[2][2] == 1)
96.
{
97.
dlgAlert.setMessage("O Player wins!");

98.
99.
100.
101.
102.
dialog,
103.
104.
105.
106.
107.
108.
109.
110.
111.
112.
113.
114.
115.
116.
dialog,
117.
118.
119.
120.
121.
122.
123.
124.
125.
126.
127.
128.
129.
130.
dialog,
131.
132.
133.
134.
135.
136.
137.
138.
139.
140.
141.
142.
143.
144.
dialog,
145.

dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
int which) {
finish();
}
});
dlgAlert.create().show();
}
if (table[0][0] == 2 && table[1][1] == 2 && table[2][2] == 2)
{
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
int which) {
finish();
}
});
dlgAlert.create().show();
}
if (table[0][2] == 1 && table[1][1] == 1 && table[2][0] == 1)
{
dlgAlert.setMessage("O Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
int which) {
finish();
}
});
dlgAlert.create().show();
}
if (table[0][2] == 2 && table[1][1] == 2 && table[2][0] == 2)
{
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface
int which) {
finish();

146.
147.
148.
149.
150.
151.

}
});
dlgAlert.create().show();
}
}

The Tic-tac-toe game is really a simple application that can be easily implemented in
Android. We will start the implementation of this application by creating the welcome screen
with 4 buttons: "Normal Mode", "Ultimate Mode", "About" and "Exit" buttons. In this article
you can read about the developing of a classic Tic-tac-toe game, that can be played by
pressing the "Normal Mode" button.
When you create an Android application in Eclipse, you can specify the option of creating a
blank activity which will be show on the start up of your application. In this activity I added 4
buttons:

start. Main.actvity

<Button android;
:id = "@+id/normalMode";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
layout_alignParentTop = "true";
android:
layout_centerHorizontal = "true";
android:
layout_marginTop = "51dp";
android:
background = "@color/button";
android:
onClick = "@string/normal";
android:
text@string/normalTextGreater<Button android;
:id = "@+id/UltimateMode";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
layout_alignLeft = "@+id/normalMode";
android:
layout_below = "@+id/normalMode";
android:
layout_marginTop = "41dp";
android:
background = "@color/button";
android:
onClick = "@string/ultimate";
android:
text@string/ultimateTextGreater<Button android;
:id = "@+id/AboutBtn";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
layout_alignLeft = "@+id/UltimateMode";
android:
layout_below = "@+id/UltimateMode";
android:
layout_marginTop = "42dp";
android:
background = "@color/button";
android:
onClick = "@string/about";
android:
text@string/aboutTextGreater<Button android;
:id = "@+id/Exit";

android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
layout_alignLeft = "@+id/AboutBtn";
android:
layout_below = "@+id/AboutBtn";
android:
layout_marginTop = "41dp";
android:
background = "@color/button";
android:
onClick = "@string/exit";
android:
text@string/exitTextGreater

Now, let's take a look on some attributes of the each button:


android:id - is an unique identifier for each element.
android:layout_width - the width of the button
android:layout_height - the height of the button
android:onClick - this attribute specifies which method in the activity java file will handle

the click on the button. For example, if we specify this attribute in the next way:
android:onClick="@string/exit"

a method with the name, specified in the string resource file with tag "exit" will handle the
click event. This entry in the resource file is:
<string name;

The next step is to write handlers for all the buttons we created.
The "Normal Mode" button will start a new classic Tic-tac-toe game by launching new
Activity, called NormalActivity:

public void normal_click(View v) {


Intent myIntent = new Intent(MainActivity.this, NormalActivity.class);
MainActivity.this.startActivity(myIntent);
}

The "Ultimate Mode" button will show a message, that this feature is under construction and
it will be available later:

public void ultimate_click(View v) {


AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Under construction");
dlgAlert.setTitle("comming soon");
dlgAlert.setCancelable(true);
dlgAlert.create().show();
}

"About" button will show the information about the author of this application:
public void about_click(View v)
{
Intent myIntent = new Intent(MainActivity.this,
About.class);
4.
MainActivity.this.startActivity(myIntent);
5.
}
1.
2.
3.

The "Exit" button will show another dialog window, but it will have an "OK" button for
confirming the exit:
public void exit_click(View v) {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Do you really want to exit");
dlgAlert.setTitle("Exit");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}

The "OK" button and handle for it is set by the next lines of code:
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());

The whole game logic is encapsulated in NormalActivity class. This class has the following
data members for representing Tic-tac-toe game board and the player's turn:
int[,] table;
bool xMove;

The layout of this activity is represented by a 3x3 grid of buttons that represent the state of
the Tic-tac-toe board:

<RelativeLayout xmlns;
:android = "http://schemas.android.com/apk/res/android";
xmlns:
tools = "http://schemas.android.com/tools";
android:
layout_width = "match_parent";
android:
layout_height = "match_parent";
android:
layout_marginLeft = "0dp";
android:
background = "@color/background";
android:
gravity = "center_horizontal";
android:
label = "@string/normal_activity_label";
android:
paddingBottom = "@dimen/activity_vertical_margin";
android:
paddingLeft = "@dimen/activity_horizontal_margin";
android:
paddingRight = "@dimen/activity_horizontal_margin";
android:
paddingTop = "@dimen/activity_vertical_margin";
tools:
context.NormalActivity<TextView android;
:id = "@+id/moveTextView";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
layout_alignParentTop = "true";
android:
layout_marginLeft = "34dp";
android:
layout_marginTop = "18dp";
android:
gravity = "center_horizontal";
android:
text = "@string/x_move";
android:
textAppearance?android:attr/textAppearanceLargeGreater<TableLayout android;
:id = "@+id/tableLayout1";
android:
layout_width = "match_parent";
android:

layout_height = "wrap_content";
android:
layout_below = "@+id/moveTextView";
android:
layout_marginTop = "32dp";
android:
gravitycenter_vertical|center_horizontal<TableRow android;
:id = "@+id/tableRow1";
android:
layout_width = "wrap_content";
android:
layout_heightwrap_content<Button android;
:id = "@+id/button1";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
height = "100dp";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
width100dpGreater<Button android;
:id = "@+id/button2";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater<Button android;
:id = "@+id/button3";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater</TableRow<TableRow android;
:id = "@+id/tableRow2";
android:

layout_width = "wrap_content";
android:
layout_heightwrap_content<Button android;
:id = "@+id/button4";
android:
layout_width = "wrap_content";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater<Button android;
:id = "@+id/button5";
android:
layout_width = "wrap_content";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater<Button android;
:id = "@+id/button6";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater</TableRow<TableRow<Button android;
:id = "@+id/button7";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";

android:
width100dpGreater<Button android;
:id = "@+id/button8";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater<Button android;
:id = "@+id/button9";
android:
layout_width = "match_parent";
android:
layout_height = "wrap_content";
android:
onClick = "@string/makeMove";
android:
text = "@string/empty";
android:
height = "100dp";
android:
width100dpGreater</TableRow</TableLayout</RelativeLayout

For this case a Table Layout is used inside of Relative layout

Every press of the cell is handled by makeMove method from NormalActivity.java. This
method gets the id of the button that is checked, check if this cell is empty. In the case the
cell is empty, a record is added to board and after this the check for result is performed. We
have to check all horizontal, vertical and diagonal lines from the board:

Start..main..java
private void checkResult() {
bool empty = false;
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
for (int i = 0; (i != 3); i++) {
for (int j = 0; (j != 3); j++) {
if ((table[i][j] == 0)) {

empty = true;
break;
}
}
}
if (!empty) {
dlgAlert.setMessage("Draw!");
dlgAlert.setTitle("Draw");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
// check horizontal lines
for (int i = 0; (i != 3); i++) {
if (((table[i][0] == 1)
&& ((table[i][1] == 1)
&& (table[i][2] == 1)))) {
dlgAlert.setMessage("O Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
if (((table[i][0] == 2)
&& ((table[i][1] == 2)
&& (table[i][2] == 2)))) {
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
}
// check vertical lines
for (int i = 0; (i != 3); i++) {
if (((table[0][i] == 1)
&& ((table[1][i] == 1)
&& (table[2][i] == 1)))) {
dlgAlert.setMessage("O Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
if (((table[0][i] == 2)

&& ((table[1][i] == 2)
&& (table[2][i] == 2)))) {
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
}
// check diagonals
if (((table[0][0] == 1)
&& ((table[1][1] == 1)
&& (table[2][2] == 1)))) {
dlgAlert.setMessage("O Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
if (((table[0][0] == 2)
&& ((table[1][1] == 2)
&& (table[2][2] == 2)))) {
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
if (((table[0][2] == 1)
&& ((table[1][1] == 1)
&& (table[2][0] == 1)))) {
dlgAlert.setMessage("O Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
if (((table[0][2] == 2)
&& ((table[1][1] == 2)
&& (table[2][0] == 2)))) {
dlgAlert.setMessage("X Player wins!");
dlgAlert.setTitle("congratulations");
dlgAlert.setCancelable(true);
dlgAlert.setPositiveButton("Ok", new DialogInterface.OnClickListener());
dlgAlert.create().show();
}
}

Anda mungkin juga menyukai