Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Array list question

Array list question

Scheduled Pinned Locked Moved C#
questioncsharplinqgraphicsdata-structures
8 Posts 5 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dennis_max27
    wrote on last edited by
    #1

    I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

    R D K L P 5 Replies Last reply
    0
    • D dennis_max27

      I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

      R Offline
      R Offline
      Rob Philpott
      wrote on last edited by
      #2

      As a suggestion, firstly don't use 5 seperate integers, use an array with 5 elements. You could do the test a couple of ways. Firstly, sort the array, then check that two neighbouring elements do not contain the same number. That guarantees uniqueness. Or, create five booleans, all false originally, then set the appopriate one on each iteration of a loop over the array. All booleans being set would signal uniqueness too.

      Regards, Rob Philpott.

      1 Reply Last reply
      0
      • D dennis_max27

        I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

        D Offline
        D Offline
        dennis_max27
        wrote on last edited by
        #3

        Changed my code, can I make it easier? TO read and write? I don't know much about C#... :) 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { //Maakt een 2 maal 5 keer getallen aan Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7); int randomNumber5 = random.Next(1, 7); //Zet de 2 maal random 5 getallen in goede volgorde (speler) List<int> randomList = new List<int>(); randomList.Add(randomNumber1); randomList.Add(randomNumber2); randomList.Add(randomNumber3); randomList.Add(randomNumber4); randomList.Add(randomNumber5); randomList.Sort(); //berekening(speler) int last = 0; foreach (int itemInRandomList in randomList) { if (itemInRandomList == last || itemInRandomList - last != 1) { last = 0; break; } last = itemInRandomList; } //Resultaat of hij gewonnen of verloren heeft(speler) if (last == 0) { lblDropped.ForeColor = System.Drawing.Color.Red; lblDropped.Text = "LOST"; } else { lblDropped.ForeColor = System.Drawing.Color.Green; lblDropped.Text = "WON"; } //Zet de 2 maal random 5 getallen in

        1 Reply Last reply
        0
        • D dennis_max27

          I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

          K Offline
          K Offline
          Keith Barrow
          wrote on last edited by
          #4

          First as suggested previously you need a 5 long array of ints. The method described above (checking for uniqueness) won't work as (2,3,4,5,6) is unique but not winning. You will also find your code improves vastly if you study OO and work out how to refactor. The two guidlines I try to live by [and often fail] are "Don't Repeat Yourself" "Keep It Simple Stupid" (DRY-KISS). For example int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7); int randomNumber5 = random.Next(1, 7); Fails the DRY test. I've separated out an example Yahtzee Roller class for you, this is one implementation of many possible and is not meant to be perfect. It contains an array of roll results, abd an array of winning values, these are compared. The amin program creates an instance of the roller, and proceeds to roll until a winning set is found. The roller:

          public class YahtzeeRoller
          {
          int[] rollResults = new int[]{0, 0, 0, 0, 0};
          int[] WinningValues = new int[] { 1, 2, 3, 4, 5 };

          Random random = new Random();
          
          public bool IsWinning
          {
              get
              {
                  // This takes all the winning values and reports which are missing
                  // in the roll results. If the count of this is 0, all winning values 
                  // are present, so the user has won.
                  // This requires .net 3.0 or above.
                  return WinningValues.Except(rollResults).Count() == 0;
              }
          }
          
          public string CurrentRoll
          {
              get
              {
                  // This is hacked just too show the results, not meant to be perfect.
                  string result = "Roll =";
                  foreach (int rollResult in rollResults)
                      result += " " + rollResult;
                  return result;
              }
          }
          
          public void Roll()
          {
              for (int i = 0; i < rollResults.Length; i++)
                  rollResults\[i\] = random.Next(1, 7); 
          }   
          

          }

          Calling Program:

          class Program
          {
          static void Main(string[] args)
          {
          YahtzeeRoller yahtzeeRoller = new YahtzeeRoller();
          //The example program will roll until it wins.
          while (!yahtzeeRoller.IsWinning)
          {
          yahtzeeRoller.Roll();
          Console.Write(yahtzeeRoller.CurrentRoll);

                  //Again, a hack implementation to get the results out.
                  if (yahtzeeRoller.IsWinning)
                      Console.WriteLine(" Yahtz
          
          1 Reply Last reply
          0
          • D dennis_max27

            I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

            L Offline
            L Offline
            Luc Pattyn
            wrote on last edited by
            #5

            Hi, - you have a single random generator, that is good. - you don't need five or ten Labels, its much easier to show all five numbers in a single Label. - you want to recognize not only "12345" but also all permutations thereof. Sorting the numbers is one way, but is unnecessary. The smart way to do this is by using a bit collection. Have a look at this:

            int all=0;
            all = all | (1<
            Now try to use that to your advantage; the end result will be less than half the code you have now.

            BTW: please use PRE tags, not CODE tags.

            :)

            Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


            I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


            D 1 Reply Last reply
            0
            • L Luc Pattyn

              Hi, - you have a single random generator, that is good. - you don't need five or ten Labels, its much easier to show all five numbers in a single Label. - you want to recognize not only "12345" but also all permutations thereof. Sorting the numbers is one way, but is unnecessary. The smart way to do this is by using a bit collection. Have a look at this:

              int all=0;
              all = all | (1<
              Now try to use that to your advantage; the end result will be less than half the code you have now.

              BTW: please use PRE tags, not CODE tags.

              :)

              Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


              I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


              D Offline
              D Offline
              dennis_max27
              wrote on last edited by
              #6

              But this isn't working at any computer?

              L 1 Reply Last reply
              0
              • D dennis_max27

                But this isn't working at any computer?

                L Offline
                L Offline
                Luc Pattyn
                wrote on last edited by
                #7

                dennis_max27 wrote:

                this

                what?

                dennis_max27 wrote:

                isn't working

                what gives?

                dennis_max27 wrote:

                any computer?

                huh? if you can't communicate what it is you want or try or experience, nobody can help you. :~

                Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]


                I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages


                1 Reply Last reply
                0
                • D dennis_max27

                  I'm making Yahtzee but I've 1 question: I've 5 stones which must be generated randomly between 1 and 6. If I've a 1,2,3,4 and 5 in this 5 stones it need's to display "GOOD" otherwise "LOST" SO 2,1,3,5,4 is "GOOD" SO 1,1,3,4,5 is "LOST" How can I do this can somebody help me please with it :^) So I can go further. I must work with array'sin my opinion but didn't have much knowledge of them :( Hope somebody can improve my code or say how I must do it. (I'm learning) My 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 W1 { public partial class w1 : Form { public w1() { InitializeComponent(); } private void w1_Load(object sender, EventArgs e) { } private void exitToolStripMenuItem_Click(object sender, EventArgs e) { Application.Exit(); } private void btnDrop_Click(object sender, EventArgs e) { Random random = new Random(); int randomNumber1C = random.Next(1, 7); int randomNumber2C = random.Next(1, 7); int randomNumber3C = random.Next(1, 7); int randomNumber4C = random.Next(1, 7); int randomNumber5C = random.Next(1, 7); lblDice1C.Text = "Dropped: " + randomNumber1C.ToString(); lblDice2C.Text = "Dropped: " + randomNumber2C.ToString(); lblDice3C.Text = "Dropped: " + randomNumber3C.ToString(); lblDice4C.Text = "Dropped: " + randomNumber4C.ToString(); lblDice5C.Text = "Dropped: " + randomNumber5C.ToString(); if (randomNumber1C == 1 && randomNumber2C == 2 && randomNumber3C == 3 && randomNumber4C == 4 && randomNumber5C == 5 || randomNumber1C == 2 && randomNumber2C == 3 && randomNumber3C == 4 && randomNumber4C == 5 && randomNumber5C == 6) { lblDroppedC.ForeColor = System.Drawing.Color.Green; lblDroppedC.Text = "WON"; } else { lblDroppedC.ForeColor = System.Drawing.Color.Red; lblDroppedC.Text = "LOST"; } int randomNumber1 = random.Next(1, 7); int randomNumber2 = random.Next(1, 7); int randomNumber3 = random.Next(1, 7); int randomNumber4 = random.Next(1, 7);

                  P Offline
                  P Offline
                  PIEBALDconsult
                  wrote on last edited by
                  #8

                  Ha! Use a System.Collections.Generic.HashSet! :-D Are you only looking for a large straight? Clear the HashSet. Generate the numbers (into an array). Iterate the array, putting each value in the HashSet. (Or you could eliminate the array if you're only interested in large straights.) Check how many values are in the HashSet -- if there are five and 1 or 6 is not present, then you have a large straight! To also check for small straights, you can track the min and max values. Large straight := Five values, max - min == 4 Small straight := Four values, max - min == 3 Also, if the HashSet contains only one value, you have a Yahtzee. If only two, you might have a full house.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups