C# exercises (9) Puzzle Game3

Visual Studio community 2015 アカウントについて (学内専用)

パズルゲームUIの改進

改進点:

ピースを順番でセットするではなく、あらかじめランダムにセットして、マウス移動できるようにする

  1. ピースをあらかじめランダムにセット
    1. initialData の改造
      // 変数関係の初期化処理
      private void initialData()
      {
          flg = new bool[9];
          data = new int[] { 0, 1, 2, 3, 4, 5, 6, 7, 8 };
          answer = new int[] { -1, -1, -1, -1, -1, -1, -1, -1, -1 };
          Random r = new Random(Environment.TickCount);
          for (int i = 0; i < 100; i++)
          {
              int a = r.Next(9);
              int b = r.Next(9);
              int n = data[a];
              data[a] = data[b];
              data[b] = n;
          }
          current = 0;
          answer = data; // chen : ランダムデータを直接結果に
          playflg = true;
          clearflg = false;
      }

       

    2. PlayBox_Paint の改造
      // PlayBoxの表示
      private void PlayBox_Paint(object sender, PaintEventArgs e)
      {
          if (img == null) { return; }
          Graphics g = e.Graphics;
          for (int i = 0; i < 9; i++)
          {
              // if (flg[i] == false) { continue; }    // chen : flg 無視
              if (answer[i] == 8) { continue; } // chen : 9個目描画しない
              int x1 = i % 3;
              int y1 = i / 3;
              int x2 = answer[i] % 3;
              int y2 = answer[i] / 3;
              Rectangle r1 = new Rectangle(100 * x1, 100 * y1, 100, 100);
              Rectangle r2 = new Rectangle(100 * x2, 100 * y2, 100, 100);
              g.DrawImage(img, r1, r2, GraphicsUnit.Pixel);
          }
          if (playflg == false)
          {
              if (clearflg)
              {
                  g.DrawString("CLEAR!!",
                      new Font("Impact", 48, FontStyle.Bold),
                      new SolidBrush(Color.Red),
                      new Point(40, 100));
              }
              else
              {
                  g.DrawString("GAMEOVER...",
                      new Font("Impact", 36, FontStyle.Bold),
                      new SolidBrush(Color.Blue),
                      new Point(20, 200));
              }
          }
      }
      
  2. マウス移動できる (隣と交換可能なら交換)
    private void swap_answer(int n, int m)
    {
        int t;
        t = answer[m];
        answer[m] = answer[n];
        answer[n] = t;
    }
    
    // PlayBoxをクリックした時の処理
    private void PlayBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (playflg == false) { return; }
        if (img == null) { return; }
        if (current > 8) { return; }
        int x = e.X / 100;
        int y = e.Y / 100;
        if (x < 0) { return; }
        if (y < 0) { return; }
        if (x >= 3) { return; }
        if (y >= 3) { return; }
    
    
        int n = x + y * 3;
        //flg[n] = true;
        //answer[n] = data[current];
        //current++;
        if ((x > 0) && answer[(x - 1) + y * 3] == 8) swap_answer(n, (x - 1) + y * 3);
        if ((x < 2) && answer[(x + 1) + y * 3] == 8) swap_answer(n, (x + 1) + y * 3);
        if ((y > 0) && answer[x + (y - 1) * 3] == 8) swap_answer(n, x + (y - 1) * 3);
        if ((y < 2) && answer[x + (y + 1) * 3] == 8) swap_answer(n, x + (y + 1) * 3);
    
        // this.checkGameEnd();
        this.Refresh();
    
    }
  3. 勝負判定
    private void PlayBox_MouseDown(object sender, MouseEventArgs e)
    {
        if (playflg == false) { return; }
        if (img == null) { return; }
        if (current > 8) { return; }
        int x = e.X / 100;
        int y = e.Y / 100;
        if (x < 0) { return; }
        if (y < 0) { return; }
        if (x >= 3) { return; }
        if (y >= 3) { return; }
    
    
        int n = x + y * 3;
        //flg[n] = true;
        //answer[n] = data[current];
        //current++;
        if ((x > 0) && answer[(x - 1) + y * 3] == 8) swap_answer(n, (x - 1) + y * 3);
        if ((x < 2) && answer[(x + 1) + y * 3] == 8) swap_answer(n, (x + 1) + y * 3);
        if ((y > 0) && answer[x + (y - 1) * 3] == 8) swap_answer(n, x + (y - 1) * 3);
        if ((y < 2) && answer[x + (y + 1) * 3] == 8) swap_answer(n, x + (y + 1) * 3);
    
        // this.checkGameEnd();
        this.checkClear();  // chen : clear check
        playflg = !clearflg; // chen :  clear なら、 not play
        this.Refresh();
    
    }

     

  4. 経過時間の表示

Leave a Reply

Your email address will not be published. Required fields are marked *