2017-02-25 182 views
1

我想對角具有在其中選擇要移動,然後選擇要移動到按鈕,按鈕的櫃檯過程中獲得的基本運動在我的C#跳棋遊戲合作,通過移動一個空間,即,但我似乎無法得到它的工作。這裏是我的代碼C#跳棋遊戲運動

我有登錄的第二種形式,但它工作正常,主要問題是運動,所以忽略小部分代碼談論F2form2

這是Form1.cs的

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 

namespace Checkers 
{ 
    public partial class Board : Form 
    { 
     bool turn = true; // true = blue turn, false = red turn 
     //Player currentPlayer; an alternative 
     int turncount = 0; 
     static String player1initial; 
     static String player2initial; 
     Player p1 = new Player(); 
     Player p2 = new Player(); 
     Grid[,] buttons = new Grid[8, 8];   

     private Grid selectedbutton; 
     private Grid MoveTobutton; 

     private char PlayerMark = 'b'; 

     public Board() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      LoginForm F2 = new LoginForm(); 
      F2.ShowDialog(); 
      P1NameLabel.Text = player1initial; 
      P2NameLabel.Text = player2initial; 
      p1.GetPlayerName = player1initial; 
      p2.GetPlayerName = player2initial; 
      p1.GetPlayerWinCount = 0; 
      p2.GetPlayerWinCount = 0; 

     } 

     public static void SetNames(String n1, String n2) 
     { 
      player1initial = n1; 
      player2initial = n2; 
     } 

