Implementation of Gobang with Android simple custom view

Time:2021-5-3

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

Let’s talk about it first. Android’s custom view is to implement a class to inherit view and implement its methods. I’m most interested in the OnDraw method, because you need to implement all the styles you want. Let’s take a look at the renderings

It’s just like Jie. It doesn’t matter if you look ugly. You can use it. After all, it’s just a simple understanding!


*protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  RectF rectF;
  for(int i= pandingx ; i < pandingx*7 ; i+=size){
    for(int z= pandingy ; z < pandingy*30 ; z+=size){
      rectF = new RectF(i , z , i+size , z +size);
      canvas.drawRoundRect(rectF , 1 ,1 ,paint);
    }
  }
  for(int i = 0 ; i <list.size() ; i++){
    int h = (int) list.get(i).get("x");
    int s = (int) list.get(i).get("y");
    if((int )list.get(i).get("flag") == 1) {
      rectF = new RectF(h - size/2, s - size/2, h + size/2, s + size/2);
      canvas.drawRoundRect(rectF, 50, 50, black);
    }else{
      rectF = new RectF(h - size/2, s - size/2, h + size/2, s + size/2);
      canvas.drawRoundRect(rectF, 50, 50, white);
    }
  }
  if(flag == 0) {
    rectF = new RectF(x - size / 2, y - size / 2, x + size / 2, y + size / 2);
    canvas.drawRoundRect(rectF, 50, 50, white);
  }else{
    rectF = new RectF(x - size / 2, y - size / 2, x + size / 2, y + size / 2);
    canvas.drawRoundRect(rectF, 50, 50, black);
  }
}

First of all, a chessboard is drawn, and the pieces are placed in an ArrayList, in which there are maps, x, y coordinates, and their attributes (black or white), as well as the boundary processing. It’s very simple. I won’t say much about it. After all, the control of playing chess must be able to click, not just look at it.

public boolean onTouchEvent(MotionEvent event) {
  int action = event.getAction();
  switch (action) {
    case MotionEvent.ACTION_ Down: // press
      x = pandingx+size;
      y = pandingy+size;
      xx = event.getX();
      yy = event.getY();
      invalidate();
      break;
    case MotionEvent.ACTION_ Move: // Mobile
      if(abs(event.getRawX()-xx)>=30){
        if(event.getRawX()>xx&&JudgeRight() == true){
          x += size;
          xx = event.getRawX();
          yy = event.getRawY();
        }else if(JudgeLeft() == true){
          x -= size;
          xx = event.getRawX();
          yy = event.getRawY();
        }
        invalidate();
      }
      if(abs(event.getRawY()-yy)>=30){
        if(event.getRawY()>yy&&JudgeButtom() == true){
          y += size;
          xx = event.getRawX();
          yy = event.getRawY();
        }else if(JudgeTop() == true){
          y -= size;
          xx = event.getRawX();
          yy = event.getRawY();
        }
        invalidate();
      }
      break;
    case MotionEvent.ACTION_ Up // release
      int i;
      for( i = 0 ; i < list.size() ; i++){
        if(x == (int)list.get(i).get("x")&&y == (int )list.get(i).get("y")){
          toast.repice();
          break;
        }
      }
      if(i == list.size()) {
        Map map = new HashMap();
        map.put("x", x);
        map.put("y", y);
        map.put("flag", flag);
        list.add(map);
        flag = (flag + 1) % 2;
        if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1)!=0){
          if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1) ==1){
            toast.winblack();
          }
          if(deal.setPiont((x-pandingx)/size , (y-pandingy)/size , flag+1) == 2){
            toast.winwhite();
          }
          list.clear();
          init();
        }
        x = -100;
        y = -100;
        invalidate();
      }
      break;
  }
  return true;
}

Maybe it’s just like Jie. It can monitor the movements of falling, sliding and lifting fingers. It looks a bit messy, but it’s about this. The deal class in it is that I put the chess pieces I’ve placed into a two-dimensional array and process them separately in it to determine whether they have five pieces in a row. OK, that’s probably all!

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 […]