OneHourProcessing 十二日目

OneHourProcessing 十二日目

十二日目の作品(昨日の続き)はこちら

今日かかった時間は「1時間36分21秒」
ここまでのトータルは「3時間2分21秒」

コード

Controller controller;

void setup()
{
  size(800, 450);
  reset();
}

void draw()
{
  background(0);
  controller.update();
  controller.display();
}

void keyPressed()
{
  controller.keyPress();
}

void mousePressed()
{
  reset();
}

void reset()
{
  controller = null;
  controller = new Controller(new PVector (8, 15), 30);
}

Controllerクラス

class Controller
{
  PVector squareNum;
  float squareSize;
  ArrayList<Puyo> puyos = new ArrayList<Puyo>();
  ArrayList<Puyo> movePuyos = new ArrayList<Puyo>();
  Puyo[][] squares;
  Puyo operatePuyo;

  Controller(PVector squareNum, float squareSize)
  {
    this.squareNum  = squareNum;
    this.squareSize = squareSize;
    squares = new Puyo[int(squareNum.x)][int(squareNum.y)];
    addPuyo();
  }

  void update()
  {
    for (Puyo puyo : movePuyos)
      puyo.update();

    for (int i = 0; i < movePuyos.size(); i++)  
      if (movePuyos.get(i).pos.y % squareSize > squareSize / 2)
      {
        Puyo puyo = movePuyos.get(i);
        int puyoX = puyo.returnX(squareSize);
        int puyoY = puyo.returnY(squareSize);
        if (puyoY >= squareNum.y - 1)
        {
          puyo.setStatus("STOP");
          squares[puyoX][int(squareNum.y - 1)] = puyo;
          puyo.pos.y = squareSize * (squareNum.y - 0.5);
          movePuyos.remove(puyo);
          i--;
        } else if (squares[puyoX][puyoY + 1] != null)
        {
          puyo.setStatus("STOP");
          squares[puyoX][puyoY] = puyo;
          puyo.pos.y = squareSize * (puyoY + 0.5);
          movePuyos.remove(puyo);
          i--;
        }
      }

    if (movePuyos.size() > 0)
      return;

    for (Puyo puyo : puyos)
      puyo.flag = true;

    for (Puyo puyo : puyos)
      if (puyo.flag)
        checkPuyo(puyo, null);

    for (int i = 0; i < puyos.size(); i++)
    {
      Puyo puyo = puyos.get(i);
      if (puyo.connectNum + 1 >= 4)
      {
        erasePuyo(puyo);
        i--;
      }
      println(puyo.pos.x + " : " + puyo.pos.y);
    }

    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);
      if (puyoY < squareNum.y - 1)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.setStatus("FALL");
          squares[puyoX][puyoY] = null;
          movePuyos.add(puyo);
        }
    }

    if (movePuyos.size() > 0)
      return;

    addPuyo();
  }

  void display()
  {
    for (Puyo puyo : puyos)
      puyo.display();
  }

  void keyPress()
  {
    if (operatePuyo == null)
      return;

    switch(keyCode)
    {
    case LEFT:
      if (operatePuyo.returnX(squareSize) > 0)
        operatePuyo.pos.x -= squareSize;
      break;
    case RIGHT:
      if (operatePuyo.returnX(squareSize) < squareNum.x - 1)
        operatePuyo.pos.x += squareSize;
      break;
    }
  }

  void addPuyo()
  {
    PVector puyoPos = new PVector (squareNum.x / 2 * squareSize + squareSize / 2, squareSize / 2);
    PVector puyoVec = new PVector (0, 3);
    float   puyoRad = squareSize;
    String  puyoSta = "FALL";
    String  puyoTyp;
    int random = int(random(3));

    if (random == 0)
      puyoTyp = "RED";
    else if (random == 1)
      puyoTyp = "YELLOW";
    else if (random == 2)
      puyoTyp = "GREEN";
    else if (random == 3)
      puyoTyp = "BLUE";
    else
      puyoTyp = "PURPLE";

    Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, puyoTyp);
    //Puyo puyo = new Puyo(puyoPos, puyoVec, puyoRad, puyoSta, "PURPLE");
    operatePuyo = puyo;
    puyos.add(puyo);
    movePuyos.add(puyo);
  }

  void checkPuyo(Puyo puyo, Puyo beforePuyo)
  {
    int puyoX = puyo.returnX(squareSize);
    int puyoY = puyo.returnY(squareSize);
    puyo.connectNum = 0;
    puyo.flag = false;

    for (int x = -1; x <= 1; x += 2)
      if (puyoX + x >= 0 && puyoX + x < squareNum.x)
      {
        Puyo checkPuyo = squares[puyoX + x][puyoY];
        if (checkPuyo != null)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
    for (int y = -1; y <= 1; y += 2)
      if (puyoY + y >= 0 && puyoY + y < squareNum.y)
      {
        Puyo checkPuyo = squares[puyoX][puyoY + y];
        if (checkPuyo != null && checkPuyo != beforePuyo)
          if (puyo.checkParents(checkPuyo))
          {
            checkPuyo(checkPuyo, puyo);
            puyo.connectNum += checkPuyo.connectNum + 1;
          }
      }
  }

  void erasePuyo(Puyo puyo)
  {
    println(puyo.parents.size());
    for (int i = 0; i < puyo.parents.size(); i++)
    {
      erasePuyo(puyo.parents.get(i));
    }
    puyos.remove(puyo);
    movePuyos.remove(puyo);
    squares[puyo.returnX(squareSize)][puyo.returnY(squareSize)] = null;
  }

  void resetPuyo()
  {
    for (Puyo puyo : puyos)
    {
      int puyoX = puyo.returnX(squareSize);
      int puyoY = puyo.returnY(squareSize);

      if (puyoY <= squareNum.y - 2)
        if (squares[puyoX][puyoY + 1] == null)
        {
          puyo.vec = new PVector(0, 3);
          puyo.status = "FALL";
          puyo.connectNum = 0;
          puyo.parents.clear();
          squares[puyoX][puyoY] = null;
        }
    }
  }
}

