Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

need help with this codding im a beginner so dont know how to do this I need to loop thru the gamestate using a for

need help with this codding im a beginner so dont know how to do this

I need to loop thru the gamestate using a for loop to tell is the square for my tic tac toe is blank and if it is then i need to draw and "O", after each player moves right now it doesnt matter which square just the first one it finds

here is my code

package com.example.dmarshall.tictactoefall17; 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] = 3f * width * (.5f / 5f); squareCenterY[0] = 3f * height * (.5f/5f); squareCenterX[1] = 5f * width * (.5f/5f); squareCenterY[1] = 3f * height * (.5f/5f); squareCenterX[2] = 7f * width * (.5f/5f); squareCenterY[2] = 3f * height * (.5f/5f); squareCenterX[3] = 3f * width * (.5f/5f); squareCenterY[3] = 5f * height * (.5f/5f); squareCenterX[4] = 5f * width * (.5f/5f); squareCenterY[4] = 5f * height * (.5f/5f); squareCenterX[5] = 7f * width * (.5f/5f); squareCenterY[5] = 5f * height * (.5f/5f); squareCenterX[6] = 3f * width * (.5f/5f); squareCenterY[6] = 7f * height * (.5f/5f); squareCenterX[7] = 5f * width * (.5f/5f); squareCenterY[7] = 7f * height * (.5f/5f); squareCenterX[8] = 7f * width * (.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); ArrayList alstr = new ArrayList<>(); alstr.add("X"); alstr.add("O"); for (int i = 0; i < 9; i++) { if (gamestate[i] == 1) { { // what is xpos, ypos of square i canvas.drawText("X", squareCenterX[i], squareCenterY[i] + yfix, foreground); } if (gamestate[i] == 2) { { canvas.drawText("O", squareCenterX[i], squareCenterY[i] + yfix, foreground); } } } } } private void calculateMove(float x, float y) { int isquare = -1; //square 0 if (x > (1.0f/5.0f * width) && (x < (2.0f/5.0f * width))){ if (y > (1.0f/5.0f * height) && (y < (2.0f/5.0f * height))){ isquare = 0;}} if (x > (2.0f/5.0f * width) && (x < (3.0f/5.0f * width))){ if (y > (1.0f/5.0f * height) && (y < (2.0f/5.0f * height))){ isquare = 1;}} //square 2 if (x > (3.0f/5.0f * width) && (x < (4.0f/5.0f * width))){ if (y > (1.0f/5.0f * height) && (y < (2.0f/5.0f * height))){ isquare = 2;}} //square 3 if (x > (1.0f/5.0f * width) && (x < (2.0f/5.0f * width))){ if (y > (2.0f/5.0f * height) && (y < (3.0f/5.0f * height))){ isquare = 3;}} //square 4 if (x > (2.0f/5.0f * width) && (x < (3.0f/5.0f * width))){ if (y > (2.0f/5.0f * height) && (y < (3.0f/5.0f * height))){ isquare = 4;}} //square 5 if (x > (3.0f/5.0f * width) && (x < (4.0f/5.0f * width))){ if (y > (2.0f/5.0f * height) && (y < (3.0f/5.0f * height))){ isquare = 5;}} //square 6 if (x > (1.0f/5.0f * width) && (x < (2.0f/5.0f * width))){ if (y > (3.0f/5.0f * height) && (y < (4.0f/5.0f * height))){ isquare = 6;}} //square 7 if (x > (2.0f/5.0f * width) && (x < (3.0f/5.0f * width))){ if (y > (3.0f/5.0f * height) && (y < (4.0f/5.0f * height))){ isquare = 7;}} //square 8 if (x > (3.0f/5.0f * width) && (x < (4.0f/5.0f * width))){ if (y > (3.0f/5.0f * height) && (y < (4.0f/5.0f * height))){ isquare = 8;}} if (isquare == -1) return; if (gamestate [isquare] == 0){ gamestate[isquare] = 1; //loop thru gamestate for loop //inside the for loop look at one elemnet of array //if its blank chnage it to 0 and break the loop } } @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

Pro SQL Server Wait Statistics

Authors: Enrico Van De Laar

1st Edition

1484211391, 9781484211397

More Books

Students also viewed these Databases questions

Question

What is mediator and how does it work?

Answered: 1 week ago

Question

=+5 Does this case provide an example of the future for IHRM?

Answered: 1 week ago

Question

=+4 How did it affect HR?

Answered: 1 week ago