我正在構建一個非常基本的「骰子蟋蟀」模擬器。我試圖跟蹤完整的局和覆蓋以及使用整數和列表,我在asp.net會話中存儲的分數。整數不遞增,列表不增長,列表不增長,列表不增長...來自會話的值c#asp.net
我有一個「innsOver」類代表一組6個交付,每個交付的整數代表球的結果。
public class innsOver
{
public int overNumber { get; set; }
public int ball1 { get; set; }
public int ball2 { get; set; }
public int ball3 { get; set; }
public int ball4 { get; set; }
public int ball5 { get; set; }
public int ball6 { get; set; }
}
我也有在此基礎上2名列表,表示2局:
List<innsOver> inns1 = new List<innsOver>();
List<innsOver> inns2 = new List<innsOver>();
我也宣告完全旅館的數量(所以我知道我是否打inns1或整數旅館2)和完整的過境人數(所以我知道有多少個過頭的人)。這些都是
int oversComplete = 0;
int innsComplete = 0;
的2個整數oversComplete
和innsComplete
和2所列出inns1
和inns2
在全球範圍內宣佈。
通過按下按鈕播放「over」,該按鈕調用playOver()
方法,該方法本身稱爲bowlDelivery()
方法。當playOver()
方法完成時,它應該遞增completeOvers
整數,將完成的內容添加到適當的innisting列表(inns1
或inns2
)並將其返回給gridView。
public void bowlOver()
{
//other processing checks which end the over is bowled from
//which batsman is the striker/non-striker and who is bowling.
for (int i = 1; i <7; i++)
{
int x = bowlDelivery();
int b = 0;
if (x == 5)
{
//this represents a chance of a wicket that was
//unsuccessful so no run scored
b = 0;
}
else if (x == 7)
{
//this means a wicket fell so i have wicket processing;
}
else if (x == 1 || x == 3)
{
b = x;
//award 1 run & change striking batsman
}
else
{
b = x;
//2, 4 or 6 runs awarded
}
//the outcome is returned to the correct ball
switch (i)
{
case 1:
activeOver.ball1 = b;
break;
case 2:
activeOver.ball2 = b;
break;
case 3:
activeOver.ball3 = b;
break;
case 4:
activeOver.ball4 = b;
break;
case 5:
activeOver.ball5 = b;
break;
case 6:
activeOver.ball6 = b;
break;
}
}
//the over result is returned to the correct inns list
switch (innsComplete)
{
case 0:
Session["inns1"] = inns1;
Session["oversComplete"] = oversComplete;
oversComplete = (int)Session["oversComplete"];
activeOver.overNumber = oversComplete + 1;
oversComplete = activeOver.overNumber;
inns1 = (List<innsOver>)Session["inns1"];
inns1.Add(activeOver);
GridView3.DataSource = inns1;
GridView3.DataBind();
Session["inns1"] = inns1;
Session["oversComplete"] = oversComplete;
break;
case 1:
Session["inns2"] = inns2;
Session["oversComplete"] = oversComplete;
oversComplete = (int)Session["oversComplete"];
activeOver.overNumber = oversComplete + 1;
oversComplete = activeOver.overNumber;
inns2 = (List<innsOver>)Session["inns2"];
inns2.Add(activeOver);
GridView3.DataSource = inns2;
GridView3.DataBind();
Session["inns2"] = inns2;
Session["oversComplete"] = oversComplete;
break;
}
}
public int bowlDelivery()
{
//currently the result of each ball is completely random but i will add weighting later
int x = rnd.Next(1, 7);
if (x == 5)
{
int y = rnd.Next(1, 6);
if(y > 4)
{
x = 7;
}
else
{
x = 5;
}
}
return x;
}
但是,此刻我只有一次返回,並且overNumber始終爲「1」。球的價值發生了變化,所以似乎只是反覆打出第一名。我期待(嘗試實現)overNumber遞增和額外的行被添加到GridView中顯示的innsX列表。
我有什麼錯誤的建議?
你確定方法**被稱爲**嗎? –
是的,因爲第一次結束會出現在gridview中。它永遠不會超過第一次。 – Dave
如何在哪裏初始化rnd? – haim770