Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Here is my question on Tic tac toe game on Android Studio Java language. I need to make it to work so that the player

Here is my question on Tic tac toe game on Android Studio Java language. I need to make it to work so that the player and AI can play together. Can you please fix it since I can't get it to work? Thank you. - computer should make legal move after human - game state is remembered if back-continue - AI - non-stupid legal move - end of game - afternate x and o between games /////gameview.java package com.example.ttts21; import android.content.Context; import android.graphics.Canvas; import android.graphics.Paint; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; public class GameView extends View { private static final String TAG = "Game View"; private float width; // screen width private float height; // screen height private Paint background = new Paint(); private Paint dark = new Paint(); // private float xInitialPos; // private float yInitialPos; private final GameActivity gameActivity; private float xPos, yPos, myFontSize, myAspectRatio, yfix; // private int[] gameState = new int[]{1,1,2,0,2,0,0,0,0}; //for now, later will be all zeros for a blank board at the start of a game private int[] gameState = new int[]{0,0,0,0,0,0,0,0,0}; private String[] chs = {"", "X", "O"}; // private int player = 1; // private int computer = 2;' private int player = 1; private int computer = 2; private boolean gameOver = false; private Paint foreground; public GameView(Context context) { super(context); this.gameActivity = (GameActivity) context; setFocusable(true); setFocusableInTouchMode(true); } ///////added private int getGameInt() { int result = 0; int scale = 1; for (int i = 0; i<9; i++) { result += gameState [i] * scale; scale *=3; } return result; } protected void putGameInt(int gameInt) { for (int i =0; i<9; i++) { gameState[i] = gameInt % 3; gameInt /= 3; } } // add to GameActivity private static final String TTTG = "TicTacToeGame"; @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause"); //save the current game getPreferences(MODE_PRIVATE).edit().putInt(TTTG, gameView.getGameInt()).commit(); } //////// @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { width = w ; height = h ; Log.d(TAG, "onSizeChanged: width " + width + ", height " + height); myFontSize = 4.0f * height / 25.0f; myAspectRatio = 1.0f; foreground = new Paint(Paint.ANTI_ALIAS_FLAG);// not in onDraw() foreground.setColor(getResources().getColor(R.color.text)); foreground.setStyle(Paint.Style.FILL); foreground.setTextSize(myFontSize); // say 0.8 of your square foreground.setTextScaleX(myAspectRatio); // aspect ratio. 1.0 is good foreground.setTextAlign(Paint.Align.CENTER); Paint.FontMetrics fm = foreground.getFontMetrics(); yfix = - (fm.ascent + fm.descent) / 2; xPos = width / 2; // for now yPos = height / 2; // for now super.onSizeChanged(w, h, oldw, oldh); } @Override protected void onDraw(Canvas canvas) { // Draw the background... background.setColor(getResources().getColor(R.color.otherbackground)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); dark.setColor(0xff222222); // should really get this from colors.xml Paint paint = new Paint(); paint.setStrokeWidth(10); paint.setAntiAlias(true); // draw a line canvas.drawLine(width / 5, 2 * height / 5, 4 * width / 5, 2 * height/ 5, paint); canvas.drawLine(width / 5, 3 * height / 5, 4 * width / 5, 3 * height/ 5, paint); canvas.drawLine(2 * width / 5, height / 5, 2 * width / 5, 4 * height/ 5, paint); canvas.drawLine(3 * width / 5, height / 5, 3 * width / 5, 4 * height/ 5, paint); //canvas.drawText("X", xPos, yPos + yfix, foreground); for (int i = 0; i<9; i++) { if(i ==0) { if(gameState[i] == 0) { canvas.drawText(" ", 3 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 1) { canvas.drawText("X", 3 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 2) { canvas.drawText("O", 3 * width / 10, 3 * height / 8, foreground); } } if(i == 1) { if(gameState[i] == 0) { canvas.drawText(" ", 5 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 1) { canvas.drawText("X", 5 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 2) { canvas.drawText("O", 5 * width / 10, 3 * height / 8, foreground); } } if(i == 2) { if(gameState[i] == 0) { canvas.drawText(" ", 7 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 1) { canvas.drawText("X", 7 * width / 10, 3 * height / 8, foreground); } if(gameState[i] == 2) { canvas.drawText("O", 7 * width / 10, 3 * height / 8, foreground); } } } } //Draw the Xs and Os in the array gameState // for (int i = 0; i < 9; i++) // { // xInitialPos = 1; // yInitialPos = 1; // //find row // yPos = yInitialPos + (int)(i/3); // //find col // xPos = xInitialPos + (int)(i%3); // // switch(gameState[i]) { // case 1: // canvas.drawText(("X"), xPos * (width/5) + (width/10), yPos * (height/5) + (height/10) + yfix, foreground); // break; // case 2: // canvas.drawText(("O"), xPos * (width/5) + (width/10), yPos * (height/5) + (height/10) + yfix, foreground); // break; // default: // break; // // } // } // // // } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction() != MotionEvent.ACTION_DOWN) return super.onTouchEvent(event); Log.d(TAG, "onTouchEvent: x " + event.getX() + ", y " + event.getY()); float x = event.getX(); float y = event.getY(); int ix = (int) (5.0f*x/width - 1); int iy = (int) (5.0f*y/height - 1); if ((!gameOver) && (ix > -1) && (ix <3) && (iy >-1) && (iy <3)) { int ind = iy * 3 + ix; if ((xo[ind] == 0) && (!checkForWin(computer))) { xo[ind] = player; if (!checkForWin(player)) { calcMove(); } invalidate(); } } return true; } private boolean calcMove() { if (xo[4] == 0) //xo: {1,0,1,0,2,0,0,0,0} { xo[4] = computer; return true; } for (int i = 0; i<9; i++) { if (xo[i] ==0) { xo[i] = computer; return true; } } return true; } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { Log.d(TAG, "onKeyDown: keycode=" + keyCode + ", event=" + event); switch (keyCode) { case KeyEvent.KEYCODE_DPAD_UP: // do something break; default: return super.onKeyDown(keyCode, event); } return true; } // check if a square is clicked private void calculateMove(float xs, float ys) { int squaresAcross; int squaresDown; int arrayIndex = -1; if((xs > width / 5) && (xs < 2 * width / 5) && (ys > height / 5) && (ys < 2 * height / 5)) { arrayIndex = 0; } if((xs > 2 * width / 5) && (xs < 3 * width / 5) && (ys > height / 5) && (ys < 2 * height / 5)) { arrayIndex = 1; } if (arrayIndex>0) { if(gameState[arrayIndex] == 0) { gameState[arrayIndex] = 1; } } } } ////strings.xml
 Settings MainActivity Tic-Tac-Toe Tic-Tac-Toe Continue Start About Exit Settings... Tic-Tac-Toe settings Difficulty Easy Medium Hard About Tic-Tac-Toe \ Tic-Tac-Toe is, you, know, tic-tac-toe, written by blah blah blah etc. etc. etc.....  

