we are working on a school project (a learning game) and atm we are using a class that generate a value from a "Stack". our problem is that after the values of the stack runs out the game / program crash and stops.
i want to change it to a array with fix values and cells in it that gives me a random value each time that i address it.
right now the code is this:
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();
}
}
any idea how i keep the name of the class, but only change the inside for a array that generade random values?
btw, the "cell" is another class that we made, and the array should be made from him:
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;
}
}
}
thanks for the help and sorry for all the notes in Hebrew!
You could use a List or an array to store the Instruments. Then generate a random integer between 0 and the number of elements you have.
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];
}
}
I haven't tested it, but i think it would work:
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)];
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.