Статус:
Offline
Реєстрація: 16.04.2009
Повідом.: 76
Реєстрація: 16.04.2009
Повідом.: 76
- 🟡 10:00 Відбій тривоги в Харківський район.Зверніть увагу, тривога ще триває у:- Харківський район#Харківський_район
- #1
[С#] Нужна помощь по сапёру (игра)
У меня 9 ошибок, почти все связанны с Invalidate(). Не знаю как исправить... Заранее спасибо. Вот код:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Net.Mail;
using System.Drawing.Imaging;
namespace Saper
{
class Saperr
{
private const int
MR = 10, // кол-во клеток по вертикали
MC = 10, // кол-во клеток по горизонтали
NM = 10, // кол-во мин
W = 40, // ширина клетки поля
H = 40; // высота клетки поля
private int[,] Pole = new int[MR + 2, MC + 2]; // минное поле
private int nMin, // кол-во найденных мин
nFlag; // кол-во поставленных флагов
private int status; // статус игры 0 - начало
// 1 - игра
// 2 - конец
private void NewGame()
{
int row, col; // индексы клетки
int n = 0; // кол-во посталенных мин
int k; // кол-во соседних мин
for (row = 1; row <= MR; row++)
for (col = 1; col <= MC; col++)
Pole[row, col] = 0;
Random rnd = new Random(); // расставляем мины
do
{
row = rnd.Next(MR) + 1;
col = rnd.Next(MC) + 1;
if (Pole[row, col] != 9)
{
Pole[row, col] = 9;
n++;
}
}
while (n != NM);
for (row = 1; row <= MR; row++)
for (col = 1; col <= MC; col++)
if (Pole[row, col] != 9)
{
k = 0;
if (Pole[row, col - 1] == 9) k++;
if (Pole[row - 1, col] == 9) k++;
if (Pole[row, col + 1] == 9) k++;
if (Pole[row + 1, col] == 9) k++;
if (Pole[row - 1, col - 1] == 9) k++;
if (Pole[row - 1, col + 1] == 9) k++;
if (Pole[row + 1, col - 1] == 9) k++;
if (Pole[row + 1, col + 1] == 9) k++;
Pole[row, col] = k;
}
status = 0; // итак игра начинаеться!
nMin = 0; // кол-во обнаруженных мин
nFlag = 0; // и флагов пока нет
}
private void showPole(System.Drawing.Graphics g, int status) // рисование поля
{
for (int row = 1; row <= MR; row++)
for (int col = 1; col <= MC; col++)
this.kletka(g, row, col, status);
}
private void kletka(System.Drawing.Graphics g, int row, int col, int status) // всё о клетке
{
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (Pole[row, col] < 100)
g.FillRectangle(SystemBrushes.ControlLight, x - 1, y - 1, x + W, y + H); // отисовывает нетронутую клетку
if (Pole[row, col] >= 100)
{
if (Pole[row, col] != 109)
g.FillRectangle(Brushes.White, x - 1, y - 1, x + W, y + W); // открываем клетку, мины нет
else
g.FillRectangle(Brushes.Red, x - 1, y - 1, x + W, y + H); // ба-бах! - мина
if ((Pole[row, col] >= 101) && (Pole[row, col] <= 108)) // подписываем сколько мин вокруг
g.DrawString((Pole[row, col] - 100).ToString(), new Font("Times New Roman", 10, System.Drawing.FontStyle.Bold), Brushes.Black, x + 3, y + 2);
}
if (Pole[row, col] >= 200) // в клетке поставлен флаг
this.flag(g, x, y);
g.DrawRectangle(Pens.Black, x - 1, y - 1, x + W, y + H);
if ((status == 2) && (Pole[row, col] % 10 == 9))
this.mina(g, x, y);
}
private void open(int row, int col) // открывет текущую и все соседние клетки
{
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (Pole[row, col] == 0)
{
Pole[row, col] = 100;
this.Invalidate(new Rectangle(x, y, W, H));
this.open(row, col - 1);
this.open(row - 1, col);
this.open(row, col + 1);
this.open(row + 1, col);
this.open(row - 1, col - 1);
this.open(row - 1, col + 1);
this.open(row + 1, col - 1);
this.open(row + 1, col + 1);
}
else
if ((Pole[row, col] < 100) && (Pole[row, col] != -3))
{
Pole[row, col] += 100;
this.Invalidate();
//this.Invalidate(new Rectangle(x, y, W, H));
}
}
private void mina(Graphics g, int x, int y)
{ // корпус6,
g.FillRectangle(Brushes.Green, x + 16, y + 26, 8, 4);
g.FillRectangle(Brushes.Green, x + 8, y + 30, 24, 4);
g.DrawPie(Pens.Black, x + 6, y + 28, 28, 16, 0, -180);
}
private void flag(System.Drawing.Graphics g, int x, int y)
{ // рисование флага
g.Rectangle(x + 5, y + 5, x, y);
}
public void WinForm() // конструктор
{
InitializeComponent(); // в элеметны невидимой рамки запишем (-3) для open()
for (int row = 0; row <= MR + 1; row++)
{
Pole[row, 0] = -3;
Pole[row, MC + 1] = -3;
}
for (int col = 0; col <= MC + 1; col++)
{
Pole[0, col] = -3;
Pole[MR + 1, col] = -3;
}
this.NewGame();
this.ClientSize = new Size(W * MC + 1, H * MR + 1); // размер формы
}
private void InitializeComponent()
{
throw new Exception("The method or operation is not implemented.");
}
private void WinForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (status == 2) return;
if (status == 0) status = 1;
int row = (int)(e.Y / H) + 1; // преоброзование координат
int col = (int)(e.X / W) + 1;
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (e.Button == MouseButtons.Left)
{
if (Pole[row, col] == 9)
{
Pole[row, col] += 100;
status = 2;
this.Invalidate();
}
else
if (Pole[row, col] < 9)
this.open(row, col);
}
if (e.Button == MouseButtons.Right)
{
if (Pole[row, col] <= 9)
{
nFlag += 1;
if (Pole[row, col] == 9)
nMin += 1;
Pole[row, col] += 200;
if ((nMin == NM) && (nFlag == NM))
{
status = 2;
this.Invalidate();
}
else
this.Invalidate(new Rectangle(x, y, W, H));
}
else // убирает флаг
if (Pole[row, col] >= 200)
{
nFlag -= 1;
Pole[row, col] -= 200;
this.Invalidate(new Rectangle(x, y, W, H));
}
}
}
private void menuItem1_Click(object sender, System.EventArgs e) // клик на "Новая игра"
{
this.NewGame();
this.Invalidate();
}
private void WinForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
this.showPole(e.Graphics, status);
}
}
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Писался на visual studio 2003
У меня 9 ошибок, почти все связанны с Invalidate(). Не знаю как исправить... Заранее спасибо. Вот код:
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Drawing;
using System.Net.Mail;
using System.Drawing.Imaging;
namespace Saper
{
class Saperr
{
private const int
MR = 10, // кол-во клеток по вертикали
MC = 10, // кол-во клеток по горизонтали
NM = 10, // кол-во мин
W = 40, // ширина клетки поля
H = 40; // высота клетки поля
private int[,] Pole = new int[MR + 2, MC + 2]; // минное поле
private int nMin, // кол-во найденных мин
nFlag; // кол-во поставленных флагов
private int status; // статус игры 0 - начало
// 1 - игра
// 2 - конец
private void NewGame()
{
int row, col; // индексы клетки
int n = 0; // кол-во посталенных мин
int k; // кол-во соседних мин
for (row = 1; row <= MR; row++)
for (col = 1; col <= MC; col++)
Pole[row, col] = 0;
Random rnd = new Random(); // расставляем мины
do
{
row = rnd.Next(MR) + 1;
col = rnd.Next(MC) + 1;
if (Pole[row, col] != 9)
{
Pole[row, col] = 9;
n++;
}
}
while (n != NM);
for (row = 1; row <= MR; row++)
for (col = 1; col <= MC; col++)
if (Pole[row, col] != 9)
{
k = 0;
if (Pole[row, col - 1] == 9) k++;
if (Pole[row - 1, col] == 9) k++;
if (Pole[row, col + 1] == 9) k++;
if (Pole[row + 1, col] == 9) k++;
if (Pole[row - 1, col - 1] == 9) k++;
if (Pole[row - 1, col + 1] == 9) k++;
if (Pole[row + 1, col - 1] == 9) k++;
if (Pole[row + 1, col + 1] == 9) k++;
Pole[row, col] = k;
}
status = 0; // итак игра начинаеться!
nMin = 0; // кол-во обнаруженных мин
nFlag = 0; // и флагов пока нет
}
private void showPole(System.Drawing.Graphics g, int status) // рисование поля
{
for (int row = 1; row <= MR; row++)
for (int col = 1; col <= MC; col++)
this.kletka(g, row, col, status);
}
private void kletka(System.Drawing.Graphics g, int row, int col, int status) // всё о клетке
{
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (Pole[row, col] < 100)
g.FillRectangle(SystemBrushes.ControlLight, x - 1, y - 1, x + W, y + H); // отисовывает нетронутую клетку
if (Pole[row, col] >= 100)
{
if (Pole[row, col] != 109)
g.FillRectangle(Brushes.White, x - 1, y - 1, x + W, y + W); // открываем клетку, мины нет
else
g.FillRectangle(Brushes.Red, x - 1, y - 1, x + W, y + H); // ба-бах! - мина
if ((Pole[row, col] >= 101) && (Pole[row, col] <= 108)) // подписываем сколько мин вокруг
g.DrawString((Pole[row, col] - 100).ToString(), new Font("Times New Roman", 10, System.Drawing.FontStyle.Bold), Brushes.Black, x + 3, y + 2);
}
if (Pole[row, col] >= 200) // в клетке поставлен флаг
this.flag(g, x, y);
g.DrawRectangle(Pens.Black, x - 1, y - 1, x + W, y + H);
if ((status == 2) && (Pole[row, col] % 10 == 9))
this.mina(g, x, y);
}
private void open(int row, int col) // открывет текущую и все соседние клетки
{
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (Pole[row, col] == 0)
{
Pole[row, col] = 100;
this.Invalidate(new Rectangle(x, y, W, H));
this.open(row, col - 1);
this.open(row - 1, col);
this.open(row, col + 1);
this.open(row + 1, col);
this.open(row - 1, col - 1);
this.open(row - 1, col + 1);
this.open(row + 1, col - 1);
this.open(row + 1, col + 1);
}
else
if ((Pole[row, col] < 100) && (Pole[row, col] != -3))
{
Pole[row, col] += 100;
this.Invalidate();
//this.Invalidate(new Rectangle(x, y, W, H));
}
}
private void mina(Graphics g, int x, int y)
{ // корпус6,
g.FillRectangle(Brushes.Green, x + 16, y + 26, 8, 4);
g.FillRectangle(Brushes.Green, x + 8, y + 30, 24, 4);
g.DrawPie(Pens.Black, x + 6, y + 28, 28, 16, 0, -180);
}
private void flag(System.Drawing.Graphics g, int x, int y)
{ // рисование флага
g.Rectangle(x + 5, y + 5, x, y);
}
public void WinForm() // конструктор
{
InitializeComponent(); // в элеметны невидимой рамки запишем (-3) для open()
for (int row = 0; row <= MR + 1; row++)
{
Pole[row, 0] = -3;
Pole[row, MC + 1] = -3;
}
for (int col = 0; col <= MC + 1; col++)
{
Pole[0, col] = -3;
Pole[MR + 1, col] = -3;
}
this.NewGame();
this.ClientSize = new Size(W * MC + 1, H * MR + 1); // размер формы
}
private void InitializeComponent()
{
throw new Exception("The method or operation is not implemented.");
}
private void WinForm_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (status == 2) return;
if (status == 0) status = 1;
int row = (int)(e.Y / H) + 1; // преоброзование координат
int col = (int)(e.X / W) + 1;
int x = (col - 1) * W + 1;
int y = (row - 1) * H + 1;
if (e.Button == MouseButtons.Left)
{
if (Pole[row, col] == 9)
{
Pole[row, col] += 100;
status = 2;
this.Invalidate();
}
else
if (Pole[row, col] < 9)
this.open(row, col);
}
if (e.Button == MouseButtons.Right)
{
if (Pole[row, col] <= 9)
{
nFlag += 1;
if (Pole[row, col] == 9)
nMin += 1;
Pole[row, col] += 200;
if ((nMin == NM) && (nFlag == NM))
{
status = 2;
this.Invalidate();
}
else
this.Invalidate(new Rectangle(x, y, W, H));
}
else // убирает флаг
if (Pole[row, col] >= 200)
{
nFlag -= 1;
Pole[row, col] -= 200;
this.Invalidate(new Rectangle(x, y, W, H));
}
}
}
private void menuItem1_Click(object sender, System.EventArgs e) // клик на "Новая игра"
{
this.NewGame();
this.Invalidate();
}
private void WinForm_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
{
this.showPole(e.Graphics, status);
}
}
static class Program
{
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
Писался на visual studio 2003