Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I created a tic tac toe game for the android. The game is set for the user vs. A

ID: 3555001 • Letter: I

Question

I created a tic tac toe game for the android. The game is set for the user vs. AI.

However, I want a user vs user. like player 1 vs player 2.

Here is the code :

package com.example.tictac;

import java.util.Random;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ActivityfourMainActivity extends Activity {
   int c[][];
int i, j, k = 0;
Button b[][];
TextView textView;
AI ai;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activityfour_main);
       setBoard();
       Button Return = (Button) findViewById(R.id.btnReturn);
           Return.setOnClickListener (new OnClickListener()
           {
               @Override
               public void onClick(View view)
               {
                   Intent intent = new Intent (ActivityfourMainActivity.this, MainActivity.class);
                   startActivity(intent);
               }
              
           });
   }

   @Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
@SuppressWarnings("unused")
       MenuItem item = menu.add("New Game");
return true;
}
  
public boolean onOptionsItemSelected(MenuItem item) {
setBoard();
return true;
}


// Set up the game board.
private void setBoard() {
ai = new AI();
b = new Button[4][4];
c = new int[4][4];


textView = (TextView) findViewById(R.id.dialogue);


b[1][3] = (Button) findViewById(R.id.one);
b[1][2] = (Button) findViewById(R.id.two);
b[1][1] = (Button) findViewById(R.id.three);


b[2][3] = (Button) findViewById(R.id.four);
b[2][2] = (Button) findViewById(R.id.five);
b[2][1] = (Button) findViewById(R.id.six);


b[3][3] = (Button) findViewById(R.id.seven);
b[3][2] = (Button) findViewById(R.id.eight);
b[3][1] = (Button) findViewById(R.id.nine);
  
for (i = 1; i <= 3; i++) {
for (j = 1; j <= 3; j++)
c[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++) {
b[i][j].setOnClickListener(new MyClickListener(i, j));
if(!b[i][j].isEnabled()) {
b[i][j].setText(" ");
b[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 (b[x][y].isEnabled()) {
b[x][y].setEnabled(false);
b[x][y].setText("O");
c[x][y] = 0;
textView.setText("");
if (!checkBoard()) {
ai.takeTurn();
}
}
}
}


private class AI {
public void takeTurn() {
if(c[1][1]==2 &&
((c[1][2]==0 && c[1][3]==0) ||
(c[2][2]==0 && c[3][3]==0) ||
(c[2][1]==0 && c[3][1]==0))) {
markSquare(1,1);
} else if (c[1][2]==2 &&
((c[2][2]==0 && c[3][2]==0) ||
(c[1][1]==0 && c[1][3]==0))) {
markSquare(1,2);
} else if(c[1][3]==2 &&
((c[1][1]==0 && c[1][2]==0) ||
(c[3][1]==0 && c[2][2]==0) ||
(c[2][3]==0 && c[3][3]==0))) {
markSquare(1,3);
} else if(c[2][1]==2 &&
((c[2][2]==0 && c[2][3]==0) ||
(c[1][1]==0 && c[3][1]==0))){
markSquare(2,1);
} else if(c[2][2]==2 &&
((c[1][1]==0 && c[3][3]==0) ||
(c[1][2]==0 && c[3][2]==0) ||
(c[3][1]==0 && c[1][3]==0) ||
(c[2][1]==0 && c[2][3]==0))) {
markSquare(2,2);
} else if(c[2][3]==2 &&
((c[2][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[3][3]==0))) {
markSquare(2,3);
} else if(c[3][1]==2 &&
((c[1][1]==0 && c[2][1]==0) ||
(c[3][2]==0 && c[3][3]==0) ||
(c[2][2]==0 && c[1][3]==0))){
markSquare(3,1);
} else if(c[3][2]==2 &&
((c[1][2]==0 && c[2][2]==0) ||
(c[3][1]==0 && c[3][3]==0))) {
markSquare(3,2);
}else if( c[3][3]==2 &&
((c[1][1]==0 && c[2][2]==0) ||
(c[1][3]==0 && c[2][3]==0) ||
(c[3][1]==0 && c[3][2]==0))) {
markSquare(3,3);
} else {
Random rand = new Random();
  
int a = rand.nextInt(4);
int b = rand.nextInt(4);
while(a==0 || b==0 || c[a][b]!=2) {
a = rand.nextInt(4);
b = rand.nextInt(4);
}
markSquare(a,b);
}
}


private void markSquare(int x, int y) {
b[x][y].setEnabled(false);
b[x][y].setText("X");
c[x][y] = 1;
checkBoard();
}
}


// check the board to see if someone has won
private boolean checkBoard() {
boolean gameOver = false;
if ((c[1][1] == 0 && c[2][2] == 0 && c[3][3] == 0)
|| (c[1][3] == 0 && c[2][2] == 0 && c[3][1] == 0)
|| (c[1][2] == 0 && c[2][2] == 0 && c[3][2] == 0)
|| (c[1][3] == 0 && c[2][3] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[1][2] == 0 && c[1][3] == 0)
|| (c[2][1] == 0 && c[2][2] == 0 && c[2][3] == 0)
|| (c[3][1] == 0 && c[3][2] == 0 && c[3][3] == 0)
|| (c[1][1] == 0 && c[2][1] == 0 && c[3][1] == 0)) {
textView.setText("Game over. You win!");
gameOver = true;
} else if ((c[1][1] == 1 && c[2][2] == 1 && c[3][3] == 1)
|| (c[1][3] == 1 && c[2][2] == 1 && c[3][1] == 1)
|| (c[1][2] == 1 && c[2][2] == 1 && c[3][2] == 1)
|| (c[1][3] == 1 && c[2][3] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[1][2] == 1 && c[1][3] == 1)
|| (c[2][1] == 1 && c[2][2] == 1 && c[2][3] == 1)
|| (c[3][1] == 1 && c[3][2] == 1 && c[3][3] == 1)
|| (c[1][1] == 1 && c[2][1] == 1 && c[3][1] == 1)) {
textView.setText("Game over. You lost!");
gameOver = true;
} else {
boolean empty = false;
for(i=1; i<=3; i++) {
for(j=1; j<=3; j++) {
if(c[i][j]==2) {
empty = true;
break;
}
}
}
if(!empty) {
gameOver = true;
textView.setText("Game over. It's a draw!");
}
}
return gameOver;
}
}

Explanation / Answer

import java.util.Scanner; public class TicTacToe { private int counter; private char posn[]=new char[10]; private char player; public static void main(String args[]) { String ch; TicTacToe Toe=new TicTacToe(); do{ Toe.newBoard(); Toe.play(); System.out.println ("Would you like to play again (Enter 'yes')? "); Scanner in =new Scanner(System.in); ch=in.nextLine(); System.out.println("ch value is "+ch); }while (ch.equals("yes")); } public void newBoard() { char posndef[] = {'0','1', '2', '3', '4', '5', '6', '7', '8', '9'}; int i; counter = 0; player = 'X'; for (i=1; i
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote