Open14

お絵かきロジックを作りたい

HANAEHANAE

設計

  • currentSheet[n][n], answerSheet[n][n]

  • currentSheetに入る状態は3種類

    • Perforted(0), Checked(1), Blank(2)
  • answerSheetに入る状態は2種類

    • Correct(0), Wrong(1)
  • マウスの右クリックでクリックされたマスに対応するcurrentSheetの要素を2に更新

  • マウスの左クリックでクリックされたマスに対応するanswerSheetの要素をチェック

    • answerSheetの要素==0ならcurrentSheetの要素=0に
    • answerSheetの要素==1ならcurrentSheetの要素=1に
HANAEHANAE

基本的にanswerSheetは照合用で、読み取りしかしない
currentSheetを書き換えていく。
currentSheet[i][j]=0になったらそれ以上の変更を受け付けないように書くのが一番易しいというか思考に集中できるモデルかもしれない

HANAEHANAE
void mousePressed()
{
  if(mouseButton==RIGHT)
  {
    println("右が押された");
  }
  else if (mouseButton==LEFT)
  {
    println("左が押された");
  }
}

これでマウスの何ボタン押されたか取得できる。CENTERも取得できるらしい。

HANAEHANAE

便利なJavaの機能を使う / ProcessingとJavaのあいだ(その1) -列挙型

Processingでもenum使えんの最高過ぎるわな👏👏

// 状態を示す列挙型の宣言
enum LifeState {
Born,
Alive,
Dead
};

// 宣言した列挙型の利用
LifeState state = LifeState.Born;


// if文やswitch文にも利用可能
if (state != LifeState.Dead) {
// ...
}

とのこと

これひょっとしてコード書き直したほうがいいかもな~、どこが何なのかわからなくなってきつつある

HANAEHANAE
enum AnswerState {
  Correct, 
    Wrong
};

int y=10;
int x=10;
AnswerState[][] answerState=new AnswerState[y][x];
AnswerState[] values = AnswerState.values();

  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      answerState[i][j]=values[(int)random(0, 2)];
    }
  }

↑できた!

生成一例

Correct, Wrong, Wrong, Correct, Correct, Wrong, Correct, Wrong, Wrong, Wrong,
Correct, Correct, Wrong, Correct, Correct, Correct, Wrong, Wrong, Wrong, Wrong,
Correct, Wrong, Correct, Wrong, Correct, Correct, Correct, Correct, Correct, Wrong,
Wrong, Correct, Wrong, Wrong, Correct, Wrong, Wrong, Wrong, Wrong, Wrong,
Wrong, Correct, Correct, Correct, Wrong, Wrong, Wrong, Correct, Wrong, Correct,
Wrong, Wrong, Correct, Correct, Correct, Correct, Correct, Correct, Correct, Correct,
Wrong, Correct, Wrong, Wrong, Correct, Wrong, Correct, Correct, Correct, Correct,
Correct, Wrong, Wrong, Wrong, Correct, Wrong, Wrong, Wrong, Wrong, Correct,
Correct, Correct, Correct, Correct, Wrong, Wrong, Wrong, Correct, Correct, Correct,
Correct, Correct, Correct, Wrong, Wrong, Correct, Wrong, Correct, Wrong, Correct,

HANAEHANAE
void mousePressed()
{
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      if (j*40<=mouseX&&mouseX<=(j+1)*40&&i*40<=mouseY&&mouseY<=(i+1)*40)
      {
        AnswerState ansSq = answerState[i][j];
        CurrentState curSq = currentState[i][j];
        println();
        println("=== CURRENT STATUS ===");
        println("i ==" +i);
        println("j =="+j);
        println("ansSq =="+ansSq);
        println("curSq =="+curSq);
        println("=== STATUS ENDED ===");
        println();
        if (mouseButton == LEFT)
        {
          println("LEFT button is pressed...");
          if (ansSq==AnswerState.Correct)
          {

            curSq=CurrentState.Perforted;
            println("now ansSq == "+ansSq);
            println("and curSq == "+ curSq);
            //println(curSq);
          } else if (ansSq==AnswerState.Wrong)
          {
            //ansSq==AnswerState.Wrong
            curSq=CurrentState.Doubted;
            //println(curSq);
            println();
            println("now ansSq == "+ansSq);
            println("and curSq == "+ curSq);
            println();
          }
          println();
          println("=== !STATUS CHANGED! ===");
          println("i ==" +i);
          println("j =="+j);
          println("ansSq =="+ansSq);
          println("curSq =="+curSq);
          println("=== STATUS ENDED ===");
          println();
        } else if (mouseButton==RIGHT)
        {
          println("RIGHT button is pressed...");
          //println("!");
          curSq = curSq==CurrentState.Blank ? CurrentState.Doubted : CurrentState.Blank;
          //println(curSq);
        }
      }
    }
  }
}

