2024-11-07|閱讀時間 ‧ 約 0 分鐘

系統開發-02發牌給玩家

    raw-image


    這一篇來解決上一篇的問題,使用Session來存取資料。

    以下修改一下程式碼,[**新增 **]是新增的程式碼,舊的且不需要的程式碼會被註解掉

    public partial class _Default : System.Web.UI.Page
    {
    Deck deck; //排堆
    Player dealer; //莊家
    Player player; //玩家

    protected void Page_Load(object sender, EventArgs e)
    {
    //**新增**
    //避免網頁載入後,每按一次按鈕都會更新(POST)
    //改為網頁載入更新就好
    if (IsPostBack == false)
    {
    ResetGame();
    }

    //ResetGame();
    UpdateUI();
    }

    protected void ResetGame() { //初始化遊戲參數與動作(初始發牌)
    //deck = new Deck();
    //dealer = new Player();
    //player = new Player();

    //**新增***
    //將資料存到Session
    Session["Deck"] = new Deck();
    Session["Dealer"] = new Player();
    Session["Player"] = new Player();

    //初始發牌
    DrawForPlayer();
    DrawForDealer();
    DrawForPlayer();
    DrawForDealer();

    //清空label內容
    lab_deck.Text = "";
    lab_dealer.Text = "";
    lab_player.Text = "";
    }

    protected void UpdateUI()
    {
    deck = (Deck)Session["Deck"];
    lab_deck.Text = deck.ShowCards();

    //**新增***
    //Label顯示前先清空內容
    lab_player.Text = "";
    lab_dealer.Text = "";

    //**新增***
    player = (Player)Session["Player"];

    //顯示玩家手牌
    for (int i = 0; i < player.cards.Count; i++)
    {
    lab_player.Text += $"<img src='./images/{player.cards[i].Rank}-{player.cards[i].Suit}.png'>";
    }

    //**新增***
    dealer = (Player)Session["Dealer"];

    //顯示莊家手牌
    for (int i = 0; i < dealer.cards.Count; i++)
    {
    lab_dealer.Text += $"<img src='./images/{dealer.cards[i].Rank}-{dealer.cards[i].Suit}.png'>";
    }
    }

    //發牌給莊家
    protected void DrawForDealer() {
    //**新增***
    deck = (Deck)Session["Deck"];
    dealer = (Player)Session["Dealer"];

    Card c = deck.DrawCard();
    dealer.AddCard(c); //加到手牌

    //**新增***
    Session["Dealer"] = dealer;
    Session["Deck"] = deck;
    }

    //發牌給玩家
    protected void DrawForPlayer() {
    //**新增***
    deck = (Deck)Session["Deck"];
    player = (Player)Session["Player"];

    Card c = deck.DrawCard();
    player.AddCard(c); //加到手牌

    //**新增***
    Session["Player"] = player;
    Session["Deck"] = deck;
    }

    //按鈕OnClick後觸發
    //玩家要牌,呼叫DrawForPlayer()
    protected void btn_hit_Click(object sender, EventArgs e)
    {
    DrawForPlayer();

    //**新增***
    UpdateUI();
    }

    //玩家停牌,換莊家要牌DrawForDealer(),直到計分比較輸贏
    protected void btn_stand_Click(object sender, EventArgs e)
    {
    DrawForDealer();

    //**新增***
    UpdateUI();
    }

    //新牌局(下一回合),呼叫NextRound()
    protected void btn_newgame_Click(object sender, EventArgs e)
    {
    ResetGame();
    }

    protected void NextRound() { } //下一回合,呼叫ResetGame()
    }

    程式執行結果:

    用Session存取資料後,排堆(Deck)裡面發出來的牌,會和按下發牌(Hit)後的牌相同,不會再因為網頁被更新而無法保留資料。

    下一篇來加上玩家的分數計算與超過21點(爆牌)的判斷。


    Ps.此程式沒有限制只能發52張牌,所以一直按下發牌(Hit)是會一直發牌的,但超過52張會發生錯誤;沒有限制的原因是21點遊戲只有兩個人玩不會發到52張,且後面會有計算點數的控制來結束牌局。


    分享至
    成為作者繼續創作的動力吧!
    © 2024 vocus All rights reserved.