android studio开发下落四子棋(1),被阿里面试官征服了

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int widthSize=MeasureSpec.getSize(widthMeasureSpec);

int widtfMode=MeasureSpec.getMode(widthMeasureSpec);

int heightSize=MeasureSpec.getSize(heightMeasureSpec);

int heightMode=MeasureSpec.getMode(heightMeasureSpec);

int width=Math.min(widthSize,heightSize);

if(widtfMode==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*ratiopieceoflineheight);

mwhitepiece=Bitmap.createScaledBitmap(mwhitepiece,piecewidth,piecewidth,false);

mbiackpiece=Bitmap.createScaledBitmap(mbiackpiece,piecewidth,piecewidth,false);

}

public void startani(int fir, int last, final Point point){

ValueAnimator animator = ValueAnimator.ofInt(fir,last);

animator.setDuration((last-fir)*100);

animator.setRepeatCount(0);

// animator.setRepeatMode(ValueAnimator.r);

animator.setInterpolator(new LinearInterpolator());

animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

@Override

public void onAnimationUpdate(ValueAnimator animation) {

int by = (int) animation.getAnimatedValue();

point.y=by;

invalidate();

}

});

animator.start();

}

public boolean onTouchEvent(MotionEvent event) {

if(misgameover){

return false;

}

int action=event.getAction();

if(action==MotionEvent.ACTION_UP){

float x=event.getX();

float y=event.getY();

Point p = getvalidpoint(x,y);

if(Mblackarray.contains§||mwhitearray.contains§)return false;

int i;

int n=p.x;

int m=p.y;

for ( i = p.y; i <=8 ; i++) {

p.set(n,i);

if(mwhitearray.contains§||Mblackarray.contains§){

break;

}else{

p.y=i;

}

}

p.y=i-1;

if(miswhite){

mwhitearray.add§;

wuziqipanel.this.setClickable(false);

startani(m,p.y,p);

wuziqipanel.this.setClickable(true);

miswhite=!miswhite;

}else{

Mblackarray.add§;

wuziqipanel.this.setClickable(false);

startani(m,p.y,p);

wuziqipanel.this.setClickable(true);

miswhite=!miswhite;

}

invalidate();

}

return true;

}

private Point getvalidpoint(float x, float y) {

return new Point((int)((x-mLineHeight/2)/mLineHeight),(int)(y/mLineHeight));

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

drawBoard(canvas);

drawpieces(canvas);

checkgameover();

}

private void checkgameover() {

boolean whitewin= checkfiveinline(mwhitearray);

boolean blackwin= checkfiveinline(Mblackarray);

if(whitewin||blackwin){

misgameover=true;

miswhitewinner=whitewin;

String text=miswhitewinner?“白棋胜利”:“黑棋胜利”;

Toast.makeText(getContext(),text,Toast.LENGTH_SHORT).show();

}

}

private boolean checkfiveinline(List points) {

for(Point p:points){

int x=p.x;

int y=p.y;

boolean win= checkhorizontal(x,y,points);

if(win)return true;

win= checkvertical(x,y,points);

if(win)return true;

win= checkleftdiagonal(x,y,points);

if(win)return true;

win= checkrightdiagonal(x,y,points);

if(win)return true;

}

return false;

}

//判断横向是否成五子

private boolean checkhorizontal(int x, int y, List points) {

int count=1;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x-i,y))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x+i,y))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

return false;

}

private boolean checkvertical(int x, int y, List points) {

int count=1;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x,y-i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x,y+i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

return false;

}

private boolean checkleftdiagonal(int x, int y, List points) {

int count=1;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x-i,y+i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x+i,y-i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

return false;

}

private boolean checkrightdiagonal(int x, int y, List points) {

int count=1;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x-i,y-i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

for (int i = 1; i <MAX_COUNT_IN_COUNT ; i++) {

if(points.contains(new Point(x+i,y+i))){

count++;

}else{

break;

}

}

if(count==MAX_COUNT_IN_COUNT)return true;

return false;

}

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-(ratiopieceoflineheight/2))*mLineHeight,

(whitepoint.y+1-(ratiopieceoflineheight/2))*mLineHeight,null);

}

for (int i = 0,n=Mblackarray.size(); i <n ; i++) {

Point blackpoint=Mblackarray.get(i);

canvas.drawBitmap(mbiackpiece,(blackpoint.x+1-(ratiopieceoflineheight/2))*mLineHeight,

(blackpoint.y+1-(ratiopieceoflineheight/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);

// int end=(int)(w-lineHeight);

int startx=(int)(lineHeight/2);

int end=(int)(w-lineHeight/2);

int y=(int)((0.5+i)*lineHeight);

// 上三行代码使旗子下在中间

// int y=(int)(i*lineHeight);

canvas.drawLine(startx,y,end,y,mpaint);

canvas.drawLine(y,startx,y,end,mpaint);

}

}

private static final String INSTANCE=“instance”;

private static final String INSTANCE_GAME_OVER=“instance_game_over”;

private static final String INSTANCE_WHITE_ARRAY=“instance_white_array”;

private static final String INSTANCE_BLACK_ARRAY=“instance_black_array”;

@Override

protected Parcelable onSaveInstanceState() {

Bundle bundle=new Bundle();

bundle.putParcelable(INSTANCE,super.onSaveInstanceState());

bundle.putBoolean(INSTANCE_GAME_OVER,misgameover);

bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY,mwhitearray);

bundle.putParcelableArrayList(INSTANCE_WHITE_ARRAY,Mblackarray);

return bundle;

}

@Override

protected void onRestoreInstanceState(Parcelable state) {

if(state instanceof Bundle){

Bundle bundle=(Bundle)state;

misgameover=bundle.getBoolean(INSTANCE_GAME_OVER);

mwhitearray=bundle.getParcelableArrayList(INSTANCE_WHITE_ARRAY);

Mblackarray=bundle.getParcelableArrayList(INSTANCE_BLACK_ARRAY);

super.onRestoreInstanceState(bundle.getParcelable(INSTANCE));

return;

}

super.onRestoreInstanceState(state);

}

public void start(){

mwhitearray.clear();

Mblackarray.clear();

miswhitewinner=false;

misgameover=false;

invalidate();

}

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

}

layout:源码

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

xmlns:tools=“http://schemas.android.com/tools”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

android:background="@drawable/bg"

tools:context=“com.imooc.wuziqi.MainActivity”>

<com.imooc.wuziqi.wuziqipanel

android:id="@+id/id_wuziqi"

android:layout_centerInParent=“true”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

/>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值