-1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default2 : System.Web.UI.Page
{
    public enum Suit
    {
        Club,
        Diamond,
        Heart,
        Spade
    }

    public static int i = 0;

    public class Card
    {
        private Suit suit;
        private int rank;
        private string pictureUrl;

        public Suit Suit
        {
            get { return suit;}
            set { suit = value; }
        }

        public int Rank
        {
            get { return rank; }
            set { rank = value; }
        }

        public string PictureUrl
        {
            get { return pictureUrl; }
            set { pictureUrl = value; }
        }

        public Card(Suit suit, int rank, string pictureUrl)
        {
            this.suit = suit;
            this.rank = rank;
            this.pictureUrl = pictureUrl;
        }
    }

    protected void InitDeck(List<Card> deck)
    {
        for (int suit = 0; suit < 4; suit++)
            for (int rank = 0; rank < 13; rank++)
            {
                string strPath = "Images\\" + (suit * 13 + rank).ToString() + ".png";
                deck.Add(new Card((Suit)suit, rank + 2, strPath));
            }
    }

    protected void InitGameBoard(Card[,] gameBoard, List<Card> deck)
    {
        Random rnd = new Random();
        int rndPosition;

        for(int i = 0; i < 5; i++)
            for (int j = 0; j < 5; j++)
            {
                if (i == 4 && j == 4)
                {
                    gameBoard[i, j] = new Card(0, 100, "Images\\b1fv.png");
                }
                else
                {
                    rndPosition = rnd.Next(0, deck.Count);
                    gameBoard[i, j] = deck[rndPosition];
                    deck.RemoveAt(rndPosition);
                }
            }
    }

    protected void DrawTable(Card[,] gameBoard)
    {
        Table gameTable = new Table();
        gameTable.GridLines = GridLines.Both;
        gameTable.BorderWidth = 4;
        gameTable.ID = "gameTable";

        ImageButton imgBtn = null;

        for (int i = 0; i < 5; i++)
        {
            TableRow tblRow = new TableRow();
            for (int j = 0; j < 5; j++)
            {
                TableCell tblCell = new TableCell();
                tblCell.HorizontalAlign = HorizontalAlign.Center;
                imgBtn = new ImageButton();
                imgBtn.ID = i.ToString() + j.ToString();
                imgBtn.Width = 80;
                imgBtn.Height = 105;
                imgBtn.ImageUrl = gameBoard[i, j].PictureUrl;
                imgBtn.Click += new ImageClickEventHandler(Checking);
                tblCell.Controls.Add(imgBtn);
                tblRow.Cells.Add(tblCell);
            }

            gameTable.Rows.Add(tblRow);           
        }

        Panel1.Controls.Add(gameTable);      
    }

    protected void Checking(System.Object sender, System.EventArgs e)
    {
        ImageButton imgBtn = (ImageButton)sender;
        int imgBtnIdNo = int.Parse(imgBtn.ID);
        Label1.Text = i.ToString();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        List<Card> deck = new List<Card>();
        Card[,] gameBoard = new Card[5, 5];     
        InitDeck(deck);
        InitGameBoard(gameBoard, deck);
        DrawTable(gameBoard);

    }
}

这是一个我正在构建的获胜形式游戏,其中生成了一个随机的 5 X 5 表,其中填充了除空白单元格之外的卡片,您需要在空白单元格周围按下图像按钮,以切换他们的位置并构建扑克牌行,但每次我按下表格不断生成的图像按钮。

4

1 回答 1

0

默认情况下,按下按钮会将事件发送到服务器(并重新加载页面)。要在客户端处理它,您需要使用 OnClientClick 属性 (http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx)

于 2012-10-03T19:59:01.480 回答