【发布时间】:2017-04-05 11:34:45
【问题描述】:
我正在构建一个非常基本的“骰子板球”模拟器。我正在尝试使用我存储在 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>();
我还声明了完整客栈的数量(所以我知道我是在玩客栈 1 还是客栈 2)和完整的回合数(所以我知道有多少回合被投球)。这些是
int oversComplete = 0;
int innsComplete = 0;
oversComplete 和 innsComplete 2 个整数以及 inns1 和 inns2 2 个列表是全局声明的。
“结束”是通过按下一个按钮来播放的,该按钮调用playOver() 方法,该方法本身调用bowlDelivery() 方法。当 playOver() 方法完成时,它应该增加 completeOvers 整数,将完成的结果添加到适当的局列表(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;
}
但是,目前我只收到一个 over 返回,并且 overNumber 始终为“1”。球值发生变化,所以它似乎只是在重复打第一个。我期待(试图实现)overNumber 递增和额外的行被添加到显示在 gridview 中的 innsX 列表中。
对我的错误有什么建议吗?
【问题讨论】:
-
你确定方法被调用吗?
-
是的,因为第一轮出现在网格视图中。它只是永远不会超过第一次。
-
rnd是如何以及在哪里初始化的? -
也在全球范围内使用
Random rnd = new Random();我应该澄清一下,除了completeOvers整数的递增和innsX列表的行数增加之外,一切都按预期工作。 -
您能否告诉我们方法调用发生在页面周期中的哪个事件?如果您能与我们分享完整的代码,那就太好了。某处发生了一些值重置,如果不查看完整代码,将很难找到它。