Example #1
0
        public override Board Move(Board b, int toX, int toY)
        {
            Board board = GoDiePointers.DeepClone(b);

            if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
            {
                bool L = false;
                if (Math.Abs(PosX - toX) + Math.Abs(PosY - toY) == 3)
                {
                    L = true;
                }
                if (L && toX <= 7 && toY <= 7)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    board.WTurn = !board.WTurn;
                }
                else
                {
                    throw new Exception("Failure of knight move");
                }
            }
            else
            {
                throw new Exception("Knight can't move on own pieces");
            }
            return(board);
        }
Example #2
0
        public static void WritePieces(Board board)
        {
            Board b = GoDiePointers.DeepClone(board);

            Board.initBoard(b);
            //Might change fs name?
            FileStream   fs   = new FileStream(Path, FileMode.Create, FileAccess.Write, FileShare.None);
            StreamWriter sw   = new StreamWriter(fs);
            int          cval = 0;

            for (int i = 1; i <= 6; i++)
            {
                switch (i)
                {
                case 1: cval = GoDiePointers.DeepClone(b.Pieces[0, 3].CVal); sw.WriteLine("Queen"); break;    //Queen

                case 2: cval = GoDiePointers.DeepClone(b.Pieces[0, 0].CVal); sw.WriteLine("Rook"); break;     //Rook

                case 3: cval = GoDiePointers.DeepClone(b.Pieces[4, 4].CVal); sw.WriteLine("Empty"); break;    //Empty

                case 4: cval = GoDiePointers.DeepClone(b.Pieces[0, 2].CVal); sw.WriteLine("Bishop"); break;   //Bishop

                case 5: cval = GoDiePointers.DeepClone(b.Pieces[1, 3].CVal); sw.WriteLine("Pawn"); break;     //Pawn

                case 6: cval = GoDiePointers.DeepClone(b.Pieces[0, 1].CVal); sw.WriteLine("Knight"); break;   //Knight
                }
                sw.Write(Math.Abs(cval).ToString() + ' ');
                sw.WriteLine();
            }
            sw.Close(); fs.Close();
        }
Example #3
0
        //Need to feed through neurons
        public double Score(Board board, bool isW)
        {
            Piece[,] values = GoDiePointers.DeepClone(board.Pieces);
            foreach (Piece p in values)
            {
                if (p is Empty)
                {
                    continue;
                }
                //backwards
                if (p.Player.IsW != isW)
                {
                    p.CVal = -Math.Abs(p.CVal);
                }
                else
                {
                    p.CVal = Math.Abs(p.CVal);
                }
            }
            double score = 0;

            foreach (Neuron n in Neurons)
            {
                n.computeCVal(values);
                if (n.layer == 3)
                {
                    Output = n;
                }
            }
            score = Output.currentVal;
            return(score);
        }
Example #4
0
        public override Board Move(Board b, int toX, int toY)
        {
            Board board = GoDiePointers.DeepClone(b);

            if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
            {
                bool throughX = false, throughY = false;
                if (toX != PosX)
                {
                    for (int i = 1; i < Math.Abs(PosX - toX); i = Math.Abs(i) + 1)
                    {
                        if (toX < PosX)
                        {
                            i = i * -1;
                        }
                        if (!(board.Pieces[PosX + i, toY] is Empty))
                        {
                            throughX = true; break;
                        }
                    }
                }
                if (toY != PosY)
                {
                    for (int i = 1; i < Math.Abs(PosY - toY); i = Math.Abs(i) + 1)
                    {
                        if (toY < PosY)
                        {
                            i = i * -1;
                        }
                        if (!(board.Pieces[toX, PosY + i] is Empty))
                        {
                            throughY = true; break;
                        }
                    }
                }
                //Shift the legal + to outside of the loop for efficiency?
                if (!throughX && !throughY && ((toX <= LegalX && toY == PosY) || (toY <= LegalY && toX == PosX)))
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { toX, toY });
                    CanCastle   = false;
                    board.WTurn = !board.WTurn;
                }
                else
                {
                    throw new Exception("Failure of rook move");
                }
                if (throughX || throughY)
                {
                    throw new Exception("Rook can't move through pieces");
                }
            }
            else
            {
                throw new Exception("Rook can't move on own pieces");
            }
            return(board);
        }
Example #5
0
 public static Piece[,] AdjustFlip(Piece[,] p, bool isW)
 {
     Piece[,] pieces = GoDiePointers.DeepClone(p);
     if (!isW)
     {
         pieces = Flip(pieces);
     }
     return(pieces);
 }