ステータス更新まではできてるんだけど描画に反映されない~~~~なんで~~~~~

HANAEHANAE
今のコード

enum AnswerState {
  Correct, 
    Wrong
};

enum CurrentState {
  Perforted, 
    Blank, 
    Doubted
}

int order = AnswerState.Wrong.ordinal();

int y=10;
int x=10;
AnswerState[][] answerState=new AnswerState[y][x];
CurrentState[][] currentState= new CurrentState[y][x];

void setup()
{
  size(800, 800);
  AnswerState[] values = AnswerState.values();
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      answerState[i][j]=values[(int)random(0, 2)];
      print(answerState[i][j]+", ");
      currentState[i][j]=CurrentState.Blank;
    }
  }
}

void draw()
{
//println();
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      //currentState[0][0]=CurrentState.Doubted;
      CurrentState paintSq = currentState[i][j];

      //CurrentState paintSq = currentState[i][j];
      //print(paintSq+", ");
      if (paintSq==CurrentState.Blank)
      {
        fill(-1);
      } else if (paintSq==CurrentState.Perforted)
      {
        fill(0);
      } else if (paintSq==CurrentState.Doubted) {
        fill(255, 0, 0);
      }
      rect(j*40, i*40, 40, 40);
      //println(paintSq);
    }
    //println();
  }
}

void mousePressed()
{
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      if (j*40<=mouseX&&mouseX<=(j+1)*40&&i*40<=mouseY&&mouseY<=(i+1)*40)
      {
        AnswerState ansSq = answerState[i][j];
        CurrentState curSq = currentState[i][j];
        println();
        println("=== CURRENT STATUS ===");
        println("i ==" +i);
        println("j =="+j);
        println("ansSq =="+ansSq);
        println("curSq =="+curSq);
        println("=== STATUS ENDED ===");
        println();
        if (mouseButton == LEFT)
        {
          println("LEFT button is pressed...");
          if (ansSq==AnswerState.Correct)
          {

            curSq=CurrentState.Perforted;
            println("now ansSq == "+ansSq);
            println("and curSq == "+ curSq);
            //println(curSq);
          } else if (ansSq==AnswerState.Wrong)
          {
            //ansSq==AnswerState.Wrong
            curSq=CurrentState.Doubted;
            //println(curSq);
            println();
            println("now ansSq == "+ansSq);
            println("and curSq == "+ curSq);
            println();
          }
          println();
          println("=== !STATUS CHANGED! ===");
          println("i ==" +i);
          println("j =="+j);
          println("ansSq =="+ansSq);
          println("curSq =="+curSq);
          println("=== STATUS ENDED ===");
          println();
        } else if (mouseButton==RIGHT)
        {
          println("RIGHT button is pressed...");
          //println("!");
          curSq = curSq==CurrentState.Blank ? CurrentState.Doubted : CurrentState.Blank;
          //println(curSq);
        }
      }
    }
  }
}

HANAEHANAE

curSq=currentState[i][j];
って書いてcurSqの値変更して配列の要素変更したぶってただけだった………己が信じられん……

HANAEHANAE
現時点のコード
enum AnswerState {
  Correct, 
    Wrong
};

enum CurrentState {
  Perforted, 
    Blank, 
    Doubted
}

int y=10;
int x=10;
AnswerState[][] answerState=new AnswerState[y][x];
CurrentState[][] currentState= new CurrentState[y][x];

