更新於 2024/11/07閱讀時間約 19 分鐘

系統開發-01初始化


    raw-image


    我們將之前寫過的發牌程式碼與撲克牌圖片,並新增Player類別,套到上一篇的程式框架內

    Default.aspx如下

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

    <!DOCTYPE html>

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
    <style>
    /*撲克牌圖片顯示在網頁上的尺寸大小*/
    img {
    width:100px;
    height:145px;
    }
    </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table border="1" style="border-collapse:collapse">
    <tr>
    <td>牌堆</td>
    <td>
    <asp:Label ID="lab_deck" runat="server" Text=""></asp:Label>
    </td>
    </tr>
    <tr>
    <td>莊家</td>
    <td>
    <asp:Label ID="lab_dealer" runat="server" Text=""></asp:Label>
    </td>
    </tr>
    <tr>
    <td>玩家</td>
    <td>
    <asp:Label ID="lab_player" runat="server" Text=""></asp:Label>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:Label ID="lab_msg" runat="server" Text=""></asp:Label>
    </td>
    </tr>
    <tr>
    <td colspan="2">
    <asp:Button ID="btn_hit" runat="server" Text="要牌(Hit)" OnClick="btn_hit_Click" />
    <asp:Button ID="btn_stand" runat="server" Text="停牌(Stand)" OnClick="btn_stand_Click" />
    <asp:Button ID="btn_newgame" runat="server" Text="新牌局" OnClick="btn_newgame_Click" />
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    Default.aspx.cs如下

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

    protected void Page_Load(object sender, EventArgs e)
    {
    ResetGame();

    UpdateUI();
    }

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

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

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

    protected void UpdateUI()
    {
    lab_deck.Text = deck.ShowCards();

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

    //顯示莊家手牌
    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() {
    Card c = deck.DrawCard();
    dealer.AddCard(c); //加到手牌
    }

    //發牌給玩家
    protected void DrawForPlayer() {
    Card c = deck.DrawCard();
    player.AddCard(c); //加到手牌
    }

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

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

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

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

    public class Player
    {
    public List<Card> cards { get; private set; }

    public Player()
    {
    cards = new List<Card>();
    Init();
    }

    public void Init()
    {
    cards.Clear();
    }

    public void AddCard(Card card)
    {
    cards.Add(card);
    }
    }

    public class Deck
    {
    private Card[] cards; //牌堆(只能透過DrawCard取得,所以設定private)
    private int dealIndex = 0; //發牌順序
    string[] suitNames = { "黑桃", "紅心", "方塊", "梅花" };

    public Deck() { //建立類別
    cards = new Card[52];
    string[] suits = { "S", "H", "D", "C" };

    int index = 0;

    for (int i = 0; i < suits.Length; i++) //四種花色
    {
    for (int j = 1; j <= 13; j++) //一種花色13張牌1-13
    {
    cards[index] = new Card(suits[i], suitNames[i], j); //初始化
    index++;
    }
    }

    Init();
    }

    public void Init()
    {
    BubbleSort(cards); //復原順序
    Shuffle(cards); //重新洗牌
    dealIndex = 0; //回復預設值
    }

    //發牌
    public Card DrawCard()
    {
    Card card = null;

    if (dealIndex < cards.Length)
    {
    card = cards[dealIndex];
    }
    //超出一副牌的52張數量

    dealIndex += 1;
    return card; // 回傳抽到的牌
    }

    void Shuffle(Card[] cards) { //洗牌(生成時,自動洗牌,所以不需要設定public)
    Random rand = new Random();

    for (int i = 0; i < cards.Length; i++)
    {
    int j = rand.Next(cards.Length); //// 產生一個隨機索引 0-51

    //例如:
    // i=0, j=12,表示第一張牌 (cards[0] 黑桃1) 與第13張牌 (cards[12]黑桃13) 互換
    // 互換後的結果為,第一張牌 (cards[0] 黑桃13,第13張牌 (cards[12]黑桃1)
    // 一直換52次(每一個位置的牌都換一次)
    Swap(cards, i, j);
    }
    }

    //將撲克牌順序由小到大排序
    void BubbleSort(Card[] cards)
    {
    for (int i = 0; i < cards.Length; i++)
    {
    for (int j = 0; j < cards.Length - 1; j++) //反過來就是由大到小排序
    {
    if (cards[j].Seq > cards[j + 1].Seq)
    {
    Swap(cards, j, j + 1);
    }
    }
    }
    }

    //陣列中兩個元素交換,通過暫存變數的方式來實現交換
    void Swap(Card[] cards, int i, int j)
    {
    Card tmp = cards[i]; //將 cards[i] 的值暫存到 temp
    cards[i] = cards[j]; //將 cards[j] 的值賦給 cards[i]
    cards[j] = tmp; //將剛才暫存的 tmp 值賦給 cards[j]
    }

    public string ShowCards()
    {
    string str = string.Empty;

    for (int i = 0; i < cards.Length; i++)
    {
    if (i % 13 == 0) //13張牌排成一行
    {
    str += $"<br>{cards[i].ToString()}"; //換行
    }
    else
    {
    str += $",{cards[i].ToString()}"; //自訂顯示方式
    }
    }

    return str;
    }
    }

    public class Card
    {
    public string Suit { get; set; } // 花色
    public string SuitName { get; set; } // 花色名稱,為了顯示排堆時方便辨識
    public int Rank { get; set; } // 點數
    public int Seq { get; set; } // 順序

    public Card(string suit, string SuitName, int rank) {
    this.Suit = suit;
    this.SuitName = SuitName;
    this.Rank = rank;
    } //建立類別

    // 顯示卡片的文字表示
    public override string ToString()
    {
    return $"{Rank} of {SuitName}";
    }
    }

    完成後,當網頁載入後就會洗好牌,並請發給莊家和玩家各兩張牌

    畫面如下

    但在按下要牌(Hit)時,會發生整個牌堆和手牌的資料都不一樣了,這是因為ASP.Net按下按鈕後,會重新載入網頁,所以Page_Load()都會重新執行一次,牌堆和手牌當然會不一樣,下一篇將使用Session儲存資料來解決這個問題。


    Page_Load()請參考ASP.Net生命週期(Life Cycle)相關網頁


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