Click here to Skip to main content
15,910,303 members
Home / Discussions / C#
   

C#

 
GeneralRe: Random Numbers Pin
Senkwe Chanda18-Nov-02 20:58
Senkwe Chanda18-Nov-02 20:58 
GeneralRe: Random Numbers Pin
Member 1810818-Nov-02 21:49
Member 1810818-Nov-02 21:49 
here's the code for the problem in question

<br />
Class BlockClass<br />
{<br />
  private enum BlockColor<br />
  {<br />
    BLUE,<br />
    GREEN,<br />
    RED<br />
  }<br />
  <br />
  // some more members here<br />
<br />
  private BlockColor ItsColor;<br />
<br />
  public void AssignColor()<br />
  {<br />
    Random rnd = new Random();<br />
    switch (rnd.Next(4))<br />
    {<br />
      case 1 :<br />
        this.ItsColor = BlockColor.BLUE;<br />
        break;<br />
      case 2 :<br />
        this.ItsColor = BlockColor.GREEN;<br />
        break;<br />
      case 1 :<br />
        this.ItsColor = BlockColor.RED;<br />
        break;<br />
  }<br />
<br />
  public BlockClass(Graphics InGraphics, <br />
                         int inSpeed,<br />
                       Point inLocation,<br />
                        bool inTopRow)<br />
  {<br />
    // some other constructor code<br />
    this.AssignColor();<br />
    // some other constructor code<br />
  }<br />
}<br />


basically AssignColor() is only used as part of the object's construction it is called once per BlockClass that is created. I choose to use an enumeration so it would be easy for me to do redraw using another switch statement to either draw the block using basic graphics routines like DrawRectangle() or load a specific image from an imagelist containing the 3 color blocks plus a blank white block for redrawing purposes

to give you an idea how this class is used:

<br />
class BlocksClass<br />
{<br />
  ArrayList TheBlocks = new ArrayList(20);<br />
<br />
  public void LayoutBlocks()<br />
  {<br />
    for(int x = 0;x < 20;x++)<br />
    {<br />
      ArrayList YArray = new ArrayList(20);<br />
      for(int y = 0;y < 20;y++)<br />
      {<br />
        BlockClass Block = new BlockClass(this.Gfx,<br />
                                          this.speed,<br />
                                          new Point((x * 7) + 3,(y * 7) + 3)),<br />
                                          (y == 0))<br />
        YArray.Add(Block);<br />
      }<br />
      this.TheBlocks.Add(YArray)<br />
    }<br />
  }   <br />
}<br />


so basically the program calculates a 20x20 playing board with 7x7 pixel boxes
by filling in the Y rows of a specific X column and then moving on to the next X column and repeating (i did this cause part of the program needs to be able to delete a square and cause those above it to drop down to replace it (figured i set up the array like this it would be easier to parse for this event)

ok now i'm done typing like... working at 3am does that to me i can't shut up i mean look at me...Sleepy | :zzz:
GeneralRe: Random Numbers Pin
James T. Johnson19-Nov-02 14:46
James T. Johnson19-Nov-02 14:46 
GeneralRe: Random Numbers Pin
Member 1810819-Nov-02 20:54
Member 1810819-Nov-02 20:54 
GeneralRe: Random Numbers Pin
LongRange.Shooter19-Nov-02 11:03
LongRange.Shooter19-Nov-02 11:03 
Generalmake it dead. Pin
imran_rafique18-Nov-02 15:19
imran_rafique18-Nov-02 15:19 
GeneralRe: make it dead. Pin
Rickard Andersson2018-Nov-02 20:48
Rickard Andersson2018-Nov-02 20:48 
GeneralRe: make it dead. Pin
imran_rafique19-Nov-02 16:24
imran_rafique19-Nov-02 16:24 
GeneralMagic TabPage question Pin
m_mond18-Nov-02 15:16
m_mond18-Nov-02 15:16 
GeneralRe: Magic TabPage question Pin
leppie19-Nov-02 0:04
leppie19-Nov-02 0:04 
GeneralVisible Property Not Working Pin
Jamie Nordmeyer18-Nov-02 7:35
Jamie Nordmeyer18-Nov-02 7:35 
GeneralRe: Visible Property Not Working Pin
David Stone18-Nov-02 8:48
sitebuilderDavid Stone18-Nov-02 8:48 
GeneralRe: Visible Property Not Working Pin
Jamie Nordmeyer18-Nov-02 9:32
Jamie Nordmeyer18-Nov-02 9:32 
GeneralRe: Visible Property Not Working Pin
David Stone18-Nov-02 10:22
sitebuilderDavid Stone18-Nov-02 10:22 
GeneralCOM+ Pin
Mazdak18-Nov-02 5:50
Mazdak18-Nov-02 5:50 
GeneralXML questions ... Pin
LongRange.Shooter18-Nov-02 5:28
LongRange.Shooter18-Nov-02 5:28 
GeneralRe: XML questions ... Pin
Christian Graus18-Nov-02 15:34
protectorChristian Graus18-Nov-02 15:34 
GeneralRe: XML questions ... Pin
Nick Parker18-Nov-02 17:22
protectorNick Parker18-Nov-02 17:22 
GeneralRe: XML questions ... Pin
Christian Graus18-Nov-02 17:28
protectorChristian Graus18-Nov-02 17:28 
GeneralRe: XML questions ... Pin
LongRange.Shooter19-Nov-02 3:25
LongRange.Shooter19-Nov-02 3:25 
GeneralRe: XML questions ... Pin
LongRange.Shooter19-Nov-02 9:42
LongRange.Shooter19-Nov-02 9:42 
GeneralRe: XML questions ... Pin
Christian Graus19-Nov-02 10:07
protectorChristian Graus19-Nov-02 10:07 
GeneralRe: XML questions ... Pin
LongRange.Shooter19-Nov-02 10:53
LongRange.Shooter19-Nov-02 10:53 
GeneralOpen An HTML File Using Web Broswer Pin
yccheok17-Nov-02 21:51
yccheok17-Nov-02 21:51 
GeneralRe: Open An HTML File Using Web Broswer Pin
Mazdak17-Nov-02 23:12
Mazdak17-Nov-02 23:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.