2012-03-05 7 views
0

私たちは学校のプロジェクト(学習ゲーム)に取り組んでいます。私たちは「スタック」から値を生成するクラスを使用しています。私たちの問題はスタックの値がなくなった後にゲーム/プログラムがクラッシュして停止することです。クラスとして設定された修正配列からランダムな値を生成しますか?

修正値とそのセルに配列を変更して、アドレスするたびにランダムな値を返します。

今のコードはこれです:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

/// <summary> 
/// Summary description for CellInfoRepository 
/// </summary> 
public class CellProducer 
{ 
    System.Collections.Generic.Stack<Cell> _stack = new Stack<Cell>(); 



    public CellProducer() 
    { 



     _stack.Push(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 }); 
     _stack.Push(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 }); 
    } 

    public Cell ProduceNextCell() 
    { 
     return _stack.Pop(); 
    } 
} 

私はクラスの名前を保つが、唯一のランダムな値をgeneradeアレイの内部をどのように変化するかを任意のアイデア?

ところで、「セル」は、私たちが作った別のクラスがあり、かつ配列が彼からなされるべきである:ヘブライ語のすべてのノート

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI.WebControls; 

/// <summary> 
/// Summary description for Cells 
/// </summary> 
public class Cell:Panel //מחלקת משושים 
{ 
    protected string _instrumentName; //שם כלי הנגינה // 
    protected int _insturmentFamily; //סוג כלי הנגינה - המשפחה // 
    protected int _myPlayer; //ערכים מספריים של 0 - אין שחקן - 1 ו2 לפי השחקנים 

    public string InstrumentName 
    { 
     get 
     { 
      return _instrumentName; 
     } 

     set 
     { 
      _instrumentName = value; 
     } 
    } 

    public int InstrumentFamily 
    { 
     get 
     { 
      return _insturmentFamily; 
     } 

     set 
     { 
      _insturmentFamily = value; 
     } 
    } 

    public int myPlayer 
    { 
     get 
     { 
      return _myPlayer; 
     } 

     set 
     { 
      _myPlayer = value; 
     } 
    } 
} 

public class CellPosition // מחלקה ששומרת את מיקומי התאים 
{ 
    private int _Column; 
    private int _Row; 

    public int Column 
    { 
     get 
     { 
      return _Column; 
     } 

     set 
     { 
      _Column = value; 
     } 
    } 

    public int Row 
    { 
     get 
     { 
      return _Row; 
     } 

     set 
     { 
      _Row = value; 
     } 
    } 
} 

の助けに感謝し、申し訳ありません!

+0

は、あなたの代わりにリストのスタックを使用している理由はありますか? –

+0

今までゲームを動作させるためにスタックを使用しました。私たちはそれをある種の配列に変更する必要があることを知っていますので、戻ってくる値はランダムになります。セルから配列を作成し、ランダムな値を生成します: "Cell [] myArry = new Cell [23];" – Assaf85

答えて

0

私はそれをテストしていませんが、私はそれが仕事だと思う:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

/// <summary> 
/// Summary description for CellInfoRepository 
/// </summary> 
public class CellProducer 
{ 
    System.Collections.Generic.List<Cell> cells = new List<Cell>(); 
    Random random = new Random(); 

    public CellProducer() 
    { 
     cells.Add(new Cell { InstrumentName = "גיטרה", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "עוד", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "סיטאר", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "כינור", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "צ'לו", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "ויולה", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "נבל", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "בנג'ו", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "תופים", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "מצילה", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "שליש", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "קסטנייטה", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "פעמוניה", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "קסילופון", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "טמבורין", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "פעמוני רוח", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "קרן יער", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "בריטון", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "טרומבון", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "טובה", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "סקסופון", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "חליל", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "קלרינט", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "משרוקית", InstrumentFamily = 3, myPlayer = 0 }); 
    } 

    public Cell ProduceNextCell() 
    { 
     return cells[random.Next(cells.Count)]; 
    } 
} 
+0

ITは!ありがとう!あなたはちょうど私の日を作った! – Assaf85

0

インストゥルメントを格納するために、リストまたは配列を使用できます。その後、0とあなたが持っている要素の数の間のランダムな整数を生成します。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 

/// <summary> 
/// Summary description for CellInfoRepository 
/// </summary> 
public class CellProducer 
{ 
    System.Collections.Generic.List<Cell> cells = new List<Cell>(); 
    int index = -1; 

    public CellProducer() 
    { 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?'??", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???'?", InstrumentFamily = 1, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "????????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "?????? ???", InstrumentFamily = 2, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "??? ???", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "??????", InstrumentFamily = 3, myPlayer = 0 }); 
     cells.Add(new Cell { InstrumentName = "???????", InstrumentFamily = 3, myPlayer = 0 }); 
    } 

    public Cell ProduceNextCell() 
    { 
     index++; 
     if(index>cells.length){ 
      index = 0; 
     return cells[index]; 
    } 
} 
+0

thats私は何をしようとしています:) – Assaf85

+0

下記のNikolaMarkovinovićの答えをご覧ください。しかし、彼の答えは毎回ランダムな値を与えることに注意してください。シーケンシャルリストが必要な場合は、今編集した答え –

+0

のコードを使用することができます。そのため、InstrumentNameをランダムに、異なるようにしたいので、助けてくれてありがとう。彼らは永遠に繰り返すことができます。 – Assaf85

関連する問題