Created
February 2, 2018 13:26
-
-
Save ssaurel/7f954c5a90aa42a48da74ab9e809eeb7 to your computer and use it in GitHub Desktop.
BoardView for the Tic-Tac-Toe game on the SSaurel's Blog
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.ssaurel.tictactoe; | |
import android.content.Context; | |
import android.graphics.Canvas; | |
import android.graphics.Color; | |
import android.graphics.Paint; | |
import android.support.annotation.Nullable; | |
import android.util.AttributeSet; | |
import android.view.MotionEvent; | |
import android.view.View; | |
public class BoardView extends View { | |
private static final int LINE_THICK = 5; | |
private static final int ELT_MARGIN = 20; | |
private static final int ELT_STROKE_WIDTH = 15; | |
private int width, height, eltW, eltH; | |
private Paint gridPaint, oPaint, xPaint; | |
private GameEngine gameEngine; | |
private MainActivity activity; | |
public BoardView(Context context) { | |
super(context); | |
} | |
public BoardView(Context context, @Nullable AttributeSet attrs) { | |
super(context, attrs); | |
gridPaint = new Paint(); | |
oPaint = new Paint(Paint.ANTI_ALIAS_FLAG); | |
oPaint.setColor(Color.RED); | |
oPaint.setStyle(Paint.Style.STROKE); | |
oPaint.setStrokeWidth(ELT_STROKE_WIDTH); | |
xPaint = new Paint(oPaint); | |
xPaint.setColor(Color.BLUE); | |
} | |
public void setMainActivity(MainActivity a) { | |
activity = a; | |
} | |
public void setGameEngine(GameEngine g) { | |
gameEngine = g; | |
} | |
@Override | |
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { | |
height = View.MeasureSpec.getSize(heightMeasureSpec); | |
width = View.MeasureSpec.getSize(widthMeasureSpec); | |
eltW = (width - LINE_THICK) / 3; | |
eltH = (height - LINE_THICK) / 3; | |
setMeasuredDimension(width, height); | |
} | |
@Override | |
protected void onDraw(Canvas canvas) { | |
drawGrid(canvas); | |
drawBoard(canvas); | |
} | |
@Override | |
public boolean onTouchEvent(MotionEvent event) { | |
if (!gameEngine.isEnded() && event.getAction() == MotionEvent.ACTION_DOWN) { | |
int x = (int) (event.getX() / eltW); | |
int y = (int) (event.getY() / eltH); | |
char win = gameEngine.play(x, y); | |
invalidate(); | |
if (win != ' ') { | |
activity.gameEnded(win); | |
} else { | |
// computer plays ... | |
win = gameEngine.computer(); | |
invalidate(); | |
if (win != ' ') { | |
activity.gameEnded(win); | |
} | |
} | |
} | |
return super.onTouchEvent(event); | |
} | |
private void drawBoard(Canvas canvas) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
drawElt(canvas, gameEngine.elt(i, j), i, j); | |
} | |
} | |
} | |
private void drawGrid(Canvas canvas) { | |
for (int i = 0; i < 2; i++) { | |
// vertical lines | |
float left = eltW * (i + 1); | |
float right = left + LINE_THICK; | |
float top = 0; | |
float bottom = height; | |
canvas.drawRect(left, top, right, bottom, gridPaint); | |
// horizontal lines | |
float left2 = 0; | |
float right2 = width; | |
float top2 = eltH * (i + 1); | |
float bottom2 = top2 + LINE_THICK; | |
canvas.drawRect(left2, top2, right2, bottom2, gridPaint); | |
} | |
} | |
private void drawElt(Canvas canvas, char c, int x, int y) { | |
if (c == 'O') { | |
float cx = (eltW * x) + eltW / 2; | |
float cy = (eltH * y) + eltH / 2; | |
canvas.drawCircle(cx, cy, Math.min(eltW, eltH) / 2 - ELT_MARGIN * 2, oPaint); | |
} else if (c == 'X') { | |
float startX = (eltW * x) + ELT_MARGIN; | |
float startY = (eltH * y) + ELT_MARGIN; | |
float endX = startX + eltW - ELT_MARGIN * 2; | |
float endY = startY + eltH - ELT_MARGIN; | |
canvas.drawLine(startX, startY, endX, endY, xPaint); | |
float startX2 = (eltW * (x + 1)) - ELT_MARGIN; | |
float startY2 = (eltH * y) + ELT_MARGIN; | |
float endX2 = startX2 - eltW + ELT_MARGIN * 2; | |
float endY2 = startY2 + eltH - ELT_MARGIN; | |
canvas.drawLine(startX2, startY2, endX2, endY2, xPaint); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment