所以,我正在尝试创建一个TicTacToe应用程序,但问题是clickNo计数器应该计算点击后(每次点击后)点击后没有更新。如果有人建议我在页面加载中尝试使用IsPostBack == false,但它完成的唯一事情就是随机弹出一个“O”。
{
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
Session["s1"] = clickNo;
}
public void buttonClicked(object Sender)
{
clickNo = (int)Session["s1"];
if (Session["s1"] != null)
{
clickNo = +1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = clickNo;
checkWinner();
}
else
{
Response.Write("Session expire");
}
}
public void checkWinner()
{
if (clickNo > 4)
{
if (btn1.Text == btn2.Text && btn2.Text == btn3.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn4.Text == btn5.Text && btn5.Text == btn6.Text && btn4.Text != "")
{
displayWinner();
}
else if (btn7.Text == btn8.Text && btn8.Text == btn9.Text && btn7.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn4.Text && btn4.Text == btn7.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn2.Text == btn5.Text && btn5.Text == btn8.Text && btn2.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn6.Text && btn6.Text == btn9.Text && btn3.Text != "")
{
displayWinner();
}
else if (btn1.Text == btn5.Text && btn5.Text == btn9.Text && btn1.Text != "")
{
displayWinner();
}
else if (btn3.Text == btn5.Text && btn5.Text == btn7.Text && btn3.Text != "")
{
displayWinner();
}
else if (clickNo == 9)
{
Response.Write("Game Draw");
clearWindow();
}
else
{
Response.Write("Session expire");
}
}
}
public void displayWinner()
{
if (clickedByPlayerNo == 1)
{
Response.Write("Winner is A");
}
else
Response.Write("Winner is B");
clearWindow();
}
public void clearWindow()
{
btn1.Text = "";
btn1.Enabled = false;
btn2.Text = "";
btn2.Enabled = false;
btn3.Text = "";
btn3.Enabled = false;
btn4.Text = "";
btn4.Enabled = false;
btn5.Text = "";
btn5.Enabled = false;
btn6.Text = "";
btn6.Enabled = false;
btn7.Text = "";
btn7.Enabled = false;
btn8.Text = "";
btn8.Enabled = false;
btn9.Text = "";
btn9.Enabled = false;
}
protected void btn1_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn2_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn3_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn4_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn5_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn6_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn7_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn8_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
protected void btn9_Click(object sender, EventArgs e)
{
buttonClicked(sender);
}
}
}
我最诚挚的感谢Red,我一直在网上搜索,看着显示器连续10个小时。这是代码的更新部分:
public partial class home : System.Web.UI.Page
{
public int clickedByPlayerNo, clickNo;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "-1";
}
}
public void buttonClicked(object Sender)
{
clickNo = Convert.ToInt32(Session["s1"]);
if (Session["s1"] != null)
{
int someValue = clickNo + 1;
if (clickNo % 2 == 0)
{
clickedByPlayerNo = 2;
}
else
{
clickedByPlayerNo = 1;
}
if (clickedByPlayerNo == 1)
{
((Button)Sender).Text = "X";
}
else
{
((Button)Sender).Text = "O";
}
((Button)Sender).Enabled = false;
Session["s1"] = someValue;
}
checkWinner();
}
答案 0 :(得分:0)
仔细研究Page_Load
方法。每次按下某个按钮postback
时,都会调用Page_Load
方法。您需要做的是在页面加载时仅填充一次会话变量。这就是我要做的事情:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Session["s1"] = "0";
}
}