課程
/移動開發(fā)
/Android
/Android-五子連珠
發(fā)現(xiàn)一個問題,為什么白棋下好之后,輪到黑棋的時候,如果點擊已經(jīng)存在白棋的地方,白棋就會變成黑棋???
2016-03-31
源自:Android-五子連珠 2-5
正在回答
package?com.imooc.wuziqi; import?android.content.Context; import?android.graphics.Bitmap; import?android.graphics.BitmapFactory; import?android.graphics.Canvas; import?android.graphics.Paint; import?android.graphics.Point; import?android.util.AttributeSet; import?android.view.MotionEvent; import?android.view.View; import?java.util.ArrayList; import?java.util.BitSet; import?java.util.List; public?class?WuziqiPanel?extends?View?{ ???private?int?mPanelWidth; ???private?float?mLineHeight; ???private?int?MAX_LINE?=?10; ???private?Paint?mPaint?=?new?Paint(); ???private?Bitmap?mWhitePiece; ???private?Bitmap?mBlackPiece; ???private?float?ratiPieceOfLineHeight?=?3?*?1.0f?/?4; ???private?boolean?mIsWhite?=?true; ???private?List<Point>mWhiteArray?=?new?ArrayList<>(); ???private?List<Point>mBlackArray?=?new?ArrayList<>(); ???public?WuziqiPanel(Context?context,?AttributeSet?attrs)?{ ???????super(context,?attrs); ???????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?widthMeasureSpace,?int?heightMeasureSpaec){ ???????//super.onMeasure(widthMeasureSpace,heightMeasureSpaec); ???????int?widthSize?=?MeasureSpec.getSize(widthMeasureSpace); ???????int?widthmode?=?MeasureSpec.getMode(widthMeasureSpace); ???????int?heightSize?=?MeasureSpec.getSize(heightMeasureSpaec); ???????int?heightMode?=?MeasureSpec.getMode(heightMeasureSpaec); ???????int?width?=?Math.min(widthSize,heightSize); ???????if?(widthmode?==?MeasureSpec.UNSPECIFIED){ ???????????width?=?heightSize; ???????}else?if?(heightMode?==?MeasureSpec.UNSPECIFIED){ ???????????width?=?widthSize; ???????} ???????setMeasuredDimension(width,?width); ???} ???@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?*?ratiPieceOfLineHeight); ???????mWhitePiece?=?Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false); ???????mBlackPiece?=?Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth,false); ???} ???@Override ???public?boolean?onTouchEvent(MotionEvent?event)?{ ???????int?action?=?event.getAction(); ???????if(?action?==?MotionEvent.ACTION_UP){ ???????????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; ???????} ???????return?true;????//上面改成ACTION_UP,則此處要改為true,否則不出現(xiàn)棋子。 ???} ???private?Point?getValidPoint(int?x,?int?y){ ???????return?new?Point(); ???} ???@Override ???protected?void?onDraw(Canvas?canvas)?{ ???????super.onDraw(canvas); ???????drawBoard(canvas); ???????drawPieces(canvas); ???} ???private?void?drawPieces(Canvas?canvas)?{ ???????for(int?i?=0,n?=?mWhiteArray.size();i?<?n;?i++) ???????{ ???????????Point?whitePoint?=?mWhiteArray.get(i); ???????????canvas.drawBitmap(mWhitePiece, ???????????????????(whitePoint.x?+?(1-ratiPieceOfLineHeight)?/?2)?*?mLineHeight, ???????????????????(whitePoint.y?+?(1-?ratiPieceOfLineHeight)?/?2)?*?mLineHeight,null); ???????} ???????for(int?i?=0,n?=?mBlackArray.size();i?<?n;?i++) ???????{ ???????????Point?blackPoint?=?mBlackArray.get(i); ???????????canvas.drawBitmap(mBlackPiece, ???????????????????(blackPoint.x?+?(1-?ratiPieceOfLineHeight)?/?2)?*?mLineHeight, ???????????????????(blackPoint.y?+?(1-?ratiPieceOfLineHeight)?/?2)?*?mLineHeight,null); ???????} ???} ???private?void?drawBoard(Canvas?canvas)?{ ???????int?w?=?mPanelWidth; ???????float?lineHeight?=?mLineHeight; ???????for(int?i?=?0;?i?<?MAX_LINE?;?i++)?{ ???????????int?startX?=?(int)?(lineHeight?/?2); ???????????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); ???????} ???} }
sulo
package com.imooc.wuziqi;import android.content.Context;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.Point;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import java.util.ArrayList;import java.util.BitSet;import java.util.List;public class WuziqiPanel extends View { ? ?private int mPanelWidth; ? ?private float mLineHeight; ? ?private int MAX_LINE = 10; ? ?private Paint mPaint = new Paint(); ? ?private Bitmap mWhitePiece; ? ?private Bitmap mBlackPiece; ? ?private float ratiPieceOfLineHeight = 3 * 1.0f / 4; ? ?private boolean mIsWhite = true; ? ?private List<Point>mWhiteArray = new ArrayList<>(); ? ?private List<Point>mBlackArray = new ArrayList<>(); ? ?public WuziqiPanel(Context context, AttributeSet attrs) { ? ? ? ?super(context, attrs); ? ? ? ?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 widthMeasureSpace, int heightMeasureSpaec){ ? ? ? ?//super.onMeasure(widthMeasureSpace,heightMeasureSpaec); ? ? ? ?int widthSize = MeasureSpec.getSize(widthMeasureSpace); ? ? ? ?int widthmode = MeasureSpec.getMode(widthMeasureSpace); ? ? ? ?int heightSize = MeasureSpec.getSize(heightMeasureSpaec); ? ? ? ?int heightMode = MeasureSpec.getMode(heightMeasureSpaec); ? ? ? ?int width = Math.min(widthSize,heightSize); ? ? ? ?if (widthmode == MeasureSpec.UNSPECIFIED){ ? ? ? ? ? ?width = heightSize; ? ? ? ?}else if (heightMode == MeasureSpec.UNSPECIFIED){ ? ? ? ? ? ?width = widthSize; ? ? ? ?} ? ? ? ?setMeasuredDimension(width, width); ? ?} ? ?@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 * ratiPieceOfLineHeight); ? ? ? ?mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false); ? ? ? ?mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth,false); ? ?} ? ?@Override ? ?public boolean onTouchEvent(MotionEvent event) { ? ? ? ?int action = event.getAction(); ? ? ? ?if( action == MotionEvent.ACTION_UP){ ? ? ? ? ? ?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; ? ? ? ?} ? ? ? ?return true; ? ?//上面改成ACTION_UP,則此處要改為true,否則不出現(xiàn)棋子。 ? ?} ? ?private Point getValidPoint(int x, int y){ ? ? ? ?return new Point(); ? ?} ? ?@Override ? ?protected void onDraw(Canvas canvas) { ? ? ? ?super.onDraw(canvas); ? ? ? ?drawBoard(canvas); ? ? ? ?drawPieces(canvas); ? ?} ? ?private void drawPieces(Canvas canvas) { ? ? ? ?for(int i =0,n = mWhiteArray.size();i < n; i++) ? ? ? ?{ ? ? ? ? ? ?Point whitePoint = mWhiteArray.get(i); ? ? ? ? ? ?canvas.drawBitmap(mWhitePiece, ? ? ? ? ? ? ? ? ? ?(whitePoint.x + (1-ratiPieceOfLineHeight) / 2) * mLineHeight, ? ? ? ? ? ? ? ? ? ?(whitePoint.y + (1- ratiPieceOfLineHeight) / 2) * mLineHeight,null); ? ? ? ?} ? ? ? ?for(int i =0,n = mBlackArray.size();i < n; i++) ? ? ? ?{ ? ? ? ? ? ?Point blackPoint = mBlackArray.get(i); ? ? ? ? ? ?canvas.drawBitmap(mBlackPiece, ? ? ? ? ? ? ? ? ? ?(blackPoint.x + (1- ratiPieceOfLineHeight) / 2) * mLineHeight, ? ? ? ? ? ? ? ? ? ?(blackPoint.y + (1- ratiPieceOfLineHeight) / 2) * mLineHeight,null); ? ? ? ?} ? ?} ? ?private void drawBoard(Canvas canvas) { ? ? ? ?int w = mPanelWidth; ? ? ? ?float lineHeight = mLineHeight; ? ? ? ?for(int i = 0; i < MAX_LINE ; i++) { ? ? ? ? ? ?int startX = (int) (lineHeight / 2); ? ? ? ? ? ?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); ? ? ? ?} ? ?}}
@Override public?boolean?onTouchEvent(MotionEvent?event)?{ ????int?action?=?event.getAction(); ????if(?action?==?MotionEvent.ACTION_UP){ ????????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; ????} ????return?true;????//上面改成ACTION_UP,則此處要改為true,否則不出現(xiàn)棋子。 }
隨憶
有在落子的時候,也就是往對應(yīng)的集合存放Point的時候先對集合進行判斷嗎?如果有值了Touch處理那塊要返回false,代表不關(guān)心這個點擊,這樣就不會把子放到已經(jīng)有的位置上了。
舉報
Android游戲開發(fā)-五子連珠,本教程通過UI與邏輯實現(xiàn)雙人對戰(zhàn)
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網(wǎng)安備11010802030151號
購課補貼聯(lián)系客服咨詢優(yōu)惠詳情
慕課網(wǎng)APP您的移動學(xué)習(xí)伙伴
掃描二維碼關(guān)注慕課網(wǎng)微信公眾號
2016-05-18
2016-05-18
package com.imooc.wuziqi;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.List;
public class WuziqiPanel extends View {
? ?private int mPanelWidth;
? ?private float mLineHeight;
? ?private int MAX_LINE = 10;
? ?private Paint mPaint = new Paint();
? ?private Bitmap mWhitePiece;
? ?private Bitmap mBlackPiece;
? ?private float ratiPieceOfLineHeight = 3 * 1.0f / 4;
? ?private boolean mIsWhite = true;
? ?private List<Point>mWhiteArray = new ArrayList<>();
? ?private List<Point>mBlackArray = new ArrayList<>();
? ?public WuziqiPanel(Context context, AttributeSet attrs) {
? ? ? ?super(context, attrs);
? ? ? ?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 widthMeasureSpace, int heightMeasureSpaec){
? ? ? ?//super.onMeasure(widthMeasureSpace,heightMeasureSpaec);
? ? ? ?int widthSize = MeasureSpec.getSize(widthMeasureSpace);
? ? ? ?int widthmode = MeasureSpec.getMode(widthMeasureSpace);
? ? ? ?int heightSize = MeasureSpec.getSize(heightMeasureSpaec);
? ? ? ?int heightMode = MeasureSpec.getMode(heightMeasureSpaec);
? ? ? ?int width = Math.min(widthSize,heightSize);
? ? ? ?if (widthmode == MeasureSpec.UNSPECIFIED){
? ? ? ? ? ?width = heightSize;
? ? ? ?}else if (heightMode == MeasureSpec.UNSPECIFIED){
? ? ? ? ? ?width = widthSize;
? ? ? ?}
? ? ? ?setMeasuredDimension(width, width);
? ?}
? ?@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 * ratiPieceOfLineHeight);
? ? ? ?mWhitePiece = Bitmap.createScaledBitmap(mWhitePiece,pieceWidth,pieceWidth,false);
? ? ? ?mBlackPiece = Bitmap.createScaledBitmap(mBlackPiece,pieceWidth,pieceWidth,false);
? ?}
? ?@Override
? ?public boolean onTouchEvent(MotionEvent event) {
? ? ? ?int action = event.getAction();
? ? ? ?if( action == MotionEvent.ACTION_UP){
? ? ? ? ? ?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;
? ? ? ?}
? ? ? ?return true; ? ?//上面改成ACTION_UP,則此處要改為true,否則不出現(xiàn)棋子。
? ?}
? ?private Point getValidPoint(int x, int y){
? ? ? ?return new Point();
? ?}
? ?@Override
? ?protected void onDraw(Canvas canvas) {
? ? ? ?super.onDraw(canvas);
? ? ? ?drawBoard(canvas);
? ? ? ?drawPieces(canvas);
? ?}
? ?private void drawPieces(Canvas canvas) {
? ? ? ?for(int i =0,n = mWhiteArray.size();i < n; i++)
? ? ? ?{
? ? ? ? ? ?Point whitePoint = mWhiteArray.get(i);
? ? ? ? ? ?canvas.drawBitmap(mWhitePiece,
? ? ? ? ? ? ? ? ? ?(whitePoint.x + (1-ratiPieceOfLineHeight) / 2) * mLineHeight,
? ? ? ? ? ? ? ? ? ?(whitePoint.y + (1- ratiPieceOfLineHeight) / 2) * mLineHeight,null);
? ? ? ?}
? ? ? ?for(int i =0,n = mBlackArray.size();i < n; i++)
? ? ? ?{
? ? ? ? ? ?Point blackPoint = mBlackArray.get(i);
? ? ? ? ? ?canvas.drawBitmap(mBlackPiece,
? ? ? ? ? ? ? ? ? ?(blackPoint.x + (1- ratiPieceOfLineHeight) / 2) * mLineHeight,
? ? ? ? ? ? ? ? ? ?(blackPoint.y + (1- ratiPieceOfLineHeight) / 2) * mLineHeight,null);
? ? ? ?}
? ?}
? ?private void drawBoard(Canvas canvas) {
? ? ? ?int w = mPanelWidth;
? ? ? ?float lineHeight = mLineHeight;
? ? ? ?for(int i = 0; i < MAX_LINE ; i++) {
? ? ? ? ? ?int startX = (int) (lineHeight / 2);
? ? ? ? ? ?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);
? ? ? ?}
? ?}
}
2016-04-12
2016-03-31
有在落子的時候,也就是往對應(yīng)的集合存放Point的時候先對集合進行判斷嗎?如果有值了Touch處理那塊要返回false,代表不關(guān)心這個點擊,這樣就不會把子放到已經(jīng)有的位置上了。