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;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
nxny();
this.Left = 0;
this.Top = 0;
this.Size = new Size(50 * nx + 100, 50 * ny + 100);
this.MaximizeBox = false;
this.FormBorderStyle = FormBorderStyle.Fixed3D;
this.Text = "all red";
for (int i = 0; i < nx; i++)
{
for (int j = 0; j < ny; j++)
{
btn[i, j] = new Button();
btn[i, j].Width = 50;
btn[i, j].Height = 50;
btn[i, j].Left = 50 + 50 * i;
btn[i, j].Top = 50 + 50 * j;
btn[i, j].BackColor = Color.SeaGreen;
btn[i, j].Text = i.ToString() + j.ToString();
btn[i, j].Click += new EventHandler(btn_Click);
btn[i, j].Tag = 0;
this.Controls.Add(btn[i, j]);
}
}
}
private static int nx, ny;
private Button[,] btn;
private int count = 0;
private string step = "";
private string word(StreamReader sr, string find)
{
int i; sr.BaseStream.Seek(0, SeekOrigin.Begin);
while (!sr.EndOfStream)
{
string[] words = sr.ReadLine().Split();
if (words[0] == find)
{
for (i = 1; words[i] == ""; ) { i++; }
return words[i];
}
}
return "EOF";
}
private void nxny()
{
FileStream fs = new FileStream("myfile.txt", FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);
sr.BaseStream.Seek(0, SeekOrigin.Begin);
string txt;
txt = word(sr, "nx"); nx = Int32.Parse(txt);
txt = word(sr, "ny"); ny = Int32.Parse(txt);
btn = new Button[nx, ny];
}
private void btn_Click(object sender, EventArgs eArgs)
{
int txt = Convert.ToInt32(((Button)sender).Text);
int i, j;
if (ny < 10)
{
i = txt / 10; j = txt % 10;
}
else
{
i = txt / 100; j = txt % 100;
}
ChangeButtonState(i, j);
ChangeButtonState(i - 1, j);
ChangeButtonState(i + 1, j);
ChangeButtonState(i, j + 1);
ChangeButtonState(i, j - 1);
count++;
step += "(" + i + "," + j + ")" + " ";
if (Win() == true)
{
wingame();
}
}
private void ChangeButtonState(int i, int j)
{
if (i < 0) { i = nx - 1; }
if (j < 0) { j = ny - 1; }
if (i == nx) { i = 0; }
if (j == ny) { j = 0; }
if (Convert.ToInt32(btn[i, j].Tag) == 0)
{
btn[i, j].Tag = 1;
btn[i, j].BackColor = Color.Red;
}
else
{
btn[i, j].Tag = 0;
btn[i, j].BackColor = Color.SeaGreen;
}
}
private bool Win()
{
for (int i = 0; i < nx; i++)
{
for (int j = 0; j < ny; j++)
{
if (Convert.ToInt32(btn[i, j].Tag) == 0)
{ return false; }
}
}
return true;
}
private void wingame()
{
MessageBox.Show(count + " steps\n" + step);
for (int i = 0; i < nx; i++)
{
for (int j = 0; j < ny; j++)
{
btn[i, j].Click -= new EventHandler(btn_Click);
}
}
FileStream outputfile = null;
outputfile = new FileStream("step.txt", FileMode.Create, FileAccess.Write);
StreamWriter writer = new StreamWriter(outputfile);
writer.BaseStream.Seek(0, SeekOrigin.End);
writer.WriteLine(step);
writer.Flush();
writer.Close();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
No comments:
Post a Comment