Is there a way to use a variable from a function in the whole form ?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yves de Munck
    New Member
    • Mar 2011
    • 4

    Is there a way to use a variable from a function in the whole form ?

    I'm making the game four on a row (with oop)
    On the form the player has to indicate if he is playing against a Person or to the computer.
    (this is done bij indicating the right radiobutton)

    Then in the code it will controle this with an if(...)
    Code:
    if (rdbSpeler.Checked == true)
      { SpelTegenSpeler(); 
      rdbCpu.Visible = false;}
    else
    { SpelTegenSpeler(); rdbSpeler.Visible = false;  }
    If the second player had to be a person the form calls the function --> MakeHumanPlayer

    Code:
    public void SpelTegenSpeler()
            {
                Speler Speler2 = new Speler(2, Color.Yellow, false);
                //Naam laten invoeren van de speler
                frmInputbox InputboxNaam2 = new frmInputbox();
                InputboxNaam2.ShowDialog();
                //Naam gelijk stellen aan het ingestelde
                Speler2.Naam = InputboxNaam2.naam;
                InputboxNaam2.Dispose();
     }
    (Speler means Player)

    And to play against a cpuPlayer i call this function

    Code:
     public void SpelTegenComputer()
            { ComputerSpeler Speler2 = new ComputerSpeler(2,Color.Yellow,false);
                //Naam toewijzen
                Speler2.Naam = "[BOT]Speler2";}
    But i cant use the made player in the rest of the form ...

    If anyone can help to solve this, i would be really glad.
    i'm kind of new to the language .
  • GaryTexmo
    Recognized Expert Top Contributor
    • Jul 2009
    • 1501

    #2
    If I'm reading this right, you want to define a computer or human player in response to an input, then be able to use that elsewhere in your class?

    I think what you need to read up on here is a Class Member. Define your player at the class level, then any method in that class can see it.

    The next part is just me suggesting something that you might find helpful... perhaps consider using a base class or even an interface to define your player, then use that to define new classes. In this way you can reduce the complexity of your program, avoiding checking whether or not the player is human or a computer. Here's an example...

    Code:
    public interface IPlayer
    {
      string PlayerName;
      void TakeTurn();
    }
    
    public class HumanPlayer : IPlayer
    {
      // whatever logic you need, implement TakeTurn in such a way that it waits for input and responds to that input
    }
    
    public class ComputerPlayer : IPlayer
    {
      // whatever logic you need, implement TakeTurn in such a way that it plays automatically 
    }
    Now in your main game class, you can do something like this...

    Code:
    public class MainGame
    {
      private List<IPlayer> m_players = new List<IPlayer>();
      private int m_activePlayer = 0;
    
      ...
    
      public CreatePlayer(bool isComputer, ...)
      {
        IPlayer newPlayer;
        if (isComputer)
          newPlayer = new ComputerPlayer(...);
        else
          newPlayer = new HumanPlayer(...);
      }
    
      ...
    
      public void LetPlayerTakeTurn()
      {
        m_players[m_activePlayer].TakeTurn();
      }
    }
    Or something to that effect... that way you don't have if statements everywhere to see what kind of player you're looking at. Additionally, you can code the logic entirely within each type of player.

    I hope that helps!

    Comment

    • yves de Munck
      New Member
      • Mar 2011
      • 4

      #3
      Thank you for your respond !
      I found the answer.

      I had to declare the 2 players at the top like this
      Code:
       public partial class frmVieropeenrij : Form
          {
              byte NieuwSpel;
              Spel GestartSpel = new Spel();
              Graphics spelbord;
      
              private Speler Speler1;
              private Speler Speler2;
      
      // rest of code
      and then i made my functions like this

      Code:
      private ComputerSpeler SpelTegenComputer()
              {
                  ComputerSpeler computerSpeler = new ComputerSpeler(2, Color.Yellow, false);
                  //Naam toewijzen
                  computerSpeler.Naam = "[BOT]Speler2";
                  lblSpeler2Naam.Text = computerSpeler.Naam;
                  //Kleur bepalen
                  lblSpeler2Naam.ForeColor = computerSpeler.KleurSpeler;
                  //Moeilijkheid bepalen
                  if (rdbMakkelijk.Checked == true)
                  {
                      computerSpeler.Moeilijkheidsgraad = "Makkelijk";
                  }
                  else if (rdbMoeilijk.Checked == true)
                  { computerSpeler.Moeilijkheidsgraad = "Moeilijk"; }
                  return computerSpeler;
              }
      (makkelijk = easy & moeilijk = hard)

      And for a personplayer

      Code:
       private PersoonSpeler SpelerAanmaken(int Spelernummer)
              {
                  Color color = Color.Red;
                  if (Spelernummer != 1) { color = Color.Yellow; }
      
                  PersoonSpeler PersoonSpeler = new PersoonSpeler(Spelernummer, color, false);
                  //Naam laten invoeren van de speler
                  frmInputbox InputboxNaam = new frmInputbox();
                  InputboxNaam.ShowDialog();
                  //Naam gelijk stellen aan het ingestelde
                  PersoonSpeler.Naam = InputboxNaam.naam;
                  InputboxNaam.Dispose();
                  return PersoonSpeler;
                  
              }
      (i hope you get it because the names of the variables are in Dutch)

      The problem now is that i have to call a method from computerspeler to let the computer play. But it wont.
      i get this error:

      Error 1 'vier_op_een_ri j__2_.Speler' does not contain a definition for 'BepaalZetCompu ter' and no extension method 'BepaalZetCompu ter' accepting a first argument of type 'vier_op_een_ri j__2_.Speler' could be found (are you missing a using directive or an assembly reference?) L:\vier op een rij\vier op een rij\vier op een rij (2)\vier op een rij (2)\frm4op1rij. cs 226 37 vier op een rij (2)


      So i dont know how to fix this :s
      ps: (ComputerSpeler inherits Speler ...)

      Comment

      Working...