Puyoクラス

class Puyo
{
  PVector pos;
  PVector vec;
  float radius;
  String status; // MASTER, SLAVE, STOP, FALL
  String type; //RED, YELLOW, GREEN, BLUE, PURPLE
  boolean flag;
  ArrayList<Puyo> parents = new ArrayList<Puyo>();
  int connectNum;

  Puyo(PVector pos, PVector vec, float radius, String status, String type)
  {
    this.pos    = pos;
    this.vec    = vec;
    this.radius = radius;
    this.status = status;
    this.type   = type;
    flag = true;
    connectNum = 0;
  }

  void update()
  {
    pos.add(vec);
  }

  void display()
  {
    if (type.equals("RED"))
      fill(255, 0, 0);
    else if (type.equals("YELLOW"))
      fill(255, 255, 0);
    else if (type.equals("GREEN"))
      fill(0, 255, 0);
    else if (type.equals("BLUE"))
      fill(0, 0, 255);
    else if (type.equals("PURPLE"))
      fill(255, 0, 255);

    ellipse(pos.x, pos.y, radius, radius);
  }

  void setStatus(String nextStatus)
  {
    switch(nextStatus)
    {
    case "STOP":
      status = "STOP";
      vec = new PVector(0, 0);
      break;

    case "FALL":
      status = "FALL";
      vec = new PVector(0, 3);
      break;
    }
  }

  void setParents(Puyo puyo)
  {
    parents.add(puyo);
  }

  boolean checkParents(Puyo puyo)
  {
    if (!type.equals(puyo.type))
      return false;
    if (!puyo.flag)
      return false;
    parents.add(puyo);
    return true;
  }

  int returnX(float squareSize)
  {
    return int(pos.x / squareSize);
  }

  int returnY(float squareSize)
  {
    return int(pos.y / squareSize);
  }
}