本文介绍了当我第一次点击时,Tic Tac Toe一直说我赢了?!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace wf_tictactoe
{
public partial class Form1 : Form
{
bool turn = true;//true = X turn; false = Y turn
int turn_count = 0;
public Form1()
{
InitializeComponent();
}
private void 장현석ToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Peter Jang - 장현석");
}
private void 나가기ToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void 파일ToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void button_click(object sender, EventArgs e)
{
Button b = (Button)sender;
if (turn)
b.Text = "X";
else
b.Text = "O";
turn = !turn;
b.Enabled = false;
turn_count++;
checkForWinner();
}
private void checkForWinner()
{
bool there_is_a_winner = false;
//horizontal checks
if ((A1.Text == A2.Text) && (A2.Text == A3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!B1.Enabled))
there_is_a_winner = true;
else if ((C1.Text == C2.Text) && (C2.Text == C3.Text) && (!C1.Enabled))
there_is_a_winner = true;
//vertical checks
else if ((A1.Text == A1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((B1.Text == B2.Text) && (B2.Text == B3.Text) && (!A2.Enabled))
there_is_a_winner = true;
else if ((C1.Text == B3.Text) && (B3.Text == C3.Text) && (!A3.Enabled))
there_is_a_winner = true;
//diagonal checks
else if ((A1.Text == B2.Text) && (B2.Text == C3.Text) && (!A1.Enabled))
there_is_a_winner = true;
else if ((A3.Text == B2.Text) && (B2.Text == C1.Text) && (!C1.Enabled))
there_is_a_winner = true;
if (there_is_a_winner)
{
disableButtons();
String winner = "";
if (turn)
winner = "O";
else
winner = "X";
MessageBox.Show(winner + " Wins!", "Yay!");
}//end if
else
{
if(turn_count == 9)
MessageBox.Show("Draw!", "Bummer!");
}
}//end checkForWinner
private void disableButtons()
{
try
{
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = false;
}//end foreach
}//end try
catch { }
}
private void 새개임ToolStripMenuItem_Click(object sender, EventArgs e)
{
turn = true;
turn_count = 0;
try
{
foreach (Control c in Controls)
{
Button b = (Button)c;
b.Enabled = true;
b.Text = "";
}//end foreach
}//end try
catch { }
}
}
}
推荐答案
else if ((A1.Text == A1.Text) && (B1.Text == C1.Text) && (!A1.Enabled))
there_is_a_winner = true;
A1 == A1
将永远为真。
这篇关于当我第一次点击时,Tic Tac Toe一直说我赢了?!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!