Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 Laba3
- {
- public partial class Form1 : Form
- {
- int size = 3;
- MapButton[] map;
- GameMap gameMap;
- int currentP = 1;
- public Form1()
- {
- InitializeComponent();
- map = new MapButton[size * size];
- int width = panel1.Width;
- int height = panel1.Height;
- int sizeBetweenRect = 5;
- int sideMin = width > height ? height : width;
- int sideOfOne = ((sideMin + sizeBetweenRect) / size);
- for (int x = 0; x < size; x++)
- {
- for (int y = 0; y < size; y++)
- {
- Console.WriteLine(x + " " + y + " " + ((x * size) + y + 1));
- map[(x * size) + y] = new MapButton(new RectangleF(
- (sideOfOne * x),
- (sideOfOne * y),
- sideOfOne - sizeBetweenRect,
- sideOfOne - sizeBetweenRect));
- }
- }
- //GameMap
- gameMap = new GameMap((byte) size, 3);
- }
- private void Form1_Load(object sender, EventArgs e)
- {
- }
- private void panel1_Paint(object sender, PaintEventArgs e)
- {
- Graphics g = e.Graphics;
- for (int i = 0; i < map.Length; i++)
- {
- map[i].onPaint(g);
- }
- if (gameMap.IsEnd() == 1)
- {
- MessageBox.Show("sdsds");
- }
- }
- private void panel1_MouseClick(object sender, MouseEventArgs e)
- {
- int mouseX = e.X;
- int mouseY = e.Y;
- for (int i = 0; i < map.Length; i++)
- {
- map[i].onPressed(delegate {
- int posX = (i - (i % size)) / size;
- int posY = (i % size);
- gameMap.DoStep((byte) posX, (byte) posY, (byte)currentP);
- map[i].setState(gameMap.getPosition((byte)posX, (byte)posY));
- currentP = currentP == 1 ? 2 : 1;
- this.Refresh();
- }, mouseX, mouseY);
- }
- }
- }
- }
- public class MapButton {
- int isSet = 0;
- RectangleF rectangle;
- Brush color = Brushes.LightGray;
- public MapButton(RectangleF rectangle)
- {
- this.rectangle = rectangle;
- }
- public void onPressed(Action action, int x, int y)
- {
- if (rectangle.Contains(x,y)) {
- action.Invoke();
- }
- }
- public void onPaint(Graphics g)
- {
- g.FillRectangle(color, rectangle);
- if (isSet == 1)
- {
- Font drawFont = new Font("Arial", ((int) (rectangle.Width * 0.7)));
- g.DrawString("X", drawFont, Brushes.Blue, rectangle);
- } else if (isSet == 2)
- {
- g.DrawEllipse(new Pen(Color.Red, ((int)(rectangle.Width * 0.05))), rectangle);
- }
- }
- public void setState(int st)
- {
- if (isSet == 0) isSet = st;
- }
- }
- class GameMap
- {
- private byte[,,] map;
- private byte size;
- private byte winComb;
- public GameMap(byte size, byte winComb)
- {
- map = new byte[size + 1, size + 1, 5];
- this.size = size;
- this.winComb = winComb;
- }
- public void DoStep(byte x, byte y, byte player)
- {
- map[y, x, 0] = player;
- }
- public byte IsEnd()
- {
- sbyte[,] comb = new sbyte[4, 2] { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 } };
- for (int y = size - 1; y >= 0; y--)
- {
- for (int x = size - 1; x >= 0; x--)
- {
- for (int i = 0; i < 4; i++)
- {
- if (x + comb[i, 1] >= 0 && map[y, x, 0] != 0 && map[y, x, 0] == map[y + comb[i, 0], x + comb[i, 1], 0] )
- {
- map[y, x, i + 1] = (byte)(map[y + comb[i, 0], x + comb[i, 1], i + 1] + 1);
- if (map[y, x, i + 1] == winComb - 1) return map[y, x, 0];
- }
- }
- }
- }
- return 0;
- }
- public byte getPosition(byte x, byte y)
- {
- return map[y, x, 0];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement