下午看了慕课网上的五子棋游戏教学,编写代码如下:
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" tools:context="com.example.jowang.wuziqi.MainActivity"> <com.example.jowang.wuziqi.wuzi android:id="@+id/wuziqi" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerInParent="true"/> RelativeLayout>五子棋代码:
public class wuzi extends View { private int panelwidth; private float lineheight; private int MAX_LINE=10; private Paint paint=new Paint(); private Bitmap white; private Bitmap black; private float ratio=3*1.0f/4; private boolean iswhite=true; private ArrayListmainactivity代码:whitearray=new ArrayList<>(); private ArrayList blackarray=new ArrayList<>(); private boolean gameover; private boolean whitewinner; private int MAXCOUNTLINE=5; public wuzi(Context context, AttributeSet attrs) { super(context, attrs); init(); } private void init() { paint.setColor(0x88000000); paint.setAntiAlias(true); paint.setDither(true); paint.setStyle(Paint.Style.STROKE); white=BitmapFactory.decodeResource(getResources(),R.drawable.qi1); black=BitmapFactory.decodeResource(getResources(),R.drawable.qi2); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthsize=MeasureSpec.getSize(widthMeasureSpec); int widthmode=MeasureSpec.getMode(widthMeasureSpec); int heightsize=MeasureSpec.getSize(heightMeasureSpec); int heightmode=MeasureSpec.getMode(heightMeasureSpec); 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); panelwidth=w; lineheight=panelwidth*1.0f/MAX_LINE; int whitewidth=(int)(lineheight*ratio); white=Bitmap.createScaledBitmap(white,whitewidth,whitewidth,false); black=Bitmap.createScaledBitmap(black,whitewidth,whitewidth,false); } @Override public boolean onTouchEvent(MotionEvent event) { if (gameover) return false; 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 (whitearray.contains(p)||blackarray.contains(p)){ return false; } if (iswhite){ whitearray.add(p); }else { blackarray.add(p); } invalidate(); iswhite=!iswhite; } return true; } private Point getValidPoint(int x, int y) { return new Point((int)(x/lineheight),(int)(y/lineheight)); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); drawBoard(canvas); drawPieces(canvas); checkGame(); } private void checkGame() { boolean whitewin=checkFiveInLine(whitearray); boolean blackwin=checkFiveInLine(blackarray); if (whitewin||blackwin){ gameover=true; whitewinner=whitewin; String text=whitewinner?"whitewin":"blackwin"; 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=left(x,y,points); if (win) return true; win=right(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<MAXCOUNTLINE;i++){ if (points.contains(new Point(x-i,y))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x+i,y))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; return false; } private boolean checkVertical(int x, int y, List points) { int count=1; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x,y-i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x,y+i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; return false; } private boolean left(int x, int y, List points) { int count=1; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x-i,y+i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x+i,y-i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; return false; } private boolean right(int x, int y, List points) { int count=1; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x+i,y+i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; for (int i=1;i<MAXCOUNTLINE;i++){ if (points.contains(new Point(x-i,y-i))){ count++; }else { break; } } if (count==MAXCOUNTLINE) return true; return false; } private void drawPieces(Canvas canvas) { for (int i=0;i<whitearray.size();i++){ Point whitepoint=whitearray.get(i); canvas.drawBitmap(white,(whitepoint.x+(1-ratio)/2)*lineheight,(whitepoint.y+(1-ratio)/2)*lineheight,null); } for (int i=0;i<blackarray.size();i++){ Point blackpoint=blackarray.get(i); canvas.drawBitmap(black,(blackpoint.x+(1-ratio)/2)*lineheight,(blackpoint.y+(1-ratio)/2)*lineheight,null); } } private void drawBoard(Canvas canvas) { int w=panelwidth; float lineHeight=lineheight; 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,paint);//horizon line canvas.drawLine(y,startX,y,endX,paint);//vertical line } } private static final String INSTANCE="instance"; private static final String INSTANCE_OVER="gameover"; private static final String INSTANCE_WHITE="white"; private static final String INSTANCE_BLACK="black"; @Override protected Parcelable onSaveInstanceState() { Bundle bundle=new Bundle(); bundle.putParcelable(INSTANCE,super.onSaveInstanceState()); bundle.putBoolean(INSTANCE_OVER,gameover); bundle.putParcelableArrayList(INSTANCE_WHITE,whitearray); bundle.putParcelableArrayList(INSTANCE_BLACK,blackarray); return bundle; } @Override protected void onRestoreInstanceState(Parcelable state) { if (state instanceof Bundle){ Bundle bundle= (Bundle) state; gameover=bundle.getBoolean(INSTANCE_OVER); whitearray=bundle.getParcelableArrayList(INSTANCE_WHITE); blackarray=bundle.getParcelableArrayList(INSTANCE_BLACK); super.onRestoreInstanceState(bundle.getParcelable(INSTANCE)); return; } super.onRestoreInstanceState(state); } public void restart(){ whitearray.clear(); blackarray.clear(); gameover=false; whitewinner=false; invalidate(); } }
public class MainActivity extends AppCompatActivity { private wuzi wuzi; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); wuzi=(wuzi)findViewById(R.id.wuziqi); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu_main,menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { int id=item.getItemId(); if (id==R.id.setting){ wuzi.restart(); return true; } return super.onOptionsItemSelected(item); } }menu-main代码:
xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity"> <item android:id="@+id/setting" android:title="restart" android:orderInCategory="100" app:showAsAction="never"/> menu>代码地址:https://github.com/jowang2016/wuziqi