using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; namespace JogoRusso { public partial class MainForm : Form { const int BUTTON_COUNT = 16; int SQUARE_ROOT = Convert.ToInt32(Math.Sqrt(BUTTON_COUNT)); int PART_COUNT = 2 * Convert.ToInt32(Math.Sqrt(BUTTON_COUNT)) + 1; Button[] btn = new Button[BUTTON_COUNT]; Label lbl; public MainForm() { this.Left = 0; this.Top = 0; this.MaximizeBox = false; this.FormBorderStyle = FormBorderStyle.Fixed3D; this.Text = "Jogo de Lógica"; for (int i = 0; i < BUTTON_COUNT; ++i) { btn[i] = new Button(); btn[i].Width = this.ClientSize.Width / PART_COUNT; btn[i].Height = this.ClientSize.Height / PART_COUNT; btn[i].Left = this.ClientSize.Width / PART_COUNT + (2 * (i % SQUARE_ROOT) * this.ClientSize.Width / PART_COUNT); btn[i].Top = this.ClientSize.Height / PART_COUNT + (2 * (i / SQUARE_ROOT) * this.ClientSize.Height / PART_COUNT); btn[i].BackColor = Color.SeaGreen; btn[i].Text = Convert.ToString(i + 1); btn[i].Click += new EventHandler(btn_Click); btn[i].Tag = 0; this.Controls.Add(btn[i]); } lbl = new Label(); lbl.Width = this.ClientSize.Width; lbl.Text = "Todos os botões devem ficar vermelhos!"; this.Controls.Add(lbl); InitializeComponent(); } void btn_Click(object sender, EventArgs e) { int i = Convert.ToInt32(((Button)sender).Text) - 1; ChangeButtonState(i); // botão pressionado ChangeButtonState((i / SQUARE_ROOT) * SQUARE_ROOT + ((i + 1) % SQUARE_ROOT)); // botão da direita ChangeButtonState((i / SQUARE_ROOT) * SQUARE_ROOT + ((i + SQUARE_ROOT - 1) % SQUARE_ROOT)); // esquerda ChangeButtonState((i + SQUARE_ROOT) % BUTTON_COUNT); // abaixo ChangeButtonState((i - SQUARE_ROOT + BUTTON_COUNT) % BUTTON_COUNT); // acima if (CheckForWin()) WonTheGame(); } void ChangeButtonState(int i) { if (Convert.ToInt32(btn[i].Tag) == 0) { btn[i].Tag = 1; btn[i].BackColor = Color.Red; } else { btn[i].Tag = 0; btn[i].BackColor = Color.SeaGreen; } } bool CheckForWin() { for (int i = 0; i < BUTTON_COUNT; i++) { if (Convert.ToInt32(btn[i].Tag) == 0) { return false; } } return true; } void WonTheGame() { lbl.Text = "Parabéns!"; for (int i = 0; i < BUTTON_COUNT; i++) { btn[i].Click -= new EventHandler(btn_Click); } } } }