第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定

落子的問題

發(fā)現(xiàn)一個問題,為什么白棋下好之后,輪到黑棋的時候,如果點擊已經(jīng)存在白棋的地方,白棋就會變成黑棋???

正在回答

4 回答

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);
???????}
???}
}


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

sulo

private Point getValidPoint(int x, int y) { return new Point((int)(x / lineHeight),(int) (y / lineHeight)); }
2016-06-25 回復(fù) 有任何疑惑可以回復(fù)我~

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);
? ? ? ?}
? ?}
}

0 回復(fù) 有任何疑惑可以回復(fù)我~
@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)棋子。
}


0 回復(fù) 有任何疑惑可以回復(fù)我~
#1

隨憶

還是不行啊,我Android studio也沒有報錯
2016-05-18 回復(fù) 有任何疑惑可以回復(fù)我~

有在落子的時候,也就是往對應(yīng)的集合存放Point的時候先對集合進行判斷嗎?如果有值了Touch處理那塊要返回false,代表不關(guān)心這個點擊,這樣就不會把子放到已經(jīng)有的位置上了。

0 回復(fù) 有任何疑惑可以回復(fù)我~

舉報

0/150
提交
取消
Android-五子連珠
  • 參與學(xué)習(xí)       38999    人
  • 解答問題       174    個

Android游戲開發(fā)-五子連珠,本教程通過UI與邏輯實現(xiàn)雙人對戰(zhàn)

進入課程
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號