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

系統開發-05介面調整

    先來修改莊家部分,發完牌後,莊家第一章牌應該是蓋起來的,所以我們來調整一下UpdateUI()即可

    protected void UpdateUI()
    {

    //......以上省略....

    dealer = (Player)Session["Dealer"];

    //顯示莊家手牌
    for (int i = 0; i < dealer.cards.Count; i++)
    {
    //**新增**
    if (i == 0) //顯示的第一張牌改為蓋幾來的圖片
    {
    lab_dealer.Text += $"<img src='./images/BACK.png'>";
    }
    else
    {
    lab_dealer.Text += $"<img src='./images/{dealer.cards[i].Rank}-{dealer.cards[i].Suit}.png'>";
    }
    }
    }

    執行結果:


    但以上有個問題,就是牌局結束後,莊家的蓋牌並不會顯示,所以再來修改一下

    新增遊戲狀態(status),分為遊戲中(status=0)和遊戲結束(status=1)兩個狀態,除了遊戲初始化設定(status=0)以外將所有的會結束牌局的地方(玩家爆牌與停牌)加入遊戲結束(status=1)

    protected void ResetGame() {        //初始化遊戲參數與動作(初始發牌)
    //將資料存到Session
    Session["Deck"] = new Deck();
    Session["Dealer"] = new Player();
    Session["Player"] = new Player();

    //**新增**
    status = 0;
    Session["Status"] = status;

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

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

    btn_hit.Enabled = true;
    btn_stand.Enabled = true;
    btn_newgame.Enabled = false;
    }

    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"];
    //**新增 **
    status = (int)Session["Status"]; //取得status

    //顯示莊家手牌
    for (int i = 0; i < dealer.cards.Count; i++)
    {
    //**新增**
    if (i == 0 && status == 0) //加入status判斷,遊戲結束(status=1)不顯示蓋牌
    {
    lab_dealer.Text += $"<img src='./images/BACK.png'>";
    }
    else
    {
    lab_dealer.Text += $"<img src='./images/{dealer.cards[i].Rank}-{dealer.cards[i].Suit}.png'>";
    }
    }
    }

    protected void btn_hit_Click(object sender, EventArgs e)
    {
    DrawForPlayer();

    if (player.IsBusted())
    {
    lab_msg.Text = $"{player.CalculatePoints()}點,你爆掉了! 莊家贏!";

    //**新增**
    status = 1;
    Session["Status"] = status;

    //牌局結束,不能再按發牌和停牌
    btn_hit.Enabled = false;
    btn_stand.Enabled = false;
    btn_newgame.Enabled = true;
    }

    UpdateUI();
    }

    protected void btn_stand_Click(object sender, EventArgs e)
    {
    player = (Player)Session["Player"];
    dealer = (Player)Session["Dealer"];

    //小於18點持續要牌
    while (dealer.CalculatePoints() < 18)
    {
    DrawForDealer();
    }

    //如果莊家沒有爆牌,比較莊家和玩家的點數
    int playerScore = player.CalculatePoints();
    int dealerScore = dealer.CalculatePoints();

    if (dealer.IsBusted() || playerScore > dealerScore)
    {
    //玩家贏
    if (dealer.IsBusted())
    {
    lab_msg.Text = $"莊家 {dealer.CalculatePoints()}點,玩家 {player.CalculatePoints()}點! 莊家爆掉了! 玩家贏!";
    }
    else
    {
    lab_msg.Text = $"莊家 {dealer.CalculatePoints()}點,玩家 {player.CalculatePoints()}點! 玩家贏!";
    }
    }
    else if (playerScore < dealerScore)
    {
    //莊家贏
    lab_msg.Text = $"莊家 {dealer.CalculatePoints()}點,玩家 {player.CalculatePoints()}點! 莊家贏!";
    }
    else
    {
    //平手
    lab_msg.Text = $"莊家 {dealer.CalculatePoints()}點,玩家 {player.CalculatePoints()}點! 雙方平手!";
    }

    //**新增**
    status = 1;
    Session["Status"] = status;

    btn_hit.Enabled = false;
    btn_stand.Enabled = false;
    btn_newgame.Enabled = true;

    UpdateUI();
    }

    執行結果:

    以上已調整好遊戲的介面,算是一個完整的遊戲了。

    後續再新增一下遊戲的回和數,與玩家輸贏次數。

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