Example #6
0
 public static Piece[,] Flip(Piece[,] p)
 {
     Piece[,] a2 = new Piece[8, 8];
     for (int i = 0; i <= 7; i++)
     {
         for (int ii = 0; ii <= 7; ii++)
         {
             a2[i, ii]      = GoDiePointers.DeepClone(p[7 - i, 7 - ii]);
             a2[i, ii].PosX = i; a2[i, ii].PosY = ii;
         }
     }
     return(a2);
 }
Example #7
0
        public override Board Move(Board b, int toX, int toY)
        {
            bool  rMove = true; bool bMove = true;
            Board board = GoDiePointers.DeepClone(b);

            try
            {
                Rook Qrook = new Rook(Player, PosX, PosY);
                board = Qrook.Move(board, toX, toY);
            }
            catch
            {
                rMove = false;
                try
                {
                    Bishop Qbish = new Bishop(Player, PosX, PosY);
                    board = Qbish.Move(board, toX, toY);
                }
                catch { bMove = false; }
            }
            finally
            {
                board = GoDiePointers.DeepClone(b);
                if (rMove)
                {
                    board.Pieces[PosX, PosY] = new Empty(PosX, PosY);
                    board.Pieces[toX, toY]   = new Queen(Player, toX, toY);
                }
                else
                {
                    if (bMove)
                    {
                        board.Pieces[PosX, PosY] = new Empty(PosX, PosY);
                        board.Pieces[toX, toY]   = new Queen(Player, toX, toY);
                    }
                    else
                    {
                        throw new Exception("QMove failure");
                    }
                }
            }
            board.WTurn = !board.WTurn;
            return(board);
        }
