Guest User

Chess

a guest
Feb 26th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.58 KB | None | 0 0
  1. //MAIN FORM CODE
  2.  
  3. using System;
  4. using System.Collections.Generic;
  5. using System.ComponentModel;
  6. using System.Data;
  7. using System.Drawing;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13.  
  14. namespace WindowsFormsApplication1
  15. {
  16.     public partial class Form1 : Form
  17.     {
  18.         public Form1()
  19.         {
  20.             InitializeComponent();
  21.         }
  22.  
  23.         List<Tile> tiles = new List<Tile>();
  24.         List<Piece> pieces = new List<Piece>();
  25.         private void Form1_Load(object sender, EventArgs e)
  26.         {
  27.            
  28.         }
  29.  
  30.         private void panel1_Paint(object sender, PaintEventArgs e)
  31.         {
  32.             int c = 1;
  33.             Graphics g = panel1.CreateGraphics();
  34.             for (int i = 0; i <= 7; i++)
  35.             {
  36.                 for (int i1 = 0; i1 <= 7; i1++)
  37.                 {
  38.                     Tile newtile = new Tile(i1, i);
  39.                     if (c == 1)
  40.                     {
  41.                         newtile.BackColor = Color.White;
  42.                         g.FillRectangle(new SolidBrush(Color.White), new Rectangle(new Point(64 * i1, 64 * i), new Size(64, 64)));
  43.                         c -= 1;
  44.                     }
  45.                     else {
  46.                         newtile.BackColor = Color.Gray;
  47.                         g.FillRectangle(new SolidBrush(Color.Gray), new Rectangle(new Point(64 * i1, 64 * i), new Size(64, 64)));
  48.                         c += 1;
  49.                     }
  50.                     newtile.Size = new Size(64, 64);
  51.                     newtile.Location = new Point(64 * newtile.x, 64 * newtile.y);
  52.                     newtile.Paint += newtile_paint;
  53.                     newtile.Click += newtile_click;
  54.                     tiles.Add(newtile);
  55.                 }
  56.                 if (c==0)
  57.                 {
  58.                     c = 1;
  59.                 }
  60.                 else
  61.                 {
  62.                     c = 0;
  63.                 }
  64.             }
  65.             foreach (Tile t in tiles)
  66.             {
  67.                 if (t.y == 0)
  68.                 {
  69.                     if (t.x == 0 | t.x == 7)
  70.                     {
  71.                         pieces.Add(new Piece(Piece.type.rook, Color.Black, t));
  72.                     }
  73.                     else if (t.x == 1 | t.x == 6)
  74.                     {
  75.                         pieces.Add(new Piece(Piece.type.knight, Color.Black, t));
  76.                     }
  77.                     else if (t.x == 2 | t.x == 5)
  78.                     {
  79.                         pieces.Add(new Piece(Piece.type.bishop, Color.Black, t));
  80.                     }
  81.                     else if (t.x == 3)
  82.                     {
  83.                         pieces.Add(new Piece(Piece.type.queen, Color.White, t));
  84.                     }
  85.                 }
  86.                 else if (t.y == 1)
  87.                 {
  88.                     pieces.Add(new Piece(Piece.type.pawn, Color.Black, t));
  89.  
  90.                 }
  91.                 else if (t.y == 6)
  92.                 {
  93.                     pieces.Add(new Piece(Piece.type.pawn, Color.White, t));
  94.                 }
  95.                 else if (t.y == 7)
  96.                 {
  97.                     if (t.x == 0 | t.x == 7)
  98.                     {
  99.                         pieces.Add(new Piece(Piece.type.rook, Color.White, t));
  100.                     }
  101.                     else if (t.x == 1 | t.x == 6)
  102.                     {
  103.                         pieces.Add(new Piece(Piece.type.knight, Color.White, t));
  104.                     }
  105.                     else if (t.x == 2 | t.x == 5)
  106.                     {
  107.                         pieces.Add(new Piece(Piece.type.bishop, Color.White, t));
  108.                     }
  109.                     else if (t.x == 3)
  110.                     {
  111.                         pieces.Add(new Piece(Piece.type.queen, Color.White, t));
  112.                     }
  113.                 }
  114.                 panel1.Controls.Add(t);
  115.             }
  116.         }
  117.         private void newtile_paint(object sender, PaintEventArgs e)
  118.         {
  119.             foreach (Tile t in tiles)
  120.             {
  121.                 foreach (Piece p in pieces)
  122.                 {
  123.                     if (p.tile == t)
  124.                     {
  125.                         if (p.color == Color.White)
  126.                         {
  127.                             if (p.rank == Piece.type.pawn)
  128.                             {
  129.                                 Graphics g1 = t.CreateGraphics();
  130.                                 g1.DrawString("♙", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  131.                             } else if (p.rank == Piece.type.rook)
  132.                             {
  133.                                 Graphics g1 = t.CreateGraphics();
  134.                                 g1.DrawString("♖", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  135.                             } else if (p.rank == Piece.type.knight)
  136.                             {
  137.                                 Graphics g1 = t.CreateGraphics();
  138.                                 g1.DrawString("♘", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  139.                             } else if (p.rank == Piece.type.bishop)
  140.                             {
  141.                                 Graphics g1 = t.CreateGraphics();
  142.                                 g1.DrawString("♗", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  143.                             } else if (p.rank == Piece.type.queen)
  144.                             {
  145.                                 Graphics g1 = t.CreateGraphics();
  146.                                 g1.DrawString("♕", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  147.                             } else if (p.rank == Piece.type.king)
  148.                             {
  149.                                 Graphics g1 = t.CreateGraphics();
  150.                                 g1.DrawString("♔", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  151.                             }
  152.                         } else if (p.color == Color.Black)
  153.                         {
  154.                             if (p.rank == Piece.type.pawn)
  155.                             {
  156.                                 Graphics g1 = t.CreateGraphics();
  157.                                 g1.DrawString("♟", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  158.                             } else if (p.rank == Piece.type.rook)
  159.                             {
  160.                                 Graphics g1 = t.CreateGraphics();
  161.                                 g1.DrawString("♜", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  162.                             } else if (p.rank == Piece.type.knight)
  163.                             {
  164.                                 Graphics g1 = t.CreateGraphics();
  165.                                 g1.DrawString("♞", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  166.                             } else if (p.rank == Piece.type.bishop)
  167.                             {
  168.                                 Graphics g1 = t.CreateGraphics();
  169.                                 g1.DrawString("♝", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  170.                             } else if (p.rank == Piece.type.queen)
  171.                             {
  172.                                 Graphics g1 = t.CreateGraphics();
  173.                                 g1.DrawString("♛", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  174.                             } else if (p.rank == Piece.type.king)
  175.                             {
  176.                                 Graphics g1 = t.CreateGraphics();
  177.                                 g1.DrawString("♚", new Font("Arial", 25), Brushes.Black, new Point(11, 16));
  178.                             }
  179.                         }
  180.                     }
  181.                 }
  182.             }
  183.         }
  184.         private void newtile_click(object sender, EventArgs e)
  185.         {
  186.             Tile t = (Tile)sender;
  187.             t.Image = Chess.Properties.Resources.green;
  188.         }
  189.     }
  190. }
  191.  
  192.  
  193. //PIECE CLASS CODE
  194.  
  195. using System;
  196. using System.Collections.Generic;
  197. using System.Linq;
  198. using System.Text;
  199. using System.Threading.Tasks;
  200. using System.Drawing;
  201.  
  202. namespace WindowsFormsApplication1
  203. {
  204.     class Piece
  205.     {
  206.         public enum type
  207.         {
  208.             pawn = 0,
  209.             rook = 1,
  210.             knight = 2,
  211.             bishop = 3,
  212.             queen = 4,
  213.             king = 5
  214.         }
  215.         private Tile m_tile;
  216.         public Tile tile
  217.         {
  218.             get { return m_tile; }
  219.             set { m_tile = value; }
  220.         }
  221.         private Color m_color;
  222.         public Color color
  223.         {
  224.             get { return m_color; }
  225.             set { m_color = value; }
  226.         }
  227.         private type m_type;
  228.         public type rank
  229.         {
  230.             get { return m_type; }
  231.             set { m_type = value; }
  232.         }
  233.         public Piece(type rank, Color c, Tile t)
  234.         {
  235.             m_type = rank;
  236.             m_color = c;
  237.             m_tile = t;
  238.         }
  239.     }
  240. }
  241.  
  242.  
  243.  
  244. //TILE CLASS CODE
  245. using System;
  246. using System.Collections.Generic;
  247. using System.Linq;
  248. using System.Text;
  249. using System.Threading.Tasks;
  250. using System.Windows.Forms;
  251. using System.Drawing;
  252.  
  253. namespace WindowsFormsApplication1
  254. {
  255.     public partial class Tile : PictureBox
  256.     {
  257.         private int m_x;
  258.         private int m_y;
  259.         public int x
  260.         {
  261.             get { return m_x; }
  262.             set { m_x = value; }
  263.         }
  264.         public int y
  265.         {
  266.             get { return m_y; }
  267.             set { m_y = value; }
  268.         }
  269.         public Tile(int xc, int yc) : base()
  270.         {
  271.             m_x = xc;
  272.             m_y = yc;
  273.         }
  274.     }
  275. }
Advertisement
Add Comment
Please, Sign In to add comment