Question: Android Studio Tic Tac Toe Game: The TicTacToeGame object should be tested by providing JUnit test cases that can be run against it. It should
Android Studio Tic Tac Toe Game: The TicTacToeGame object should be tested by providing JUnit test cases that can be run against it. It should provide a couple to test cases that show complete games being played with the board configurations being printed out on the logcat terminal or run window. How to implement this? I'm slightly confused about how to write the test methods. Code: package com.comp1601.tictactoegame; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import android.widget.Toast; import java.util.Random; import static android.content.ContentValues.TAG; public class MainActivity extends Activity { int XWins = 0; int OWins = 0; int board[][]; int i, j, k = 0; private TextView NumXWins; private TextView NumOWins; Button buttons[][]; TextView textView; private static final StringBuilder buffer = new StringBuilder(); AI ai; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); NumOWins = findViewById(R.id.owins); NumXWins = findViewById(R.id.xwins); boardSetforGame(); } @Override public boolean onCreateOptionsMenu(Menu menu) { super.onCreateOptionsMenu(menu); MenuItem item = menu.add("New Game"); return true; } public boolean onOptionsItemSelected(MenuItem item) { boardSetforGame(); return true; } // Set up the game board. private void boardSetforGame() { ai = new AI(); buttons = new Button[4][4]; board = new int[4][4]; textView = (TextView) findViewById(R.id.dialogue); buttons[1][3] = (Button) findViewById(R.id.box1); buttons[1][2] = (Button) findViewById(R.id.box2); buttons[1][1] = (Button) findViewById(R.id.box3); buttons[2][3] = (Button) findViewById(R.id.box4); buttons[2][2] = (Button) findViewById(R.id.box5); buttons[2][1] = (Button) findViewById(R.id.box6); buttons[3][3] = (Button) findViewById(R.id.box7); buttons[3][2] = (Button) findViewById(R.id.box8); buttons[3][1] = (Button) findViewById(R.id.box9); for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) board[i][j] = 2; } textView.setText("Click a button to start."); // add the click listeners for each button for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { buttons[i][j].setOnClickListener(new MyClickListener(i, j)); if (!buttons[i][j].isEnabled()) { buttons[i][j].setText(" "); buttons[i][j].setEnabled(true); } } } } class MyClickListener implements View.OnClickListener { int x; int y; public MyClickListener(int x, int y) { this.x = x; this.y = y; } public void onClick(View view) { if (buttons[x][y].isEnabled()) { buttons[x][y].setEnabled(false); buttons[x][y].setText("X"); board[x][y] = 0; textView.setText(""); if (!whoWon()) { ai.computerPlay(); } } } } private class AI { public void computerPlay() { if (board[1][1] == 2 && ((board[1][2] == 0 && board[1][3] == 0) || (board[2][2] == 0 && board[3][3] == 0) || (board[2][1] == 0 && board[3][1] == 0))) { setButtonText(1, 1); } else if (board[1][2] == 2 && ((board[2][2] == 0 && board[3][2] == 0) || (board[1][1] == 0 && board[1][3] == 0))) { setButtonText(1, 2); } else if (board[1][3] == 2 && ((board[1][1] == 0 && board[1][2] == 0) || (board[3][1] == 0 && board[2][2] == 0) || (board[2][3] == 0 && board[3][3] == 0))) { setButtonText(1, 3); } else if (board[2][1] == 2 && ((board[2][2] == 0 && board[2][3] == 0) || (board[1][1] == 0 && board[3][1] == 0))) { setButtonText(2, 1); } else if (board[2][2] == 2 && ((board[1][1] == 0 && board[3][3] == 0) || (board[1][2] == 0 && board[3][2] == 0) || (board[3][1] == 0 && board[1][3] == 0) || (board[2][1] == 0 && board[2][3] == 0))) { setButtonText(2, 2); } else if (board[2][3] == 2 && ((board[2][1] == 0 && board[2][2] == 0) || (board[1][3] == 0 && board[3][3] == 0))) { setButtonText(2, 3); } else if (board[3][1] == 2 && ((board[1][1] == 0 && board[2][1] == 0) || (board[3][2] == 0 && board[3][3] == 0) || (board[2][2] == 0 && board[1][3] == 0))) { setButtonText(3, 1); } else if (board[3][2] == 2 && ((board[1][2] == 0 && board[2][2] == 0) || (board[3][1] == 0 && board[3][3] == 0))) { setButtonText(3, 2); } else if (board[3][3] == 2 && ((board[1][1] == 0 && board[2][2] == 0) || (board[1][3] == 0 && board[2][3] == 0) || (board[3][1] == 0 && board[3][2] == 0))) { setButtonText(3, 3); } else { Random rand = new Random(); int a = rand.nextInt(4); int b = rand.nextInt(4); while (a == 0 || b == 0 || board[a][b] != 2) { a = rand.nextInt(4); b = rand.nextInt(4); } setButtonText(a, b); } } private void setButtonText(int x, int y) { buttons[x][y].setEnabled(false); buttons[x][y].setText("O"); board[x][y] = 1; whoWon(); } } // check the board to see if someone has won private boolean whoWon() { boolean gameEnded = false; if ((board[1][1] == 0 && board[2][2] == 0 && board[3][3] == 0) || (board[1][3] == 0 && board[2][2] == 0 && board[3][1] == 0) || (board[1][2] == 0 && board[2][2] == 0 && board[3][2] == 0) || (board[1][3] == 0 && board[2][3] == 0 && board[3][3] == 0) || (board[1][1] == 0 && board[1][2] == 0 && board[1][3] == 0) || (board[2][1] == 0 && board[2][2] == 0 && board[2][3] == 0) || (board[3][1] == 0 && board[3][2] == 0 && board[3][3] == 0) || (board[1][1] == 0 && board[2][1] == 0 && board[3][1] == 0)) { Toast.makeText(this, "X wins", Toast.LENGTH_SHORT).show(); XWins++; NumXWins.setText("X Wins: " + XWins); gameEnded = true; boardSetforGame(); } else if ((board[1][1] == 1 && board[2][2] == 1 && board[3][3] == 1) || (board[1][3] == 1 && board[2][2] == 1 && board[3][1] == 1) || (board[1][2] == 1 && board[2][2] == 1 && board[3][2] == 1) || (board[1][3] == 1 && board[2][3] == 1 && board[3][3] == 1) || (board[1][1] == 1 && board[1][2] == 1 && board[1][3] == 1) || (board[2][1] == 1 && board[2][2] == 1 && board[2][3] == 1) || (board[3][1] == 1 && board[3][2] == 1 && board[3][3] == 1) || (board[1][1] == 1 && board[2][1] == 1 && board[3][1] == 1)) { Toast.makeText(this, "O wins, you lost!", Toast.LENGTH_SHORT).show(); OWins++; NumOWins.setText("O Wins: " + OWins); gameEnded = true; boardSetforGame(); } else { boolean empty = false; for (i = 1; i <= 3; i++) { for (j = 1; j <= 3; j++) { if (board[i][j] == 2) { empty = true; break; } } } if (!empty) { gameEnded = true; Toast.makeText(this, "Draw!", Toast.LENGTH_SHORT).show(); boardSetforGame(); } } return gameEnded; } }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
