Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I currently have the code below started but need to write the methods to finish a tic-tac-toe game in Android Studio. Any help is appreciated.

I currently have the code below started but need to write the methods to finish a tic-tac-toe game in Android Studio. Any help is appreciated.

MainActivity.java:

package edu.example.tic_tac_toe; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.view.View.OnClickListener; import android.widget.TextView; import java.util.Random; public class MainActivity extends AppCompatActivity implements OnClickListener { // Buttons making up the board private Button mBoardButtons[]; // Array for the game board private char mBoard[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'}; // Various text displayed private TextView mInfoTextView; // Game reset button private Button mNewGame; //Game Variables private static final int BOARD_SIZE = 9; private static final char HUMAN_PLAYER = 'X'; private static final char COMPUTER_PLAYER = 'O'; private char turn = 'X'; private int win = 0; private int move = -1; private Random mRand = new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Get refernce to the buttons mBoardButtons = new Button[BOARD_SIZE]; mBoardButtons[0] = (Button) findViewById(R.id.square1); mBoardButtons[1] = (Button) findViewById(R.id.square2); mBoardButtons[2] = (Button) findViewById(R.id.square3); mBoardButtons[3] = (Button) findViewById(R.id.square4); mBoardButtons[4] = (Button) findViewById(R.id.square5); mBoardButtons[5] = (Button) findViewById(R.id.square6); mBoardButtons[6] = (Button) findViewById(R.id.square7); mBoardButtons[7] = (Button) findViewById(R.id.square8); mBoardButtons[8] = (Button) findViewById(R.id.square9); // Get reference to the TextView and New Game button mInfoTextView = (TextView) findViewById(R.id.infoLabel); mNewGame = (Button) findViewById(R.id.NewGame); // Add listeners to the buttons for (int i = 0; i < mBoardButtons.length; i++) { mBoardButtons[i].setText(""); mBoardButtons[i].setEnabled(true); mBoardButtons[i].setOnClickListener(this); } mNewGame.setOnClickListener(this); } public void getComputerMove(){ int move; } public void displayBoard(){ } public int checkForWinner(){ return 0; } public void showStatus(){ } @Override public void onClick(View v) { switch (v.getId()) { case R.id.square1: if (win == 0) { if (turn == HUMAN_PLAYER) { turn = COMPUTER_PLAYER; mInfoTextView.setText("Computer's Turn"); mBoard[0] = HUMAN_PLAYER; mBoardButtons[0].setText("X"); displayBoard(); win = checkForWinner(); showStatus(); if (win == 0) { getComputerMove(); turn = HUMAN_PLAYER; mInfoTextView.setText("Human's Turn"); displayBoard(); win = checkForWinner(); showStatus(); } } } break; } } }

activity_main.xml:

   

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Databases 28th British National Conference On Databases Bncod 28 Manchester Uk July 2011 Revised Selected Papers Lncs 7051

Authors: Alvaro A.A. Fernandes ,Alasdair J.G. Gray ,Khalid Belhajjame

2011th Edition

3642245765, 978-3642245763

More Books

Students also viewed these Databases questions

Question

What magazine and ads did you choose to examine?

Answered: 1 week ago