Coder Social home page Coder Social logo

checkers's Introduction

checkers

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 checkers
{
    public partial class Form1 : Form
    {
        bool firstChoose = false;
        bool redTurn = true;
        bool turnFinished = false;
        int someValue = 1;
        int correctJump = 2;
        int correctJump_2 = 2;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // creating the playground
            int leftPosField = 40;
            int topPosField = 20;

            for (int rows = 0; rows < 8; rows++)
            {
                for (int cols = 0; cols < 8; cols++)
                {
                    FieldHolder.fieldMatrix[rows, cols] = new Button();
                    FieldHolder.fieldMatrix[rows, cols].Size = new System.Drawing.Size(70, 70);
                    FieldHolder.fieldMatrix[rows, cols].Text = "";
                    FieldHolder.fieldMatrix[rows, cols].Left = leftPosField;
                    FieldHolder.fieldMatrix[rows, cols].Top = topPosField;
                    // deleting borders
                    FieldHolder.fieldMatrix[rows, cols].TabStop = false;
                    FieldHolder.fieldMatrix[rows, cols].FlatStyle = FlatStyle.Flat;
                    FieldHolder.fieldMatrix[rows, cols].FlatAppearance.BorderSize = 0;
                    FieldHolder.fieldMatrix[rows, cols].FlatAppearance.BorderColor = Color.FromArgb(0, 255, 255, 255); //transparent
                    // even && even || odd && odd
                    if ((rows % 2 == 0 && cols % 2 == 0) || (rows % 2 == 1 && cols % 2 == 1))
                    {
                        FieldHolder.fieldMatrix[rows, cols].BackColor = Color.LightYellow;

                        Controls.Add(FieldHolder.fieldMatrix[rows, cols]);
                    }
                    // even && odd || odd && even
                    else
                    {
                        FieldHolder.fieldMatrix[rows, cols].BackColor = Color.Black;

                        Controls.Add(FieldHolder.fieldMatrix[rows, cols]);

                        if (rows == 0 || rows == 1 || rows == 2)
                        {

                            FieldHolder.fieldMatrix[rows, cols].Text = "o";
                            FieldHolder.fieldMatrix[rows, cols].ForeColor = Color.Red;
                            FieldHolder.fieldMatrix[rows, cols].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                            EmptyInfoHolder.emptyData[rows, cols] = false;

                        }
                        if (rows == 5 || rows == 6 || rows == 7)
                        {
                            FieldHolder.fieldMatrix[rows, cols].Text = "o";
                            FieldHolder.fieldMatrix[rows, cols].ForeColor = Color.White;
                            FieldHolder.fieldMatrix[rows, cols].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                            EmptyInfoHolder.emptyData[rows, cols] = false;
                            FieldHolder.fieldMatrix[rows, cols].Click += new System.EventHandler(this.white_Click);
                        }
                        if (EmptyInfoHolder.emptyData[rows, cols] == true)
                        {
                            FieldHolder.fieldMatrix[rows, cols].Click += new System.EventHandler(this.space_Click);
                        }
                    }
                    leftPosField += 70;
                }
                leftPosField = 40;
                topPosField += 70;
            }

        }

        private void white_Click(object sender, EventArgs e)
        {
            var myFigure = sender as Button;
            foreach (var item in FieldHolder.fieldMatrix)
            {
                if (item.BackColor == Color.DarkBlue)
                {
                    item.BackColor = Color.Black;
                }
            }
            FigureHolder.figure.Clear();
            FigureHolder.figure.Add(myFigure);
            FigureHolder.figure[0].BackColor = Color.DarkBlue;
            if (firstChoose == false)
            {
                firstChoose = true;
            }
        }
        private void space_Click(object obj, EventArgs eve)
        {
            var mySpace = obj as Button;
            for (int i = 0; i < 8;i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (mySpace == FieldHolder.fieldMatrix[i, j] && FieldHolder.fieldMatrix[(i +1),(j-1)].ForeColor == Color.Red)
                    {
                        correctJump = 0;
                        break;
                    }
                    if (mySpace == FieldHolder.fieldMatrix[i, j] && FieldHolder.fieldMatrix[(i + 1), (j + 1)].ForeColor == Color.Red)
                    {
                        correctJump_2 = 0;
                        break;
                    }
                }
            }
            if ((firstChoose == true && mySpace.Text != "o") && (((FigureHolder.figure[0].Top - mySpace.Top == 70) && (Math.Abs(mySpace.Left - FigureHolder.figure[0].Left) == 70)) || (FigureHolder.figure[0].Top - mySpace.Top == 140 && Math.Abs(mySpace.Left - FigureHolder.figure[0].Left) == 140 && (correctJump==0 || correctJump_2==0))))
            {

                mySpace.Text = "o";
                mySpace.ForeColor = Color.White;
                mySpace.Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));

                mySpace.Click += new System.EventHandler(this.white_Click);
                mySpace.Click -= new System.EventHandler(this.space_Click);
                FigureHolder.figure[0].Text = "";
                FigureHolder.figure[0].BackColor = Color.Black;
                FigureHolder.figure[0].Click -= new System.EventHandler(this.white_Click);
                FigureHolder.figure[0].Click += new System.EventHandler(this.space_Click);

                if (FigureHolder.figure[0].Top - mySpace.Top == 140 && Math.Abs(mySpace.Left - FigureHolder.figure[0].Left) == 140)
                {

                    if (mySpace.Left - FigureHolder.figure[0].Left == 140)
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            for (int j = 0; j < 8; j++)
                            {

                                if (mySpace == FieldHolder.fieldMatrix[i, j])
                                {

                                    int newRedDelY = i + 1;

                                    int newRedDelX = j - 1;
                                    FieldHolder.fieldMatrix[newRedDelY, newRedDelX].Text = "";
                                    FieldHolder.fieldMatrix[newRedDelY, newRedDelX].Click += new System.EventHandler(space_Click);
                                    someValue = 100;
                                    break;
                                }
                            }
                            if (someValue == 100)
                            {
                                break;
                            }
                        }
                    }
                    else
                    {
                        for (int i = 0; i < 8; i++)
                        {
                            for (int j = 0; j < 8; j++)
                            {

                                if (mySpace == FieldHolder.fieldMatrix[i, j])
                                {

                                    int newRedDelY = i + 1;
                                    int newRedDelX = j + 1;
                                
                                    FieldHolder.fieldMatrix[newRedDelY, newRedDelX].Text = "";
                                    FieldHolder.fieldMatrix[newRedDelY, newRedDelX].Click += new System.EventHandler(space_Click);
                                }
                            }
                        }
                    }
                }
                firstChoose = false;
                turnFinished = false;

                // moving red figures
                if (redTurn == true)
                {
                    
                    foreach (var item in FieldHolder.fieldMatrix)
                    {
                        for (int v = 0; v < 8; v++)
                        {
                            
                            for (int h = 0; h < 8; h++)
                            {
                                 
                                if (v < 6 && h < 6 && FieldHolder.fieldMatrix[v,h].Text == "o" && FieldHolder.fieldMatrix[v,h].ForeColor == Color.Red && (FieldHolder.fieldMatrix[(v+1),(h+1)].ForeColor== Color.White && FieldHolder.fieldMatrix[(v + 1), (h + 1)].Text == "o" && FieldHolder.fieldMatrix[(v+2),(h+2)].Text == "" ))
                                {
                                    
                                    FieldHolder.fieldMatrix[v, h].Text = "";
                                    FieldHolder.fieldMatrix[(v+2), (h + 2) ].Text = "o";
                                    FieldHolder.fieldMatrix[(v + 2), (h + 2)].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                                    FieldHolder.fieldMatrix[(v + 2), (h + 2)].ForeColor = Color.Red;
                                    FieldHolder.fieldMatrix[(v + 1), (h + 1)].Text = "";
                                    FieldHolder.fieldMatrix[(v + 1), (h + 1)].Click += new System.EventHandler(this.space_Click);
                                    FieldHolder.fieldMatrix[(v + 1), (h + 1)].Click -= new System.EventHandler(this.white_Click);
                                    FieldHolder.fieldMatrix[(v + 2), (h + 2)].Click -= new System.EventHandler(this.space_Click);

                                    FieldHolder.fieldMatrix[v, h].Click += new System.EventHandler(this.space_Click);
                                    turnFinished = true;
                                    break;
                                }
                                else if (h>=2 && v >=2 && v < 6 && h < 6 && FieldHolder.fieldMatrix[v, h].Text == "o" && FieldHolder.fieldMatrix[v, h].ForeColor == Color.Red && (FieldHolder.fieldMatrix[(v + 1), (h - 1)].ForeColor == Color.White && FieldHolder.fieldMatrix[(v + 1), (h - 1)].Text == "o" && FieldHolder.fieldMatrix[(v + 2), (h - 2)].Text == ""))
                                {

                                    FieldHolder.fieldMatrix[v, h].Text = "";
                                    FieldHolder.fieldMatrix[(v + 2), (h - 2)].Text = "o";
                                    FieldHolder.fieldMatrix[(v + 2), (h - 2)].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                                    FieldHolder.fieldMatrix[(v + 2), (h - 2)].ForeColor = Color.Red;
                                    FieldHolder.fieldMatrix[(v + 1), (h - 1)].Text = "";
                                    FieldHolder.fieldMatrix[(v + 1), (h - 1)].Click += new System.EventHandler(this.space_Click);
                                    FieldHolder.fieldMatrix[(v + 1), (h - 1)].Click -= new System.EventHandler(this.white_Click);
                                    FieldHolder.fieldMatrix[(v + 2), (h - 2)].Click -= new System.EventHandler(this.space_Click);

                                    FieldHolder.fieldMatrix[v, h].Click += new System.EventHandler(this.space_Click);
                                    turnFinished = true;
                                    break;
                                }
                                else if ((item == FieldHolder.fieldMatrix[v, h]) && FieldHolder.fieldMatrix[v, h].Text == "o" && FieldHolder.fieldMatrix[v, h].ForeColor == Color.Red && v != 7 && h != 7)
                                {
                                    int newV = v + 1;
                                    int newHRight = h + 1;
                                    if (newHRight == 8)
                                    {
                                        newHRight = 7;
                                    }
                                    //MessageBox.Show(Convert.ToString(v) + " " + Convert.ToString(h));

                                    if (FieldHolder.fieldMatrix[newV, newHRight].Text == "")
                                    {

                                        FieldHolder.fieldMatrix[v, h].Text = ""; 
                                        FieldHolder.fieldMatrix[newV, newHRight].Text = "o";
                                        FieldHolder.fieldMatrix[newV, newHRight].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                                        FieldHolder.fieldMatrix[newV, newHRight].ForeColor = Color.Red;

                                        FieldHolder.fieldMatrix[newV, newHRight].Click -= new System.EventHandler(this.space_Click);

                                        FieldHolder.fieldMatrix[v, h].Click += new System.EventHandler(this.space_Click);
                                        turnFinished = true;
                                        break;
                                    }


                                }
                                else if (v != 7 && h == 7 && item == FieldHolder.fieldMatrix[v, h] && FieldHolder.fieldMatrix[v, h].Text == "o" && FieldHolder.fieldMatrix[v, h].ForeColor == Color.Red)
                                {
                                    int newV = v + 1;

                                    int newHLeft = h - 1;
                                    if (newHLeft == 8)
                                    {
                                        newHLeft = 7;
                                    }
                                    //MessageBox.Show(Convert.ToString(v) + " " + Convert.ToString(h));
                                    if (FieldHolder.fieldMatrix[newV, newHLeft].Text == "")
                                    {
                                        FieldHolder.fieldMatrix[v, h].Text = "";
                                        FieldHolder.fieldMatrix[newV, newHLeft].Text = "o";
                                        FieldHolder.fieldMatrix[newV, newHLeft].Font = new System.Drawing.Font("Microsoft Sans Serif", 32F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(204)));
                                        FieldHolder.fieldMatrix[newV, newHLeft].ForeColor = Color.Red;

                                        FieldHolder.fieldMatrix[newV, newHLeft].Click -= new System.EventHandler(this.space_Click);

                                        FieldHolder.fieldMatrix[v, h].Click += new System.EventHandler(this.space_Click);
                                        turnFinished = true;
                                        break;
                                    }
                                }
                            }
                            if (turnFinished == true)
                            {
                                break;
                            }
                        }
                        if (turnFinished == true)
                        {
                            break;
                        }
                    }
                }
            }
        }
    }
    class FieldHolder
    {
        static public Button[,] fieldMatrix = new Button[8, 8];
    }
    class EmptyInfoHolder
    {
        static public bool[,] emptyData = new bool[8, 8] { { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true }, { true, true, true, true, true, true, true, true } };
    }
    class FigureHolder
    {
        static public List<Button> figure = new List<Button>();
    }
    class FigureLeftHolder
    {
        static public List<int> figureLeft = new List<int>();
    }
    class FigureTopHolder
    {
        static public List<int> figureTop = new List<int>();
    }
    class RandomNumHolder
    {
        static public List<int> randPosNum = new List<int>();
    }

checkers's People

Contributors

iq01660 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.