Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

Lottery ~ C#

Tdata

Sponsor

I'm trying to write a Lottery system with C#. What I want it to do is allow me to enter people names and the number of tickets then have it draw 1. But I've ended up hardcoded in the people and going with one ticket per person. See below...:

Code:
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

 

namespace CSR

{

    public partial class CSR_Lottery_Main : Form

    {

        public CSR_Lottery_Main()

        {

            InitializeComponent();

        }

        public static Int16[] times = new Int16[19];

 

 

        private void button1_Click(object sender, EventArgs e)

        {

            string[] emp_name;

            emp_name = new string[] {

            "{CSR}Fire Raven Keshik", 

            "101st Raven unit", 

            "10th Raven Guard Cluster", 

            "13th Assault Cluster (The  Howling Coyotes)", 

            "1st Raven Elite Keshik", 

            "21st Raven Rangers",

            "63rd Raven Brigade",

            "6th Raven Battle Cluster",

            "97th Raven Strike Cluster (The Soaring Ravens)",

            "9th Raven Striker",

            "DADI",

            "Druid",

            "Enac",

            "Final light",

            "Metallic Ministry",

            "Pyro Empire",

            "Ravenloft Cluster",

            "Shinbusen",

            "SRT Corp.",

            "The Lords of Chaos",

            "The Shark Empire",

            "The WolfPack Mercenary Company",

            "Wraith Galaxy:  The Rangers"};

 

 

            Random random = new Random();

 

            int w = random.Next();

            w = w % 23;

            winner.Text = emp_name[w]; 

            string cur = Convert.ToString(w + 1);

            currnum.Text = cur;

            

            

 

        }

 

    }

}

Can anyone help me with turning this ^ into what I want?
 
Well, I don't know C# specifically, but I can give you some pseudo-code for it. Let me know if I misunderstood what you wanted

Code:
 

class Lottery{

   tickets[] //Array of names of tickets

 

   addTickets(name, number){

      for i in 0...number{

      tickets.push_back(name)

   }

 

   drawTicket(){

      i = randomnumber between 0 and tickets.size

 

      return tickets[i]

   }

}

 

Also, do not combine the form and the data. I am guessing that the form is a windows GUI form, right? That GUI form is supposed to be used to view the data, not store it. If you make a separate Lottery class, and have the GUI Form query it on the button click. it will make your life and the life of anyone trying to help a lot easier.

hope that helps :) let me know if I misunderstood what you were asking for.
 
I threw together an example... the full solution (driver included) can be found here:

http://archived.ceroscuro.com/Lottery.zip