///////gameactivity.java

package com.example.ttts21; import android.app.Activity; import android.os.Bundle; import android.util.Log; public class GameActivity extends Activity { private static final String TAG = "Game Activity"; private GameView gameView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "onCreate"); gameView = new GameView(this); setContentView(gameView); gameView.requestFocus(); //get the state of the old game int iGame = getPreferences(MODE_PRIVATE).getInt(TTTG,0); //find out if it's a new game boolean newGame = getIntent().getBooleanExtra("NewGame", false); //if it isn't restore the state of the game if(!newGame)gameView.putGameInt(iGame); } } 

///////activity_main.xml

    

////////mainactivity.java

package com.example.ttts21; import android.app.Activity; import android.content.Intent; import android.content.res.Configuration; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; public class MainActivity extends Activity implements OnClickListener { private static final String TAG = "TicTacToe"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Set up click listeners for all the buttons View continueButton = findViewById(R.id.continue_button); continueButton.setOnClickListener(this); View newButton = findViewById(R.id.new_button); newButton.setOnClickListener(this); View aboutButton = findViewById(R.id.about_button); aboutButton.setOnClickListener(this); View exitButton = findViewById(R.id.exit_button); exitButton.setOnClickListener(this); } // ... public void onClick(View v) { switch (v.getId()) { case R.id.about_button: Intent i = new Intent(this, About.class); startActivity(i); break; // More buttons go here (if any) ... case R.id.new_button: Intent g = new Intent(this, GameActivity.class); g.putExtra("NewGame", true); //a new game startActivity(g); break; case R.id.continue_button: g = new Intent(this,GameActivity.class); g.putExtra("NewGame", false); //restore old game startActivity(g); break; case R.id.exit_button: finish(); break; } } public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Log.d(TAG, "onConfigurationChanged " + newConfig.orientation); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_main, menu); return true; } } 

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

More Books

Students also viewed these Databases questions