Example #8
0
        public static void Play(Board b)
        {
            Board  b2 = GoDiePointers.DeepClone(b);
            Player PW = new Player(true); NeuralNet NNW = new NeuralNet(PW, 3, 10);

            Data.ReadNs(NNW);
            Player PB = new Player(false); NeuralNet NNB = new NeuralNet(PB, 3, 10);

            Data.ReadNs(NNB);
            foreach (Piece p in b2.Pieces)
            {
                if (p is Empty)
                {
                    continue;
                }
                if (p.Player.IsW == true)
                {
                    p.Player = PW;
                }
                else
                {
                    p.Player = PB;
                }
            }
            List <Neuron> BestNeurons = GoDiePointers.DeepClone(NNW.Neurons);

            Random random             = new Random();

            //Amount of weights to change
            int changeCount = 5;

            for (int j = 0; j < changeCount; j++)
            {
                //For neurons
                //It increases/decreases the weight by rand.next(x, y)% [normally]
                //It currently is used as an input for the sigmoid (as a randomizing factor)
                double randomVal = random.Next(-14, 14);
                //For pieces
                double pieceRVal  = random.Next(1, 19) / 10.00;
                int    randNeuron = random.Next(0, NNW.Neurons.Count);
                int    randthing  = random.Next(1, 2);
                int    X          = random.Next(0, 7);
                int    Y          = random.Next(0, 7);
                try
                {
                    if (randthing == 1)
                    {
                        if (BestNeurons[randNeuron].layer == 0)
                        {
                            NNW.Neurons[randNeuron].weights[X, Y] =
                                Sigmoid.sigmoid(randomVal);
                        }
                        else
                        {
                            KeyValuePair <Neuron, double> kvp = NNW.Neurons[randNeuron].layWeights.ElementAt(random.Next(0, NNW.Neurons[randNeuron].layWeights.Count));
                            NNW.Neurons[randNeuron].layWeights[kvp.Key] =
                                Sigmoid.sigmoid(randomVal);
                        }
                    }
                    if (randthing == 2)
                    {
                        if (BestNeurons[randNeuron].layer == 0)
                        {
                            NNB.Neurons[randNeuron].weights[X, Y] = Sigmoid.sigmoid(randomVal);
                        }
                        else
                        {
                            KeyValuePair <Neuron, double> kvp = NNB.Neurons[randNeuron].layWeights.ElementAt(random.Next(0, NNB.Neurons[randNeuron].layWeights.Count));
                            NNB.Neurons[randNeuron].layWeights[kvp.Key] =
                                Sigmoid.sigmoid(randomVal);
                        }
                    }

                    /*
                     * Disabled for now
                     * also, it has a 50% chance of selecting the empty squares with the current x/y randomizer
                     * //Changing class values?
                     * if (randthing == 3)
                     * {
                     *  b.Pieces[X, Y].CVal = (int)(pieceRVal * (GoDiePointers.DeepClone(b.Pieces[X, Y].CVal)));
                     * }
                     * //Repeat to equalize chances of neuron vs piece
                     * if (randthing == 4)
                     * {
                     *  b2.Pieces[Y, X].CVal = (int)(pieceRVal * (GoDiePointers.DeepClone(b.Pieces[X, Y].CVal)));
                     * }
                     */
                }
                catch (Exception ex) { Console.WriteLine(ex); return; }
            }

            //At movecap, end playing, and write whoever had a higher score to the weight list file
            int moveCap = 100;
            int i       = 1;

            //While it has not moved too many times, and while no-one has won, play
            //Run in parallel?

            //Using two boards to allow for different piece cvals, unless I want to put that into the NN class?
            while (i <= moveCap && !b.WWin && !b.BWin && !b2.WWin && !b2.BWin && !b.Stale && !b2.Stale)
            {
                if (b.WTurn)
                {
                    b2.Pieces = NNW.Move(b, true).Pieces; Board.PrintBoard(b2); b2.WTurn = false; i++;
                }
                if (!b2.WTurn)
                {
                    b.Pieces = NNB.Move(b2, false).Pieces; Board.PrintBoard(b); b.WTurn = true; i++;
                }
                else
                {
                    Console.WriteLine("NN Failure"); break;
                }
            }
            //Will need to check whether pieces read/write properly in the future

            //If white won, write white's data
            if (b.WWin || b2.WWin) /*Data.WritePieces(b);*/ Data {
Example #9
0
        public override Board Move(Board b, int toX, int toY)
        {
            Board board = GoDiePointers.DeepClone(b);
            bool  move  = true;

            if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
            {
                //standard move
                if (toX == PosX + LegalX && toY == PosY && toX <= 7 && toY <= 7 && board.Pieces[toX, toY] is Empty)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    move        = false; enPass = false; twoStep = false;
                    board.WTurn = !board.WTurn;
                }
                //capture
                if (!(board.Pieces[toX, toY] is Empty) && move && (toY == PosY + 1 || toY == PosY - 1) && toX == PosX + LegalX && toX <= 7 && toY <= 7 &&
                    board.Pieces[toX, toY].Player.IsW != Player.IsW)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    move        = false; enPass = false; twoStep = false;
                    board.WTurn = !board.WTurn;
                }
                //twostep
                if (board.Pieces[toX, toY] is Empty && twoStep == true && toX == PosX + (2 * LegalX) && toY == PosY && toX <= 7 && toY <= 7 && move)
                {
                    if (board.Pieces[PosX + LegalX, PosY] is Empty)
                    {
                        board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                        PosX = toX; PosY = toY;
                        board.Pieces.SetValue(this, new int[] { PosX, PosY });
                        twoStep     = false; move = false; enPass = true;
                        board.WTurn = !board.WTurn;
                    }
                }
                //enpass
                if ((toY == PosY + 1 || toY == PosY - 1) && toX == PosX + LegalX && toX <= 7 && toY <= 7 && board.Pieces[toX - LegalX, toY] is Pawn &&
                    ((Pawn)board.Pieces[toX - LegalX, toY]).enPass && move && board.Pieces[toX - LegalX, toY].Player.IsW != Player.IsW && board.Pieces[toX, toY] is Empty)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    board.Pieces.SetValue(new Empty(toX - LegalX, toY), new int[] { toX - LegalX, toY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    move        = false; twoStep = false;
                    board.WTurn = !board.WTurn;
                }
                if (move)
                {
                    throw new Exception("Failure of pawn move");
                }
            }
            else
            {
                throw new Exception("Failure of pawn move");
            }
            //Promotion
            if (PosX == 7 || PosX == 0)
            {
                board.Pieces.SetValue(new Queen(Player, PosX, PosY), new int[] { PosX, PosY });
            }

            return(board);
        }
Example #10
0
        public override Board Move(Board b, int toX, int toY)
        {
            Board board = GoDiePointers.DeepClone(b);

            if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
            {
                //Castling
                //Does not work in the traditional way where you can't castle when in danger
                if (CanCastle && toX == PosX && toY == 2 || toY == 6)
                {
                    if (toY == 2)
                    {
                        if (board.Pieces[PosX, toY - 2] is Rook && ((Rook)board.Pieces[PosX, toY - 2]).CanCastle)
                        {
                            if (board.Pieces[PosX, toY + 1] is Empty && board.Pieces[PosX, toY - 1] is Empty && board.Pieces[PosX, toY] is Empty)
                            {
                                board.Pieces.SetValue(new Empty(PosX, toY - 2), new int[] { PosX, toY - 2 });        //Rook
                                board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });              //King
                                board.Pieces.SetValue(new King(Player, PosX, toY), new int[] { PosX, toY });         //King
                                board.Pieces.SetValue(new Rook(Player, PosX, toY + 1), new int[] { PosX, toY + 1 }); //Rook
                                board.WTurn = !board.WTurn;
                                return(board);
                            }
                            else
                            {
                                throw new Exception("Can't move through pieces");
                            }
                        }
                        else
                        {
                            throw new Exception("The rook can't castle");
                        }
                    }
                    if (toY == 6)
                    {
                        if (board.Pieces[PosX, toY + 1] is Rook && ((Rook)board.Pieces[PosX, toY + 1]).CanCastle)
                        {
                            if (board.Pieces[PosX, toY - 1] is Empty && board.Pieces[PosX, toY] is Empty)
                            {
                                board.Pieces.SetValue(new Empty(PosX, toY + 1), new int[] { PosX, toY + 1 });        //Rook
                                board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });              //King
                                board.Pieces.SetValue(new King(Player, PosX, toY), new int[] { PosX, toY });         //King
                                board.Pieces.SetValue(new Rook(Player, PosX, toY - 1), new int[] { PosX, toY - 1 }); //Rook
                                board.WTurn = !board.WTurn;
                                return(board);
                            }
                            else
                            {
                                throw new Exception("Can't move through pieces");
                            }
                        }
                        else
                        {
                            throw new Exception("The rook can't castle");
                        }
                    }
                }
                if ((toX == PosX + LegalX || toX == PosX - LegalX || toX == PosX) && (toY == PosY + LegalY || toY == PosY - LegalY || toY == PosY) && toX <= 7 && toY <= 7)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    CanCastle   = false;
                    board.WTurn = !board.WTurn;
                }
                else
                {
                    throw new Exception("Failure of king move");
                }
            }
            else
            {
                throw new Exception("Failure of king move");
            }
            return(board);
        }
