Professional Documents
Culture Documents
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
/**
* Reset each button in the grid to be blank and enabled.
*/
private void resetButtons() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
if (R.getChildAt(x) instanceof Button) {
Button B = (Button) R.getChildAt(x);
B.setText("");
B.setEnabled(true);
}
}
}
}
TextView t = (TextView) findViewById(R.id.titleText);
t.setText(R.string.app_name);
}
/**
* Method that returns true when someone has won and false when nobody has.<br />
* It also display the winner on screen.
*
* @return
*/
private boolean checkWin() {
char winner = '\0';
if (checkWinner(board, 3, 'X')) {
winner = 'X';
} else if (checkWinner(board, 3, 'O')) {
winner = 'O';
}
if (winner == '\0') {
return false; // nobody won
} else {
// display winner
TextView T = (TextView) findViewById(R.id.titleText);
T.setText(winner + " wins");
return true;
}
}
/**
* This is a generic algorithm for checking if a specific player has won on a tic tac toe
board of any size.
*
* @param board the board itself
* @param size the width and height of the board
* @param player the player, 'X' or 'O'
* @return true if the specified player has won
*/
private boolean checkWinner(char[][] board, int size, char player) {
// check each column
for (int x = 0; x < size; x++) {
int total = 0;
for (int y = 0; y < size; y++) {
if (board[x][y] == player) {
total++;
}
}
if (total >= size) {
return true; // they win
}
}
}
}
if (total >= size) {
return true; // they win
}
return false; // nobody won
}
/**
* Disables all the buttons in the grid.
*/
private void disableButtons() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
if (T.getChildAt(y) instanceof TableRow) {
TableRow R = (TableRow) T.getChildAt(y);
for (int x = 0; x < R.getChildCount(); x++) {
if (R.getChildAt(x) instanceof Button) {
Button B = (Button) R.getChildAt(x);
B.setEnabled(false);
}
}
}
}
}
/**
* This will add the OnClickListener to each button inside out TableLayout
*/
private void setupOnClickListeners() {
TableLayout T = (TableLayout) findViewById(R.id.tableLayout);
for (int y = 0; y < T.getChildCount(); y++) {
noughtsTurn = !noughtsTurn;
// check if anyone has won
if (checkWin()) {
disableButtons();
}
}
}
}
}
ServerActivity.java
package com.example.NaughtsAndCrosses;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.SocketException;
import java.util.Enumeration;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
public class ServerActivity extends Activity {
private TextView serverStatus;
// DEFAULT IP
public static String SERVERIP = "10.0.2.15";
// DESIGNATE A PORT
public static final int SERVERPORT = 8080;
private Handler handler = new Handler();
private ServerSocket serverSocket;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.server);
serverStatus = (TextView) findViewById(R.id.server_status);
SERVERIP = getLocalIpAddress();
new Handler().post(new ServerThread());
/*
}
public class ServerThread implements Runnable {
public void run() {
try {
if (SERVERIP != null) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Listening on IP: " + SERVERIP);
}
});
serverSocket = new ServerSocket(SERVERPORT);
while (true) {
}
} else {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Couldn't detect internet connection.");
}
});
}
} catch (Exception e) {
handler.post(new Runnable() {
@Override
public void run() {
serverStatus.setText("Error");
}
});
e.printStackTrace();
}
}
}
// GETS THE IP ADDRESS OF YOUR PHONE'S NETWORK
private String getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress()) { return
inetAddress.getHostAddress().toString(); }
}
}
</LinearLayout>
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:orientation="vertical" >
<Button
android:id="@+id/newGameBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_column="0"
android:layout_gravity="center_horizontal"
android:onClick="newGame"
android:text="New Game"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TableLayout
android:id="@+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/topLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/topRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="1"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="2"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
<Button
android:id="@+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_column="3"
android:height="100dp"
android:text="O"
android:textAppearance="?android:attr/textAppearanceLarge"
android:width="100dp" />
</TableRow>
</TableLayout>
<TextView
android:id="@+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="@android:color/black" />
</LinearLayout>
Server.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/server_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
***************************************************************************
***
Output: