Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

im making a tic-tac-toe game for android studio and i need a little help the next step i have to make it to say wether

im making a tic-tac-toe game for android studio and i need a little help

the next step i have to make it to say wether my touch event is inside one of the 9 squares of the board or not

i was told i can use and array for this ir use a bunch of if statements, was wondering the best way to approach this

thanks for any help

heres my code

 import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.KeyEvent; import android.view.MotionEvent; import android.view.View; import java.util.ArrayList; 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 final GameActivity gameActivity; private float xPos = 0.0f; private float yPos = 0.0f; private float myFontSize; private float myAspectRatio = 1.0f; private float yfix; private int[] gamestate = {0,0,0,0,0,0,0,0,0}; private float[] squareCenterX = {0,0,0,0,0,0,0,0,0}; private float[] squareCenterY = {0,0,0,0,0,0,0,0,0}; //learn about 2d array before graduate private Paint foreground; float[] gameState = {0,1,2,1,2,0,1,0,0}; public GameView(Context context) { super(context); this.gameActivity = (GameActivity) context; setFocusable(true); setFocusableInTouchMode(true); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { width = w ; height = h ; myFontSize = height * (.8f/5.0f); Log.d(TAG, "onSizeChanged: width " + width + ", height " + height); super.onSizeChanged(w, h, oldw, oldh); dark.setAntiAlias(true); dark.setColor(Color.BLACK); dark.setStrokeWidth(10); 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); // fix for y position Paint.FontMetrics fm = foreground.getFontMetrics(); yfix = - (fm.ascent + fm.descent) / 2; squareCenterX[0] = 2f * height * (.5f / 5f); squareCenterY[0] = 3f * height * (.5f/5f); squareCenterX[1] = 3.5f * height * (.5f/5f); squareCenterY[1] = 3f * height * (.5f/5f); squareCenterX[2] = 5f * height * (.5f/5f); squareCenterY[2] = 3f * height * (.5f/5f); squareCenterX[3] = 2f * height * (.5f/5f); squareCenterY[3] = 5f * height * (.5f/5f); squareCenterX[4] = 3.5f * height * (.5f/5f); squareCenterY[4] = 5f * height * (.5f/5f); squareCenterX[5] = 5f * height * (.5f/5f); squareCenterY[5] = 5f * height * (.5f/5f); squareCenterX[6] = 2f * height * (.5f/5f); squareCenterY[6] = 7f * height * (.5f/5f); squareCenterX[7] = 3.5f * height * (.5f/5f); squareCenterY[7] = 7f * height * (.5f/5f); squareCenterX[8] = 5f * height * (.5f/5f); squareCenterY[8] = 7f * height * (.5f/5f); } @Override protected void onDraw(Canvas canvas) { // Draw the background... background.setColor(getResources().getColor(R.color.otherbackground)); canvas.drawRect(0, 0, getWidth(), getHeight(), background); // draw a line canvas.drawLine( width / 5, 2 * height / 5, 4 * width / 5, 2 * height/ 5, dark); canvas.drawLine( width / 5, 3 * height / 5, 4 * width / 5, 3 * height/ 5, dark); canvas.drawLine( 2 * width / 5, height / 5, 2 * width / 5, 4 * height/ 5, dark); canvas.drawLine( 3 * width / 5, height / 5, 3 * width / 5, 4 * height/ 5, dark); xPos = 3.5f * height * (.5f/5f); yPos = 5f * height * (.5f/5f); canvas.drawText("X", xPos, yPos + yfix, foreground); ArrayList alstr = new ArrayList<>(); alstr.add("X"); alstr.add("O"); //canvas.drawtext((point.x + .325), (point.y + .325), (point.x - .325), (point.y - .325), paint)) //loop thru sqaures in game state array, drawing x and o in appropiate place } private void calculateMove(float x, float y) { //square1 = //if so which one? //is the square empty? //and find and empty square //and put an O there } @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(); // function to update game array calculateMove(x,y); invalidate(); // request onDraw 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; } int getGameInt() { int result = 0; int scale = 1; for (int i = 0; i< 9; i++) { result += gameState [i] * scale; scale *=3; } return result; } void putGameInt(int gameInt) { for (int i = 0; i < 9; i++) { gameState[i] = gameInt % 3; gameInt /= 3; } } } 

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

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions