あなたがアメリカ出身で、クラッカーバレルに行ったことがあるなら、おそらくボードゲームをしたことがあります。 1つだけ残っています。ちょうどピラミッドや三角形の中国チェッカーに似ています。C#、1つの方法を開始する2つのボタンに問題がある
ボタンを作成してフォームに追加するフォームがあり、フォーム上でのジャンプの仕組みに関するすべてのルールを持つ "TheBoard"クラスがあります。私のフォームでは、これをすべて実行する必要があるボタンクリッカーメソッドもあります。
私はレンガの壁に当たったようです。ボードクラスのif
ステートメント全体を移動するために、2回目のクリックを受け入れるようにするというロジックを理解できません。ボードクラスのmoveメソッドのパラメータにはint x
があり、これはパラメータとしてクリックしたボタンです。私は動きの後半が欠けているように感じる。どのように私は2つのボタンクリック(ペグの開始位置とペグの終了位置)を登録するために私の移動メソッドを取得するのですか?フォームの
コード:TheBoardクラスの
public partial class Form1 : Form
{
private Button[] btn = new Button[15];
private TheBoard myboard = new TheBoard();
public Form1()
{
InitializeComponent();
int buttonsPerRow = 1;
int index = 0;
while (index < btn.Length)
{
int increment = this.Width/(buttonsPerRow + 1);
for (int j = 1; j <= buttonsPerRow; j++)
{
btn[index] = new Button
{
//other style elements of the button
Name = "btn" + index
}
btn[index].Click += new EventHandler(this.My_Click);
Controls.Add(btn[index]);
index++;
}
buttonsPerRow++;
}
}
private void My_Click(object sender, EventArgs e) {
myboard.getValues();
Button b = (Button)sender;
string bName = b.Name;
// Now pull off the btn
string num = bName.Substring(3, bName.Length - 3);
// Parsing the number to an int
int x = Int32.Parse(num);
myboard.move(x);
int[] color = myboard.getValues();
for (int i = 0; i < 15; i++)
{
color = myboard.getValues();
if (color[i] == TheBoard.hasPeg)
{
btn[i].BackColor = System.Drawing.Color.Yellow;
}
else
btn[i].BackColor = System.Drawing.Color.Black;
}//for
}
}
コード:
class TheBoard
{
static public int hasPeg = 100;
static public int noPeg = 50;
private int[] board;
private int firstMove; //1st click
public TheBoard()
{
board = new int[15];
board[0] = noPeg;
for(int i = 1; i < 15; i++)
{
board[i] = hasPeg;
}
firstMove = -1; //giving last move a location, starting it at the beginning
}
public int move(int x)
{
if(firstMove == -1)
{
firstMove = x;
return 0;
}
// blank at 0
// if you click a blank your 1st move
if (firstMove == noPeg)
{
Console.WriteLine("You cant move if there isn't a peg.");
return 666;
}
// first---------------------------------------middle-----------------------end
if (firstMove == 1 && board[0] == hasPeg && board[3] == hasPeg && board[6] == noPeg)
{
RemovePeg(board[0], board[3], board[6]);
return 0;
}
if (firstMove == 1 && board[0] == hasPeg && board[2] == hasPeg && board[4] == noPeg)
{
RemovePeg(board[0], board[2], board[4]);
return 0;
}
//etc for remaining firstMove possibilities
firstMove = -1;
return 5;
}
private int RemovePeg(int first, int second, int goal) {
board[goal] = hasPeg;
board[first] = noPeg;
board[second] = noPeg;
return 0;
}
public int[] getValues()
{
return board;
}
}
ボタンに「onclick」イベントを割り当てる必要があります。 – DaniCE
私は初心者ですから、どういう意味なのかよくわかりません。各ボタンを作成するforループやform1のmy_Clickには? –