void setup()
{
  size(800, 800);
  AnswerState[] values = AnswerState.values();
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      answerState[i][j]=values[(int)random(0, 2)];
      currentState[i][j]=CurrentState.Blank;
    }
  }
}

void draw()
{

  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {

      CurrentState paintSq = currentState[i][j];

      if (paintSq==CurrentState.Blank)
      {
        fill(-1);
      } else if (paintSq==CurrentState.Perforted)
      {
        fill(0);
      } else if (paintSq==CurrentState.Doubted) {
        fill(255, 0, 0);
      }
      rect(j*40, i*40, 40, 40);
    }
  }
}

void mousePressed()
{
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      if (j*40<=mouseX&&mouseX<=(j+1)*40&&i*40<=mouseY&&mouseY<=(i+1)*40)
      {
        AnswerState ansSq = answerState[i][j];
        CurrentState curSq = currentState[i][j];

        if (mouseButton == LEFT)
        {
          currentState[i][j] = ansSq==AnswerState.Correct ? CurrentState.Perforted : CurrentState.Doubted;
        } else if (mouseButton==RIGHT)
        {
          if (currentState[i][j]!=CurrentState.Perforted)
          {
            currentState[i][j] = curSq==CurrentState.Blank ? CurrentState.Doubted : CurrentState.Blank;
          }
        }
      }
    }
  }
}
HANAEHANAE
現時点でのコード
/*
currentSheet[n][n], answerSheet[n][n]
 
 currentSheetに入る状態は3種類
 Perforted(0), Checked(1), Blank(2)
 
 answerSheetに入る状態は2種類
 Correct(0), Wrong(1)
 マウスの右クリックでクリックされたマスに対応するcurrentSheetの要素を2に更新
 
 マウスの左クリックでクリックされたマスに対応するanswerSheetの要素をチェック
 
 answerSheetの要素==0ならcurrentSheetの要素=0に
 answerSheetの要素==1ならcurrentSheetの要素=1に
 */

int y=10;
int x=10;

int [][] answerSheet = new int [y][x];
int [][] currentSheet = new int [y][x];
void setup()
{
  size(800, 800);

  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      answerSheet[i][j]=(int)random(0, 2);
      currentSheet[i][j]=1;
      String answer;
      if(answerSheet[i][j]==0)
      {answer = "〇";}
      else
      {
        answer = "×";
      }
      
      print(answer);
    }
    println();
  }
}
void draw()
{
  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      if (currentSheet[i][j]==0)
      {
        fill(0);
        // Perforted: 0: Black
      } else if (currentSheet[i][j]==2)
      {
        fill(255, 0, 0);
        // Doubted: 2: Red
      } else
      {
        fill(-1);
        // Blank: 1: White
      }

      rect(j*40, i*40, 40, 40);
    }
  }
}

void mousePressed()
{
  /*
  if(mouseButton==RIGHT)
   {
   println("右が押された");
   }
   else if (mouseButton==LEFT)
   {
   println("左が押された");
   }*/

  for (int i=0; i<y; i++)
  {
    for (int j=0; j<x; j++)
    {
      if (j*40<=mouseX&&mouseX<=(j+1)*40)
      {
        if (i*40<=mouseY&&mouseY<=(i+1)*40)
        {
          //クリックされたマスに対応する配列の要素がcurrentSheet[i][j]になる
          if (mouseButton == LEFT)
          {
            if (answerSheet[i][j]==0)
            {
              currentSheet[i][j]=0;
            } else if (answerSheet[i][j]==1)
            {
              currentSheet[i][j]=2;
            }
          } else if (mouseButton == RIGHT)
          {
            if (currentSheet[i][j]!=0)
            {          
              currentSheet[i][j]=2;
              /*
              println("right");
              ↑これは動いたから判定の中に入るまではできてるんだけどな~
              println(currentSheet[i][j]);
              ↑2吐き出してくれたわ、更新までできてるってことは表示がおかしいだけか
              完!!
              */
              
           }
          }
        }
      }
    }
  }
}