0

I'm making a Simon Says game, but unlike normal Simon Says games (where random sequences are played), I wish to make the sequences non-random and based on instructions.text

So for example: instructions.text could include "Red,Yellow,Red,Green".

And the game will interpret the text into what the sequence will be.

Right now, I have code that makes the sequence random... please could you push me in the right direction as to how I can make this non-random. Thank you in advance.

        public Color [] newSequence (int length)
    {
        Color [] array = new Color[length];
        Random rand = new Random(DateTime.Now.Millisecond);// don't inline w/ colors[] - wont be random

        for (int i=0; i<length; i++) {
            array[i] = colors[rand.Next(0,4)];
        }
        this.sequence=array;
        return array;
    }
Ashtopher
  • 51
  • 1
  • 1
  • 10
  • Use a deterministic seed – SLaks Feb 28 '14 at 14:08
  • First, you should have a section that reads instructions.txt – Kuzgun Feb 28 '14 at 14:09
  • If you moved the seeded value of rand, outside of your method, then seed it only once, then it would generate the same random list each time the program was run. (Obviously not seeding it from the time now. Hope that helps – Angus Connell Feb 28 '14 at 14:09

1 Answers1

3

Upon loading your game, you could read the contents of the instructions.txt file into an Array. This Array would then serve as the sequence buffer.

Here's a short (incomplete) example. You could either provide integer values on each line, or human-readable color definitions (Red, Green, Blue), which would then be parsed to their integer counterparts.

public Color [] newSequence(int length) {
    string line;
    int counter = 0;
    Color [] array = new Color[length];
    System.IO.StreamReader file = new System.IO.StreamReader("instructions.txt");

    while ((line = file.ReadLine()) != null) {
        array[i] = /* instantiate your new color here, based upon the value on each line */
    }
    file.close();
    this.sequence = array;
    return array;
}

Reference for the file input snippet: here

Alternatively, if you wish to eliminate the need for the instructions.txt file, initialize your random number generator with a static seed.

Random rand = new Random(293102);

This way, the progressive values will always be the same.

Community
  • 1
  • 1
Seidr
  • 4,946
  • 3
  • 27
  • 39
  • Thank you so much! This is a huge help for me. I'm getting a reference error for file.close (I'm sure I referenced systems.io). I must have missed something... – Ashtopher Feb 28 '14 at 14:30