我在Android开发方面有点新鲜,我已经完成了开发棋盘游戏的任务。我已经设法使用画布类绘制板,现在剩下的就是在每个点引入对象,以便它们可以作为游戏的一部分沿着线移动。我已经有了游戏规则,但现在我的问题是:
如何跟踪从电路板上单击的点,以便规则类可以识别它。例如,如果我点击第一个点,它应该从数组中知道单击了板[0]?
请帮忙。 这就是我在画布上画板的方法。
public class BoardGame extends SurfaceView implements Runnable{
Thread thread = null;
boolean canDraw = false;
Canvas canvas;
SurfaceHolder surfaceHolder;
Paint black, blackDot, background,paint;
Path tri;
Path d_square = new Path();
public BoardGame(Context context) {
super(context);
surfaceHolder = getHolder();
}
//For making the board suit every screen
private int toPxs(int dps) {
return (int) (dps * getResources().getDisplayMetrics().density);
}
//Drawing the board everytime the game starts
public void run() {
prepPaintBrushes();
while (canDraw == true) {
if (!surfaceHolder.getSurface().isValid()) {
continue;
}
canvas = surfaceHolder.lockCanvas();
canvas.drawRect(0, 0, getWidth(), getHeight(), background);
drawDensitySquare(50, 200, 270, 390);
drawDensitySquare(160, 90, 160, 500);
triangle(160,90,270,200,50,200);
triangle(160,500,50,390,270,390);
triangle(50, 200,270,390,50,390);
triangle(270,200,270,390,50,390);
drawDensitySquare(50,200,270,295);
pieces(160,90,6,paint);
canvas.drawPoint(50,200,paint);
canvas.drawText("Game Score: ", getX() + 20, getY()+50, paint);
canvas.drawText("Turn: ", getX()+300, getY()+50, paint);
canvas.drawText("Moves: ", getX()+20, getY()+90, paint);
surfaceHolder.unlockCanvasAndPost(canvas);
}
}
//Paint brushes used
private void prepPaintBrushes() {
black = new Paint();
black.setColor(Color.BLACK);
black.setStyle(Paint.Style.STROKE);
black.setStrokeWidth(2);
blackDot = new Paint();
blackDot.setColor(Color.BLACK);
blackDot.setStyle(Paint.Style.FILL_AND_STROKE);
background = new Paint();
background.setColor(Color.GRAY);
background.setStyle(Paint.Style.FILL);
paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(30);
}
public void pieces(int x,int y,int r, Paint paint){
int xdp1,ydp1,rd;
xdp1 =toPxs(x);
ydp1 = toPxs(y);
rd = toPxs(r);
this.canvas.drawCircle(xdp1,ydp1,rd,paint);
}
private void drawDensitySquare(int x1, int y1, int x2, int y2) {
int xdp1, ydp1, xdp2, ydp2;
xdp1 = toPxs(x1);
ydp1 = toPxs(y1);
xdp2 = toPxs(x2);
ydp2 = toPxs(y2);
d_square.moveTo(xdp1, ydp1);
d_square.lineTo(xdp2, ydp1);
d_square.moveTo(xdp2, ydp1);
d_square.lineTo(xdp2, ydp2);
d_square.moveTo(xdp2, ydp2);
d_square.lineTo(xdp1, ydp2);
d_square.moveTo(xdp1, ydp2);
d_square.lineTo(xdp1, ydp1);
this.canvas.drawPath(d_square, black);
}
private void triangle(int x1,int x2,int x3,int x4,int x5,int x6) {
tri = new Path();
int xdp1, xdp3, xdp2, xdp4, xdp5, xdp6;
xdp1 = toPxs(x1);
xdp2 = toPxs(x2);
xdp3 = toPxs(x3);
xdp4 = toPxs(x4);
xdp5 = toPxs(x5);
xdp6 = toPxs(x6);
tri.moveTo(xdp1, xdp2);
tri.lineTo(xdp3, xdp4);
tri.moveTo(xdp3, xdp4);
tri.lineTo(xdp5, xdp6);
tri.moveTo(xdp5, xdp6);
tri.lineTo(xdp1, xdp2);
this.canvas.drawPath(tri, black);
}
public void pause() {
canDraw = false;
while (true) {
try {
thread.join();
break;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
thread = null;
}
public void resume() {
canDraw = true;
thread = new Thread(this);
thread.start();
}
}
这些是董事会的规则和结构。
01
02 03 04
05 06 07
08 09 10
11
公共类规则{
private int[] board;
private final int EMPTY_FIELD = 0;
private int blackMarkers;
private int whiteMarkers;
private int turn;
public Rules() {
board = new int[12];
blackMarkers = 3;
whiteMarkers = 1;
}
public boolean validMove(int from, int to) {
//Not a valid move
if(!isValidMove(from, to)) {
return false;
}
// Move the marker to it's new position
board[to] = board[from];
board[from] = EMPTY_FIELD;
// Change turn
if(turn == Constants.WHITE) {
turn = Constants.BLACK;
} else {
turn = Constants.WHITE;
}
return true;
}
public boolean isValidMove(int from, int to) {
//Can only move to it's neighbors.
switch (to) {
case 1:
return (from == 2 || from == 3 || from == 4);
case 2:
return (from == 1 || from == 3 || from == 5 || from == 6);
case 3:
return (from == 1 || from == 2 || from == 4|| from == 6);
case 4:
return (from == 1 || from == 3 || from == 6|| from == 7);
case 5:
return (from == 2 || from == 6 || from == 8);
case 6:
return (from == 2 || from == 3|| from == 4|| from == 5|| from == 7|| from == 8|| from == 9|| from == 10);
case 7:
return (from == 4 || from == 6|| from == 10);
case 8:
return (from == 5 || from == 6 || from == 9|| from == 11);
case 9:
return (from == 6 || from == 8|| from == 10|| from == 11);
case 10:
return (from == 6 || from == 7 || from == 9|| from == 11);
case 11:
return (from == 8 || from == 9 || from == 10);
}
return false;
}
public boolean canRemove(int partOfLine) {
// Check if the argument is part of a line on the board
if(board[partOfLine] == EMPTY_FIELD) {
return false;
}
if((partOfLine == 1 || partOfLine == 2) && (board[1] == board[2])) {
return true;
}
if((partOfLine == 1 || partOfLine == 4) && (board[1] == board[4])) {
return true;
}
if((partOfLine == 8 || partOfLine == 11) && (board[8] == board[11])) {
return true;
}
if((partOfLine == 10 || partOfLine == 11) && (board[10] == board[11])) {
return true;
}
if((partOfLine == 2 || partOfLine == 3 || partOfLine == 4) && (board[2] == board[3] && board[3] == board[4])) {
return true;
}
if((partOfLine == 2 || partOfLine == 6 || partOfLine == 10) && (board[2] == board[6] && board[6] == board[10])) {
return true;
}
if((partOfLine == 2 || partOfLine == 5 || partOfLine == 8) && (board[2] == board[5] && board[5] == board[8])) {
return true;
}
if((partOfLine == 4 || partOfLine == 7 || partOfLine == 10) && (board[4] == board[7] && board[7] == board[10])) {
return true;
}
if((partOfLine == 4 || partOfLine == 6 || partOfLine == 8) && (board[4] == board[6] && board[6] == board[8])) {
return true;
}
if((partOfLine == 1 || partOfLine == 3 || partOfLine == 6 || partOfLine == 9 || partOfLine == 11) && (board[1] == board[3] && board[3] == board[6] && board[6] == board[9] && board[9] == board[11])) {
return true;
}
if((partOfLine == 5 || partOfLine == 6 || partOfLine == 7) && (board[5] == board[6] && board[6] == board[7])) {
return true;
}
if((partOfLine == 8 || partOfLine == 9 || partOfLine == 10) && (board[8] == board[9] && board[9] == board[10])) {
return true;
}
return false;
}
}