2010-05-31 3 views
0

tic tac toe 게임용입니다. 모든 컨트롤의 텍스트가 비어 있지 않은지 확인하기 위해 확인 문을 만드는 데 도움이 필요합니다. 그렇다면 이전 코드를 얻은 사람이 그 코드를 발견했을 경우 무승부가 발생합니다. 내 코드를 사용하여 좋은 예를 들어 주시겠습니까?컨트롤이 비어 있지 않은지 확인하기 위해 확인 문을 작성하는 데 도움이 필요합니다.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Drawing; 
namespace MyGame 
{ 
    public class Result1 
    { 


     static private int[,] Winners = new int[,] 
        { 
         // main gameplay Ex: if x is on 0,1,2 x is the winner 
         {0,1,2}, 
         {3,4,5}, 
         {6,7,8}, 
         {0,3,6}, 
         {1,4,7}, 
         {2,5,8}, 
         {0,4,8}, 
         {2,4,6}, 
        }; 


     static public bool CheckWinner(Button[] myControls) 
     { 
      //bolean statement to check for the winner 
      bool gameOver = false; 
      for (int i = 0; i < 8; i++) 
      { 
       int a = Winners[i, 0]; 
       int b = Winners[i, 1]; 
       int c = Winners[i, 2]; 

       Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c]; 
       if (b1.Text == "" || b2.Text == "" || b3.Text == "") 
        continue; 

       if (b1.Text == b2.Text && b2.Text == b3.Text) 
       { 

        b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral; 
        b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
        gameOver = true; 
        xWinnerForm xWinnerForm = new xWinnerForm(); 
        xWinnerForm.ShowDialog(); //only works with show not showDialog method gets overloaded (b1.Text + " is the Winner"); to get around this I added and image showing the last player 


       } 


      } 


     return gameOver; 
     } 





    } 
} 

답변

0

이미 빈 공간을 확인하고 당신이 빈 공간을 찾아 모든 경우에 무승부가없는 것입니다 코드를 가지고 있음을 관찰합니다. 가능한 모든 공간을 반복하면서 승자 나 빈 공간을 찾지 못하면 분명히 무승부입니다.

static public bool CheckWinner(Button[] myControls) 
{ 
    //bolean statement to check for the winner 
    bool gameOver = false; 
    bool foundEmpty = false; 
    for (int i = 0; i < 8; i++) 
    { 
     int a = Winners[i, 0]; 
     int b = Winners[i, 1]; 
     int c = Winners[i, 2]; 

     Button b1 = myControls[a], b2 = myControls[b], b3 = myControls[c]; 
     if (b1.Text == "" || b2.Text == "" || b3.Text == "") 
     { 
      foundEmpty = true; 
      continue; 
     } 

     if (b1.Text == b2.Text && b2.Text == b3.Text) 
     { 

      b1.BackColor = b2.BackColor = b3.BackColor = Color.LightCoral; 
      b1.Font = b2.Font = b3.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Italic & System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); 
      gameOver = true; 
      xWinnerForm xWinnerForm = new xWinnerForm(); 
      xWinnerForm.ShowDialog(); //only works with show not showDialog method gets overloaded (b1.Text + " is the Winner"); to get around this I added and image showing the last player 


     } 


    } 
    if (!gameOver && !foundEmpty) 
    { 
     // must be a draw 
     gameOver = true; 
    } 

    return gameOver; 
} 
+0

정말 대단히 감사합니다. –

0

모든 컨트롤이 myControls 배열에있는 것처럼 보입니다. 당신은 오히려 쉽게 조회 할 수 LINQ를 사용할 수 있습니다

if (myControls.All(c => c.Text != "") { 
    // Draw 
} 
+0

나는 이것을 시도했지만, 단지 3 개의 박스 만 선택했을 때 이벤트를 발생시킨다. 어떻게 해결할 수 있는가? 나는 프로그래밍에 익숙하지 않아서, 나는 그것을 잘 이해하지 못한다. –

관련 문제