public boolean onTouchEvent(MotionEvent event) { float touchX = event.getX();// 点击的x坐标 float touchY = event.getY();// 点击的y坐标 if (touchX < startX || touchX > startX + (GRID_NUM - 1) * GRID_WIDTH || touchY < startY || touchY > startY + (GRID_NUM - 1) * GRID_WIDTH) {// 点击到棋盘以外的位置 } 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 + "]"); if (chess_flag == 0) {// 此句表示在最开始下棋的时候每次都是黑棋先下 chess[index_x][index_y] = CHESS_BLACK; chessFix[index_y][index_x] = 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; chessFix[index_y][index_x] = 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; chessFix[index_y][index_x] = CHESS_BLACK; chess_flag = CHESS_BLACK; } // SF(index_x, index_y); if (isHFive(index_x, index_y) || isVFive(index_x, index_y) || isLTFive(index_x, index_y) || isRTFive(index_x, index_y)) { if (isWin == 1) { Toast.makeText(this.getContext(), "黑棋胜利!", Toast.LENGTH_SHORT).show(); } else if (isWin == 2) { Toast.makeText(this.getContext(), "白棋胜利!", Toast.LENGTH_SHORT).show(); } } } invalidate();// 点击完成后,通知重绘即再次执行onDraw方法 return super.onTouchEvent(event); } |
|
15分 |
设置一个标志变量,比如说boolean flag = false;当没有一方胜利时,通过flag=false来判断是否运行点击屏幕下子重绘的代码。如果flag = true;则不执行下面代码……。
|
5分 |
嗯,一个小小的标志位即可
你解决了对方下棋时己方无法操作,应该也能完成 |
如果我要重新开始的话要调用什么呢 |
|
重新开始可以等于新布局~ |
|
没弄懂。。 |