Я пытаюсь изменить свои методы на класс. Я сделал это, но некоторые из этих работ не выполняются:Как преобразовать метод в класс?
Каков правильный путь (object sender, EventArgs e)
?
Это код, чтобы использовать метод:
public void GetPlayerChoice_Click(object sender, EventArgs e)
{
ChangeProperties(sender, e);
PlayerWon();
}
Это метод:
//Change the properties of the buttond and cheack who's turn is it.
public void ChangeProperties(object sender, EventArgs e)
{
var btn = sender as Button;
if (playerX.Turn == true)
{
btn.Text = playerX.Name;
btn.BackColor = Color.FromArgb(255, 15, 0);
playerX.Turn = false;
playerO.Turn = true;
buttonUser1.BackColor = Color.FromArgb(255, 156, 53);
buttonUser2.BackColor = Color.FromArgb(255, 255, 255);
}
else
{
btn.Text = playerO.Name;
btn.BackColor = Color.FromArgb(96, 255, 40);
playerX.Turn = true;
playerO.Turn = false;
buttonUser1.BackColor = Color.FromArgb(255, 255, 255);
buttonUser2.BackColor = Color.FromArgb(255, 156, 53);
}
btn.Enabled = false;
}
//Cheack if there is a winer.
public void PlayerWon()
{
if ((button1.Text == button2.Text && button2.Text == button3.Text) || //horizontal
(button4.Text == button5.Text && button5.Text == button6.Text) || //horizontal
(button7.Text == button8.Text && button8.Text == button9.Text) || //horizontal
(button1.Text == button4.Text && button4.Text == button7.Text) || //vertical
(button2.Text == button5.Text && button5.Text == button8.Text) || //vertical
(button3.Text == button6.Text && button6.Text == button9.Text) || //vertical
(button1.Text == button5.Text && button5.Text == button9.Text) || //slanted
(button3.Text == button5.Text && button5.Text == button7.Text)) //slanted
{
EnableAllButtonFalse();
if (playerX.Turn == true)
{
MessageBox.Show("O is the winner");
playerO.Score += 1;
}
if (playerX.Turn == false)
{
MessageBox.Show("X is the winner");
playerX.Score += 1;
}
NewGame();
}
}
//Enable false all button in the game.
public void EnableAllButtonFalse()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = false;
}
}
}
//Enable true all button in the game.
public void EnableAllButtonTrue()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = true;
}
}
}
public void NewGame()
{
EnableAllButtonTrue();
if(playerX.Turn == false)
{
playerX.Turn = true;
playerO.Turn = false;
}
else
{
playerX.Turn = false;
playerO.Turn = true;
}
}
Когда я изменил свою программу, чтобы использовать методы, которые он не работал надлежащим. Он сделал только некоторые из кода. Я не понимаю, почему это происходит.
Это код, чтобы использовать класс:
public void GetPlayerChoice_Click(object sender, EventArgs e)
{
ExtensionMethods ex = new ExtensionMethods();
ex.Sender = sender;
ex.E = e;
ex.ChangeProperties(sender, e);
ex.PlayerWon();
}
Это класс:
class ExtensionMethods : Form1
{
private object sender;
private EventArgs e;
public object Sender { get; set; }
public EventArgs E { get; set; }
//Change the properties of the buttond and cheack who's turn is it.
public void ChangeProperties(object sender, EventArgs e)
{
var btn = sender as Button;
if (playerX.Turn == true)
{
btn.Text = playerX.Name;
btn.BackColor = Color.FromArgb(255, 15, 0);
playerX.Turn = false;
playerO.Turn = true;
buttonUser1.BackColor = Color.FromArgb(255, 156, 53);
buttonUser2.BackColor = Color.FromArgb(255, 255, 255);
}
else
{
btn.Text = playerO.Name;
btn.BackColor = Color.FromArgb(96, 255, 40);
playerX.Turn = true;
playerO.Turn = false;
buttonUser1.BackColor = Color.FromArgb(255, 255, 255);
buttonUser2.BackColor = Color.FromArgb(255, 156, 53);
}
btn.Enabled = false;
}
//Cheack if there is a winer.
public void PlayerWon()
{
if ((button1.Text == button2.Text && button2.Text == button3.Text) || //horizontal
(button4.Text == button5.Text && button5.Text == button6.Text) || //horizontal
(button7.Text == button8.Text && button8.Text == button9.Text) || //horizontal
(button1.Text == button4.Text && button4.Text == button7.Text) || //vertical
(button2.Text == button5.Text && button5.Text == button8.Text) || //vertical
(button3.Text == button6.Text && button6.Text == button9.Text) || //vertical
(button1.Text == button5.Text && button5.Text == button9.Text) || //slanted
(button3.Text == button5.Text && button5.Text == button7.Text)) //slanted
{
EnableAllButtonFalse();
if (playerX.Turn == true)
{
MessageBox.Show("O is the winner");
playerO.Score += 1;
}
if (playerX.Turn == false)
{
MessageBox.Show("X is the winner");
playerX.Score += 1;
}
NewGame();
}
}
//Enable false all button in the game.
public void EnableAllButtonFalse()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = false;
}
}
}
//Enable true all button in the game.
public void EnableAllButtonTrue()
{
foreach (Control c in Controls)
{
if (c is Button)
{
c.Enabled = true;
}
}
}
public void NewGame()
{
EnableAllButtonTrue();
if(playerX.Turn == false)
{
playerX.Turn = true;
playerO.Turn = false;
}
else
{
playerX.Turn = false;
playerO.Turn = true;
}
}
Является ли это домашнее задание/задание? Если это так, ваш лектор/преподаватель, вероятно, хочет, чтобы вы упростили свою программу, разделив разделы на свои собственные методы. – cybermonkey
Это много кода, и трудно понять, где проблема основана на вашем вопросе. «некоторые вещи работают, некоторые вещи не делают», будьте конкретными, чтобы помочь нам помочь вам. – Kritner
Каков правильный способ передачи отправителю? – coco