So I just started programming in java, and tried to make a TicTicToeGame, which succeeded :) The problem is, that you can only play 1 game of TicTacToe, so I wanted to make a button to restart the game and a button to quit the game. Since I added this and try to run it on my phone it crashes when I start it up. Here is my MainActivity.java:
package com.example.testjk;
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.graphics.Color;
import android.inputmethodservice.ExtractEditText;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements OnClickListener{
private TicTacToeGame mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBoardButtons = new Button[mGame.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);
Button mTen = (Button) findViewById(R.id.ten);
mTen.setOnClickListener((OnClickListener) this);
Button mEleven = (Button) findViewById(R.id.eleven);
mEleven.setOnClickListener((OnClickListener) this);
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humancount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mHumanCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeGame();
startNewGame();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.ten:
startNewGame();
break;
case R.id.eleven:
MainActivity.this.finish();
break;
}
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if(mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner ==2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else if (winner ==3)
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player,location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
{
mBoardButtons[location].setTextColor(Color.RED);
}
}
}
This is my strings.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">testjk</string>
<string name="hello_world">Hello world!</string>
<string name="turn_human">Jouw beurt.</string>
<string name="first_human">Jij mag eerst beginnen.</string>
<string name="turn_computer">computer is aan het nadenken</string>
<string name="result_tie">Gelijkspel!</string>
<string name="result_human_wins">net aan gewonnen!</string>
<string name="result_android_wins">sukkel!</string>
<string name="one">1</string>
<string name="two">2</string>
<string name="three">3</string>
<string name="four">4</string>
<string name="five">5</string>
<string name="six">6</string>
<string name="seven">7</string>
<string name="eight">8</string>
<string name="nine">9</string>
<string name="ten">nieuw spel</string>
<string name="eleven">stoppen</string>
<string name="info">Info</string>
<string name="human">JIJ: </string>
<string name="ties">Gelijkspel: </string>
<string name="android">Computer: </string>
<string name="contact_heading">Contact Informatie</string>
<string name="action_settings">Settings</string>
<string name="contact_info">
<b>Name:</b> Wouter\n
<b>Achternaam</b> de Jong\n
<b>Email</b> [email protected]
</string>
<string name="about_heading">over </string>
<string name="TicTacToeGame">Boter Kaas en Eieren van wouter de jong</string>
</resources>
This is my activity_main.xml:
<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_marginTop="26dp"
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="com.example.testjk.MainActivity" >
<TableLayout
android:id="@+id/PlayArea"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >
<Button
android:id="@+id/one"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/one"
android:textSize="70dp" />
<Button
android:id="@+id/two"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/two"
android:textSize="70dp" />
<Button
android:id="@+id/three"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/three"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >
<Button
android:id="@+id/four"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/four"
android:textSize="70dp" />
<Button
android:id="@+id/five"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/five"
android:textSize="70dp" />
<Button
android:id="@+id/six"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/six"
android:textSize="70dp" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="clip_horizontal" >
<Button
android:id="@+id/seven"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/seven"
android:textSize="70dp" />
<Button
android:id="@+id/eight"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/eight"
android:textSize="70dp" />
<Button
android:id="@+id/nine"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="@string/nine"
android:textSize="70dp" />
</TableRow>
</TableLayout>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/information"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal|clip_horizontal|fill_horizontal" >
<TextView
android:id="@+id/human"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/human" />
<TextView
android:id="@+id/humancount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/ties"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/ties" />
<TextView
android:id="@+id/tiesCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp" />
<TextView
android:id="@+id/androidcount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/android" />
<TextView
android:id="@+id/androidCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableRow>
<TextView
android:id="@+id/information"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/PlayArea"
android:layout_below="@+id/PlayArea"
android:gravity="center"
android:text="@string/info"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/tableRow4"
android:layout_marginTop="32dp" >
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/ten"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="@string/ten" />
<Button
android:id="@+id/eleven"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:text="@string/eleven" />
</TableRow>
</TableLayout>
</RelativeLayout>
TicTacToeGame.java:
package com.example.testjk;
import java.util.Random;
public class TicTacToeGame {
private char mBoard[];
private final static int BOARD_SIZE = 9;
public static final char HUMAN_PLAYER = 'X';
public static final char ANDROID_PLAYER = '0';
public static final char EMPTY_SPACE = ' ';
private Random mRand;
public static int getBOARD_SIZE() {
return BOARD_SIZE;
}
public TicTacToeGame(){
mBoard = new char[BOARD_SIZE];
for (int i = 0; i < BOARD_SIZE; i++)
mBoard[i] = EMPTY_SPACE;
mRand = new Random();
}
public void clearBoard()
{
for (int i = 0;i < BOARD_SIZE; i++)
{
mBoard[i] = EMPTY_SPACE;
}
}
public void setMove(char player, int location)
{
mBoard[location] = player;
}
public int getComputerMove()
{
int move;
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
{
char curr = mBoard[i];
mBoard[i] = ANDROID_PLAYER;
if (checkForWinner() == 3)
{
setMove(ANDROID_PLAYER, i);
return i;
}
else
mBoard[i] = curr;
}
}
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
{
char curr = mBoard[i];
mBoard[i] = HUMAN_PLAYER;
if (checkForWinner() == 2)
{
setMove(ANDROID_PLAYER, i);
return i;
}
else
mBoard[i] = curr;
}
}
do
{
move = mRand.nextInt(getBOARD_SIZE());
}while (mBoard[move]==HUMAN_PLAYER || mBoard[move] == ANDROID_PLAYER);
setMove(ANDROID_PLAYER, move);
return move;
}
public int checkForWinner()
{
for (int i=0;i<=6;i+=3)
{
if (mBoard[i] == HUMAN_PLAYER &&
mBoard[i+1] == HUMAN_PLAYER &&
mBoard[i+2] == HUMAN_PLAYER)
return 2;
if (mBoard[i] == ANDROID_PLAYER &&
mBoard[i+1] == ANDROID_PLAYER &&
mBoard[i+2] == ANDROID_PLAYER)
return 3;
}
for (int i = 0; i <=2; i++)
{
if (mBoard[i] == HUMAN_PLAYER &&
mBoard[i+3] == HUMAN_PLAYER &&
mBoard[i+6] == HUMAN_PLAYER)
return 2;
if (mBoard[i] == ANDROID_PLAYER &&
mBoard[i+3] == ANDROID_PLAYER &&
mBoard[i+6] == ANDROID_PLAYER)
return 3;
}
if((mBoard[0] == HUMAN_PLAYER &&
mBoard[4] == HUMAN_PLAYER &&
mBoard[8] == HUMAN_PLAYER)||
(mBoard[2] == HUMAN_PLAYER &&
mBoard[4] == HUMAN_PLAYER &&
mBoard[6] == HUMAN_PLAYER))
return 2;
if((mBoard[0] == ANDROID_PLAYER &&
mBoard[4] == ANDROID_PLAYER &&
mBoard[8] == ANDROID_PLAYER)||
(mBoard[2] == ANDROID_PLAYER &&
mBoard[4] == ANDROID_PLAYER &&
mBoard[6] == ANDROID_PLAYER))
return 3;
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != HUMAN_PLAYER && mBoard[i] != ANDROID_PLAYER)
return 0;
}
return 1;
}
}
AndroidManiFest.xml in my bin/res folder:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testjk"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_hoi"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
game_menu.xml in my res/menu folder:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
</menu>
Those are all my classes I edited. And this is the logcat I get: http://i.imgur.com/XhcNyy7.png
package com.example.testjk;
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.TextView;
public class MainActivity extends Activity implements OnClickListener{
private TicTacToeGame mGame;
private Button mBoardButtons[];
private TextView mInfoTextView;
private TextView mHumanCount;
private TextView mTieCount;
private TextView mAndroidCount;
private int mHumanCounter = 0;
private int mTieCounter = 0;
private int mAndroidCounter = 0;
private boolean mHumanFirst = true;
private boolean mGameOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mBoardButtons = new Button[mGame.getBOARD_SIZE()];
mBoardButtons[0] = (Button) findViewById(R.id.one);
mBoardButtons[1] = (Button) findViewById(R.id.two);
mBoardButtons[2] = (Button) findViewById(R.id.three);
mBoardButtons[3] = (Button) findViewById(R.id.four);
mBoardButtons[4] = (Button) findViewById(R.id.five);
mBoardButtons[5] = (Button) findViewById(R.id.six);
mBoardButtons[6] = (Button) findViewById(R.id.seven);
mBoardButtons[7] = (Button) findViewById(R.id.eight);
mBoardButtons[8] = (Button) findViewById(R.id.nine);
Button mTen = (Button) findViewById(R.id.ten);
mTen.setOnClickListener((OnClickListener) this);
Button mEleven = (Button) findViewById(R.id.eleven);
mEleven.setOnClickListener((OnClickListener) this);
mInfoTextView = (TextView) findViewById(R.id.information);
mHumanCount = (TextView) findViewById(R.id.humancount);
mTieCount = (TextView) findViewById(R.id.tiesCount);
mAndroidCount = (TextView) findViewById(R.id.androidCount);
mHumanCount.setText(Integer.toString(mHumanCounter));
mTieCount.setText(Integer.toString(mTieCounter));
mHumanCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeGame();
startNewGame();
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.ten:
startNewGame();
break;
case R.id.eleven:
MainActivity.this.finish();
break;
}
}
private void startNewGame()
{
mGame.clearBoard();
for (int i = 0; i < mBoardButtons.length; i++)
{
mBoardButtons[i].setText("");
mBoardButtons[i].setEnabled(true);
mBoardButtons[i].setOnClickListener(new ButtonClickListener(i));
}
if (mHumanFirst)
{
mInfoTextView.setText(R.string.first_human);
mHumanFirst = false;
}
else
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
mHumanFirst = true;
}
}
private class ButtonClickListener implements View.OnClickListener
{
int location;
public ButtonClickListener(int location)
{
this.location = location;
}
public void onClick(View view)
{
if (!mGameOver)
{
if(mBoardButtons[location].isEnabled())
{
setMove(mGame.HUMAN_PLAYER, location);
int winner = mGame.checkForWinner();
if (winner == 0)
{
mInfoTextView.setText(R.string.turn_computer);
int move = mGame.getComputerMove();
setMove(mGame.ANDROID_PLAYER, move);
winner = mGame.checkForWinner();
}
if (winner == 0)
mInfoTextView.setText(R.string.turn_human);
else if (winner == 1)
{
mInfoTextView.setText(R.string.result_tie);
mTieCounter++;
mTieCount.setText(Integer.toString(mTieCounter));
mGameOver = true;
}
else if (winner ==2)
{
mInfoTextView.setText(R.string.result_human_wins);
mHumanCounter++;
mHumanCount.setText(Integer.toString(mHumanCounter));
mGameOver = true;
}
else if (winner ==3)
{
mInfoTextView.setText(R.string.result_android_wins);
mAndroidCounter++;
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGameOver = true;
}
}
}
}
}
private void setMove(char player, int location)
{
mGame.setMove(player,location);
mBoardButtons[location].setEnabled(false);
mBoardButtons[location].setText(String.valueOf(player));
if (player == mGame.HUMAN_PLAYER)
mBoardButtons[location].setTextColor(Color.GREEN);
else
{
mBoardButtons[location].setTextColor(Color.RED);
}
}
}
Its working here as i do not use appcompact 7 for my development, i used activity to extend your class and i have not use your menu.xml either , first delete that file and try building your code if that does not work use this file it is working fine my side with game restarting and application closing also. Thanks Use this and get back in case of any issue. see its working here
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments