通过自定义view来做一个五子棋
来源:互联网 发布:java empty array 编辑:程序博客网 时间:2024/06/06 00:02
这是在慕课网找到的,如果有兴趣的同学可以去看看,老师讲的很好,我这只有成品代码
:
自定义view:
public class Wuziqi extends View{ private int mPanelWidth; private float mLineHeight; private int MAX_LINE=15; private int MAX_COUNT_IN_LINE=5; private Paint mpaint=new Paint(); private Bitmap mWhitePiece; private Bitmap mBlackPiece; private float ratioPieceOfLineHeight = 3 * 1.0f / 4; //白棋先手,或者是轮到白棋了 private boolean mIsWhite=true; private List<Point> mWhiteArray=new ArrayList<>(); private List<Point> mBlackArray=new ArrayList<>(); private boolean mIsGameOver; private boolean mIsWhiteWinner; public Wuziqi(Context context) { this(context,null); } public Wuziqi(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public Wuziqi(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); setBackgroundColor(0x44ff0000); //初始化 init(); } private void init() { mpaint.setColor(0x88000000); mpaint.setAntiAlias(true); mpaint.setDither(true); mpaint.setStyle(Paint.Style.STROKE); //获取黑白棋 mWhitePiece= BitmapFactory.decodeResource(getResources(),R.drawable.stone_w2); mBlackPiece= BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1); } //测量 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); //宽 int widthSize = MeasureSpec.getSize(widthMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); //高 int heightSize = MeasureSpec.getSize(heightMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int witch = Math.min(widthSize, heightSize); if(widthMode==MeasureSpec.UNSPECIFIED){ witch = heightSize; }else if(heightMode==MeasureSpec.UNSPECIFIED){ witch = widthSize; } setMeasuredDimension(witch,witch); } //对关于尺寸的成员变量进行赋值 @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mPanelWidth = w; mLineHeight = mPanelWidth * 1.0f / MAX_LINE; int PieceWidth = (int) (mLineHeight* ratioPieceOfLineHeight); //白棋 mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece,PieceWidth,PieceWidth,false); //黑棋 mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece,PieceWidth,PieceWidth,false); } @Override public boolean onTouchEvent(MotionEvent event) { if(mIsGameOver){ return false; } int action = event.getAction(); if(action==MotionEvent.ACTION_DOWN){ int x= (int) event.getX(); int y= (int) event.getY(); Point p=getValidPoint(x,y); if(mWhiteArray.contains(p)||mBlackArray.contains(p)){ return false; } if(mIsWhite){ mWhiteArray.add(p); }else{ mBlackArray.add(p); } invalidate(); mIsWhite=!mIsWhite; } return true; } private Point getValidPoint(int x, int y) { return new Point((int) (x/mLineHeight) ,(int)(y/mLineHeight)); } //绘制 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //绘制棋盘 drawBoard(canvas); //绘制棋子 drawPiece(canvas); //五子连的逻辑判断 checkGameOver(); } private void checkGameOver() { //白棋赢 boolean whiteWin= checkFiveInLine(mWhiteArray);//黑棋赢 boolean blackWin= checkFiveInLine(mBlackArray); //判断有一个赢就游戏Over if(whiteWin||blackWin){ mIsGameOver=true; mIsWhiteWinner = whiteWin; String text=mIsWhiteWinner ? "白棋胜利" : "黑棋胜利"; Toast.makeText(getContext(),text,Toast.LENGTH_SHORT).show(); start(); } } public void start(){ mWhiteArray.clear(); mBlackArray.clear(); mIsGameOver=false; mIsWhiteWinner=false; invalidate(); } private boolean checkFiveInLine(List<Point> mWhiteArray) { for (Point p:mWhiteArray) { int x=p.x; int y=p.y; boolean win= checkHorizontal(x,y,mWhiteArray); if(win){return true;} win= checkVertical(x,y,mWhiteArray); if(win){return true;} win= checkRight(x,y,mWhiteArray); if(win){return true;} win= checkLeft(x,y,mWhiteArray); if(win){return true;} } return false; } //判断棋子是不是横向的连续的五个 private boolean checkHorizontal(int x, int y, List<Point> mWhiteArray) { int count=1; //左 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x-i,y))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } //右 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x+i,y))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } return false; } //判断棋子是不是纵向的连续的五个 private boolean checkVertical(int x, int y, List<Point> mWhiteArray) { int count=1; //上 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x,y-i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } //下 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x,y+i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } return false; } //判断棋子是不是左斜的连续的五个 private boolean checkLeft(int x, int y, List<Point> mWhiteArray) { int count=1; //左上 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x-i,y+i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } //左下 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x+i,y-i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } return false; } //判断棋子是不是右斜的连续的五个 private boolean checkRight(int x, int y, List<Point> mWhiteArray) { int count=1; //右上 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x-i,y-i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } //右下 for (int i = 1; i < MAX_COUNT_IN_LINE; i++) { if(mWhiteArray.contains(new Point(x+i,y+i))){ count++; }else{ break; } } if(count==MAX_COUNT_IN_LINE){ return true; } return false; } private void drawPiece(Canvas canvas) { //绘制白棋 for (int i = 0, n= mWhiteArray.size();i < n; i++) { Point whitePoint =mWhiteArray.get(i); canvas.drawBitmap(mWhitePiece, (whitePoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight, (whitePoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null); } //绘制黑棋 for (int i = 0, n= mBlackArray.size();i < n; i++) { Point blackPoint =mBlackArray.get(i); canvas.drawBitmap(mBlackPiece, (blackPoint.x+(1-ratioPieceOfLineHeight)/2)*mLineHeight, (blackPoint.y+(1-ratioPieceOfLineHeight)/2)*mLineHeight,null); } } private void drawBoard(Canvas canvas) { int w=mPanelWidth; float lineHeight=mLineHeight; for (int i = 0; i < MAX_LINE; i++) { //设置起始的X坐标 int startX= (int) (lineHeight/2); //设置结束的X坐标 int endX= (int) (w-lineHeight/2); int y= (int) ((0.5+i)*lineHeight); //画横向 canvas.drawLine(startX,y,endX,y,mpaint); //画纵向 canvas.drawLine(y,startX,y,endX,mpaint); } }}
调用
<text.bwie.com.wuziqi.Wuziqi android:layout_width="match_parent" android:layout_height="match_parent" />
阅读全文
0 0
- 通过自定义view来做一个五子棋
- FruitLoadView 一个自定义view可用来做加载view
- 自定义View之一五子棋
- 用html5来做五子棋
- 自定义View实现五子棋游戏
- 自定义view(二)五子棋
- android自定义View 五子棋小游戏
- 自定义View做的一个Clock案例
- 自定义View做的一个Clock案例
- 自定义view 做一个圆形的progress
- 使用自定义View做一个小开关
- 通过android官方自定义View案例说开来
- 自定义View通过进度来变化背景颜色
- Android 开发 -------- 自定义View 画 五子棋
- 自定义View+五子棋的简单实现
- Android之自定义view的五子棋游戏
- android自定义View之五子棋小游戏
- Android自定义View实现五子棋小游戏
- 操作(添加/修改/删除 )sql表字段说明/描述的语句
- C++中*与&的区别
- c++串口上位机协议包封装
- 从数据库反向生成代码
- RecyclerView.Adapter通用性
- 通过自定义view来做一个五子棋
- 项目总结
- UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 205: illegal multibyte sequence
- Redis系列三
- 数据结构上机2——链表
- SSM框架——使用MyBatis Generator自动创建代码
- Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法
- GameEntity(三)—— EntityStrategyHelper
- 如何在Web页面里使用高拍仪扫描上传图像