2016-06-25 13 views
0

問題が発生しました。私はaiと対戦できるチックタックのつま先を作ろうとしています。しかし、この問題は本当に気にしません。変数をCに代入するとリストが変わります#

[0,1,0]
[1、2,0]
[0、2、0]

0が空スポットである:基板は、2Dリスト魔女は次のように見えるです
1は、プレイヤー1(円)
2であるプレイヤー2(クロス)

あるしかし、私は、私はループのためにこれを作ったように、すべてのスポットスルー反復処理する必要があるという問題があります。

List<List<int>> boardTry; 
List<List<int>> board = new List<List<int>> { new List<int> { 0, 0, 0 }, new List<int> { 0, 0, 0, }, new List<int> { 0, 0, 0 } }; 

for (int rowNum = 0; rowNum < 3; rowNum ++) 
      { 
       for (int colNum = 0; colNum < 3; colNum++) 
       { 
        // Check if spot is empty 
        if (board[rowNum][colNum] == 0) 
        { 
         boardTry = board; 
         showBoard(board); 

         boardTry[rowNum][colNum] = 2; 
        } 
       } 
      } 

私は、リストがどのように見えるかを見るためにshowBoardというメソッドを作成しました。

void showBoard(List<List<int>> board) 
     { 
      StringBuilder sb = new StringBuilder(); 

      foreach (List<int> row in board) 
      { 
       sb.Append("["); 
       foreach(int col in row) 
       { 
        sb.Append(col.ToString() + " "); 
       } 
       sb.Append("]"); 
      } 

      MessageBox.Show(sb.ToString(0, 8) + "\n" + sb.ToString(8, 8) + "\n" + sb.ToString(16, 8)); 

     } 

しかし、私はこのコードを実行するときに問題があるがboardリストがboardTryと変化していることです。だからboardTryboardに割り当てるたびに、boardboardTryに等しくなっています。

このコードを実行すると、これが表示されます。 enter image description here

答えて

4

あなたがこれを行うと:

boardTry = board; 

それは、新しいリストを作成しません。 boardTryboardは両方とも同じリストを参照する変数です。したがって、boardTryの変更はboardにも行われます。

Here's a very recent question and answerこれは、ほぼ同じ質問とその回避策について説明しています。短いバージョンでは、既存のリストを新しい変数に代入したくないということです。既存のリストのコピーである新しいリストを作成したいとします。

ここでは、既存のボードを新しいボードにコピーする機能の例を示します。これは少し冗長です:

List<List<int>> CopyBoard(List<List<int>> original) 
{ 
    var copied = new List<List<int>>(); 
    foreach (var innerList in original) 
    { 
     copied.Add(new List<int>(innerList)); 
    } 
    return copied; 
} 

ここではLINQ式と同じことがあります。元の機能を利用して1つの行に詰め込むと、読みにくくなってしまうので、私たちは自分自身を良く感じるからです。

List<List<int>> CopyBoard(List<List<int>> original) 
{ 
    return new List<List<int>>(original.Select(innerList=> innerList.ToList())); 
} 
+0

本当に良い説明をありがとう!ほんとうにありがとう。 – Gewoo

0

このチックタックトーコントロールを試してみてください(あなたはそれを修正することができます):

using System; 
using System.Linq; 
using System.Drawing; 
using System.Windows.Forms; 
using System.Collections.Generic; 

public class TicTacToe : Panel 
{ 
    public TicTacToe() 
    { 
     // You can change these properties. 
     this.Size = new Size(281, 360); 
     this.BackColor = Color.White; 

     // Add the labels. 
     this.Controls.AddRange(new Control[] { wins, losses, turn }); 

     // To generate random numbers for the A.I 
     Random opponentPlay = new Random(); 

     // A.I turn 
     opponentDelay.Tick += delegate 
     { 
      opponentDelay.Interval = 20; 
      Point opponentPos = new Point(opponentPlay.Next(0, 3), opponentPlay.Next(0, 3)); 
      if (board[opponentPos.X][opponentPos.Y].Text.Length == 0) 
      { 
       board[opponentPos.X][opponentPos.Y].Text = "X"; 
       opponentDelay.Interval = 500; 
       opponentDelay.Stop(); 
       UpdateGame(); 
      } 
     }; 

     // Setup board. 
     for (int x = 0; x < board.Count; x++) 
     { 
      for (int y = 0; y < board.Count; y++) 
      { 
       board[x][y].Location = new Point(4 + (x * 92), 4 + (y * 92)); 
       // Add player turn event. 
       board[x][y].Click += delegate (object sender, EventArgs e) 
       { 
        if (turn.Name == "1") 
        { 
         if (((Button)sender).Text.Length == 0) 
         { 
          ((Button)sender).Text = "0"; 
          UpdateGame(); 
         } 
        } 
       }; 

       this.Controls.Add(board[x][y]); 
      } 
     } 
    } 

