本文主要是介绍android游戏开发之我的小小游戏1——五子棋游戏2之判断输赢,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
在这里判断输赢主要就是判断有没有在横向、纵向、左斜向、右斜向四个方向有五个同一种颜色的棋子连在一起,判断的思路就是扫描二维数组,看在着这个方向上有没有连续的同一个颜色的棋子,如果判断到某一方胜利的话,应该禁止再继续下棋,此时应锁掉屏幕,让程序对触摸事件不响应。
//判断棋子的输赢private boolean checkWin(int chessFlag){for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){//判断纵向if(i+4<GRID_NUM && chess[i][j] == chessFlag && chess[i+1][j] == chessFlag && chess[i+2][j] == chessFlag && chess[i+3][j] == chessFlag && chess[i+4][j] == chessFlag )return true;
//判断横向if(j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i][j+1] == chessFlag && chess[i][j+2] == chessFlag && chess[i][j+3] == chessFlag && chess[i][j+4] == chessFlag)return true;
//判断左斜向if(i+4<GRID_NUM && j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i+1][j+1] == chessFlag && chess[i+2][j+2] == chessFlag && chess[i+3][j+3] == chessFlag && chess[i+4][j+4] == chessFlag)return true;
//判断右斜向if(i-4>0 && j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i-1][j+1] == chessFlag && chess[i-2][j+2] == chessFlag && chess[i-3][j+3] == chessFlag && chess[i-4][j+4] == chessFlag)return true;}}return false;}//判断旗下满了的情况,即出现平局的情况private boolean checkFull(){for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){if(chess[i][j] == 0)return false;}}return true;}
然后可以设置一个标志,用来判断下载正在下棋还是已经赢了的情况,如果是赢了的话,每次在触摸屏幕的时候就不需要再重绘了,节省资源,尽量不要在绘图中做判断,只用屏蔽invalidate()函数就可以了。
下面把完整的代码贴上来:
public class GameView extends View {//屏幕的宽和高private int screenWidth = 0;private int screenHeight = 0;//画棋盘的起始位置private int startX = 0;private int startY = 0;//棋盘中每个格子的高和宽private int GRID_WIDTH = 40;private int GRID_NUM = 12;//要画的棋盘中的线数private Paint paint = null;//表示棋子的二维数组,其中数组中的每一个元素代表棋盘上的一个点private int[][] chess = new int[GRID_NUM][GRID_NUM];private int CHESS_BLACK = 1;//表示棋子的颜色,1代表黑色,2代表白色,0达标没有棋子private int CHESS_WHITE = 2;private int chess_flag = 0;//用于记录上一次下的棋子的颜色,1为黑色,2为白色,0是刚开始下棋,上一次没下棋子private boolean winFlag = false;//判断输赢的标志 public GameView(Context context) {super(context);this.setFocusable(true);//获取焦点,以感知触屏事件paint = new Paint();//实例化一个画笔paint.setAntiAlias(true);//设置画笔去锯齿,没有此语句,画的线或图片周围不圆滑}@Overrideprotected void onDraw(Canvas canvas) {//重写View中的该方法,该方法主要承担绘图的工作,每刷新一次,就调用一次该方法super.onDraw(canvas);screenWidth = this.getWidth();screenHeight = this.getHeight();startX = (screenWidth - GRID_WIDTH*(GRID_NUM-1))/2;startY = (screenHeight -GRID_WIDTH*(GRID_NUM-1))/2;System.out.println(".screenWidth.."+screenWidth+"...startX.."+startX);canvas.drawColor(0xffffd700);//把屏幕的底色绘成黄色,此处不仅仅是起把屏幕绘成这种颜色的作用,还有刷屏的作用,对以前绘制的进行清除paint.setColor(0xff458b00);//此处是把画笔变成绿色,是绘制的棋盘变成绿色for(int i=0;i<GRID_NUM;i++){//画横线canvas.drawLine(startX, startY+i*GRID_WIDTH,startX+(GRID_NUM-1)*GRID_WIDTH , startY+i*GRID_WIDTH, paint);//画纵线canvas.drawLine(startX+i*GRID_WIDTH, startY,startX+i*GRID_WIDTH , startY+(GRID_NUM-1)*GRID_WIDTH, paint);}//绘制棋子for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){//定义绘制输赢文字的位置int textX = screenWidth/2-50;int textY = screenHeight - 60;if(chess[i][j] == CHESS_BLACK){paint.setColor(0xff000000);//黑色画笔,画黑棋canvas.drawCircle(startX+i*GRID_WIDTH,startY+j*GRID_WIDTH , 15, paint);if(checkWin(CHESS_BLACK)){paint.setColor(0xffCD00CD);//绘制文字是画笔的颜色paint.setTextSize(16);//设置绘出的字体的大小canvas.drawText("黑方胜,白方跪!", textX, textY, paint);winFlag = true;}}if(chess[i][j] == CHESS_WHITE){paint.setColor(0xffffffff);//白色画笔,画白棋canvas.drawCircle(startX+i*GRID_WIDTH,startY+j*GRID_WIDTH , 15, paint);if(checkWin(CHESS_WHITE)){paint.setColor(0xffCD00CD);paint.setTextSize(16);canvas.drawText("白方胜,黑方跪!", textX, textY, paint);winFlag = true;}}}}}//重写View的监听触摸事件的方法@Overridepublic boolean onTouchEvent(MotionEvent event) {float touchX = event.getX();float touchY = event.getY();if(touchX < startX || touchX>startX+(GRID_NUM-1)*GRID_WIDTH || touchY < startY || touchY>startY+(GRID_NUM-1)*GRID_WIDTH){//点击到棋盘以外的位置System.out.println("......哥们点跑偏了,呵呵");}else{//根据点击的位置,从而获知在棋盘上的哪个位置,即是数组的脚标int index_x = Math.round((touchX-startX)/GRID_WIDTH);int index_y = Math.round((touchY-startY)/GRID_WIDTH);System.out.println("..."+index_x+"..."+index_y);System.out.println("...startX"+startX+"...touchX"+touchX);if(chess_flag == 0){//此句表示在最开始下棋的时候每次都是黑棋先下chess[index_x][index_y] = CHESS_BLACK;chess_flag = CHESS_BLACK;}else if( chess_flag == CHESS_BLACK && chess[index_x][index_y] == 0){chess[index_x][index_y] = CHESS_WHITE;chess_flag = CHESS_WHITE;}else if(chess_flag == CHESS_WHITE && chess[index_x][index_y] == 0){chess[index_x][index_y] = CHESS_BLACK;chess_flag = CHESS_BLACK;}}if(!winFlag || checkFull())//没有平棋或赢的时候开始刷新{invalidate();//点击完成后,通知重绘即再次执行onDraw方法}return super.onTouchEvent(event);} //判断棋子的输赢private boolean checkWin(int chessFlag){for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){if(i+4<GRID_NUM && chess[i][j] == chessFlag && chess[i+1][j] == chessFlag && chess[i+2][j] == chessFlag && chess[i+3][j] == chessFlag && chess[i+4][j] == chessFlag )return true;if(j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i][j+1] == chessFlag && chess[i][j+2] == chessFlag && chess[i][j+3] == chessFlag && chess[i][j+4] == chessFlag)return true;if(i+4<GRID_NUM && j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i+1][j+1] == chessFlag && chess[i+2][j+2] == chessFlag && chess[i+3][j+3] == chessFlag && chess[i+4][j+4] == chessFlag)return true;if(i-4>0 && j+4<GRID_NUM && chess[i][j] == chessFlag && chess[i-1][j+1] == chessFlag && chess[i-2][j+2] == chessFlag && chess[i-3][j+3] == chessFlag && chess[i-4][j+4] == chessFlag)return true;}}return false;}//判断旗下满了的情况,即出现平局的情况private boolean checkFull(){for(int i=0;i<GRID_NUM;i++){for(int j=0;j<GRID_NUM;j++){if(chess[i][j] == 0)return false;}}return true;}
}
下面看看每种情况赢的样子:
这篇关于android游戏开发之我的小小游戏1——五子棋游戏2之判断输赢的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!