2009-11-29 8 views
1

여기 블랙 잭 프로그램에 문제가 있습니다. 현재 이곳에다른 클래스에서 클래스에 액세스하는 방법?

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

namespace WindowsFormsApplication1 
{ 
    public class blackjack 
    { 

    static string[] playercards = new string[11]; 
    static string hitstay = ""; 
    static int total = 0, count = 1, dealertotal = 0; 
    static Random cardshuffler = new Random(); 
    static Form1 f1 = new Form1(); 




    public static void start() 
    { 


     dealertotal = cardshuffler.Next(15, 22); 
     playercards[0] = deal(); 
     playercards[1] = deal(); 
     f1.TextValue = "test"; 

     bj(); 

    } 


    private static void hit() 
    { 

     count += 1; 
     playercards[count] = deal(); 
     f1.playertb.Text += "you were dealed a(n) " + playercards[count] + ".your new total is " + total + "."; 
     if (total.Equals(21)) 
     { 
      f1.playertb.Text += "you got blackjack! the dealer's total was " + dealertotal + ".would you like to play again?"; 
      playagain(); 
     } 
     else if (total > 21) 
     { 
      f1.playertb.Text += "you busted, therefore you lost. sorry. the dealer's total was " + dealertotal + ".would you like to play again? y/n"; 
      playagain(); 
     } 
     else if (total < 21) 
     { 
      do 
      { 
       f1.playertb.Text += "would you like to hit or stay?"; 

      } while (!hitstay.Equals(f1.Hit) && !hitstay.Equals(f1.Stay)); 
      bj(); 
     } 
    } 

    private static string deal() 
    { 
     string Card = ""; 
     int cards = cardshuffler.Next(1, 14); 
     switch (cards) 
     { 
      case 1: Card = "Two"; total += 2; 
       break; 
      case 2: Card = "Three"; total += 3; 
       break; 
      case 3: Card = "Four"; total += 4; 
       break; 
      case 4: Card = "Five"; total += 5; 
       break; 
      case 5: Card = "Six"; total += 6; 
       break; 
      case 6: Card = "Seven"; total += 7; 
       break; 
      case 7: Card = "Eight"; total += 8; 
       break; 
      case 8: Card = "Nine"; total += 9; 
       break; 
      case 9: Card = "Ten"; total += 10; 
       break; 
      case 10: Card = "Jack"; total += 10; 
       break; 
      case 11: Card = "Queen"; total += 10; 
       break; 
      case 12: Card = "King"; total += 10; 
       break; 
      case 13: Card = "Ace"; total += 11; 
       break; 

     } 
     return Card; 
    } 

    static void bj() 
    { 
     if (hitstay.Equals (f1.Hit)) 
     { 
      hit(); 
     } 
     else if (hitstay.Equals(f1.Stay)) 
     { 
      if (total > dealertotal && total <= 21) 
      { 
f1.PlayerText += "you won! the dealer busted with " + dealertotal + " as their total" + "your total was " + total; 
       playagain(); 
      } 
      else if (total < dealertotal) 
      { 
f1.PlayerText += "sorry, you lost! the dealer's total was " + dealertotal; 
       playagain(); 
      } 

     } 
    } 

    private static void playagain() 
    { 



    } 


} 
} 

내에서 Form1.cs 코드 :이 BlackJack.cs의 코드 내가 양식에 "재생"버튼을 누르면

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

namespace WindowsFormsApplication1 
{ 



public partial class Form1 : Form 
{ 



    public Form1() 
    { 
     InitializeComponent(); 


    } 

    public void Form1_FormClosing(object sender, FormClosingEventArgs e) 
    { 
     DialogResult result = MessageBox.Show 
      ("Are You Sure You Want To Close?", 
      "", 
      MessageBoxButtons.OK, 
      MessageBoxIcon.Warning); 

     if (result == DialogResult.Cancel) 
      e.Cancel = true; 

    } 



     public void button1_Click(object sender, EventArgs e) 
    { 
     BetForm betForm = new BetForm(); 


     betForm.StartPosition = FormStartPosition.CenterParent; 
     if (betForm.ShowDialog() == DialogResult.OK) 
     { 
      MessageBox.Show("You bet $" + betForm.Bet); 
     } 
    } 

    public string TextValue 
    { 

     set 
     { 
      playertb.Text = value; 
     } 

    } 





public string PlayerText 
    { 
    get { return playertb.Text; } 
    set { playertb.Text = value; } 
    } 

    private void Stay_Click(object sender, EventArgs e) 
    { 

    } 

    private void Play_Click(object sender, EventArgs e) 
    { 
     blackjack.start(); 
    } 


} 
} 

이 문제는이 가정입니다

것을 BlackJack 클래스에서 start()를 수행합니다. 그것을 수행 할 때 무언가는 "playertb"라는 텍스트 상자에 기록되어야합니다. 텍스트 상자에 아무 것도 쓰지 않고 그것에 대해 아무 것도 할 수 없습니다. 도와주세요.

+0

숙제가 있습니까? 그렇다면 나는 그런 태그를 붙이는 것이 좋습니다. – JohnFx

답변

0

블랙 잭 클래스에서는 f1이라는 Form1의 두 번째 인스턴스를 만들지 만 표시하지 않습니다. Form1의 첫 번째 인스턴스에서 blackjack.Start()를 호출합니다. 나는이 나 자신을 설계 할 경우

blackjack.start(this); 

,하지만, 내가 blackjack을 만들 것, 당신의 Form1 클래스에 다음

static Form1 f1 = null; // declare it, but don't initialise it (we'll get 
          // it when start() is called) 

public static void start(Form1 form) 
    { 

     // set the static reference to the calling form, 

     f1 = form 

     dealertotal = cardshuffler.Next(15, 22); 
     playercards[0] = deal(); 
     playercards[1] = deal(); 
     f1.TextValue = "test"; 

     bj(); 

    } 

:

는 즉시 문제를 해결하려면 다음을 시도 할 수 있습니다 생성자가있는 비 정적 클래스 현재로서는 한 번에 1 개의 블랙 잭 게임 만 실행할 수 있습니다. 그러면 이벤트를 통해 게임의 변화를 알릴 것입니다. 이렇게하면 블랙 잭 클래스가 Form1에 대해 알아야하는 상황을 방지 할 수 있습니다. 원칙적으로, 블랙 잭은 UI 레이어에 대해 거의 또는 전혀 알지 못합니다. 이렇게하면 코드를 훨씬 더 이식성 있고 재사용 할 수 있습니다.

+0

form1에 'static blackjack blaj = new blackjack'을 입력하고 blackjack.start의 istead를 사용하면 다음과 같은 오류 메시지가 나타납니다. Member 'WindowsFormsApplication1.blackjack.start()'는 인스턴스 참조로 액세스 할 수 없습니다. 형식 이름 대신 – Bob

+0

을 입력하십시오. 편집을 보지 못했습니다. – Bob

+0

@Charlie : 생성자가없는 클래스를 인스턴스화 할 수 없다는 것은 사실이 아닙니다. 당신이 무슨 말을하고 있는지 모른다면, 아무 말도하지 않는 것이 가장 좋습니다. 그렇게하면 사람들을 잘못 알고있는 것이 아닙니다. –

관련 문제