Closed Thread
Results 1 to 2 of 2

Thread: Trouble passing varibles...

  1. #1
    Join Date
    Jan 2008
    Posts
    7
    Rep Power
    0

    Trouble passing varibles...

    Im trying to write pretty basic dice rolling program it rolls 4 dye sorts them high to low then discards the lowest and adds the rest. Im using the built in Random to make it but for some reason I cannot change the varibles they all stay zero when I compile it... here is the code...

    Code:
    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 Dice roller
    {
        public partial class Form1 : Form
        {
            public int D1;      //Die roll 1,2,3,4
            public int D2;
            public int D3;
            public int D4;
    
            public int High;     //Order rolls for high to low
            public int SecondHigh;
            public int Low;
            public int Discard;
    
            public int highSort1;
            public int highSort2;
            public int lowSort1;
            public int lowSort2;
    
            public int lowRoll; //lowest Roll #
    
            public int D6roll;  //The d6 rolled amount
    
            public int Roll;     //The Roll amount
    
            public int x;   //Roll controller
    
            
    
            public Form1()
            {
                InitializeComponent();
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
    
            }
            private void Rolldice_Click(object sender, EventArgs e)
            {
                D6roll = 6;
                rollDice(D6roll , x);
                //Display();
                //Rolld6();
            }
    
         
            //public int Rolld6()
            //{
            //    //Random num = new Random();
            //    //D6roll = num.Next(1, 6);
            //    D6roll = 6;
            //    debug.Text = "" + D6roll;           
            //    debug.Text = "" + x;
            //    rollDice(D6roll, x);
            //    return 1;
    
            //}
    
            public int  rollDice(int D6roll, int x)
            {
    
                switch (x)
                {
                    case 0:
                        //setd1();
                        D1 = D6roll;
                        x++;
                        //Rolld6();
                        rollDice(D6roll,x);
                        break;
    
                    case 1:
                        D2 = D6roll;
                        x++;
                        //Rolld6();
                        rollDice(D6roll, x);
                        break;
    
                    case 2:
                        D3 = D6roll;
                        x++;
                        //Rolld6();
                        rollDice(D6roll, x);
                        break;
    
                    case 3:
                        D4 = D6roll;
                        x++;
                        //Rolld6();
                        rollDice(D6roll, x);
                        break;
                    case 4:
                        x = 0;
                        sortDicehighLow(D1,D2,D3,D4);
                        break;
                }
                sortDicehighLow(D1, D2, D3, D4);
                
                return 1;
            }
            public int sortDicehighLow(int D1, int D2, int D3, int D4)
            {
                if (D1 >= D2)
                {
                    D1 = highSort1;
                    D2 = lowSort1;
    
                    if (D3 >= D4)
                    {
                        D3 = highSort2;
                        D4 = lowSort2;
                        sortHighlow(highSort1,highSort2, lowSort1, lowSort2);
                    }
                    else
                    {
                        D4 = highSort2;
                        D3 = lowSort2;
                        sortHighlow(highSort1,highSort2, lowSort1, lowSort2);
                    }
    
                }
                   else
                   {
                       D1 = highSort1;
                       D2 = lowSort1;
                             if (D3 >= D4)
                                {
                                    D3 = highSort2;
                                    D4 = lowSort2;
                                   // sortHighlow();
                                }
                             else
                                {
                                    D4 = highSort2;
                                    D3 = lowSort2;
                                 //   sortHighlow();
                                }
                       
                   }
                sortHighlow(highSort1, highSort2, lowSort1, lowSort2);
                return 1;
               }
               public int sortHighlow(int highSort1, int highSort2, int lowSort1, int lowSort2)
               {
                   if (highSort1 >= highSort2)
                   {
                       highSort1 = High;
                       highSort2 = SecondHigh;
                       if (lowSort1 >= lowSort2)
                       {
                           lowSort1 = Low;
                           lowSort2 = Discard;
                       }
                       else
                       {
                           lowSort2 = Low;
                           lowSort1 = Discard;
                       }
                   }
                   else
                   {
                       highSort2 = High;
                       highSort1 = SecondHigh;
                       if (lowSort1 >= lowSort2)
                       {
                           lowSort1 = Low;
                           lowSort2 = Discard;
                       }
                       else
                       {
                           lowSort2 = Low;
                           lowSort1 = Discard;
                       }
    
                   }
                   Display();
                   return 1;
                   
               }
               public void Display()
               {
                   Roll1.Text = "" + D1;
                   Roll2.Text = "" + D2;
                   Roll3.Text = "" + D3;
                   Roll4.Text = "" + D4;
    
                   Highest.Text = "" + High;
                   secondHighest.Text = "" + SecondHigh;
                   thirdHighest.Text = "" + Low;
                   Discarded.Text = "" +Discard;
               }
    I commented out the RNG and the calls to it trying just to pass a set 6 for the Variable D6roll because when I use the function RollD6 i get a stack over flow exception because it thinks there is an infinite loop any help is appreciated thanks.

    edit I fixed the problem passing the varibles but I still get the infinite loop problem...
    Last edited by WingedPanther; 07-29-2008 at 12:08 PM. Reason: add code tags

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    bvaessen is offline Learning Programmer
    Join Date
    Jul 2008
    Location
    Limburg
    Posts
    30
    Rep Power
    14

    Re: Trouble passing varibles...

    try to make a small flowchart of the behaviour you need and then from the code you have, or just write down what methods call which other methods.
    rolldice_click() calls rolldice() which calls rolldice() again, dependant on x, so that could finish some time. However, rolldice() also calls rolld6() which calls rolldice() again. That is indeed a infinite loop because the routines keep calling eachother forever. I did not look into your code further, so that I don't know why and what it does, but I guess you should introduce some construction to end it, or just check the status of a checkbox in between. If you really want to make something that keeps calling, better try something like a simple timer and put the calls in the event handler. Hope this helped.

    ------------------------------------------------------------------------------------
    Need professional help? See Launch-IT :: Home
    Ben Vaessen :: Home

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Passing variable
    By NumOne in forum PHP Development
    Replies: 1
    Last Post: 04-25-2011, 01:11 PM
  2. Passing a random Value
    By Alex_j in forum C# Programming
    Replies: 7
    Last Post: 05-17-2010, 06:12 AM
  3. help passing values
    By Lucian in forum C and C++
    Replies: 2
    Last Post: 12-13-2009, 10:24 AM
  4. Passing Parameters
    By FakeRobotGymnast in forum Python
    Replies: 0
    Last Post: 02-01-2009, 05:47 PM
  5. passing parameters by references trouble
    By Hawk1 in forum C and C++
    Replies: 8
    Last Post: 10-10-2008, 03:17 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts