我创建了两组类继承人一个类:
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class TicTacToeTutorial extends Activity {
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;
/** Called when the activity is first created. */
@Override
public 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);
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));
mAndroidCount.setText(Integer.toString(mAndroidCounter));
mGame = new TicTacToeGame();
startNewGame();
}
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
{
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);
}
}
}
继承人的第二部分:
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;
}
}
我想将加速度计添加到此代码中它是我学校项目的一部分我想大家帮忙谢谢我也是android编码的初学者我试试如果你看到任何错误让我知道。