I assumed that any lottery would need the follow this basic interface (if you haven't dealt with interfaces, basically I'm saying that any lottery would need these methods. For any lottery you'll want to be able to add tickets, have a drawing, find out how many tickets any given user has, and list all users in your lottery).

Lottery.cs (interface)
Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Lotto

{

    interface Lottery

    {

        // adds "count" number of tickets for the user with name passed as param

        void AddTickets(string user, int count);

 

        // returns name of winner

        string DrawTicket();

 

        // returns how many tickets user passed by parameter has

        int GetTicketCount(string user);

 

        // get the name of everyone in the lottery

        List<string> GetAllUsers();

    }

}

 

So using that information I created 3 implementations of a lottery: Basic, which uses just a list of strings; Intermediate, which uses a hashtable to map a string to the amount of tickets a user has (represented as an int); and Advanced, which stores a list of Person objects, each of which can store any amount of information about each person in the lottery (including name, and number of tickets, as well as other generic info like street address, email, whatever).

I recommend you download the example to see how they're used, but I will post them here for the casual reader.

BasicLottery.cs
Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Lotto

{

    public class BasicLottery : Lottery

    {

        private List<String> tickets = new List<string>();

 

        public void AddTickets(string user, int count) {

            for (int i = 0; i < count; i++) {

                tickets.Add(user);

            }

        }

 

        public string DrawTicket() {

            Random random = new Random();

            int winnerIndex = random.Next(tickets.Count);

            return tickets[winnerIndex];

        }

 

        public int GetTicketCount(string desiredUser) {

            int count = 0;

 

            foreach (string user in tickets) {

                if (user == desiredUser) {

                    count++;

                }

            }

 

            return count;

        }

 

        public List<String> GetAllUsers() {

            List<String> results = new List<string>();

            foreach (string user in tickets) {

                if (!results.Contains(user)) {

                    results.Add(user);

                }

            }

 

            return results;

        }

    }

}

 

IntermediateLottery.cs
Code:
using System;

using System.Collections;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Lotto

{

    public class IntermediateLottery : Lottery

    {

        Hashtable tickets = new Hashtable();

        int totalTicketCount {

            get {

                int _total = 0;

                foreach (int _ticketCount in tickets.Values) {

                    _total += _ticketCount;

                }

 

                return _total;

            }

        }

 

        public void AddTickets(string user, int count) {

            if (tickets.ContainsKey(user)) {

                int previousCount = (int) tickets[user];

                tickets[user] = previousCount + count;

            } else {

                tickets.Add(user, count);

            }

        }

 

        public string DrawTicket() {

            Random random = new Random();

            int winningTicketNumber = random.Next(totalTicketCount);

            int countedSoFar = 0;

 

            foreach (KeyValuePair<string, int> ticket in tickets) {

                countedSoFar += (int) ticket.Value;

 

                if (countedSoFar > winningTicketNumber) {

                    return ticket.Key;

                }

            }

 

            // if it gets to this point, something went seriously wrong.

            // we have no winner... what do we do?!  When in doubt.....

            return "Ceroscuro";

        }

 

        public int GetTicketCount(string user) {

            return (int)tickets[user];

        }

 

        public List<String> GetAllUsers() {

            List<String> results = new List<string>();

            foreach (string user in tickets.Keys) {

                if (!results.Contains(user)) {

                    results.Add(user);

                }

            }

 

            return results;

        }

    }

}

 

AdvancedLottery.cs
Code:
using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

 

namespace Lotto

{

    public class AdvancedLottery : Lottery

    {

        List<Person> participants = new List<Person>();

        int totalTicketCount {

            get {

                int _total = 0;

                foreach (Person participant in participants) {

                    _total += participant.ticketCount;

                }

 

                return _total;

            }

        }

        

        public void AddTickets(string name, int count) {

            if (GetUser(name) != null) {

                // If the user exists, add a ticket for them

                AddToTicketCount(name, count);

            } else {

                // otherwise make a new user (defaults to 1 ticket)

                participants.Add(new Person(name, count));

            }

        }

 

        public string DrawTicket() {

            Random random = new Random();

            int winningTicketNumber = random.Next(totalTicketCount);

            int countedSoFar = 0;            

 

            foreach (Person participant in participants) {

                countedSoFar += participant.ticketCount;

 

                if (countedSoFar > winningTicketNumber) {

                    return participant.name;

                }

            }

 

            // if it gets to this point, something went seriously wrong.

            // we have no winner... what do we do?!  When in doubt.....

            return "Ceroscuro";

        }

 

        public int GetTicketCount(string name) {

            Person participant = GetUser(name);

 

            if (participant != null) {

                return participant.ticketCount;

            } else {

                return 0;

            }

        }

 

        private bool AddToTicketCount(string name, int amountToAdd) {

            foreach (Person participant in participants) {

                if (participant.name == name) {

                    participant.ticketCount += amountToAdd; // add ticket for user

                    return true;

                }

            }

 

            return false;

        }

 

        // Returns null if no user exists with that name.

        private Person GetUser(string name) {

            foreach (Person participant in participants) {

                if (participant.name == name) {

                    return participant;

                }

            }

 

            return null;

        }

 

 

        public List<String> GetAllUsers() {

            List<String> results = new List<string>();

            foreach (Person participant in participants) {

                if (!results.Contains(participant.name)) {

                    results.Add(participant.name);

                }

            }

 

            return results;

        }

 

 

        private class Person

        {

            public string name;

            public int ticketCount;

            

            public Person(string name, int count) {

                this.name = name;

                this.ticketCount = count;

            }

 

            /****************************************************************

             * Here you can also add other information that might be useful *

             * in a real lottery, such as:                                  *

             *                                                              *

             * int idNumber;                                                *                                                             

             * string streetAddress;                                        *

             * DateTime birthday;                                           *

             * etc.                                                         *

             ****************************************************************/

        }

    }

}

 

Hope this helps! Feel free to ask me questions should they arise.
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top