示例#1
0
    //private static int maxDepth = 4;
    private void getMove(Pices[,] p1, Pices p, int x, int y)
    {
        Moves m = new Moves(p, x, y);

        move = new List <Moves>();
        move = m.getMoves(p1);
    }
示例#2
0
    private void SelectPiece(int x, int y)
    {
        //out of bounds
        if (x < 0 || x >= 8 || y < 0 || y >= 8)
        {
            return;
        }

        Pices p = pices[x, y];

        if (p != null && p.isWhite == isWhite)
        {
            if (forcedPieces.Count == 0)
            {
                selectedPiece = p;
                startDrag     = mouseOver;
            }
            else
            {
                //look for the pice under our forced pices list
                if (forcedPieces.Find(fp => fp == p) == null)
                {
                    return;
                }
                selectedPiece = p;
                startDrag     = mouseOver;
            }
            // Debug.Log(selectedPiece.name);
        }
    }
示例#3
0
    } //getiing tile center position

    private void SpawnAllCehssPices()
    {
        MoveHistory      = new List <string>();
        activeChessPices = new List <GameObject>();
        piceses          = new Pices[8, 8];
        enPassant        = new int[2] {
            -1, -1
        };

        //--------Spawn white team
        //konge
        SpawnChessPices(0, 4, 0, piceOrientationWhite);

        //Droning
        SpawnChessPices(1, 3, 0, piceOrientationWhite);

        //Tårn
        SpawnChessPices(2, 0, 0, piceOrientationWhite);
        SpawnChessPices(2, 7, 0, piceOrientationWhite);

        //Løber
        SpawnChessPices(3, 2, 0, piceOrientationWhite);
        SpawnChessPices(3, 5, 0, piceOrientationWhite);

        //Hest
        SpawnChessPices(4, 1, 0, piceOrientationWhite);
        SpawnChessPices(4, 6, 0, piceOrientationWhite);

        //Bunde
        for (int i = 0; i < 8; i++)
        {
            SpawnChessPices(5, i, 1, piceOrientationWhite);
        }

        //--------Spawn black team
        //konge
        SpawnChessPices(6, 4, 7, piceOrientationBlack);

        //Droning
        SpawnChessPices(7, 3, 7, piceOrientationBlack);

        //Tårn
        SpawnChessPices(8, 0, 7, piceOrientationBlack);
        SpawnChessPices(8, 7, 7, piceOrientationBlack);

        //Løber
        SpawnChessPices(9, 2, 7, piceOrientationBlack);
        SpawnChessPices(9, 5, 7, piceOrientationBlack);

        //Hest
        SpawnChessPices(10, 1, 7, piceOrientationBlack);
        SpawnChessPices(10, 6, 7, piceOrientationBlack);

        //Bunde
        for (int i = 0; i < 8; i++)
        {
            SpawnChessPices(11, i, 6, piceOrientationBlack);
        }
    } //Spawining all piceses at the begining
示例#4
0
 public Moves(Pices p, int i, int j)
 {
     pice  = p;
     x     = i;
     y     = j;
     nextX = 0;
     nextY = 0;
 }
示例#5
0
 public Moves(Pices p, int i, int j, int ni, int nj)
 {
     pice  = p;
     x     = i;
     y     = j;
     nextX = ni;
     nextY = nj;
 }
示例#6
0
 public Moves()
 {
     pice  = null;
     x     = 0;
     y     = 0;
     nextX = 0;
     nextY = 0;
 }
示例#7
0
 public Field(Pices pice, bool anPassanCreating, bool anPassanAble, bool rokadeAble, bool firstMove, bool piceCapturedLastMove)
 {
     Pice = pice;
     AnPassan_creating    = anPassanCreating;
     AnPassan_able        = anPassanAble;
     Rokade_able          = rokadeAble;
     FirstMove            = firstMove;
     PiceCapturedLastMove = piceCapturedLastMove;
 }
        private void AddPiceToGrid(UniformGrid grid, Pices pice)
        {
            switch (pice)
            {
            case Pices.Non:

                break;

            case Pices.BlackKing:
                grid.Children.Add(BlackKing);
                break;

            case Pices.BlackQueen:
                grid.Children.Add(BlackQueen);
                break;

            case Pices.BlackBishop:
                grid.Children.Add(BlackBishop);
                break;

            case Pices.BlackKnight:
                grid.Children.Add(BlackKnight);
                break;

            case Pices.BlackRook:
                grid.Children.Add(BlackRook);
                break;

            case Pices.BlackPawn:
                grid.Children.Add(BlackPawn);
                break;

            case Pices.WhiteKing:
                grid.Children.Add(WhiteKing);
                break;

            case Pices.WhiteQueen:
                grid.Children.Add(WhiteQueen);
                break;

            case Pices.WhiteBishop:
                grid.Children.Add(WhiteBishop);
                break;

            case Pices.WhiteKnight:
                grid.Children.Add(WhiteKnight);
                break;

            case Pices.WhiteRook:
                grid.Children.Add(WhiteRook);
                break;

            case Pices.WhitePawn:
                grid.Children.Add(WhitePawn);
                break;
            }
        }
示例#9
0
    private void GeneratePiece(int x, int y)
    {
        bool       isWhitePicese = (y < 3) ? true:false;
        GameObject go            = Instantiate((isWhitePicese) ? whitePiecePrefab:blackPiecePrefab) as GameObject;

        go.transform.SetParent(transform);
        Pices p = go.GetComponent <Pices>();

        pices[x, y] = p;
        MovePiece(p, x, y);
    }
示例#10
0
    private bool Trymove2(int x1, int y1, int x2, int y2, Pices select, Pices[,] pices)
    {
        bool rightmove = false, Killed = false;

        select = pices[x1, y1];
        List <Pices> forcedPieces = ScanforPossibleMove(x1, y1, pices);
        //multiplayer support
        Vector2 startDrag = new Vector2(x1, y1);
        Vector2 endDrag   = new Vector2(x2, y2);


        //out of bounds
        if (x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8)
        {
            return(false);
            // if (select != null)
            //    rightmove = false;

            //let the piece be at the same position
        }
        //if the start drag and the stop drag is same
        if (select != null)
        {
            if (endDrag == startDrag)
            {
                rightmove = false;
            }
        }
        //check if its vaild move
        if (select.ValidMove(pices, x1, y1, x2, y2))
        {
            if (Mathf.Abs(x2 - x1) == 2)
            {
                Pices p = pices[(x1 + x2) / 2, (y1 + y2) / 2];
                if (p != null)
                {
                    Killed = true;
                }
            }

            if (forcedPieces.Count != 0 && !Killed)
            {
                rightmove = false;
            }

            rightmove = true;
        }
        else
        {
            rightmove = false;
        }
        return(rightmove);
    }
示例#11
0
    private List <Pices> ScanforPossibleMove(Pices p, int x, int y)
    {
        forcedPieces = new List <Pices>();

        if (pices[x, y].IsForceToMove(pices, x, y))
        {
            forcedPieces.Add(pices[x, y]);
        }


        return(forcedPieces);
    }
示例#12
0
    private string MakeMoveString(Pices c, int x, int y)
    {
        if (c.PicesLetter == 'K')
        {
            if (c.CurrentX == 4 && c.CurrentY == 0 && c.HasMoved == false && x == 2 && y == 0)
            {
                return("O-O-O-");
            }
            if (c.CurrentX == 4 && c.CurrentY == 7 && c.HasMoved == false && x == 2 && y == 7)
            {
                return("O-O-O+");
            }
            if (c.CurrentX == 4 && c.CurrentY == 0 && c.HasMoved == false && x == 6 && y == 0)
            {
                return("O-O---");
            }
            if (c.CurrentX == 4 && c.CurrentY == 7 && c.HasMoved == false && x == 6 && y == 7)
            {
                return("O-O+++");
            }
        }

        if (c != null)
        {
            string move = "";                          //initialising string
            move = move + c.PicesLetter.ToString();
            move = move + XKoordiantes[c.CurrentX, 0]; //adding start x koordianl letter to string
            move = move + (c.CurrentY + 1).ToString(); //adding start y koordinat to string
            move = move + XKoordiantes[x, 0];          //adding x koordianl letter to string
            move = move + (y + 1).ToString();          //adding y koordinat to string
            if (piceses[x, y] != null)
            {
                move = move + piceses[x, y].PicesLetter;
            }
            else if (enPassantMade)
            {
                move = move + "P";
            }
            else
            {
                move = move + "-";
            }
            return(move);
        }
        else
        {
            return("A mistake has been made: The pice used in MakeMoveString did not exist (handmade note)");
        }
    }
示例#13
0
    private void UpdatePieceDrag(Pices p)
    {
        if (!Camera.main)
        {
            Debug.Log("NO CAMERA");
            return;
        }

        RaycastHit hit;

        if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 25.0f, LayerMask.GetMask("Board")))
        {
            p.transform.position = hit.point + Vector3.up;
        }
    }
示例#14
0
    }     //initialising data

    private void Update()
    {
        UodateSelectionr(); //updating mouse position
        if (Input.GetMouseButtonDown(0))
        {
            if (selectionX >= 0 && selectionY >= 0) //if the mouse position is inside the board
            {
                //selecting or moving piece
                if (selectedPices == null)
                {
                    SelectPices(selectionX, selectionY);
                }
                else
                {
                    MovePices(selectionX, selectionY);
                }
            }
        } //when mouse clicked
        if (Input.GetKeyDown("p"))
        {
            List <string> Moveses = AllMoves();
            for (int i = 0; i < Moveses.Count; i++)
            {
                print(Moveses[i]);
            }
        } //find all moves
        if (Input.GetKeyDown("r"))
        {
            ReversMove();
        } //reverse button
        if (Input.GetKeyDown("n"))
        {
            Pices c = selectedPices.GetComponent <Pices>();
            print(c.CurrentX + ":" + c.CurrentY);
        } //Selected pice letter
        if (Input.GetKeyDown("h"))
        {
            foreach (string m in MoveHistory)
            {
                print(m);
            }
        } //Print history
        if (Input.GetKeyDown("l"))
        {
            print(MoveHistory.Count);
        }
    } //gameloop
示例#15
0
    private int calVal(Pices p, int i, int j)
    {
        int value;
        {
            int[,] tableWeight = { { 4, 0, 4, 0, 4, 0, 4, 0 },
                                   { 0, 4, 0, 3, 0, 3, 0, 3 },
                                   { 3, 0, 2, 0, 2, 0, 4, 0 },
                                   { 0, 4, 0, 2, 0, 1, 0, 3 },
                                   { 3, 0, 2, 0, 1, 0, 4, 0 },
                                   { 0, 4, 0, 2, 0, 2, 0, 3 },
                                   { 3, 0, 3, 0, 3, 0, 4, 0 },
                                   { 0, 4, 0, 4, 0, 4, 0, 4 } };


            if (p.isWhite == true)
            {
                if (i >= 0 && j <= 2)
                {
                    value = 7;
                }
                else
                {
                    value = 5;
                }
            }
            else
            {
                if (i >= 0 && j <= 5)
                {
                    value = 7;
                }
                else
                {
                    value = 5;
                }
            }
            return(value * tableWeight[i, j]);
        }
    }
示例#16
0
    } //gameloop

    private void SelectPices(int x, int y)
    {
        bool hasPosibleMoves = false; //used to not selecting

        if (piceses[x, y] == null)    //if there are no piece where cliked then nothing
        {
            return;
        }

        if (piceses[x, y].isWhite != isWhiteTurn) //if the pice are on oponent team do nothing
        {
            return;
        }

        allowedMoves = piceses[x, y].PossibleMove(); //pulling posible moves from piece
        //if there are no moves dount select the pice
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 8; j++)
            {
                if (allowedMoves[i, j])
                {
                    hasPosibleMoves = true;
                    break;
                }
            }
            if (hasPosibleMoves)
            {
                break;
            }
        } //testing all places for posible move
        if (!hasPosibleMoves)
        {
            return;
        } //if the peace has no move do nothing
        selectedPices = piceses[x, y]; //selecting piece
        BoardHeighlights.Instance.HighlightAllowedMoves(allowedMoves);  //heighlighting allowed moves
    }  //Selecting or reselecting piece
示例#17
0
    public List <string> AllMoves()
    {
        List <string> AllMoves = new List <string>();

        for (int i = 0; i < activeChessPices.Count; i++)
        {
            Pices c = activeChessPices[i].GetComponent <Pices>();
            if (c.isWhite == isWhiteTurn)
            {
                for (int k = 0; k < 8; k++)
                {
                    for (int n = 0; n < 8; n++)
                    {
                        if (c.PossibleMove()[k, n])
                        {
                            AllMoves.Add(MakeMoveString(c, k, n));
                        }
                    }
                }
            }
        }
        return(AllMoves);
    }
示例#18
0
    private void EndTurn()
    {
        int x = (int)endDrag.x;
        int y = (int)endDrag.y;

        //promotion
        if (selectedPiece != null)
        {
            if (selectedPiece.isWhite && !selectedPiece.isKing && y == 7)
            {
                selectedPiece.isKing = true;
                selectedPiece.transform.Rotate(Vector3.right * 180);
            }
            else if (!selectedPiece.isWhite && !selectedPiece.isKing && y == 0)
            {
                selectedPiece.isKing = true;
                selectedPiece.transform.Rotate(Vector3.right * 180);
            }
        }
        selectedPiece = null;
        startDrag     = Vector2.zero;

        if (ScanforPossibleMove(selectedPiece, x, y).Count != 0 && hasKilled)
        {
            return;
        }

        isWhiteTurn = !isWhiteTurn;
        isWhite     = !isWhite;
        if (!isWhiteTurn)
        {
            aiMove();
        }
        hasKilled = false;
        CheckVictory();
    }
示例#19
0
 public void MovePiece(Pices p, int x, int y)
 {
     p.transform.position = (Vector3.right * x) + (Vector3.forward * y) + offset + offset2;
 }
示例#20
0
    private void Trymove(int x1, int y1, int x2, int y2)
    {
        forcedPieces = ScanforPossibleMove();
        //multiplayer support
        startDrag     = new Vector2(x1, y1);
        endDrag       = new Vector2(x2, y2);
        selectedPiece = pices[x1, y1];

        //out of bounds
        if (x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8)
        {
            if (selectedPiece != null)
            {
                MovePiece(selectedPiece, x1, y1);
            }


            selectedPiece = null;
            startDrag     = Vector2.zero;
            return;
        }

        if (selectedPiece != null)
        {
            if (endDrag == startDrag)
            {
                MovePiece(selectedPiece, x1, y1);
                selectedPiece = null;
                startDrag     = Vector2.zero;
                return;
            }
        }
        //check if its vaild move
        if (selectedPiece.ValidMove(pices, x1, y1, x2, y2))
        {
            if (Mathf.Abs(x2 - x1) == 2)
            {
                Pices p = pices[(x1 + x2) / 2, (y1 + y2) / 2];
                if (p != null)
                {
                    pices[(x1 + x2) / 2, (y1 + y2) / 2] = null;
                    Destroy(p.gameObject);
                    hasKilled = true;
                }
            }

            if (forcedPieces.Count != 0 && !hasKilled)
            {
                MovePiece(selectedPiece, x1, y1);
                selectedPiece = null;
                startDrag     = Vector2.zero;
                return;
            }

            pices[x2, y2] = selectedPiece;
            pices[x1, y1] = null;
            MovePiece(selectedPiece, x2, y2);

            EndTurn();
        }
        else
        {
            MovePiece(selectedPiece, x1, y1);
            selectedPiece = null;
            startDrag     = Vector2.zero;
            return;
        }

        //  MovePiece(selectedPiece, x2, y2);
    }
示例#21
0
 public Field(Pices pice) : this(pice, false, false, false, false, false)
 {
 }
示例#22
0
 public Forced(Pices p, int i, int j)
 {
     forcedPice = p;
     x          = i;
     y          = j;
 }
示例#23
0
    private int FindBoardValue(bool isWhite)
    {
        List <GameObject> activeChessPices = Board_Manager.Instance.activeChessPices;
        int Value = 0;

        foreach (GameObject p in activeChessPices)
        {
            Pices c = p.GetComponent <Pices>();
            if (c.isWhite == isWhite)
            {
                if (c.GetType() == typeof(Konge))
                {
                    Value += 20;
                }
                else if (c.GetType() == typeof(Droning))
                {
                    Value += 9;
                }
                else if (c.GetType() == typeof(Tårn))
                {
                    Value += 5;
                }
                else if (c.GetType() == typeof(Løber))
                {
                    Value += 3;
                }
                else if (c.GetType() == typeof(Springer))
                {
                    Value += 3;
                }
                else if (c.GetType() == typeof(Bunde))
                {
                    Value += 1;
                }
                else if (c.PicesLetter == 'F')
                {
                    //print("Somthings Wrong, There was an _F_ in Minimax Value System");
                }
            }
            if (c.isWhite == !isWhite)
            {
                if (c.GetType() == typeof(Konge))
                {
                    Value -= 1000;
                }
                else if (c.GetType() == typeof(Droning))
                {
                    Value -= 9;
                }
                else if (c.GetType() == typeof(Tårn))
                {
                    Value -= 5;
                }
                else if (c.GetType() == typeof(Løber))
                {
                    Value -= 3;
                }
                else if (c.GetType() == typeof(Springer))
                {
                    Value -= 3;
                }
                else if (c.GetType() == typeof(Bunde))
                {
                    Value -= 1;
                }
                else if (c.PicesLetter == 'F')
                {
                    //print("Somthings Wrong, There was an _F_ in Minimax Value System");
                }
            }
        }
        test += 1;
        return(Value);
    }