     private void exitToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      Application.Exit(); 
     }   

     private void Start_Click(object sender, EventArgs e) 
     { 
      buttons[0, 0] = new Grid(A1, 'b',1); 
      buttons[0, 1] = new Grid(B1, ' ',2); 
      buttons[0, 2] = new Grid(C1, 'b',3); 
      buttons[0, 3] = new Grid(D1, ' ', 4); 
      buttons[0, 4] = new Grid(E1, 'b', 5); 
      buttons[0, 5] = new Grid(F1, ' ', 6); 
      buttons[0, 6] = new Grid(G1, 'b', 7); 
      buttons[0, 7] = new Grid(H1, ' ', 8); 
      buttons[1, 0] = new Grid(A2, ' ',9); 
      buttons[1, 1] = new Grid(B2, 'b',10); 
      buttons[1, 2] = new Grid(C2, ' ',11); 
      buttons[1, 3] = new Grid(D2, 'b', 12); 
      buttons[1, 4] = new Grid(E2, ' ', 13); 
      buttons[1, 5] = new Grid(F2, 'b', 14); 
      buttons[1, 6] = new Grid(G2, ' ', 15); 
      buttons[1, 7] = new Grid(H2, 'b', 16); 
      buttons[2, 0] = new Grid(A3, 'b',17); 
      buttons[2, 1] = new Grid(B3, ' ',18); 
      buttons[2, 2] = new Grid(C3, 'b',19); 
      buttons[2, 3] = new Grid(D3, ' ', 20); 
      buttons[2, 4] = new Grid(E3, 'b', 21); 
      buttons[2, 5] = new Grid(F3, ' ', 22); 
      buttons[2, 6] = new Grid(G3, 'b', 23); 
      buttons[2, 7] = new Grid(H3, ' ', 24); 
      buttons[3, 0] = new Grid(A4, ' ', 25); 
      buttons[3, 1] = new Grid(B4, ' ', 26); 
      buttons[3, 2] = new Grid(C4, ' ', 27); 
      buttons[3, 3] = new Grid(D4, ' ', 28); 
      buttons[3, 4] = new Grid(E4, ' ', 29); 
      buttons[3, 5] = new Grid(F4, ' ', 30); 
      buttons[3, 6] = new Grid(G4, ' ', 31); 
      buttons[3, 7] = new Grid(H4, ' ', 32); 
      buttons[4, 0] = new Grid(A5, ' ', 33); 
      buttons[4, 1] = new Grid(B5, ' ', 34); 
      buttons[4, 2] = new Grid(C5, ' ', 35); 
      buttons[4, 3] = new Grid(D5, ' ', 36); 
      buttons[4, 4] = new Grid(E5, ' ', 37); 
      buttons[4, 5] = new Grid(F5, ' ', 38); 
      buttons[4, 6] = new Grid(G5, ' ', 39); 
      buttons[4, 7] = new Grid(H5, ' ', 40); 
      buttons[5, 0] = new Grid(A6, ' ', 41); 
      buttons[5, 1] = new Grid(B6, 'r', 42); 
      buttons[5, 2] = new Grid(C6, ' ', 43); 
      buttons[5, 3] = new Grid(D6, 'r', 44); 
      buttons[5, 4] = new Grid(E6, ' ', 45); 
      buttons[5, 5] = new Grid(F6, 'r', 46); 
      buttons[5, 6] = new Grid(G6, ' ', 47); 
      buttons[5, 7] = new Grid(H6, 'r', 48); 
      buttons[6, 0] = new Grid(A7, 'r', 49); 
      buttons[6, 1] = new Grid(B7, ' ', 50); 
      buttons[6, 2] = new Grid(C7, 'r', 51); 
      buttons[6, 3] = new Grid(D7, ' ', 52); 
      buttons[6, 4] = new Grid(E7, 'r', 53); 
      buttons[6, 5] = new Grid(F7, ' ', 54); 
      buttons[6, 6] = new Grid(G7, 'r', 55); 
      buttons[6, 7] = new Grid(H7, ' ', 56); 
      buttons[7, 0] = new Grid(A8, ' ', 57); 
      buttons[7, 1] = new Grid(B8, 'r', 58); 
      buttons[7, 2] = new Grid(C8, ' ', 59); 
      buttons[7, 3] = new Grid(D8, 'r', 60); 
      buttons[7, 4] = new Grid(E8, ' ', 61); 
      buttons[7, 5] = new Grid(F8, 'r', 62); 
      buttons[7, 6] = new Grid(G8, ' ', 63); 
      buttons[7, 7] = new Grid(H8, 'r', 64); 

      FillGrid(); 
      Start.Hide(); 
      checkcurrentplayer(); 
     } 

     private void FillGrid() 
     { 
      try 
      { 
       for (int i = 0; i < 8; i++) 
       { 
        for (int j = 0; j < 8; j++) 
        { 
         buttons[i, j].GetButton.Click += new System.EventHandler(Button_Click); 

         if (buttons[i, j].getmark == 'r') 
         { 
          buttons[i, j].GetButton.Image = Checkers.Properties.Resources.red; 
         } 

         else if (buttons[i, j].getmark == 'b') 
         { 
          buttons[i, j].GetButton.Image = Checkers.Properties.Resources.blue; 
         } 
         else if (buttons[i, j].GetButton.BackColor == Color.Black) 
         { 
          buttons[i, j].GetButton.Enabled = false; 
         } 
         else 
         { 
          buttons[i, j].GetButton.Image = null; 

         } 
        } 
       } 
      } 
      catch (IndexOutOfRangeException) 
      { 
       MessageBox.Show("Error: please click 'X' in Program Window and exit."); 
      } 
     } 

     private void CheckForWinner() 
     { 
      bool winner = false; 
      String winnertext = ""; 

      for (int i = 0; i < 8; i++) 
      { 
       for (int j = 0; j < 8; j++) 
       { 
        if (buttons[i, j].GetButton.Image == Checkers.Properties.Resources.blue) 
        { 
         if (turn == true) 
         { 
          disablebuttons(); 
          winnertext = (p1.GetPlayerName); 
          p1.GetPlayerWinCount += 1; 
          winner = true; 
         } 
        } 
        else if (buttons[i, j].GetButton.Image == Checkers.Properties.Resources.red) 
        { 
         if (turn == false) 
         { 
          disablebuttons(); 
          winnertext = (p2.GetPlayerName); 
          p2.GetPlayerWinCount += 1; 
          winner = true; 
         } 
        } 
        MessageBox.Show(winnertext + " Wins!"); 
       } 
      } 
     } 

     private void disablebuttons() 
     { 
      foreach (Control c in Controls) 
      { 
       if (c.GetType() == typeof(Button)) 
       { 
        Button b = (Button)c; 
        b.Enabled = false; 
       } 

      }// end foreach 
     } 

     private void newGameToolStripMenuItem_Click(object sender, EventArgs e) 
     { 
      turn = true; 

      foreach (Control c in Controls) 
      { 
       try 
       { 
        Button b = (Button)c; 
        b.Enabled = true; 
        b.Image = null; 
        FillGrid(); 
       } 
       catch { } 

      }// end foreach 
     } 

     private void checkcurrentplayer() 
     { 
      if (turn == true) 
      { 
       currentplayerlabel.Text = ("Current Player: " + p1.GetPlayerName); 
      } 
      else 
      { 
       currentplayerlabel.Text = ("Current Player: " + p2.GetPlayerName); 
      } 
     } 

     private void Button_Click(object sender, EventArgs e) 
     { 
      Button currentbtn = (Button)sender; 
      checkcurrentplayer(); 

      if (selectedbutton != null) 
      { 
       for (int i = 0; i < 8; i++) 
       { 
        for (int j = 0; j < 8; j++) 
        { 
         if (currentbtn.Image == buttons[i, j].GetButton.Image) 
         { 
          if (buttons[i, j].GetButton.Image == null) 
          { 
           MoveTobutton = buttons[i, j]; 
           MoveTobutton.GetButton.Image = selectedbutton.GetButton.Image; 
           selectedbutton.GetButton.Image = null; 
           selectedbutton = null; 
           turn = !turn; 
          } 
         } 
        } 
       } 
      } 
      else 
      { 
       for (int i = 0; i < 8; i++) 
       { 
        for (int j = 0; j < 8; j++) 
        { 
         if (currentbtn.Image == buttons[i, j].GetButton.Image) 
         { 
          if (turn == true) 
          { 
           if (buttons[i, j].GetButton.Image == Checkers.Properties.Resources.blue) 
           { 
            selectedbutton = buttons[i, j]; 
           } 
          } 
          else if (turn == false) 
          { 
           if (buttons[i, j].GetButton.Image == Checkers.Properties.Resources.red) 
           { 
            selectedbutton = buttons[i, j]; 
           } 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
} 

下面是代碼到我的網類

namespace Checkers 
{ 
    public class Grid 
    { 
     private Button button; 
     private char Mark; 
     private int location; 

     public Grid(Button _button, char _mark, int _location) 
     { 
      button = _button; 
      Mark = _mark; 
      location = _location; 
     } 

     public Button GetButton 
     { 
      get { return button; } 
      set { button = value; } 
     } 

     public char getmark 
     { 
      get { return Mark; } 
      set { Mark = value; } 
     } 

     public int getlocation 
     { 
      get { return location; } 
      set { location = value; } 
     } 
    } 
} 

Picture of gameboard itself and picture of gameboard after start button has been clicked, after grid is filled start button is hidden.

Picture of Gameboard itself

這是一個基本沒有驗證解決方案的代碼(參考其他人做c#che ckers程序)這應該工作得很好。

private void Mouse_Click(object sender, MouseEventArgs e) 
    { 
     Button currentbutton = (Button)sender; 

     if (selectedbutton != null) 
     { 
      MoveTobutton = currentbutton; 
      Image currentimage = selectedbutton.Image; 
      selectedbutton.Image = MoveTobutton.Image; 
      MoveTobutton.Image = currentimage; 
      selectedbutton = null; 
     } 
     else 
     { 
      selectedbutton = currentbutton; 

     } 
    } 
+1

什麼是「我似乎無法得到它的工作」是什麼意思?它會拋出異常?告訴我們這個例外。它不符合你的預期?它有什麼作用?你期望什麼? –

+0

當我點擊一個按鈕與計數器,然後另一個按鈕,它什麼都不做。 – CP71

+0

我會嘗試添加開始遊戲被點擊後的董事會的照片。 – CP71

回答

0
private void Mouse_Click(object sender, MouseEventArgs e) 
{ 
    Button currentbutton = (Button)sender; 

    if (selectedbutton != null) 
    { 
     MoveTobutton = currentbutton; 
     Image currentimage = selectedbutton.Image; 
     selectedbutton.Image = MoveTobutton.Image; 
     MoveTobutton.Image = currentimage; 
     selectedbutton = null; 
    } 
    else 
    { 
     selectedbutton = currentbutton; 

    } 
}