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桌子,上面装满了除空白单元格以外的卡片,你需要按一个空白单元格周围的图像按钮,切换它们的位置和建筑物扑克行做了手,但是当我按下图像按钮时,桌子不断生成。
答案 0 :(得分:0)
默认情况下,按下按钮会将事件发送到服务器(并重新加载页面)。要在客户端中处理它,您需要使用OnClientClick属性(http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.onclientclick.aspx)