0

Boardのオブジェクトを保持する 2D 配列 を作成しましたGameCell

デフォルト.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">
    <title></title>
    <link rel="stylesheet" href="includes/css/style.css" />
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <h1>ארבע בשורה</h1>
        <div id="Board">
            <asp:Label ID="Label1" runat="server"></asp:Label>
            <asp:PlaceHolder ID="MyBoard" runat="server" />
        </div>
    </div>
    </form>
</body>
</html>

Default.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    Game.CreateControls();
}

protected void Page_Init(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        Game.Set(MyBoard, Label1);
        Game.Reset();
    }
}

GameCell.cs:

public class GameCell : ImageButton
{
    public Position Position; // Justs holds the properties of Row and Col
    public CellType Type;

    public GameCell(Position Pos)
    {
        this.Position = new Position(Pos);
        this.Type = CellType.Empty;
    }
}

ゲーム.cs:

public class Game
{
    public const int R = 6;
    public const int C = 9;

    public static GameCell[,] Board;
    public static PlaceHolder Holder;
    public static Label Log;

    public static CellType CurrentType;

    public static string[] Images = new string[] { "red", "empty", "blue" };

    public static void Set(PlaceHolder Holder, Label Log)
    {
        Game.Reset();
        Game.Holder = Holder;
        Game.Log = Log;
    }

    public static void CreateControls()
    {
        for (int i = 0; i < Game.R; i++)
        {
            for (int j = 0; j < Game.C; j++)
            {
                Game.Board[i, j].ImageUrl = Game.ImageUrlByType(Game.Board[i, j].Type);
                Game.Board[i, j].ID = i + "_" + j;
                Game.Board[i, j].Click += new ImageClickEventHandler(Image_Click);

                Game.Holder.Controls.Add(Game.Board[i, j]);
            }
        }
    }

    public static void Image_Click(object sender, EventArgs e)
    {
        int row = 0;
        int col = int.Parse(((GameCell)sender).ID[2].ToString());

        if (Game.Board[row, col].Type == CellType.Empty)
        {
            while (row < Game.R - 1 && Game.Board[row, col].Type == CellType.Empty)
            {
                row++;
            }

            Game.SetImage(row, col);
            Game.CurrentType = (CellType)(-(int)Game.CurrentType);
            Log.Text = col + " " + row + " " + Game.CurrentType;

            Game.Board[row, col].Enabled = false;
        }
    }

    public static void SetImage(int r, int c)
    {
        Game.Board[r, c].Type = Game.CurrentType;
        Game.Board[r, c].ImageUrl = Game.ImageUrlByType(Game.CurrentType);
    }

    public static void Reset()
    {
        Game.Board = new GameCell[R, C];
        Game.CurrentType = CellType.Blue;

        for (int i = 0; i < Game.R; i++)
        {
            for (int j = 0; j < Game.C; j++)
            {
                Game.Board[i, j] = new GameCell(new Position(i, j));
            }
        }
    }

    public static string ImageUrlByType(CellType t)
    {
        return "includes/images/" + Game.Images[(int)t + 1] + ".png";
    }
}

コントロールはレンダリングされ、プロジェクトを最初に起動するときにクリックできますが、そのうちの 1 つをクリックすると、ホルダーにコントロールが追加されません ( MyBoard)。

なぜこうなった?

編集:

解決策を見つけました。MyBoard引数として渡してそれをCreateControls行いました。page_load の後、参照が信頼できなくなった (オブジェクトに保存した) 可能性があります。

4

1 に答える 1

2

が呼び出されるたびにPage_Load、コントロールを再作成します。それらは静的メンバーに保存されます。つまり、この Web サイトのすべてのユーザーが共通のボードを共有します。これは本当にあなたが望むものですか?ゲームをセッションに保存するほうがよいのではないでしょうか? (もちろん、あなたの要件はわかりませんが、Web アプリケーションでは常に静的メンバーを避けるようにしています)。

可能な解決策は、コントロールを再作成する前に、ポストバックではないかどうかを確認することです。

if (!IsPostback)
{

}
于 2012-12-04T18:10:37.433 に答える