2009-11-29 7 views
1

J'ai un problème avec mon programme BlackJack. Voici le code dans BlackJack.cs:Comment accéder à une classe d'une classe différente?

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() 
    { 



    } 


} 
} 

Maintenant, voici mon code 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.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(); 
    } 


} 
} 

Le problème est que lorsque j'appuie sur le bouton « Play » sur la forme, il est censé pour exécuter start() dans la classe BlackJack. Quand il fonctionne, quelque chose est supposé être écrit dans "playertb" qui est une zone de texte. Rien n'est écrit dans la zone de texte et je ne peux rien y faire. S'il vous plaît aider.

+0

Est-ce un devoir? Si c'est le cas, je suggérerais de le marquer comme tel. – JohnFx

Répondre

0

Dans la classe blackjack, vous créez une seconde instance de Form1 appelée f1, mais vous ne l'affichez jamais. Vous appelez blackjack.Start() à partir de votre première instance de Form1.

Pour résoudre le problème immédiat, vous pouvez essayer ce qui suit:

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(); 

    } 

alors, dans votre classe Form1,

blackjack.start(this); 

Bien que, si je concevais moi-même, je ce serait faire blackjack une classe non statique avec un constructeur. Pour le moment, vous ne pouvez faire qu'un seul jeu de blackjack à la fois. Je signalerais alors des changements dans le jeu via des événements. Cela empêche la situation où la classe blackjack doit savoir sur Form1. En principe, le blackjack devrait savoir très peu ou rien sur la couche d'interface utilisateur. Cela rend votre code beaucoup plus portable et réutilisable.

+0

j'ai tapé 'blackjack statique blaj = new blackjack' dans form1 et l'a utilisé istead de blackjack.start et puis j'obtiens une erreur en disant ceci: Member 'WindowsFormsApplication1.blackjack.start()' ne peut pas être accédé avec une référence d'instance; le qualifier avec un nom de type à la place – Bob

+0

oh n'a pas vu votre édition – Bob

+0

@Charlie: Il n'est pas vrai qu'une classe sans un constructeur ne peut pas être instanciée. Si vous ne savez pas de quoi vous parlez, mieux vaut ne rien dire. De cette façon, vous ne trompez pas les gens. –

Questions connexes