Android custom view to achieve Gobang games

Time:2021-5-2

This example for you to share the Android Gobang game specific code, for your reference, the specific content is as follows

Picture:

code:

package com.example.fiveson;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class FiveSon extends View {

 public FiveSon(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 init(context, null);
 }

 public FiveSon(Context context, AttributeSet attrs) {
 super(context, attrs);
 // TODO Auto-generated constructor stub
 init(context, attrs);
 }

 public FiveSon(Context context, AttributeSet attrs, int defStyleAttr) {
 super(context, attrs, defStyleAttr);
 // TODO Auto-generated constructor stub
 init(context, attrs);
 }
 
 private Paint paint = new Paint();
 private Paint circlePaint = new Paint();
 
 //Flag: which row and column does the message represent?
 private List<int[]> chesses = new LinkedList<int[]>();
 
 private void init(Context context, AttributeSet attrs)
 {
 chesses.add(new int[]{3,5});
 chesses.add(new int[]{3,6});
 }
 
 
 int spacing = 100;
 
 
 @Override
 protected void onDraw(Canvas canvas) {
 // TODO Auto-generated method stub
 super.onDraw(canvas);
 
 int width = getWidth();
 int height = getHeight();
 
 
 //1 draw the X coordinate
 
 int num = width / spacing;
 
 paint.setColor(Color.BLACK);
 for(int i=0;i<=num;i++)
 {
  canvas.drawLine(spacing*i, 0, spacing*i, height, paint);
 }
 
 num = height/spacing;
 
 for(int i=0;i<=num;i++)
 {
  canvas.drawLine(0,spacing*i, width,spacing*i, paint);
 }
 
 //2 draw the Y coordinate
 
 
 
 //Draw the flag
 
 if(!chesses.isEmpty())
 {
  
  int count = chesses.size();
  
  for(int i=0;i<count;i++)
  {
  //Row and column information
  int[] location = chesses.get(i);
  
  int rawX = location[1] * spacing;
  int rawY = location[0] * spacing;
  
  if(i%2==0)
  {
   circlePaint.setColor(Color.BLACK);
  }else {
   circlePaint.setColor(Color.GREEN);
  }
  
  canvas.drawCircle(rawX, rawY, 20, circlePaint);
  }
  
 }
 }
 
 
 /**
 *Define your own space:
 * 
 *How to deal with your evil or not, how to deal with time and love you:
 * onTouchEvent()
 * onKeyEvent()
 * 
 *Of course, it can also be passed 
 * setOnTouchListener()
 * setOnKeyListener();
 * 
 *External operation:
 * setOnTouchListener(OnTouchListener l) 
 * setOnKeyListener(OnKeyListener l) 
 * 
 * 
 * 
 * */
 
 @Override
 public boolean onTouchEvent(MotionEvent event) {
 
 boolean isComsumed = false;
 
 int action = event.getAction();
 
 if(action == MotionEvent.ACTION_DOWN)
 {
//Analyze the coordinates of the click to infer the number of rows and columns
  
  int ex = (int)event.getX();
  int ey = (int)event.getY();
  
  int colNum = (int)(ex / spacing);
  int rowNum = (int)(ey / spacing);
  
  int colcc = ex % spacing;
  int rowcc = ey % spacing;
  
  if(colcc>=(spacing/2))
  {
  colcc++;
  }
  if(rowcc>=(spacing/2))
  {
  rowcc++;
  }
  
//Infer when to add 
  int len = chesses.size();
  
  if(len>0)
  {
  boolean has = false;
  for(int i = len-1;i>=0;i--)
  {
   int[] chs = chesses.get(i);
   if(rowNum == chs[0] && colNum == chs[1])
   {
   has = true;
   break;
   }
  }
  if(!has)
  {
   chesses.add(new int[]{rowNum,colNum});
   invalidate();
  }

  }else {
  chesses.add(new int[]{rowNum,colNum});
  invalidate();
  }
  
  isComsumed = true;
  
 }else {
  isComsumed = super.onTouchEvent(event);
 }
 
 return super.onTouchEvent(event);
 
 }
 
}

The above is the whole content of this article, I hope to help you learn, and I hope you can support developer more.

Recommended Today

Looking for frustration 1.0

I believe you have a basic understanding of trust in yesterday’s article. Today we will give a complete introduction to trust. Why choose rust It’s a language that gives everyone the ability to build reliable and efficient software. You can’t write unsafe code here (unsafe block is not in the scope of discussion). Most of […]