Bogdan BucurIrinel Archip
Published

Automated Chess Board

With this project ,we aim to create a way to play physical chess with someone even if that person is on the other side of the world.

AdvancedWork in progressOver 4 days2,458
Automated Chess Board

Things used in this project

Hardware components

Arty A7-35T
Digilent Arty A7-35T
×1
Digilent PMOD MTDS
×1
NEMA 17 Stepper Motor
OpenBuilds NEMA 17 Stepper Motor
×2
Capacitive Touch Sensor Breakout - MPR121
Adafruit Capacitive Touch Sensor Breakout - MPR121
×10
ball bearings
×4
linear bearings
×2
rods
×4
aluminium sheets
×5
copper sheets
×5
OSB
×1
Electromagnet
×1

Software apps and online services

Vivado 2017.4
Sharp Develop
Xilinx SDK

Hand tools and fabrication machines

Drill Machine
Screwdriver
Soldering Station
Hot glue gun (generic)
Hot glue gun (generic)
caulk gun

Story

Read more

Schematics

Schematic

Code

Chess_app

C#
/*
 * Created by SharpDevelop.
 * User: Bogdan
 * Date: 14.02.2018
 * Time: 01:12
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace Chess
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		String [,] table = new String[10, 10];
		int ok = 0;
		int x, y, xs, ys;
		Boolean sel = false;
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
			this.DoubleBuffered = true;			
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			Application.Exit();
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			int i, j;
			ok = 1;
			
			for(i=0; i<10; i++)
				for(j=0; j<10; j++)
					table[i, j] = "0";
			for(i=1; i<9; i++)
			{
				table[2, i] = "BP";
				table[7, i] = "WP";
			}
			
			table[1, 1] = "BR";
			table[8, 1] = "WR";
			table[1, 2] = "BKn";
			table[8, 2] = "WKn";
			table[1, 3] = "BB";
			table[8, 3] = "WB";
			table[1, 4] = "BQ";
			table[8, 4] = "WQ";
			table[1, 5] = "BK";
			table[8, 5] = "WK";
			table[1, 6] = "BB";
			table[8, 6] = "WB";
			table[1, 7] = "BKn";
			table[8, 7] = "WKn";
			table[1, 8] = "BR";
			table[8, 8] = "WR";
			button2.Enabled = false;
			button3.Enabled = false;
			
			this.Refresh();
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			int i, j;
			ok = 2;
			
			for(i=0; i<10; i++)
				for(j=0; j<10; j++)
					table[i, j] = "0";			
			for(i=1; i<9; i++)
			{
				table[2, i] = "WP";
				table[7, i] = "BP";
			}
			
			table[1, 1] = "WR";
			table[8, 1] = "BR";
			table[1, 2] = "WKn";
			table[8, 2] = "BKn";
			table[1, 3] = "WB";
			table[8, 3] = "BB";
			table[1, 4] = "WK";
			table[8, 4] = "BK";
			table[1, 5] = "WQ";
			table[8, 5] = "BQ";
			table[1, 6] = "WB";
			table[8, 6] = "BB";
			table[1, 7] = "WKn";
			table[8, 7] = "BKn";
			table[1, 8] = "WR";
			table[8, 8] = "BR";
			button2.Enabled = false;
			button3.Enabled = false;
			
			this.Refresh();
		}
		
		void MainFormPaint(object sender, PaintEventArgs e)
		{
			paintTable(e);
			if(ok != 0)
			{
				paintPieces(e);
				if(sel && table[y, x] != "0")
					select(e);
			}
		}
		
		void paintTable(PaintEventArgs e)
		{
			int row, col;
			Pen marginPen = new Pen(Color.FromArgb(234, 219, 151), 3);
			SolidBrush blackRec = new SolidBrush(Color.FromArgb(183, 60, 23));
			SolidBrush whiteRec = new SolidBrush(Color.FromArgb(218, 218, 218));
			
			if(ok == 1)
			{
				e.Graphics.FillRectangle(blackRec, 50, 50, 600, 300);
				e.Graphics.FillRectangle(whiteRec, 50, 350, 600, 300);
			}
			if(ok == 2)
			{
				e.Graphics.FillRectangle(whiteRec, 50, 50, 600, 300);
				e.Graphics.FillRectangle(blackRec, 50, 350, 600, 300);
			}
			
			for(row=1; row<9; row++)
				for(col=1; col<9; col++)
				{
					if((row%2==0 && col%2==0) || (row%2!=0 && col%2!=0))
						e.Graphics.FillRectangle(whiteRec, 60 * col + 50, 60 * row + 50, 60, 60);
					else
						e.Graphics.FillRectangle(blackRec, 60 * col + 50, 60 * row + 50, 60, 60);
				}
			
			for(row=0; row<11; row++)
				e.Graphics.DrawLine(marginPen, 50, 60 * row + 50, 650, 60 * row + 50);
			for(col=0; col<11; col++)
				e.Graphics.DrawLine(marginPen, 60 * col + 50, 50, 60 * col + 50, 650);
		}
		
		void paintPieces(PaintEventArgs e)
		{
			int row, col;
			for(row=0; row<10; row++)
				for(col=0; col<10; col++)
					if(table[row, col] != "0")
						e.Graphics.DrawImage(Image.FromFile("Images/" + table[row, col] + ".png"), 60 * col + 55, 60 * row + 55, 50, 50);				
		}
		
		void select(PaintEventArgs e)
		{
			SolidBrush s = new SolidBrush(Color.FromArgb(170, 234, 219, 151));
			e.Graphics.FillRectangle(s, 60 * x + 52, 60 * y + 52, 57, 57);
			e.Graphics.DrawImage(Image.FromFile("Images/" + table[y, x] + ".png"), 60 * x + 55, 60 * y + 55, 50, 50);
			sel = false;
		}
		
		void MainFormMouseClick(object sender, MouseEventArgs e)
		{
			int row, col;
			String color="";
			if(ok == 1)
				color = "W";
			if(ok == 2)
				color = "B";
			if(ok!=0 && !sel)
			{
				for(row=0; row<10; row++)
					for(col=0; col<10; col++)
						if(e.X >= 60 * col + 55 && e.X <= 60 * col + 105 && e.Y >= 60 * row + 55 && e.Y <= 60 * row + 105)
						{
							x = col;
							y = row;
						}
				if(table[y, x].Substring(0, 1) == color)
				{
					sel = true;
					xs = x;
					ys = y;
				}
				
				if(valid())
				{
					table[y, x] = table[ys, xs];
					table[ys, xs] = "0";
				}
			}
			this.Refresh();
		}
		
		Boolean valid()
		{
			if(x >= 1 && x <= 8 && y >= 1 && y <= 8)
			{
				if(pMove() && table[ys, xs].Substring(1) == "P")
					return true;
				if(rMove() && table[ys, xs].Substring(1) == "R")
					return true;
				if(knMove() && table[ys, xs].Substring(1) == "Kn")
					return true;
				if(bMove() && table[ys, xs].Substring(1) == "B")
					return true;
				if(qMove() && table[ys, xs].Substring(1) == "Q")
					return true;
				if(kMove() && table[ys, xs].Substring(1) == "K")
					return true;
			}
			return false;
		}
		
		Boolean pMove()
		{
			String color="";
			if(ok == 1)
				color = "B";
			if(ok == 2)
				color = "W";
			if((x == xs && ((y == ys - 2 && ys == 7 && table[ys - 1, xs] == "0") || y == ys - 1) && table[y, x] == "0") || ((x == xs - 1 || x == xs + 1) && y == ys - 1 && table[y, x].Substring(0, 1) == color && table[y, x] != "0" && table[y, x] != color + "K"))
				return true;
			
			return false;
		}
		
		Boolean rMove()
		{
			int i;
			String color="";
			if(ok == 1)
				color = "B";
			if(ok == 2)
				color = "W";
			if(x == xs && y != ys && table[y, x] != color + "K")
			{
				i = ys;
				while(i != y)
				{
					if(y < ys)
						i--;
					if(y > ys)
						i++;
					if((table[i, xs] != "0" && table[i, xs].Substring(0, 1) != color) || (table[i, xs].Substring(0, 1) == color && i != y))
						return false;
				}
				return true;
			}
			if(x != xs && y == ys && table[y, x] != color + "K")
			{
				i = xs;
				while(i != x)
				{
					if(x < xs)
						i--;
					if(x > xs)
						i++;
					if((table[ys, i] != "0" && table[ys, i].Substring(0, 1) != color) || (table[ys, i].Substring(0, 1) == color && i != x))
						return false;
				}
				return true;
			}
			return false;
		}
		
		Boolean knMove()
		{
			String color="";
			if(ok == 1)
				color = "B";
			if(ok == 2)
				color = "W";
			if(table[y, x] != color + "K")
				if(((x == xs + 1 || x == xs - 1) && (y == ys - 2 || y == ys + 2)) || ((x == xs + 2 || x == xs - 2) && (y == ys - 1 || y == ys + 1)))
					return true;
			return false;
		}
		
		Boolean bMove()
		{
			int i, j;
			String color="";
			if(ok == 1)
				color = "B";
			if(ok == 2)
				color = "W";
			if(table[y, x] != color + "K" && (xs + ys == x + y || xs - x == ys - y))
			{
				i = ys;
				j = xs;
				while(i != y && j != x)
				{
					if(x < xs)
						j--;
					if(x > xs)
						j++;
					if(y < ys)
						i--;
					if(y > ys)
						i++;
					if((table[i, j] != "0" && table[i, j].Substring(0, 1) != color) || (table[i, j].Substring(0, 1) == color && i != y && j != x))
						return false;
				}
				if(x != xs)
					return true;
			}		
			return false;
		}
		
		Boolean qMove()
		{
			if(rMove() || bMove())
				return true;
			return false;
		}
		
		Boolean kMove()
		{
			if(x == xs + 1 || x == xs - 1 || x == xs)
			{
				if(y == ys + 1 || y == ys - 1)
					return true;
				if(x != xs && y == ys)
					return true;
			}
			return false;
		}
		
	}
}

Credits

Bogdan Bucur

Bogdan Bucur

1 project • 0 followers
Irinel Archip

Irinel Archip

1 project • 1 follower

Comments