public static bool HorizontalThreatCheck(int kingX, int kingY, Dictionary <int, Dictionary <int, ChessPiece> > scenario, PieceColor enemy)
        {
            for (int i = 1; i <= 8; i++)
            {
                if (HelperMaths.IsInRange(kingY + i, 0, 7))
                {
                    if (scenario[kingX][kingY + i] != null)
                    {
                        if (scenario[kingX][kingY + i].ToString() == enemy.ToString() + PieceType.Rook.ToString() ||
                            scenario[kingX][kingY + i].ToString() == enemy.ToString() + PieceType.Queen.ToString())
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            for (int i = 1; i <= 8; i++)
            {
                if (HelperMaths.IsInRange(kingY - i, 0, 7))
                {
                    if (scenario[kingX][kingY - i] != null)
                    {
                        if (scenario[kingX][kingY - i].ToString() == enemy.ToString() + PieceType.Rook.ToString() ||
                            scenario[kingX][kingY - i].ToString() == enemy.ToString() + PieceType.Queen.ToString())
                        {
                            return(true);
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                else
                {
                    break;
                }
            }

            return(false);
        }
Beispiel #2
0
        private void NewGame(object sender, EventArgs e)
        {
            ResetColors();
            ResetCoordinates();
            ResetPieceImages();

            playerTurn   = PieceColor.White;
            lblTurn.Text = playerTurn.ToString() + " Turn";

            List <string> piecesCodifications = ReadInitialSetup();
            PieceFactory  pieceFactory        = new PieceFactory();

            board = new Board();

            for (int y = 0; y < 10; y++)
            {
                for (int x = 0; x < 10; x++)
                {
                    string pieceCodification = piecesCodifications[y * 10 + x];
                    Point  position          = new Point(x, y);
                    board.squares[y, x] = new Square(pieceFactory.CreatePiece(pieceCodification, position));

                    if (board.squares[y, x].piece != null)
                    {
                        ((PictureBox)chessBoardPanel.Controls[y * 10 + x]).Image = board.squares[y, x].piece.Image;
                    }
                }
            }
            //AiTimer();
        }
Beispiel #3
0
        .First().Pieces;                              // Hämta pjäserna från spelaren.

        /// <summary>
        /// Adds a player to a game which is present in memory.
        /// </summary>
        /// <param name="player">Instance of Player.</param>
        /// <param name="color">Identifies the player and sets piece color.</param>
        public void AddPlayer(PieceColor color)
        {
            Player player = new Player(Players.Count + 1);

            if (!colors.Contains(color))
            {
                throw new NotSupportedException($"Color '{color.ToString()}' already selected.");
            }

            // correctionFactor förskjuts med 10 för varje spelare.
            int correctionFactor = Players.Count * 10;

            for (int i = 0; i < 4; i++)
            {
                player.Pieces.Add(new Piece(color, i + 1, 0, correctionFactor));
            }
            colors.Remove(color);

            if (Players.Count > 3)
            {
                throw new NotSupportedException($"Between 2 and 4 participants required. Participant count: {Players.Count}");
            }
            else
            {
                Players.Add(player);
            }
        }
 public ChessPiece(PieceColor color, PieceType type)
 {
     AllowDrop = true;
     _color    = color;
     _type     = type;
     moveCount = 0;
     if (Color == PieceColor.Black)
     {
         keyLetter = ChessBoard.BlackKeyLetter;
         keyNumber = ChessBoard.BlackKeyNumber;
     }
     else
     {
         keyLetter = ChessBoard.WhiteKeyLetter;
         keyNumber = ChessBoard.WhiteKeyNumber;
     }
     movableTiles    = new List <ChessBoardTile>();
     capturableTiles = new List <ChessBoardTile>();
     Size            = new System.Drawing.Size(60, 60);
     Image           = System.Drawing.Image.FromFile("../../images/" + color.ToString() + type.ToString() + ".png");
     SizeMode        = PictureBoxSizeMode.StretchImage;
     MouseDown      += ChessPiece_MouseDown;
     MouseHover     += ChessPiece_MouseHover;
     DragOver       += ChessPiece_DragOver;
     DragDrop       += ChessPiece_DragDrop;
 }
Beispiel #5
0
 /// <summary>
 /// Method that asks the player to select a piece to play
 /// </summary>
 public void ShowSelectPieceText(PieceColor color, int id)
 {
     Console.ForegroundColor = ConsoleColor.White;
     Console.WriteLine();
     Console.WriteLine($"Player {id}, with color {color.ToString()} is playing.");
     Console.WriteLine("Choose the piece you want to play from 1-6.");
     Console.WriteLine();
 }
        //-----------------------------------------------------------------

        // King check functions

        //-----------------------------------------------------------------

        public static bool EnemyPawnCheck(int kingX, int kingY, Dictionary <int, Dictionary <int, ChessPiece> > scenario, PieceColor enemy)
        {
            if (enemy == PieceColor.Black)
            {
                if (HelperMaths.IsInRange(kingX - 1, 0, 7) && HelperMaths.IsInRange(kingY - 1, 0, 7))
                {
                    if (scenario[kingX - 1][kingY - 1]?.ToString() == enemy.ToString() + "Pawn")
                    {
                        return(true);
                    }
                }

                if (HelperMaths.IsInRange(kingX - 1, 0, 7) && HelperMaths.IsInRange(kingY + 1, 0, 7))
                {
                    if (scenario[kingX - 1][kingY + 1]?.ToString() == enemy.ToString() + "Pawn")
                    {
                        return(true);
                    }
                }
            }

            if (enemy == PieceColor.White)
            {
                if (HelperMaths.IsInRange(kingX + 1, 0, 7) && HelperMaths.IsInRange(kingY - 1, 0, 7))
                {
                    if (scenario[kingX + 1][kingY - 1]?.ToString() == enemy.ToString() + "Pawn")
                    {
                        return(true);
                    }
                }

                if (HelperMaths.IsInRange(kingX + 1, 0, 7) && HelperMaths.IsInRange(kingY + 1, 0, 7))
                {
                    if (scenario[kingX + 1][kingY + 1]?.ToString() == enemy.ToString() + "Pawn")
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
Beispiel #7
0
        void HandleTurn(bool moved, bool pieceEaten)
        {
            if (!moved)
            {
                return;
            }

            if (!pieceEaten)
            {
                turn = turn == PieceColor.Red ? PieceColor.Black : PieceColor.Red;
            }

            Console.WriteLine("It is now {0}'s turn", turn.ToString());
        }
Beispiel #8
0
        public void SelectSquare(string squareName)
        {
            var NewSelection = SquareUtils.GetSquareFromName(squareName);

            Window.UpdateWarning("");

            if (CurrentSelectedSquare == null) // no preselection
            {
                if (NewSelection.Piece != null && NewSelection.Piece.Color != CurrentPlayer)
                {
                    Window.UpdateWarning("Piece cannot move this turn");
                    return;
                }

                Window.SelectSquare(NewSelection.Name);
                CurrentSelectedSquare = NewSelection;
                if (NewSelection.Piece != null)
                {
                    Window.HighlightSquares(NewSelection.Piece.GetValidMoves().Select(x => x.Name).ToArray());
                }
            }
            else if (CurrentSelectedSquare.Name != NewSelection.Name) // found preselection
            {
                if (CurrentSelectedSquare.Piece != null)
                {
                    Window.UnHighlightSquares(CurrentSelectedSquare.Piece.GetValidMoves().Select(x => x.Name).ToArray());
                }

                if (CurrentSelectedSquare.Piece == null)
                {
                    Window.UnselectSquare(CurrentSelectedSquare.Name);
                    Window.SelectSquare(NewSelection.Name);
                    CurrentSelectedSquare = NewSelection;
                }
                else if (CurrentSelectedSquare.Piece.GetValidMoves().Where(x => x.Name == NewSelection.Name).Count() > 0)
                {
                    //move
                    MovePiece(CurrentSelectedSquare, NewSelection, CurrentSelectedSquare.Piece);
                    CurrentSelectedSquare = null;
                    CurrentPlayer         = CurrentPlayer == PieceColor.White ? PieceColor.Black : PieceColor.White;
                    Window.UpdateCurrentPlayerLabel(CurrentPlayer.ToString());
                }
                else
                {
                    Window.UnselectSquare(CurrentSelectedSquare.Name);
                    CurrentSelectedSquare = null;
                }
            }
        }
    private void UpdatePosition(PieceColor color, float distance, int index)
    {
        string     nameOfSquare = ((PieceSquares)index).ToString();
        PieceColor oldColor     = positions[index].pieceColor;

        positions[index]            = new PieceInfo(DateTime.UtcNow, color, distance);
        pieces[index].Item2.MyColor = color;
        if (color != PieceColor.None)
        {
            pieces[index].Item1.enabled = true;
            AutonomousGazeBehavior.Instance?.EventPieceMoved(nameOfSquare);
        }
        else
        {
            pieces[index].Item1.enabled = false;
        }
        if (color == PieceColor.None)
        {
            Dbg.Log(LogMessageType.PIECE_LOST, new List <string>()
            {
                nameOfSquare, oldColor.ToString()
            });
        }
        else
        {
            Dbg.Log(LogMessageType.PIECE_FOUND, new List <string>()
            {
                nameOfSquare, color.ToString()
            });
        }


        ////Transform the board positions into a matrix that is more efficient to perform calculations on
        //Vector4 fourthColumn = new Vector4(containsPiece(15), containsPiece(14), containsPiece(13), containsPiece(12));
        //Vector4 thirdColumn = new Vector4(containsPiece(11), containsPiece(10), containsPiece(9), containsPiece(8));
        //Vector4 secondColumn = new Vector4(containsPiece(7), containsPiece(6), containsPiece(5), containsPiece(4));
        //Vector4 firstColumn = new Vector4(containsPiece(3), containsPiece(2), containsPiece(1), containsPiece(0));
        //Matrix4x4 matrix4X4 = new Matrix4x4(firstColumn, secondColumn, thirdColumn, fourthColumn);

        ////Send it to the class responsible for managing buttons regarding the pieces
        ReduceCognitiveLoad.Instance.BoardStateUpdated(GetBoardState());
    }
Beispiel #10
0
    private void InstantiateWizardButton(int positionInArray)
    {
        GameObject buttonObject = (GameObject)Instantiate(Resources.Load("WizardButton"));

        buttonObject.GetComponent <WizardButtonPressed>().Source = Type;
        if (Type == DialogType.Piece)
        {
            buttonObject.GetComponent <WizardButtonPressed>().pieceName = name;
            LastColorUsed = color.ToString();
        }
        if (name == "Hint")
        {
            buttonObject.GetComponent <WizardButtonPressed>().pieceName = transform.parent.name;
        }

        //Debug.Log("!!!!!!!!!!!!!!!!!!" + buttonObject.GetComponent<WizardButtonPressed>().pieceName + "  ??????" + Type);
        buttonObject.transform.SetParent(gameObject.transform);
        buttonObject.transform.position = transform.position - new Vector3(0, 0, -100);

        Text text = buttonObject.GetComponentInChildren <Text>();

        if (positionInArray != -1)
        {
            buttonObject.transform.localPosition = buttonPositions[positionInArray];
            buttonObject.transform.localScale    = new Vector3(1, 1, 2);
            text.text = options[positionInArray].ToString();
        }
        //else //TODO: Hacky way to allow the gaze, change this!
        //{
        //    buttonObject.transform.localPosition = new Vector3(0, 0, 0);
        //    buttonObject.transform.localScale = new Vector3(1, 1, 1);
        //    if (!(Source == DialogSource.Robot))
        //        text.text = "Gaze";
        //    else text.text = "Clear Gaze";
        //}

        buttonObject.name = transform.name;
        buttonObject.transform.SetParent(transform.root);
        buttonObjects.Add(buttonObject);
    }
Beispiel #11
0
        internal List <Move> GetPossibleMoves(PieceColor color, bool skipKingCheck)
        {
            string key = color.ToString() + skipKingCheck.ToString();
            var    ls  = (color == PieceColor.Black) ? BlackPieces : WhitePieces;

            List <Move> moves = new List <Move>();

            foreach (var p in ls)
            {
                moves.AddRange(p.FindMoves(this));
            }
            var finalResults = (from m in moves
                                where IsValidMove(m) && (skipKingCheck || !DoesMovePutKingInCheck(color, m))
                                select m).ToList();

            if (finalResults.Count == 0)
            {
                // Okay, we have nowhere to go.
                // Is this because we are in stalemate (currently not in checK?)
                // Or checkmate? (currently in check)
                // Third option: if there are no pieces, it is a 'pass'.
                // This is used for unit testing.
                if (ls.Count == 0)
                {
                    return(new List <Move>());
                }

                bool currentlyInCheck = IsKingInCheck(this, color);
                if (currentlyInCheck)
                {
                    throw new CheckmateException();
                }
                else
                {
                    throw new StalemateException();
                }
            }

            return(finalResults);
        }
Beispiel #12
0
        /*private void AiTimer() {
         *
         *  System.Timers.Timer aTimer = new System.Timers.Timer();
         *  aTimer.Elapsed += new ElapsedEventHandler(CheckAiTurn);
         *  aTimer.Interval = 10;
         *  aTimer.Enabled = true;
         *
         *
         * }*/

        /*private void CheckAiTurn(object source, ElapsedEventArgs e)
         * {
         *  Random rand = new Random();
         *  if ((int)playerTurn == 1)
         *  {
         *      int pictureBoxIndex =rand.Next(99);
         *      Point squarePosition = XYPosition(pictureBoxIndex);
         *      Square clickedSquare = board.squares[squarePosition.Y, squarePosition.X];
         *
         *      if (IsSelectingPieceToMove(clickedSquare) && clickedSquare.piece.pieceColor == playerTurn)
         *      {
         *          originSquare = pictureBoxIndex;
         *          ColorSelectedPiece();
         *          possibleMoves = board.CalculatePossibleMoves(squarePosition);
         *          ColorPossibleMoves();
         *      }
         *      else
         *      {
         *          destinationSquare = pictureBoxIndex;
         *
         *          if (IsSelectingDestination())
         *          {
         *              if (IsDestinationValid(squarePosition))
         *              {
         *                  MovePiece(squarePosition);
         *                  SwitchTurn();
         *                  ResetCoordinates();
         *                  ResetColors();
         *              }
         *              else
         *              {
         *                  ResetCoordinates();
         *                  ResetColors();
         *              }
         *          }
         *      }
         *  }
         * }*/

        private void SwitchTurn()
        {
            playerTurn   = (PieceColor)(((int)playerTurn + 1) % 2);
            lblTurn.Text = playerTurn.ToString() + " Turn";
        }
Beispiel #13
0
 internal void SetPlayerName()
 {
     _playerNameTopBarUI.text = string.IsNullOrEmpty(Name) ? string.Format("{0} player", PieceColor.ToString())
                                                           : Name;
 }
        public static bool HorseThreatCheck(int kingX, int kingY, Dictionary <int, Dictionary <int, ChessPiece> > scenario, PieceColor enemy)
        {
            if (HelperMaths.IsInRange(kingX - 2, 0, 7) && HelperMaths.IsInRange(kingY - 1, 0, 7))
            {
                if (scenario[kingX - 2][kingY - 1]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX - 2, 0, 7) && HelperMaths.IsInRange(kingY + 1, 0, 7))
            {
                if (scenario[kingX - 2][kingY + 1]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX + 2, 0, 7) && HelperMaths.IsInRange(kingY - 1, 0, 7))
            {
                if (scenario[kingX + 2][kingY - 1]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX + 2, 0, 7) && HelperMaths.IsInRange(kingY + 1, 0, 7))
            {
                if (scenario[kingX + 2][kingY + 1]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            //

            if (HelperMaths.IsInRange(kingX + 1, 0, 7) && HelperMaths.IsInRange(kingY + 2, 0, 7))
            {
                if (scenario[kingX + 1][kingY + 2]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX - 1, 0, 7) && HelperMaths.IsInRange(kingY + 2, 0, 7))
            {
                if (scenario[kingX - 1][kingY + 2]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX + 1, 0, 7) && HelperMaths.IsInRange(kingY - 2, 0, 7))
            {
                if (scenario[kingX + 1][kingY - 2]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            if (HelperMaths.IsInRange(kingX - 1, 0, 7) && HelperMaths.IsInRange(kingY - 2, 0, 7))
            {
                if (scenario[kingX - 1][kingY - 2]?.ToString() == enemy.ToString() + "Horseman")
                {
                    return(true);
                }
            }

            return(false);
        }
Beispiel #15
0
 public void StartGame()
 {
     CurrentPlayer = PieceColor.White;
     Window.UpdateCurrentPlayerLabel(CurrentPlayer.ToString());
 }
Beispiel #16
0
        internal void ReportOutcome()
        {
            bool       humanPlayer    = WhiteHumanPlayer || BlackHumanPlayer;
            PieceColor previousPlayer = (CurrentTurn == PieceColor.White)? PieceColor.Black : PieceColor.White;

            if (IsCheckMate())
            {
                MidGame = false;
                Debug.WriteLine("Checkmate: " + previousPlayer + " wins!");
                if (humanPlayer)
                {
                    MessageBox.Show(previousPlayer.ToString() + " wins!", "Checkmate");
                }
            }

            if (IsStaleMate())
            {
                MidGame = false;
                Debug.WriteLine("Stalemate: Game is a draw!");
                if (humanPlayer)
                {
                    MessageBox.Show("Game is a draw!", "Stalemate");
                }
            }

            if (IsThreeFoldRepetition())
            {
                if (humanPlayer)
                {
                    MessageBoxResult response = MessageBox.Show("Do you want to declare a draw?",
                                                                "Threefold Repetition", MessageBoxButton.YesNo, MessageBoxImage.Warning);

                    if (response == MessageBoxResult.Yes)
                    {
                        MidGame = false;
                        MessageBox.Show("Game is a draw!", "Threefold Repetition");
                    }
                }
            }

            if (InsufficientMaterial())
            {
                MidGame = false;
                Debug.WriteLine("Insufficient Material: Game is a draw!");
                if (humanPlayer)
                {
                    MessageBox.Show("Game is a draw!", "Insufficient Material");
                }
            }

            if (!IsProgressive())
            {
                MidGame = false;
                Debug.WriteLine("Lack of Progress: Game is a draw!");
                if (humanPlayer)
                {
                    MessageBox.Show("Game is a draw!", "Lack of Progress");
                }
            }

            if (IsFiveFoldRepetition())
            {
                MidGame = false;
                Debug.WriteLine("Fivefold Repetition: Game is a draw!");
                if (humanPlayer)
                {
                    MessageBox.Show("Game is a draw!", "Fivefold Repetition");
                }
            }
        }
Beispiel #17
0
        public void ChangeColor(PieceColor color)
        {
            var message = new Message(CommandType.ColorChange, color.ToString());

            SendMessage(message);
        }
Beispiel #18
0
 public override string ToString()
 {
     return(color.ToString() + " " + kind.ToString());
 }
Beispiel #19
0
 /// <summary>
 /// Small helper to get the mangled key for an image
 /// </summary>
 /// <param name="job"></param>
 /// <param name="color"></param>
 /// <returns></returns>
 private static string GetPieceImageKey(PieceClass job, PieceColor color)
 {
     return(String.Concat(job.ToString(), color.ToString()));
 }
Beispiel #20
0
 public override string ToString()
 {
     return(color.ToString() + (bomb ? "!" : ""));
 }
Beispiel #21
0
        public void LoadTexture(Scene scene)
        {
            string texturePath = type.name + color.ToString();

            texture = scene.Content.Load <Texture2D>("Pieces/" + texturePath);
        }