博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
安卓五子棋(1)
阅读量:7116 次
发布时间:2019-06-28

本文共 6795 字,大约阅读时间需要 22 分钟。

先实现,简单五子棋和判断输赢的功能

主要是自定义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; } }

转载于:https://www.cnblogs.com/lyjsmile/p/5286864.html

你可能感兴趣的文章
成都Uber优步司机奖励政策(3月31日)
查看>>
jquery通过ajax方法获取json数据不执行success
查看>>
字符数组转换成数字
查看>>
URL,URLConnection,HttPURLConnection的使用
查看>>
PHP对象和接口抽象类注意事项
查看>>
转: android apk 防止反编译技术(1~5连载)
查看>>
[唐诗]古风(其三十一)-李白
查看>>
触发器创建删除等操作
查看>>
Java版 数字金额大写转换
查看>>
Linux性能及调优指南(翻译)
查看>>
C#.Net 如何动态加载与卸载程序集(.dll或者.exe)0-------通过应用程序域AppDomain加载和卸载程序集...
查看>>
坑系列 —— 缓存+哈希=高并发?
查看>>
VS调试异常代码 HRESULT:0x80070057 (E_INVALIDARG)解决方法
查看>>
ASP.NET Core 中文文档 第二章 指南(4.10)检查自动生成的Detail方法和Delete方法
查看>>
PHP程序员学习路线
查看>>
伯乐在线-技术分享
查看>>
性能测 试理论篇
查看>>
IIS和tomcat共用80端口
查看>>
ES6的模块化
查看>>
Eclipse中.setting目录下文件介绍
查看>>