2013-01-05 19 views
-1

我正在重新創建經典的Reversi遊戲,但我在嘗試正確更改對角線顏色時遇到了麻煩。我有兩個循環,但他們不能正常工作任何人都可以看看我的代碼,並告訴我我做錯了什麼。Reversi like game對角線檢測顏色(任何幫助?)

(請看看我的for循環)

此圖片說明了什麼我的問題是:

enter image description here

這是我的代碼:

 private void ClickedButton(object sender, EventArgs e) 
     { 
      Button s = (Button)sender; 
      int x = int.Parse(s.Name.Split()[0]); 
      int y = int.Parse(s.Name.Split()[1]); 

      if (cnt == 0) 
      { 
       cnt = 1; 
       s.BackColor = Color.Black; 

       for (int i = 3; i > x; --i) 
       { 
        for (int j = 0; j < y; ++j) 
        { 
         if (b[i, j].BackColor == Color.Black) 
         { 
          b[i - 1, j + 1].BackColor = Color.Black; 

         } 
        } 
       } 
      } 
      else 
      { 
       cnt = 0; 
       s.BackColor = Color.Red; 
      } 

      // MessageBox.Show("you have clicked button: " + x +" "+ y); 
     } 
    } 
} 
+0

這看起來不像Reversi。白色瓷磚在哪裏? – juharr

+0

真的嗎?!因爲它不完整,但這不是現階段的重點。首先,我需要正確檢測對角線的顏色,然後進一步開發。對不起,如果這不能滿足你的眼睛 – Tacit

+1

你有沒有嘗試在調試器中的代碼步進? –

回答

1
private void ClickedButton(object sender, EventArgs e) 
    { 
     Button s = (Button)sender; 
     int x = int.Parse(s.Name.Split()[0]); 
     int y = int.Parse(s.Name.Split()[1]); 

     if (b[x, y].BackColor == Color.Red || b[x, y].BackColor == Color.Black) 
      return; 

     var color = cnt == 1 ? Color.Red : Color.Black; 
     cnt = 1 - cnt; 
     b[x, y].BackColor = color; 

      int len = 4; 

      var directions = new[] 
      { 
      new {x = 0, y = 1}, 
      new {x = 0, y = -1}, 
      new {x = 1, y = 0}, 
      new {x = -1, y = 0}, 
      new {x = -1, y = -1}, 
      new {x = 1, y = -1}, 
      new {x = -1, y = 1}, 
      new {x = 1, y = 1} 
      }; 
      b[x, y].BackColor = color; 

      foreach (var dir in directions) 
      { 
      for (var i = 1; i < len; ++i) 
      { 
       var xi = x + i * dir.x; 
       var yi = y + i * dir.y; 
       if (xi < 0 || xi >= len || yi < 0 || yi >= len) 
       break; 
       if (b[xi, yi].BackColor != Color.Black && b[xi, yi].BackColor != Color.Red) 
       break; 
       if (b[xi, yi].BackColor == color) 
       { 
       for (var j = 1; j < i; ++j) 
        b[x + j * dir.x, y + j * dir.y].BackColor = color; 
       break; 
       } 
      } 
      } 
    } 
+0

嗯,我用了整個事情並沒有真的做任何事情..(我接近解決它的另一種方式,但感謝但努力) – Tacit

+0

我得到它的工作 – Tacit

+0

哇真的很好謝謝@DarkGray – Tacit

0

回答

private void ClickedButton(object sender, EventArgs e) 
     { 
      Button s = (Button)sender; 
      int x = int.Parse(s.Name.Split()[0]); 
      int y = int.Parse(s.Name.Split()[1]); 

      // if (b[x, y].BackColor != Color.Red && b[x, y].BackColor != Color.Black) 
      // return; 

      var color = cnt == 1 ? Color.Red : Color.Black; 
      cnt = 1 - cnt; 
      b[x, y].BackColor = color; 

      int len = 4; 

      var directions = new[] 
      { 
      new {x = 0, y = 1}, 
      new {x = 0, y = -1}, 
      new {x = 1, y = 0}, 
      new {x = -1, y = 0}, 
      new {x = -1, y = -1}, 
      new {x = 1, y = -1}, 
      new {x = -1, y = 1}, 
      new {x = 1, y = 1} 
      }; 
      b[x, y].BackColor = color; 

      foreach (var dir in directions) 
      { 
       for (var i = 1; i < len; ++i) 
       { 
        var xi = x + i * dir.x; 
        var yi = y + i * dir.y; 
        if (xi < 0 || xi >= len || yi < 0 || yi >= len) 
         break; 
        if (b[xi, yi].BackColor != Color.Black && b[xi, yi].BackColor != Color.Red) 
         break; 
        if (b[xi, yi].BackColor == color) 
        { 
         for (var j = 1; j < i; ++j) 
          b[x + j * dir.x, y + j * dir.y].BackColor = color; 
         break; 
        } 
       } 
      } 
     }