先实现,简单五子棋和判断输赢的功能
主要是自定义View,再将View实例化到活动中就可以了,
继承View后,重写Ondraw()方法,用validate()实现视图更新,就可以实现这个功能了
这几天只写了这么多,接下来继续完成其他的功能
下面附上ChessView的代码:
package com.lyj.fivechess; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.util.Log; import android.view.MotionEvent; import android.view.View; /** * Created by Administrator on 2016/3/11. */ public class ChessView extends View { //屏幕宽高 private int height; private int width; //棋盘画笔 private Paint paint_panel; //白棋画笔 private Paint paint_chess_white; //黑棋画笔 private Paint paint_chess_black; //棋盘间隔 private int chessheight; //保存棋子,所有棋子状态,0为空白,1为黑棋,2为白棋; int[][] Allchess=new int[15][15]; //判断黑棋下还是白棋下,默认黑棋下 boolean isBlack=true; boolean FLAG=false; public ChessView(Context context) { super(context); init(); } public ChessView(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init(){ paint_panel=new Paint(); paint_panel.setColor(Color.DKGRAY); paint_panel.setAntiAlias(true); paint_panel.setStrokeWidth(2); paint_chess_black=new Paint(); paint_chess_black.setColor(Color.BLACK); paint_chess_black.setAntiAlias(true); paint_chess_white=new Paint(); paint_chess_white.setColor(Color.WHITE); paint_chess_white.setAntiAlias(true); Log.v("TAG", ">>>>>>>>>"); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); height=h; width=w; chessheight=(width-60)/14; Log.d("TAG", height + ">>>>>>>>>>>" + width + ">>>>>>>>>" + chessheight); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //棋盘绘制 for(int x=0;x<15;x++){ canvas.drawLine(30+chessheight*x,30,30+chessheight*x,width-30,paint_panel); canvas.drawLine(30,30+chessheight*x,width-30,30+chessheight*x,paint_panel); } canvas.drawCircle(30+chessheight*3,30+chessheight*3,6,paint_chess_black); canvas.drawCircle(30+chessheight*3,30+chessheight*11,6,paint_chess_black); canvas.drawCircle(30+chessheight*11,30+chessheight*3,6,paint_chess_black); canvas.drawCircle(30+chessheight*11,30+chessheight*11,6,paint_chess_black); canvas.drawCircle(30+chessheight*7,30+chessheight*7,6,paint_chess_black); for (int i = 0; i < 15; i++) { for (int j = 0; j < 15; j++) { if (Allchess[i][j] == 1) { //绘制黑棋 canvas.drawCircle( (float) 30 + chessheight * i, (float) 30 + chessheight * j, chessheight / 2, paint_chess_black); } else if (Allchess[i][j] == 2) { //绘制白棋 canvas.drawCircle( (float) 30 + chessheight * i, (float) 30 + chessheight * j, chessheight / 2, paint_chess_white); } } } } @Override public boolean onTouchEvent(MotionEvent event) { if(event.getAction()==MotionEvent.ACTION_UP){ int x=(int)event.getX(); int y=(int)event.getY(); Log.d("TAG","x="+x+"<><><><><>"+"y="+y); if(x<15||x>width-30||y<15||y>width){ //点出棋盘外 Log.d("TAG","点出棋盘外"); }else { int tempx = (x - 30) / chessheight; int tempy = (y - 30) / chessheight; Log.d("TAG","tempx="+tempx+"<><><><><>"+"tempy="+tempy); if(Allchess[tempx][tempy]==0) { if (isBlack) { //下黑棋 Allchess[tempx][tempy] = 1; isBlack = false; if(FLAG==false) { if (CheckOver(tempx, tempy)) { Log.d("TAG", "游戏结束黑方胜"); FLAG = true; } invalidate(); } } else { //下白棋 Allchess[tempx][tempy] = 2; isBlack = true; if(FLAG==false) { if (CheckOver(tempx, tempy)) { Log.d("TAG", "游戏结束白方胜"); FLAG = true; } invalidate(); } } }else { //当前位置有棋子了,重新选择地点下子 } } } return true; } private boolean CheckOver(int x,int y){ boolean isOver=false; int count=1; /** *判断X轴 是否胜利 */ int i=1; while (Allchess[x][y] == Allchess[x + i][y]) { i++; count++; } i=1; while (Allchess[x][y]==Allchess[x-i][y]){ i++; count++; } if(count>=5){ isOver=true; }else { count = 1; } /** * 判断Y轴,是否胜利 */ i=1; while(Allchess[x][y]==Allchess[x][y+i]){ i++; count++; } i=1; while (Allchess[x][y]==Allchess[x][y-i]){ i++; count++; } if(count>=5){ isOver=true; } /** * 判断右斜是否有胜利 */ count=1; i=1; while (Allchess[x][y]==Allchess[x+i][y-i]){ i++; count++; } i=1; while (Allchess[x][y]==Allchess[x-i][y+i]){ count++; i++; } if(count>=5){ isOver=true; } /** * 判断左斜是否有胜利 */ count=1; i=1; while (Allchess[x][y]==Allchess[x-i][y-i]){ i++; count++; } i=1; while (Allchess[x][y]==Allchess[x+i][y+i]){ count++; i++; } if(count>=5){ isOver=true; } return isOver; } }