package co.test;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
//import android.widget.Toast;
public class LineView extends View {
private int x, y;
int chessPointXTouch = 0;
int chessPointYTouch = 0;
// 前台setContentView(R.layout.main)无效
// public LineView(Context context) {
// super(context);
// }
Context context;
ChessColor color;
public LineView(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
arrayListPointChess = new ArrayList<PointXY>();
arrayListLinePoint = new ArrayList<PointXY>();
color = ChessColor.gray;
}
private int row;
private int col;
private int screenWidth;
private int screenHeight;
private int gridWidth;
public void setRow(int row) {
this.row = row;
}
public void setCol(int col) {
this.col = col;
}
public void setScreenWidth(int screenWidth) {
this.screenWidth = screenWidth;
}
public void setScreenHeight(int screenHeight) {
this.screenHeight = screenHeight;
}
public void setGridWidth(int gridWidth) {
this.gridWidth = gridWidth;
}
private final ArrayList<PointXY> arrayListLinePoint;
private final ArrayList<PointXY> arrayListPointChess;
public ArrayList<PointXY> getArrayListPoint() {
return arrayListLinePoint;
}
private boolean isTouched = false;
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(Color.GRAY);
// arrayListLinePoint = new ArrayList<PointXY>();
int pointX;
int pointY;
for (int i = 0; i < row; i++) {
canvas.drawLine(0, 10 + i * gridWidth, screenWidth, 10 + i
* gridWidth, paint);// 横线
pointY = 10 + i * gridWidth;
for (int j = 0; j < col; j++) {
canvas.drawLine(10 + j * gridWidth, 0, 10 + j * gridWidth,
screenHeight, paint);// 竖线
pointX = 10 + j * gridWidth;
// 交叉点的保存
if (arrayListLinePoint.size() < this.row * this.col) {
arrayListLinePoint.add(new PointXY(pointX, pointY, null));
}
}
}
if (isTouched == true) {
Paint paintc = new Paint();
int count = 0;
for (PointXY pointxy : arrayListPointChess) {
if (pointxy.getChessColor() == ChessColor.gray) {
paintc.setColor(Color.GRAY);
} else if (pointxy.getChessColor() == ChessColor.blue) {
paintc.setColor(Color.BLUE);
}
canvas.drawCircle(pointxy.getPointX(), pointxy.getPointY(), 10,
paintc);
}
// test----------------↓
if (count == 5) {
hasWin(color);
}
// test----------------↑
if (color == ChessColor.gray) {
color = ChessColor.blue;
} else if (color == ChessColor.blue) {
color = ChessColor.gray;
}
}
}
// test----------------start
private int contains(ArrayList<PointXY> arrayListPointChess2) {
int iCount = 0;
ArrayList<Integer> intArray = new ArrayList<Integer>();
List<Integer> intArraySub = new ArrayList<Integer>();
for (int i = 10; i <= 170; i = i + 30) {
intArray.add(i);
}
intArraySub = intArray.subList(0, 3);
// pointxy.getPointX() == chessPointXTouch - 30
for (PointXY pointxy : arrayListPointChess2) {
if (intArray.contains(pointxy.getPointX())
&& pointxy.getPointY() == chessPointYTouch
&& pointxy.getChessColor() == color) {
iCount++;
}
}
return iCount;
}
// test----------------end
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = (int) event.getX();
y = (int) event.getY();
isTouched = true;
getMatchPoint(x, y);
// arrayListPointChess.add(new PointXY(x, y, color));
invalidate();
// postInvalidate();
case MotionEvent.ACTION_MOVE:
}
// TODO Auto-generated method stub
return true;
}
/**
* 通过触摸点找到最接近的点
*
* @param x
* 触摸点横坐标
* @param y
* 触摸点纵坐标
*/
private void getMatchPoint(int x, int y) {
boolean xRightTrue = false;
boolean xLeftTrue = false;
boolean yRightTrue = false;
boolean yLeftTrue = false;
int chessPointX = 0;
int chessPointY = 0;
boolean findPointX = false;
boolean findPointY = false;
boolean flag1 = false, flag2 = false;
for (int k = 0; k < arrayListLinePoint.size(); k++) {
xRightTrue = arrayListLinePoint.get(k).getPointX() + 10 < x
&& x <= arrayListLinePoint.get(k + 1).getPointX();
xLeftTrue = arrayListLinePoint.get(k).getPointX() <= x
&& x < arrayListLinePoint.get(k).getPointX() + 10;
// yRightTrue = arrayListLinePoint.get(k).getPointY() + 10 < y
// && y <= arrayListLinePoint.get(k + 1).getPointY();
yRightTrue = arrayListLinePoint.get(k).getPointY() + 10 < y
&& y <= (arrayListLinePoint.get(k).getPointY() + this.gridWidth);
yLeftTrue = arrayListLinePoint.get(k).getPointY() <= y
&& y < arrayListLinePoint.get(k).getPointY() + 10;
if (findPointX == false) {
if (xRightTrue == true) {
chessPointX = arrayListLinePoint.get(k + 1).getPointX();
} else if (xLeftTrue == true) {
chessPointX = arrayListLinePoint.get(k).getPointX();
}
if (chessPointX != 0) {
findPointX = true;
flag1 = true;
}
}
if (findPointY == false) {
if (yRightTrue == true) {
// chessPointY = arrayListLinePoint.get(k + 1).getPointY();
chessPointY = arrayListLinePoint.get(k).getPointY()
+ this.gridWidth;
} else if (yLeftTrue == true) {
chessPointY = arrayListLinePoint.get(k).getPointY();
}
if (chessPointY != 0) {
findPointY = true;
flag2 = true;
}
}
if (flag1 == true && flag2 == true) {
arrayListPointChess.add(new PointXY(chessPointX, chessPointY,
color));
// test---sita
int index;
int mod = chessPointX % this.gridWidth;
if (mod == 0) {
index = (chessPointY / this.gridWidth) * this.col
+ (chessPointX / this.gridWidth);
} else {
index = (chessPointY / this.gridWidth) * this.col
+ (chessPointX / this.gridWidth) + 1;
}
arrayListLinePoint.set(index - 1, new PointXY(chessPointX,
chessPointY, color));
// test---ue
chessPointXTouch = chessPointX;
chessPointYTouch = chessPointY;
break;
}
}
}
/**
* 判断是否赢了
*
* @param color2
*
* @return
*/
private void hasWin(ChessColor color) {
String strColor = "";
if (color == ChessColor.blue) {
strColor = "蓝色";
} else if (color == ChessColor.gray) {
strColor = "灰色";
}
Toast.makeText(context, strColor + "is Winner", Toast.LENGTH_LONG)
.show();
}
}
临时文件五子棋
最新推荐文章于 2023-10-24 10:34:41 发布