Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hi There, My assignment for this lesson in Java is Tic Tac Toe. I have most of the game set up and the GUI. I
Hi There, My assignment for this lesson in Java is Tic Tac Toe. I have most of the game set up and the GUI. I need help with few other items. Here is my code:
package com.positiveid.tictactoe; import android.os.Bundle; import android.support.design.widget.FloatingActionButton; import android.support.design.widget.Snackbar; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.util.Log; import android.view.View; import android.view.Menu; import android.view.MenuItem; import android.widget.Button; import java.util.Random; public class MainActivity extends AppCompatActivity { Button btn[]=new Button[9]; Button btnNewGame; enum Cell {EMPTY, X, O} Cell cells[]=new Cell[9]; Random rand=new Random(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Toolbar toolbar=( Toolbar ) findViewById(R.id.toolbar); setSupportActionBar(toolbar); FloatingActionButton fab=( FloatingActionButton ) findViewById(R.id.fab); fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) .setAction("Action", null).show(); } }); btn[0]=( Button ) findViewById(R.id.btn0); btn[1]=( Button ) findViewById(R.id.btn1); btn[2]=( Button ) findViewById(R.id.btn2); btn[3]=( Button ) findViewById(R.id.btn3); btn[4]=( Button ) findViewById(R.id.btn4); btn[5]=( Button ) findViewById(R.id.btn5); btn[6]=( Button ) findViewById(R.id.btn6); btn[7]=( Button ) findViewById(R.id.btn7); btn[8]=( Button ) findViewById(R.id.btn8); btnNewGame=( Button ) findViewById(R.id.btnNewGame); for (int i=0; i < 9; i++) { final int finalI=i; btn[i].setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MyMsg", "Button " + finalI + " clicked"); PlayGame(finalI); } }); } btnNewGame.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Log.d("MyMsg", "Button NewGame clicked"); NewGame(); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id=item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } // New Game button pressed // Clear all cells. void NewGame() { for (int i=0; i < 9; i++) { cells[i]=Cell.EMPTY; btn[i].setText("EMPTY"); btn[i].setEnabled(true); // my enabling buttons / disabling } } // Play the actual game. void PlayGame(int num) { // Human mars cell first... MarkCell(num, Cell.X); // Human is an X if(CheckWinner(Cell.X)){ Log.d("MyMsg,", "Human Wins!!!"); } if (CheckFull()) { Log.d("MyMsg", "Cat Game"); } else { // Computer attempts to mark his cell boolean emptyCellFound=false; int computerCellChoice; while (!emptyCellFound) { // Computer not smart. Just picks a random empty cell. computerCellChoice=rand.nextInt(9); // 0-8 if (cells[computerCellChoice] == Cell.EMPTY) { emptyCellFound=true; Log.d("MymMsg", "Computer chose cell " + computerCellChoice); MarkCell(computerCellChoice, Cell.O); } } if (CheckWinner(Cell.O)){ Log.d("MyMsg", "Computer Wins!!!"); } } } // Mark cells with X or O void MarkCell(int num, Cell markedCell) { cells[num]=markedCell; btn[num].setText(markedCell.toString()); btn[num].setEnabled(false); // my disabling / enabling } // See if any cells are open or not. boolean CheckFull() { boolean full = true; for (int i = 0; i < 9; i++) { if (cells[i] == Cell.EMPTY) { full = false; break; } } return full; } // See if there is a winner. boolean CheckWinner(Cell mark){ if ( cells[0] == mark && cells [1] == mark && cells[2] == mark || cells[3] == mark && cells [4] == mark && cells[5] == mark || cells[6] == mark && cells [7] == mark && cells[8] == mark || // horizontal wins cells[0] == mark && cells [3] == mark && cells[6] == mark || cells[1] == mark && cells [4] == mark && cells[7] == mark || cells[2] == mark && cells [5] == mark && cells[8] == mark || // vertical wins cells[0] == mark && cells [4] == mark && cells[8] == mark || cells[6] == mark && cells [4] == mark && cells[2] == mark) // diagonal wins { return true; }else{ return false; } } } Here is what I need done: 1-Keep score for both the human and the computer. You will need to do some additional programming to determine that the game has ended when the human wins, or the computer wins or that it is a cat (tie) game.
2-Disable buttons as they are played. When game is done, only the New Game button will be enabled. (I think this one is done, although I don't mind a better way?)
2-Add logic so the computer makes smart (intelligent) moves. (Not sure how to do this one? get rid of the rand?) Thank you,
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started