2015-11-06 45 views
-2

我想为使用Windows窗体和c#的两个玩家连续(gomoku)游戏做一个简单的五。我把一个带有图片的电影箱放在窗体上。现在我想将标签放在画板的所有交叉点上,以便用户可以点击它们并将它们的背景颜色更改为黑色或白色。c#gomoku游戏标签数组

  1. 如何使标签创建可点击的窗体上?

    public partial class Form1 : Form 
    { 
        int labelCount = 0; 
        int iteration = 0; 
    
        public Form1() 
        { 
         InitializeComponent(); 
         Label[] board = new Label[361]; 
    
         for (int i = 0; i < 361; i++) 
         { 
          board[i] = new Label 
          { 
           Name = "label" + i, 
           Height = 55, 
           Width = 55, 
           MinimumSize = new Size(55, 55), 
           Text = "label " + i 
          }; 
         } 
    
         int x = 0; 
         int y = 0; 
    
         foreach (var Label in board) 
         { 
          if (x >= 580) 
          { 
           x = 0; 
           y = y + Label.Height + 55; 
          } 
    
          Label.Location = new Point(x, y); 
          this.Controls.Add(Label); 
          x += Label.Width; 
         }   
        } 
    } 
    
  2. 我应该做一个一维[361]或二维数组[{A,1}, {A,2}....{D,1}]轻松地检查是否有赢家?我怎样才能将它连接到创建的标签,使阵列数据与板上的物体相对应?

+0

bumpppppppppppp – Caza

回答

0

很抱歉如果不理解您的问题。对于Q.1添加361个标签,您可以尝试下面的代码。我希望它能帮助你。

public int x = 0; 
    public int y = 0; 

    private Label[] moku = new Label[361]; 

    private void Form1_Load(object sender, EventArgs e) 
    { 
     try 
     { 
      for (int i = 0; i < 361; i++) 
      { 
       moku[i] = new Label(); 
       moku[i].Parent = pictureBox1;//make the picturebox parent 
       moku[i].Location = new Point(x, y); 
       moku[i].Text = "O"; 
       moku[i].Name = "moku" + i; 
       moku[i].BackColor = Color.Transparent; 
       pictureBox1.Controls.Add(moku[i]); 
       y += 55; 
       if (y >= 361) { x += 55; y = 0; x+=55; } 
      } 
     }catch(Exception er) 
     { 
      MessageBox.Show(er.ToString()); 
     } 


    } 
0

我更喜欢使用2D数组,因为如果您想检查周围的盒子,它更容易。

表设计:

enter image description here

完整的源:

using System; 
using System.Drawing; 
using System.Windows.Forms; 

namespace WindowsFormsApplication6 
{ 
    public enum Player 
    { 
     Empty = 0, 
     White, 
     Black 
    } 

    public partial class Form1 : Form 
    { 
     // initialize board of 5x5 
     private Player[,] board = new Player[5, 5]; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      DrawBoard(); 
     } 

     private void DrawBoard() 
     { 
      for (var i = 0; i <= board.GetUpperBound(0); i++) 
      { 
       for (var j = 0; j <= board.GetUpperBound(1); j++) 
       { 
        // for name and text 
        var name = string.Format("{0}, {1}", i, j); 

        var label = new Label() 
        { 
         Name = name, // name of label 
         Size = new Size(55, 55), 
         BorderStyle = BorderStyle.FixedSingle, 
         Location = new Point(i * 55, j * 55), // location depends on iteration 
         Text = name 
        }; 

        label.Click += ClickLabel; // subscribe the Click event handler 

        pictureBox1.Controls.Add(label); // add label to a container 
       } 
      } 
     } 

     // this event handler will handle all the labels click event 
     private void ClickLabel(object sender, EventArgs e) 
     { 
      var label = (Label)sender; // this is the label that you click 
      var x = Convert.ToInt32(label.Name.Split(',')[0]); 
      var y = Convert.ToInt32(label.Name.Split(',')[1]); 

      // change the color 
      if (radPlayerBlack.Checked) 
      { 
       // Player Black 
       label.ForeColor = Color.White; 
       label.BackColor = Color.Black; 
       board[x, y] = Player.Black; 
      } 
      else 
      { 
       // Player White 
       label.ForeColor = Color.Black; 
       label.BackColor = Color.White; 
       board[x, y] = Player.White; 
      } 
     } 
    } 
} 

您可以查看2D阵列的黑色或白色的值。这是我在Visual Studio中快速查看时的值。

enter image description here enter image description here

+0

如果你想的事件处理程序添加到您的标签,我在我的解决方案提供它。 – Han