Advertisement
Pit_Anonim

Untitled

Mar 29th, 2021
867
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Windows.Forms;
  10.  
  11. namespace Laba3
  12. {
  13.     public partial class Form1 : Form
  14.     {
  15.         int size = 3;
  16.  
  17.         MapButton[] map;
  18.         GameMap gameMap;
  19.  
  20.         int currentP = 1;
  21.         public Form1()
  22.         {
  23.             InitializeComponent();
  24.  
  25.             map = new MapButton[size * size];
  26.  
  27.             int width = panel1.Width;
  28.             int height = panel1.Height;
  29.  
  30.             int sizeBetweenRect = 5;
  31.  
  32.             int sideMin = width > height ? height : width;
  33.             int sideOfOne = ((sideMin + sizeBetweenRect) / size);
  34.  
  35.             for (int x = 0; x < size; x++)
  36.             {
  37.                 for (int y = 0; y < size; y++)
  38.                 {
  39.                     Console.WriteLine(x + " " + y + " " + ((x * size) + y + 1));
  40.                     map[(x * size) + y] = new MapButton(new RectangleF(
  41.                         (sideOfOne * x),
  42.                         (sideOfOne * y),
  43.                         sideOfOne - sizeBetweenRect,
  44.                         sideOfOne - sizeBetweenRect));
  45.                 }
  46.             }
  47.  
  48.  
  49.             //GameMap
  50.             gameMap = new GameMap((byte) size, 3);
  51.         }
  52.  
  53.         private void Form1_Load(object sender, EventArgs e)
  54.         {
  55.  
  56.         }
  57.  
  58.         private void panel1_Paint(object sender, PaintEventArgs e)
  59.         {
  60.             Graphics g = e.Graphics;
  61.  
  62.             for (int i = 0; i < map.Length; i++)
  63.             {
  64.                 map[i].onPaint(g);
  65.             }
  66.  
  67.             if (gameMap.IsEnd() == 1)
  68.             {
  69.                 MessageBox.Show("sdsds");
  70.             }
  71.         }
  72.  
  73.         private void panel1_MouseClick(object sender, MouseEventArgs e)
  74.         {
  75.             int mouseX = e.X;
  76.             int mouseY = e.Y;
  77.  
  78.             for (int i = 0; i < map.Length; i++)
  79.             {
  80.                 map[i].onPressed(delegate {
  81.  
  82.                     int posX = (i - (i % size)) / size;
  83.                     int posY = (i % size);
  84.  
  85.                     gameMap.DoStep((byte) posX, (byte) posY, (byte)currentP);
  86.                     map[i].setState(gameMap.getPosition((byte)posX, (byte)posY));
  87.  
  88.                     currentP = currentP == 1 ? 2 : 1;
  89.                     this.Refresh();
  90.  
  91.                 }, mouseX, mouseY);
  92.             }
  93.         }
  94.  
  95.     }
  96. }
  97.  
  98. public class MapButton {
  99.  
  100.     int isSet = 0;
  101.  
  102.     RectangleF rectangle;
  103.     Brush color = Brushes.LightGray;
  104.  
  105.     public MapButton(RectangleF rectangle)
  106.     {
  107.         this.rectangle = rectangle;
  108.     }
  109.  
  110.     public void onPressed(Action action, int x, int y)
  111.     {
  112.         if (rectangle.Contains(x,y)) {
  113.             action.Invoke();
  114.         }
  115.     }
  116.  
  117.     public void onPaint(Graphics g)
  118.     {
  119.         g.FillRectangle(color, rectangle);
  120.  
  121.         if (isSet == 1)
  122.         {
  123.             Font drawFont = new Font("Arial", ((int) (rectangle.Width * 0.7)));
  124.             g.DrawString("X", drawFont, Brushes.Blue, rectangle);
  125.         } else if (isSet == 2)
  126.         {
  127.             g.DrawEllipse(new Pen(Color.Red, ((int)(rectangle.Width * 0.05))), rectangle);
  128.         }
  129.     }
  130.  
  131.     public void setState(int st)
  132.     {
  133.         if (isSet == 0) isSet = st;
  134.     }
  135. }
  136.  
  137. class GameMap
  138. {
  139.     private byte[,,] map;
  140.     private byte size;
  141.     private byte winComb;
  142.  
  143.     public GameMap(byte size, byte winComb)
  144.     {
  145.         map = new byte[size + 1, size + 1, 5];
  146.         this.size = size;
  147.         this.winComb = winComb;
  148.     }
  149.  
  150.     public void DoStep(byte x, byte y, byte player)
  151.     {
  152.         map[y, x, 0] = player;
  153.     }
  154.  
  155.     public byte IsEnd()
  156.     {
  157.         sbyte[,] comb = new sbyte[4, 2] { { 0, 1 }, { 1, 1 }, { 1, 0 }, { 1, -1 } };
  158.         for (int y = size - 1; y >= 0; y--)
  159.         {
  160.             for (int x = size - 1; x >= 0; x--)
  161.             {
  162.                 for (int i = 0; i < 4; i++)
  163.                 {
  164.                     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] )
  165.                     {
  166.                         map[y, x, i + 1] = (byte)(map[y + comb[i, 0], x + comb[i, 1], i + 1] + 1);
  167.                         if (map[y, x, i + 1] == winComb - 1) return map[y, x, 0];
  168.                     }
  169.                 }
  170.             }
  171.         }
  172.         return 0;
  173.     }
  174.  
  175.     public byte getPosition(byte x, byte y)
  176.     {
  177.         return map[y, x, 0];
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement