2017-04-05 95 views
1

我正在構建一個非常基本的「骰子蟋蟀」模擬器。我試圖跟蹤完整的局和覆蓋以及使用整數和列表,我在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個整數oversCompleteinnsComplete和2所列出inns1inns2在全球範圍內宣佈。

通過按下按鈕播放「over」,該按鈕調用playOver()方法,該方法本身稱爲bowlDelivery()方法。當playOver()方法完成時,它應該遞增completeOvers整數,將完成的內容添加到適當的innisting列表(inns1inns2)並將其返回給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列表。

我有什麼錯誤的建議?

+0

你確定方法**被稱爲**嗎? –

+0

是的,因爲第一次結束會出現在gridview中。它永遠不會超過第一次。 – Dave

+0

如何在哪裏初始化rnd? – haim770

回答

1

在每個case聲明開始時你有Session["inns1"] = inns1;innsX從哪裏來?它是一個每次都會被設置到Session的新列表嗎?在這種情況下,看起來Session["innsX"]每次都會被覆蓋,因此只有一條記錄顯示在網格中。

+0

嗨,謝謝你。我想我正在做你說的話:'activeOver.overNumber = oversComplete + 1; oversComplete = activeOver.overNumber;'然後最後在會話中存儲新的'oversComplete'。所以我的信念是我比'completeOvers'多'activeOver' 1,然後將'completeOvers'設置爲'activeOver',這與'oversComplete ++'相同?我相信? – Dave

+0

我現在看到了。抱歉。 –

+0

沒問題!我不是一個職業球員,它不會像我一樣錯過那樣的事情! :) – Dave