示例#24
0
    public bool IsForceToMove(Pices [,] board, int x, int y)
    {
        if (isWhite || isKing)
        {
            //top left
            if (x >= 2 && y <= 5)
            {
                Pices p = board[x - 1, y + 1];
                //if there is a pices and it is not the same color as ours
                if (p != null && p.isWhite != isWhite)
                {
                    //check if its possible to land aftet the jump
                    if (board[x - 2, y + 2] == null)
                    {
                        return(true);
                    }
                }
            }
            //top right
            if (x <= 5 && y <= 5)
            {
                Pices p = board[x + 1, y + 1];
                //if there is a pices and it is not the same color as ours
                if (p != null && p.isWhite != isWhite)
                {
                    //check if its possible to land aftet the jump
                    if (board[x + 2, y + 2] == null)
                    {
                        return(true);
                    }
                }
            }
        }
        if (!isWhite || isKing)
        {
            //bottom left
            if (x >= 2 && y >= 2)
            {
                Pices p = board[x - 1, y - 1];
                //if there is a pices and it is not the same color as ours
                if (p != null && p.isWhite != isWhite)
                {
                    //check if its possible to land aftet the jump
                    if (board[x - 2, y - 2] == null)
                    {
                        return(true);
                    }
                }
            }
            //bottom right
            if (x <= 5 && y >= 2)
            {
                Pices p = board[x + 1, y - 1];
                //if there is a pices and it is not the same color as ours
                if (p != null && p.isWhite != isWhite)
                {
                    //check if its possible to land aftet the jump
                    if (board[x + 2, y - 2] == null)
                    {
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
示例#25
0
    public void MoveFromeString(string move)
    {
        int x = -1;
        int y = -1;

        if (move == "O-O---")
        {
            if (isWhiteTurn)
            {
                BoardSelection(4, 0);
                MovePices(6, 0);
                return;
            }
            else
            {
                print("Rokade not possible");
            }
        }
        else if (move == "O-O+++")
        {
            if (!isWhiteTurn)
            {
                BoardSelection(4, 7);
                MovePices(6, 7);
                return;
            }
            else
            {
                print("Rokade not possible");
            }
        }
        else if (move == "O-O-O-")
        {
            if (isWhiteTurn)
            {
                BoardSelection(4, 0);
                MovePices(2, 0);
                return;
            }
            else
            {
                print("Rokade not possible");
            }
        }
        else if (move == "O-O-O+")
        {
            if (!isWhiteTurn)
            {
                BoardSelection(4, 7);
                MovePices(2, 7);
                return;
            }
            else
            {
                print("Rokade not possible");
            }
        }

        else if (move.Length == 6)
        {
            y = int.Parse(move.Substring(2, 1)) - 1;

            for (int j = 0; j < 8; j++)
            {
                if (move.Substring(1, 1) == XKoordiantes[j, 0])
                {
                    x = int.Parse(XKoordiantes[j, 1]);
                }
            }

            for (int i = 0; i < activeChessPices.Count; i++)
            {
                Pices c = activeChessPices[i].GetComponent <Pices>();
                if (c.PicesLetter.ToString() == move.Substring(0, 1) || c.PicesLetter.ToString() == "F")     //F is when the revers EnPassent is made and the pawn letter hasent been initiatet yet
                {
                    if (c.CurrentX == x && c.CurrentY == y)
                    {
                        for (int j = 0; j < 8; j++)
                        {
                            if (move.Substring(3, 1) == XKoordiantes[j, 0])
                            {
                                x = int.Parse(XKoordiantes[j, 1]);
                            }
                        }
                        y = int.Parse(move.Substring(4, 1)) - 1;
                        BoardSelection(c.CurrentX, c.CurrentY);
                        MovePices(x, y);
                        return;
                    }
                }
            }
        }     //if a non pownd moves with more than one pice posability
    }
        public void Runner()
        {
            ChessCom.LookForMatchResult result = _connection.Call.LookForMatch(new ChessCom.UserIdentity {
                UserToken = _userToken
            });
            if (result.Succes)
            {
                _isSingelplayer            = false;
                Instance._matchToken       = result.MatchToken;
                Instance._opponentUsername = result.OpponentUsername;
                Instance.Dispatcher.Invoke(() => {
                    _mediaPlayer.Stop();
                    _mediaPlayer.Open(MatchStartSoundUri);
                    _mediaPlayer.Play();
                    StartMatch(result.IsWhitePlayer, result.MatchToken, result.Rules, result.Timerules);
                });

                var matchStream = _connection.Call.Match();
                _matchStream = matchStream;
                //Open match stream call
                _matchStream.RequestStream.WriteAsync(new ChessCom.MovePacket {
                    AskingForDraw = false, DoingMove = false, MatchToken = _matchToken, UserToken = _userToken
                });
                bool matchEnded = false;
                while (!matchEnded)
                {
                    if (!matchStream.ResponseStream.MoveNext().Result)
                    {
                        matchEnded = true;
                    }
                    if (matchEnded)
                    {
                        continue;
                    }
                    ChessCom.MoveResult move = matchStream.ResponseStream.Current;

                    //if (move.)
                    //{

                    //}

                    if (move.MoveHappned)
                    {
                        if (move.Move.AvailableMoves.Count > 0) //Prossess board from server
                        {
                            Instance.Dispatcher.Invoke(() => {
                                if (move.Move.From != _myLastMove.From || move.Move.To != _myLastMove.To)
                                {
                                    WriteTextNonInvoke("Opponent did move other!");
                                }
                                //Pices killedPice = _globalState.DoMoveTo(move.Move.To);
                                _globalState.UpdateFromMove(move.Move);
                                if ((Pices)move.Move.CapturedPice != Pices.Non)
                                {
                                    _killedPices.Add((Pices)move.Move.CapturedPice);
                                }
                                _lastMoveFrom = _globalState.CanSeeField(_clientIsPlayer, move.Move.From) ? move.Move.From : "";
                                _lastMoveTo   = _globalState.CanSeeField(_clientIsPlayer, move.Move.To) ? move.Move.To : "";
                                ClearBoard();
                                UpdateBoardFromGlobalState();

                                _mediaPlayer.Stop();
                                _mediaPlayer.Open(MoveSoundUri);
                                _mediaPlayer.Play();
                            });
                        }
                        else //Prossess board self
                        {
                            if (move.Move.From != _myLastMove.From || move.Move.To != _myLastMove.To)
                            {
                                Instance.Dispatcher.Invoke(() => {
                                    WriteTextNonInvoke("Opponent did move!");
                                    //messageBox.AppendText("Opponent did move!\n");
                                    //messageBox.CaretPosition = messageBox.CaretPosition.DocumentEnd;
                                    //messageBox.BringIntoView();
                                    //messageBox.Focus();
                                    _globalState.Selected = move.Move.From;
                                    Pices killedPice      = _globalState.DoMoveTo(move.Move.To);
                                    if (killedPice != Pices.Non)
                                    {
                                        _killedPices.Add(killedPice);
                                    }
                                    _globalState.Selected = null;
                                    _lastMoveFrom         = _globalState.CanSeeField(_clientIsPlayer, move.Move.From) ? move.Move.From : "";
                                    _lastMoveTo           = _globalState.CanSeeField(_clientIsPlayer, move.Move.To) ? move.Move.To : "";
                                    ClearBoard();
                                    UpdateBoardFromGlobalState();

                                    _mediaPlayer.Stop();
                                    _mediaPlayer.Open(MoveSoundUri);
                                    _mediaPlayer.Play();
                                });
                            }
                        }

                        if (_globalState.WhiteTurn)
                        {
                            _timer?.Stop();
                            //WriteTextInvoke("Black time before: " + _blackStaticTimeLeft.ToString(@"mm\:ss") + $" secSpent {move.Move.SecSpent} secs left {move.SecsLeft}" );
                            _blackStaticTimeLeft = TimeSpan.FromSeconds(move.SecsLeft);
                            _blackTimeSpan       = _blackStaticTimeLeft;
                            //WriteTextInvoke("Black time after: " + _blackStaticTimeLeft.ToString(@"mm\:ss"));
                            Instance.Dispatcher.Invoke(() => { lbBlackTimeLeft.Content = _blackTimeSpan.ToString(@"mm\:ss"); });
                            _timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
                            {
                                lbWhiteTimeLeft.Content = _whiteTimeSpan.ToString(@"mm\:ss");
                                if (_whiteTimeSpan == TimeSpan.Zero)
                                {
                                    _timer.Stop();
                                    InvokedRanoutOfTime();
                                }
                                else if (_whiteTimeSpan < TimeSpan.FromSeconds(10))
                                {
                                    _mediaPlayerTimeRunningOut.Stop();
                                    _mediaPlayerTimeRunningOut.Play();
                                }
                                _whiteTimeSpan = _whiteTimeSpan.Add(TimeSpan.FromSeconds(-1));
                            }, Application.Current.Dispatcher);

                            _timer.Start();
                        }
                        else
                        {
                            _timer?.Stop();
                            _whiteStaticTimeLeft = TimeSpan.FromSeconds(move.SecsLeft);
                            _whiteTimeSpan       = _whiteStaticTimeLeft;
                            Instance.Dispatcher.Invoke(() => { lbWhiteTimeLeft.Content = _whiteTimeSpan.ToString(@"mm\:ss"); });
                            _timer = new DispatcherTimer(new TimeSpan(0, 0, 1), DispatcherPriority.Normal, delegate
                            {
                                lbBlackTimeLeft.Content = _blackTimeSpan.ToString(@"mm\:ss");
                                if (_blackTimeSpan == TimeSpan.Zero)
                                {
                                    _timer.Stop();
                                    InvokedRanoutOfTime();
                                }
                                else if (_blackTimeSpan < TimeSpan.FromSeconds(10))
                                {
                                    _mediaPlayerTimeRunningOut.Stop();
                                    _mediaPlayerTimeRunningOut.Play();
                                }
                                _blackTimeSpan = _blackTimeSpan.Add(TimeSpan.FromSeconds(-1));
                            }, Application.Current.Dispatcher);

                            _timer.Start();
                        }
                    }
                    if (move.OpponentAskingForDraw)
                    {
                        Instance.Dispatcher.Invoke(() =>
                        {
                            string popupText        = "Draw?";
                            string textBoxText      = "Opponent is asking for draw. Do you accept?";
                            MessageBoxButton button = MessageBoxButton.YesNo;
                            MessageBoxImage icon    = MessageBoxImage.Error;
                            var drawResult          = MessageBox.Show(textBoxText, popupText, button, icon);
                            if (drawResult == MessageBoxResult.Yes)
                            {
                                _matchStream.RequestStream.WriteAsync(new ChessCom.MovePacket {
                                    AskingForDraw = true
                                });
                                matchEnded = true;
                            }
                        });
                    }
                    else if (move.MatchEvent == ChessCom.MatchEvent.UnexpectedClosing)
                    {
                        Instance.Dispatcher.Invoke(() =>
                        {
                            string popupText   = "UnexpextedClosing";
                            string textBoxText = "Opponents client unexpectedly closed";
                            WriteTextNonInvoke(textBoxText);
                            MessageBoxButton button = MessageBoxButton.OK;
                            MessageBoxImage icon    = MessageBoxImage.Exclamation;
                            MessageBox.Show(textBoxText, popupText, button, icon);
                            EndOfMatch(_clientIsPlayer == ClientIsPlayer.White?ChessCom.MatchEvent.WhiteWin: ChessCom.MatchEvent.BlackWin);
                            matchEnded = true;
                        });
                    }
                    else if (move.MatchEvent == ChessCom.MatchEvent.WhiteWin || move.MatchEvent == ChessCom.MatchEvent.BlackWin)
                    {
                        Instance.Dispatcher.Invoke(() =>
                        {
                            EndOfMatch(move.MatchEvent);
                            matchEnded = true;
                        });
                        _matchStream.RequestStream.WriteAsync(new ChessCom.MovePacket {
                            CheatMatchEvent = ChessCom.MatchEvent.ExpectedClosing, AskingForDraw = false, DoingMove = false, MatchToken = _matchToken, UserToken = _userToken
                        }).Wait();
                        _matchStream.RequestStream.CompleteAsync();
                    }
                }
                _matchStream = null;
            }
            else
            {
                string           popupText   = "Failed";
                string           textBoxText = "Failed to find match";
                MessageBoxButton button      = MessageBoxButton.OK;
                MessageBoxImage  icon        = MessageBoxImage.Error;
                MessageBox.Show(textBoxText, popupText, button, icon);
            }
        }
示例#27
0
    public bool ValidMove(Pices [,] board, int x1, int y1, int x2, int y2)
    {
        if (x2 < 0 || x2 >= 8 || y2 < 0 || y2 >= 8)

        {
            return(false);
        }
        //if you are moving on top of another piece
        if (board[x2, y2] != null)
        {
            return(false);
        }
        int deltaMove  = (int)Mathf.Abs(x1 - x2);
        int deltaMoveY = y2 - y1;

        if (isWhite || isKing)
        {
            if (deltaMove == 1)
            {
                if (deltaMoveY == 1)
                {
                    return(true);
                }
            }
            else if (deltaMove == 2)
            {
                if (deltaMoveY == 2)
                {
                    Pices p = board[(x1 + x2) / 2, (y1 + y2) / 2];
                    if (p != null && p.isWhite != isWhite)
                    {
                        return(true);
                    }
                }
            }
        }

        if (!isWhite || isKing)
        {
            if (deltaMove == 1)
            {
                if (deltaMoveY == -1)
                {
                    return(true);
                }
            }
            else if (deltaMove == 2)
            {
                if (deltaMoveY == -2)
                {
                    Pices p = board[(x1 + x2) / 2, (y1 + y2) / 2];
                    if (p != null && p.isWhite != isWhite)
                    {
                        return(true);
                    }
                }
            }
        }

        return(false);
    }
示例#28
0
    private float FindDiff(string move, bool yourKing, bool isWhite)
    {
        Pices c = Board_Manager.Instance.activeChessPices[0].GetComponent <Pices>();
        Pices check;
        float Diff;
        float nowX   = 0;
        float nowY   = 0;
        float movedX = 0;
        float movedY = 0;

        if (move == "O-O-O-" || move == "O-O-O+" || move == "O-O---" || move == "O-O+++")
        {
            return(0);
        }

        foreach (GameObject p in Board_Manager.Instance.activeChessPices)
        {
            check = p.GetComponent <Pices>();
            if (yourKing && isWhite && check.PicesLetter == 'K' && !check.isWhite || !yourKing && !isWhite && check.PicesLetter == 'K' && !check.isWhite)
            {
                c = check;
            }
            else if (!yourKing && isWhite && check.PicesLetter == 'K' && check.isWhite || yourKing && !isWhite && check.PicesLetter == 'K' && check.isWhite)
            {
                c = check;
            }
        }

        float kingX = (float)c.CurrentX;
        float kingY = (float)c.CurrentY;


        for (int j = 0; j < 8; j++)
        {
            if (move.Substring(1, 1) == Board_Manager.Instance.XKoordiantes[j, 0])
            {
                nowX = float.Parse(Board_Manager.Instance.XKoordiantes[j, 1]) + 1f;
            }
        }
        nowY = float.Parse(move.Substring(2, 1));

        for (int j = 0; j < 8; j++)
        {
            if (move.Substring(3, 1) == Board_Manager.Instance.XKoordiantes[j, 0])
            {
                movedX = float.Parse(Board_Manager.Instance.XKoordiantes[j, 1]) + 1f;
            }
        }
        movedY = float.Parse(move.Substring(4, 1));

        Diff = Mathf.Sqrt(Mathf.Pow(nowX - kingX, 2f) + Mathf.Pow(nowY - kingY, 2f)) - Mathf.Sqrt(Mathf.Pow(movedX - kingX, 2f) + Mathf.Pow(movedY - kingY, 2f));

        if (move.Substring(5, 1) == "K" && WinOrientedWhite && isWhite || !isWhite && WinOrientedBlack && move.Substring(5, 1) == "K")
        {
            return(100);
        }

        if (move.Substring(5, 1) != "-" && AgressiveBlack && !isWhite || move.Substring(5, 1) != "-" && AgressiveWhite && isWhite)
        {
            return(FindGain(move.Substring(5, 1)));
        }


        if (Diff <= 0)
        {
            return(0);
        }

        return(Diff);
    }
示例#29
0
    }  //Selecting or reselecting piece

    private void MovePices(int x, int y)
    {
        enPassantMade = false;

        if (allowedMoves[x, y])
        {
            Pices c = piceses[x, y];

            if (c != null && c.isWhite != isWhiteTurn)
            {
                //Removing pices from game
                activeChessPices.Remove(c.gameObject);
                Destroy(c.gameObject);

                if (c.GetType() == typeof(Konge))
                {
                    if (!isMinimaxing)
                    {
                        //win and end game
                        GameEnded = true;
                        EndGame(true);
                        return;
                    }
                }
            } //if there is an oponnent piece on place kill it

            if (x == enPassant[0] && y == enPassant[1])
            {
                if (isWhiteTurn)
                {
                    c = piceses[x, y - 1];
                }
                else
                {
                    c = piceses[x, y + 1];
                }
                activeChessPices.Remove(c.gameObject);
                Destroy(c.gameObject);
                enPassantMade = true;
            }//destry if enPassan was made
            enPassant[0] = -1;
            enPassant[1] = -1;

            if (!MiniMaxing)
            {
                if (selectedPices.GetType() == typeof(Bunde))
                {
                    if (y == 7)
                    {
                        activeChessPices.Remove(selectedPices.gameObject); //removing pawn from active piceses
                        Destroy(selectedPices.gameObject);                 //removing pawn from game
                        SpawnChessPices(1, x, y, piceOrientationWhite);    //Spawning rook in game
                        selectedPices = piceses[x, y];                     //selecting rook
                    } //white promotion

                    if (y == 0)
                    {
                        activeChessPices.Remove(selectedPices.gameObject);
                        Destroy(selectedPices.gameObject);
                        SpawnChessPices(7, x, y, piceOrientationBlack);
                        selectedPices = piceses[x, y];
                    } //black promotion

                    if (selectedPices.CurrentY == 1 && y == 3)
                    {
                        enPassant[0] = x;
                        enPassant[1] = y - 1;
                    } //saving pawn move for enPassant
                    else if (selectedPices.CurrentY == 6 && y == 4)
                    {
                        enPassant[0] = x;
                        enPassant[1] = y + 1;
                    } //saving pawn move for enPassant
                }     //Promotion
            }
            if (selectedPices.GetType() == typeof(Konge) && !selectedPices.HasMoved) //Rokade
            {
                if (x == 2 && y == 0) //white long Rokade
                {
                    MoveHistory.Add(MakeMoveString(selectedPices, x, y));
                    //Moving king
                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(x, y);
                    selectedPices.SetPosition(x, y);
                    piceses[x, y]          = selectedPices;
                    selectedPices.HasMoved = true;
                    //Moving Rook
                    selectedPices = piceses[0, 0];

                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(3, 0);
                    selectedPices.SetPosition(3, 0);
                    piceses[3, 0]          = selectedPices;
                    selectedPices.HasMoved = true;
                    isWhiteTurn            = !isWhiteTurn;
                    BoardHeighlights.Instance.HideHighlights();
                    selectedPices = null;

                    return;
                }
                else if (x == 6 && y == 0) //white short Rokade
                {
                    MoveHistory.Add(MakeMoveString(selectedPices, x, y));
                    //Moving king
                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(x, y);
                    selectedPices.SetPosition(x, y);
                    piceses[x, y]          = selectedPices;
                    selectedPices.HasMoved = true;
                    //Moving Rook
                    selectedPices = piceses[7, 0];

                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(5, 0);
                    selectedPices.SetPosition(5, 0);
                    piceses[5, 0]          = selectedPices;
                    selectedPices.HasMoved = true;
                    isWhiteTurn            = !isWhiteTurn;
                    BoardHeighlights.Instance.HideHighlights();
                    selectedPices = null;
                    return;
                }
                else if (x == 6 && y == 7) //black short Rokade
                {
                    MoveHistory.Add(MakeMoveString(selectedPices, x, y));
                    //Moving king
                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(x, y);
                    selectedPices.SetPosition(x, y);
                    piceses[x, y]          = selectedPices;
                    selectedPices.HasMoved = true;
                    //Moving Rook
                    selectedPices = piceses[7, 7];

                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(5, 7);
                    selectedPices.SetPosition(5, 7);
                    piceses[5, 7]          = selectedPices;
                    selectedPices.HasMoved = true;
                    isWhiteTurn            = !isWhiteTurn;
                    BoardHeighlights.Instance.HideHighlights();
                    selectedPices = null;
                    return;
                }
                else if (x == 2 && y == 7) //black long Rokade
                {
                    MoveHistory.Add(MakeMoveString(selectedPices, x, y));
                    //Moving king
                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(x, y);
                    selectedPices.SetPosition(x, y);
                    piceses[x, y]          = selectedPices;
                    selectedPices.HasMoved = true;
                    //Moving Rook
                    selectedPices = piceses[0, 7];

                    piceses[selectedPices.CurrentX, selectedPices.CurrentY] = null;
                    selectedPices.transform.position = GetTileCenter(3, 7);
                    selectedPices.SetPosition(3, 7);
                    piceses[3, 7]          = selectedPices;
                    selectedPices.HasMoved = true;
                    isWhiteTurn            = !isWhiteTurn;
                    BoardHeighlights.Instance.HideHighlights();
                    selectedPices = null;
                    return;
                }
            } //Rokade

            //moving piece and chaning turn
            MoveHistory.Add(MakeMoveString(selectedPices, x, y));
            piceses [selectedPices.CurrentX, selectedPices.CurrentY] = null;
            selectedPices.transform.position = GetTileCenter(x, y);
            selectedPices.SetPosition(x, y);
            piceses [x, y]         = selectedPices;
            selectedPices.HasMoved = true;


            //did pice make a check
            if (selectedPices.isWhite)
            {
                if (selectedPices.Check() != blackCheck)
                {
                    blackCheck = !blackCheck;
                }
            }
            else
            {
                if (selectedPices.Check() != whiteCheck)
                {
                    whiteCheck = !whiteCheck;
                }
            }
            isWhiteTurn = !isWhiteTurn;
        }


        BoardHeighlights.Instance.HideHighlights();
        selectedPices = null;
    } //Moving Pice