Example #11
0
        public override Board Move(Board b, int toX, int toY)
        {
            //Unecessary?
            for (int i = -7; i <= 7; i++)
            {
                if (PosX + i == toX && PosY + i == toY)
                {
                    break;
                }
                if (PosX - i == toX && PosY + i == toY)
                {
                    break;
                }
                if (i == 7)
                {
                    throw new Exception("Failure of bishop move");
                }
            }
            Board board = GoDiePointers.DeepClone(b);

            if (board.Pieces[toX, toY] is Empty || board.Pieces[toX, toY].Player.IsW != Player.IsW)
            {
                bool throughPiece = false;
                int  xFactor = -1; int yFactor = -1;
                if (PosX < toX)
                {
                    xFactor = 1;
                }
                if (PosY < toY)
                {
                    yFactor = 1;
                }
                if ((Math.Abs(PosX - toX) + Math.Abs(PosY - toY)) % 2 == 0 && toX <= 7 && toY <= 7)
                {
                    for (int i = 1; i <= ((Math.Abs(PosX - toX) + Math.Abs(PosY - toY)) / 2) - 1; i = Math.Abs(i) + 1)
                    {
                        int ii = GoDiePointers.DeepClone(i);
                        i  = i * xFactor;
                        ii = ii * yFactor;
                        if (!(board.Pieces[PosX + i, PosY + ii] is Empty))
                        {
                            throughPiece = true;
                        }
                    }
                }
                else
                {
                    throughPiece = true; throw new Exception("Failure of bishop move");
                }
                if (throughPiece)
                {
                    throw new Exception("Can't move through pieces");
                }
                if ((Math.Abs(PosX - toX) + (PosY - toY)) % 2 == 0 && toX <= 7 && toY <= 7 && !throughPiece)
                {
                    board.Pieces.SetValue(new Empty(PosX, PosY), new int[] { PosX, PosY });
                    PosX = toX; PosY = toY;
                    board.Pieces.SetValue(this, new int[] { PosX, PosY });
                    board.WTurn = !board.WTurn;
                }
                else
                {
                    throw new Exception("Failure of bishop move");
                }
            }
            else
            {
                throw new Exception("Bishop can't move on own pieces");
            }
            return(board);
        }