    // Check game for wins, losses, and draws. 
    public void UpdateGame() 
    { 
     // Check if player won. 
     if (board[0][0].Text == "0" && board[1][0].Text == "0" && board[2][0].Text == "0") 
      EndGame(0); 
     else if (board[0][1].Text == "0" && board[1][1].Text == "0" && board[2][1].Text == "0") 
      EndGame(0); 
     else if (board[0][2].Text == "0" && board[1][2].Text == "0" && board[2][2].Text == "0") 
      EndGame(0); 
     else if (board[0][0].Text == "0" && board[0][1].Text == "0" && board[0][2].Text == "0") 
      EndGame(0); 
     else if (board[1][0].Text == "0" && board[1][1].Text == "0" && board[1][2].Text == "0") 
      EndGame(0); 
     else if (board[2][0].Text == "0" && board[2][1].Text == "0" && board[2][2].Text == "0") 
      EndGame(0); 
     else if (board[0][0].Text == "0" && board[1][1].Text == "0" && board[2][2].Text == "0") 
      EndGame(0); 
     else if (board[0][2].Text == "0" && board[1][1].Text == "0" && board[2][0].Text == "0") 
      EndGame(0); 

     // Check if opponent won. 
     if (board[0][0].Text == "X" && board[1][0].Text == "X" && board[2][0].Text == "X") 
      EndGame(1); 
     else if (board[0][1].Text == "X" && board[1][1].Text == "X" && board[2][1].Text == "X") 
      EndGame(1); 
     else if (board[0][2].Text == "X" && board[1][2].Text == "X" && board[2][2].Text == "X") 
      EndGame(1); 
     else if (board[0][0].Text == "X" && board[0][1].Text == "X" && board[0][2].Text == "X") 
      EndGame(1); 
     else if (board[1][0].Text == "X" && board[1][1].Text == "X" && board[1][2].Text == "X") 
      EndGame(1); 
     else if (board[2][0].Text == "X" && board[2][1].Text == "X" && board[2][2].Text == "X") 
      EndGame(1); 
     else if (board[0][0].Text == "X" && board[1][1].Text == "X" && board[2][2].Text == "X") 
      EndGame(1); 
     else if (board[0][2].Text == "X" && board[1][1].Text == "X" && board[2][0].Text == "X") 
      EndGame(1); 

     // Check if nobody won. 
     if (board[0][0].Text != "" && board[0][1].Text != "" && board[0][2].Text != "" && board[1][0].Text != "" && board[1][1].Text != "" && board[1][2].Text != "" && board[2][0].Text != "" && board[2][1].Text != "" && board[2][2].Text != "") 
      EndGame(2); 

     // Change turn. 
     if (turn.Name == "2") 
     { 
      turn.Name = "1"; 
      turn.Text = "Your turn"; 
     } 
     else 
     { 
      turn.Name = "2"; 
      turn.Text = "Opponents turn"; 
      opponentDelay.Start(); 
     } 
    } 

    // End game (or end round). 
    public void EndGame(int win) 
    { 
     if (win == 0) 
     { 
      MessageBox.Show("You Win!", "Tic Tac Toe"); 
      wins.Name = (Convert.ToInt32(wins.Name) + 1).ToString(); 
      wins.Text = "Wins: " + wins.Name; 
     } 
     else if (win == 1) 
     { 
      MessageBox.Show("Sorry but you lost, better luck next time...", "Tic Tac Toe"); 
      losses.Name = (Convert.ToInt32(losses.Name) + 1).ToString(); 
      losses.Text = "Losses: " + losses.Name; 
     } 
     else 
     { 
      MessageBox.Show("Draw! No one won...", "Tic Tac Toe"); 
     } 

     // Reset board. 
     for (int x = 0; x < board.Count; x++) 
     { 
      for (int y = 0; y < board.Count; y++) 
      { 
       board[x][y].Text = ""; 
      } 
     } 

     // Set the turn. 
     turn.Name = "2"; 
    } 

    // Variables 
    public Label wins = new Label() { Text = "Wins: 0", Name = "0", Location = new Point(30, 310), AutoSize = false, Size = new Size(54, 17) }; 
    public Label losses = new Label() { Text = "Losses: 0", Name = "0", Location = new Point(95, 310), AutoSize = false, Size = new Size(66, 17) }; 
    public Label turn = new Label() { Text = "Your turn", Name = "1", Location = new Point(175, 310) }; 
    public Timer opponentDelay = new Timer() { Interval = 500 }; 

    // Instead of buttons and int lists, where you have to add click event and get button index to change int list and button text, just use a button list and click evnt which read the button text and changes it. 
    public List<List<Button>> board = new List<List<Button>> 
    { 
     new List<Button> 
     { 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) } 
     }, 
     new List<Button> 
     { 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) } 
     }, 
     new List<Button> 
     { 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) }, 
      new Button() { Text = "", Font = new Font(SystemFonts.DefaultFont.Name, 26f, FontStyle.Bold), UseVisualStyleBackColor = true, TabStop = false, Size = new Size(90, 90) } 
     } 
    }; 
} 

用途:

enter image description here:それはどのようなものか

TicTacToe TicTacToeGame = new TicTacToe(); 
...Controls.Add(TicTacToeGame); 

関連する問題