当前位置:   article > 正文

android studio开发下落四子棋,海量算法高频面试题精编解析_android studio算法

android studio算法

import android.view.MotionEvent;

import android.view.View;

import android.view.animation.LinearInterpolator;

import android.view.animation.TranslateAnimation;

import android.widget.ImageView;

import android.widget.Toast;

import java.io.FileReader;

import java.util.ArrayList;

import java.util.List;

/**

  • Created by Administrator on 2018/7/29.

*/

public class wuziqipanel extends View {

private int mpanelWidth;

private float mLineHeight;

private int MAX_LINE=10;

private int MAX_COUNT_IN_COUNT=4;

TranslateAnimation translateAnimation=null;

private Paint mpaint=new Paint();

private Bitmap mwhitepiece;

private Bitmap mbiackpiece;

private float ratiopieceoflineheight=3*1.0f/4;

// wuziqipanel wuziqi=new wuziqipanel();

// private ImageView greenImage = null;

private boolean miswhite=true;

private ArrayList mwhitearray=new ArrayList<>();

private ArrayList Mblackarray=new ArrayList<>();

private boolean misgameover;

private boolean miswhitewinner;

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

mbiackpiece= BitmapFactory.decodeResource(getResources(),R.drawable.stone_b1);

}

@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 st

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

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

artani(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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/798364
推荐阅读
相关标签
  

闽ICP备14008679号