コード例 #1
0
ファイル: King.cs プロジェクト: remy22/chess_game-C_Sharp
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);
            //collision check
            bool isCollision = false;
            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return false; //collision same colors
                }
                isCollision = true;
            }
            //valid move check
            if (Math.Abs(lastCol-nextCol) <= 1 && Math.Abs(lastRow-nextRow) <= 1)
            {
                if (isCollision)
                {
                    base.GetFigure(currentPosition, nextRow, nextCol);
                }
                currentPosition.Position[lastRow, lastCol].Name = Names.None;
                currentPosition.Position[nextRow, nextCol].Name = Names.King;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #2
0
 public void GetFigure(FigurePositions currentPosition, int nextRow, int nextCol)
 {
     if (StartProgram.CurrentPlayer == "white")
     {
         StartProgram.WhitePlayer.AddTakenFigure(currentPosition.Position[nextRow, nextCol].Name);
     }
     else
     {
         StartProgram.BlackPlayer.AddTakenFigure(currentPosition.Position[nextRow, nextCol].Name);
     }
 }
コード例 #3
0
 public void GetFigure(FigurePositions currentPosition, int nextRow, int nextCol)
 {
     if (StartProgram.CurrentPlayer == "white")
     {
         StartProgram.WhitePlayer.AddTakenFigure(currentPosition.Position[nextRow, nextCol].Name);
     }
     else
     {
         StartProgram.BlackPlayer.AddTakenFigure(currentPosition.Position[nextRow, nextCol].Name);
     }
 }
コード例 #4
0
        public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
        {
            switch (currentPlayer.Color)
            {
            case Colors.Black:
            {
                //left fork
                if (col - 1 >= 0 && row + 1 <= 7)
                {
                    if (currentPosition.Position[row + 1, col - 1].Name == Names.King && currentPosition.Position[row + 1, col - 1].Color != currentPlayer.Color)
                    {
                        return(true);        //chess
                    }
                }
                //right fork
                if (col + 1 <= 7 && row + 1 <= 7)
                {
                    if (currentPosition.Position[row + 1, col + 1].Name == Names.King && currentPosition.Position[row + 1, col + 1].Color != currentPlayer.Color)
                    {
                        return(true);        //chess
                    }
                }
                break;
            }

            case Colors.White:
            {
                //left fork
                if (col - 1 >= 0 && row - 1 >= 0)
                {
                    if (currentPosition.Position[row - 1, col - 1].Name == Names.King && currentPosition.Position[row - 1, col - 1].Color != currentPlayer.Color)
                    {
                        return(true);        //chess
                    }
                }
                //right fork
                if (col + 1 <= 7 && row - 1 >= 0)
                {
                    if (currentPosition.Position[row - 1, col + 1].Name == Names.King && currentPosition.Position[row - 1, col + 1].Color != currentPlayer.Color)
                    {
                        return(true);        //chess
                    }
                }
                break;
            }
            }
            return(false);
        }
コード例 #5
0
ファイル: Pawn.cs プロジェクト: sashomaga/chess_game-C_Sharp
        public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
        {
            switch (currentPlayer.Color)
            {
                case Colors.Black:
                    {
                        //left fork
                        if (col - 1 >= 0 && row + 1 <=7)
                        {
                            if (currentPosition.Position[row+1, col - 1].Name == Names.King && currentPosition.Position[row+1, col-1].Color != currentPlayer.Color)
                            {
                                return true; //chess
                            }
                        }
                        //right fork
                        if (col + 1 <= 7 && row + 1 <=7)
                        {
                            if (currentPosition.Position[row+1, col + 1].Name == Names.King && currentPosition.Position[row+1, col+1].Color != currentPlayer.Color)
                            {
                                return true; //chess
                            }
                        }
                        break;
                    }
                case Colors.White:
                    {
                        //left fork
                        if (col - 1 >= 0 && row - 1 >= 0)
                        {
                            if (currentPosition.Position[row-1, col - 1].Name == Names.King && currentPosition.Position[row-1, col-1].Color != currentPlayer.Color)
                            {
                                return true; //chess
                            }
                        }
                        //right fork
                        if (col + 1 <= 7 && row - 1 >= 0)
                        {
                            if (currentPosition.Position[row-1, col + 1].Name == Names.King && currentPosition.Position[row-1, col+1].Color != currentPlayer.Color)
                            {
                                return true; //chess
                            }
                        }
                        break;
                    }

            }
            return false;
        }
コード例 #6
0
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;

            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return(false); //collision same colors
                }
                isCollision = true;
            }
            //valid move check
            if ((Math.Abs(lastCol - nextCol) == 1 && Math.Abs(lastRow - nextRow) == 2) || (Math.Abs(lastCol - nextCol) == 2 && Math.Abs(lastRow - nextRow) == 1))
            {
                if (isCollision)
                {
                    base.GetFigure(currentPosition, nextRow, nextCol);
                }
                currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                currentPosition.Position[nextRow, nextCol].Name  = Names.Knight;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #7
0
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;

            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return(false); //collision same colors
                }
                isCollision = true;
            }

            //road check
            //find direction
            if (nextRow < lastRow && nextCol < lastCol) //up-left
            {
                //x -1 y -1
                for (int x = lastRow - 1, y = lastCol - 1; x > nextRow; x--, y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextRow > lastRow && nextCol < lastCol) //down-left
            {
                //x + 1 y - 1
                for (int x = lastRow + 1, y = lastCol - 1; x < nextRow; x++, y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextRow < lastRow && nextCol > lastCol) //up-right
            {
                //x-1 y+1
                for (int x = lastRow - 1, y = lastCol + 1; x > nextRow; x--, y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextRow > lastRow && nextCol > lastCol) // down-right
            {
                //x+1 y+1
                for (int x = lastRow + 1, y = lastCol + 1; x < nextRow; x++, y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }

            //valid move check
            if (Math.Abs(lastCol - nextCol) == Math.Abs(lastRow - nextRow))
            {
                if (isCollision)
                {
                    //TODO:
                    if (StartProgram.CurrentPlayer == "white")
                    {
                        StartProgram.WhitePlayer.AddTakenFigure(currentPosition.Position[nextRow, nextCol].Name);
                    }
                    else
                    {
                        StartProgram.BlackPlayer.AddTakenFigure(currentPosition.Position[lastRow, lastCol].Name);
                    }
                }
                currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                currentPosition.Position[nextRow, nextCol].Name  = Names.Bishop;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #8
0
 public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     //up-left x-1 y-1
     for (int x = row - 1, y = col - 1; x >= 0 && y >= 0; x--, y--)
     {
         if (currentPosition.Position[x, y].Name != Names.None)
         {
             if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
             {
                 return(true); //chess
             }
             else
             {
                 break;
             }
         }
     }
     //up-right x-1 y+1
     for (int x = row - 1, y = col + 1; x >= 0 && y <= 7; x--, y++)
     {
         if (currentPosition.Position[x, y].Name != Names.None)
         {
             if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
             {
                 return(true); //chess
             }
             else
             {
                 break;
             }
         }
     }
     //down-left x+1 y-1
     for (int x = row + 1, y = col - 1; x <= 7 && y >= 0; x++, y--)
     {
         if (currentPosition.Position[x, y].Name != Names.None)
         {
             if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
             {
                 return(true); //chess
             }
             else
             {
                 break;
             }
         }
     }
     //down-right x+1 y+1
     for (int x = row + 1, y = col + 1; x <= 7 && y <= 7; x++, y++)
     {
         if (currentPosition.Position[x, y].Name != Names.None)
         {
             if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
             {
                 return(true); //chess
             }
             else
             {
                 break;
             }
         }
     }
     return(false);
 }
コード例 #9
0
ファイル: Queen.cs プロジェクト: sashomaga/chess_game-C_Sharp
        public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
        {
            //bishop moves
            //up-left x-1 y-1
            for (int x = row - 1, y = col - 1; x >= 0 && y >= 0; x--, y--)
            {
                if (currentPosition.Position[x, y].Name != Names.None)
                {
                    if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //up-right x-1 y+1
            for (int x = row - 1, y = col + 1; x >= 0 && y <= 7; x--, y++)
            {
                if (currentPosition.Position[x, y].Name != Names.None)
                {
                    if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //down-left x+1 y-1
            for (int x = row + 1, y = col - 1; x <= 7 && y >= 0; x++, y--)
            {
                if (currentPosition.Position[x, y].Name != Names.None)
                {
                    if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //down-right x+1 y+1
            for (int x = row + 1, y = col + 1; x <= 7 && y <= 7; x++, y++)
            {
                if (currentPosition.Position[x, y].Name != Names.None)
                {
                    if (currentPosition.Position[x, y].Name == Names.King && currentPosition.Position[x, y].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }

            //Rook moves
            //up
            for (int i = row - 1; i >= 0; i--)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //down
            for (int i = row + 1; i <= 7; i++)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //left
            for (int i = col - 1; i >= 0; i--)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //right
            for (int i = col + 1; i <= 7; i++)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            return false;
        }
コード例 #10
0
ファイル: Rook.cs プロジェクト: sashomaga/chess_game-C_Sharp
        public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
        {
            //check all directions
            //up
            for (int i = row - 1; i >= 0; i--)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //down
            for (int i = row + 1; i <= 7; i++)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //left
            for (int i = col - 1; i >= 0; i--)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //right
            for (int i = col + 1; i <= 7; i++)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return true; //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return false;
        }
コード例 #11
0
ファイル: Pawn.cs プロジェクト: sashomaga/chess_game-C_Sharp
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;
            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return false; //collision same colors
                }
                isCollision = true;
            }

            //road check for two cells move
            if (Math.Abs(lastRow - nextRow) == 2)
            {
                if (nextRow - lastRow < 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.White) //up - white
                {
                    if (currentPosition.Position[lastRow - 1, lastCol].Name != Names.None)
                    {
                        return false; // blocked road
                    }
                }
                else //down - black
                {
                    if (currentPosition.Position[lastRow + 1, lastCol].Name != Names.None)
                    {
                        return false; // blocked road
                    }
                }
            }

            //valid move check
            //up - white
            if (isCollision == true && currentPosition.Position[lastRow, lastCol].Color == Colors.White && Math.Abs(nextCol - lastCol) == 1 && nextRow - lastRow == -1)
            {
                base.GetFigure(currentPosition, nextRow, nextCol);
                currentPosition.Position[lastRow, lastCol].Name = Names.None;
                currentPosition.Position[nextRow, nextCol].Name = Names.Pawn;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return true;
            }
            //down - black
            if (isCollision == true && currentPosition.Position[lastRow, lastCol].Color == Colors.Black && Math.Abs(nextCol - lastCol) == 1 && nextRow - lastRow == 1)
            {
                //TODO: collision
                base.GetFigure(currentPosition, nextRow, nextCol);
                currentPosition.Position[lastRow, lastCol].Name = Names.None;
                currentPosition.Position[nextRow, nextCol].Name = Names.Pawn;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return true;
            }
            if ((lastCol == nextCol && Math.Abs(lastRow - nextRow) == 1) || (lastCol == nextCol && Math.Abs(lastRow - nextRow) == 2 && (lastRow == 1 || lastRow == 6)))
            {
                if (nextRow - lastRow < 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.Black)
                {
                    return false; //wrong direction
                }
                else if (nextRow - lastRow > 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.White)
                {
                    return false; //wrong direction
                }

                if (isCollision == false)
                {
                    currentPosition.Position[lastRow, lastCol].Name = Names.None;
                    currentPosition.Position[nextRow, nextCol].Name = Names.Pawn;
                    currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                    return true;
                }
                else
                {
                    return false;
                }

            }
            return false;
        }
コード例 #12
0
ファイル: Bishop.cs プロジェクト: remy22/chess_game-C_Sharp
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;
            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return false; //collision same colors
                }
                isCollision = true;
            }

            //road check
            //find direction
            if (nextRow < lastRow && nextCol < lastCol) //up-left
            {
                //x -1 y -1
                for (int x = lastRow - 1, y = lastCol - 1; x > nextRow; x--, y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }

            }
            else if (nextRow > lastRow && nextCol < lastCol) //down-left
            {
                //x + 1 y - 1
                for (int x = lastRow + 1, y = lastCol - 1; x < nextRow; x++, y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }
            }
            else if (nextRow < lastRow && nextCol > lastCol) //up-right
            {
                //x-1 y+1
                for (int x = lastRow - 1, y = lastCol + 1; x > nextRow; x--, y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }

            }
            else if (nextRow > lastRow && nextCol > lastCol) // down-right
            {
                //x+1 y+1
                for (int x = lastRow + 1, y = lastCol + 1; x < nextRow; x++, y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }
            }

            //valid move check
            if (Math.Abs(lastCol - nextCol) == Math.Abs(lastRow - nextRow))
            {
                if (isCollision)
                {
                    base.GetFigure(currentPosition, nextRow, nextCol);
                }
                currentPosition.Position[lastRow, lastCol].Name = Names.None;
                currentPosition.Position[nextRow, nextCol].Name = Names.Bishop;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #13
0
 public abstract bool MoveFigure(string coordinates, FigurePositions currentPosition);
コード例 #14
0
 public virtual bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     return(false);
 }
コード例 #15
0
 public virtual bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     return false;
 }
コード例 #16
0
 public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     //left-up
     if (row - 2 >= 0 && col - 1 >=0)
     {
         if (currentPosition.Position[row - 2, col - 1].Name == Names.King && currentPosition.Position[row - 2, col - 1].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     if (row - 1 >= 0 && col - 2 >=0)
     {
         if (currentPosition.Position[row - 1, col - 2].Name == Names.King && currentPosition.Position[row - 1, col - 2].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     //right-up
     if (row - 2 >= 0 && col + 1 <= 7)
     {
         if (currentPosition.Position[row - 2, col + 1].Name == Names.King && currentPosition.Position[row - 2, col + 1].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     if (row - 1 >= 0 && col + 2 <= 7)
     {
         if (currentPosition.Position[row - 1, col + 2].Name == Names.King && currentPosition.Position[row - 1, col + 2].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     //left-down
     if (row + 2 <= 7 && col - 1 >= 0)
     {
         if (currentPosition.Position[row + 2, col - 1].Name == Names.King && currentPosition.Position[row + 2, col - 1].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     if (row + 1 <= 7 && col - 2 >= 0)
     {
         if (currentPosition.Position[row + 1, col - 2].Name == Names.King && currentPosition.Position[row + 1, col - 2].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     //right-down
     if (row + 2 <= 7 && col + 1 <= 7)
     {
         if (currentPosition.Position[row + 2, col + 1].Name == Names.King && currentPosition.Position[row + 2, col + 1].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     if (row - 1 >= 0 && col + 2 <= 7)
     {
         if (currentPosition.Position[row + 1, col + 2].Name == Names.King && currentPosition.Position[row + 1, col + 2].Color != currentPlayer.Color)
         {
             return true; //chess
         }
     }
     return false;
 }
コード例 #17
0
 public abstract bool MoveFigure(string coordinates, FigurePositions currentPosition);
コード例 #18
0
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;

            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return(false); //collision same colors
                }
                isCollision = true;
            }


            //road check
            if (nextRow > lastRow) //down
            {
                //x+1
                for (int x = lastRow + 1, y = lastCol; x < nextRow; x++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextRow < lastRow) //up
            {
                //x-1
                for (int x = lastRow - 1, y = lastCol; x > nextRow; x--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextCol > lastCol) //left
            {
                //y+1
                for (int x = lastRow, y = lastCol + 1; y < nextCol; y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }
            else if (nextCol < lastCol) //right
            {
                //y-1
                for (int x = lastRow, y = lastCol - 1; y > nextCol; y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return(false); //road is not clear
                    }
                }
            }

            //valid move check
            if (lastRow == nextRow || lastCol == nextCol)
            {
                if (isCollision)
                {
                    base.GetFigure(currentPosition, nextRow, nextCol);
                }
                currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                currentPosition.Position[nextRow, nextCol].Name  = Names.Rook;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return(true);
            }
            else
            {
                return(false);
            }
        }
コード例 #19
0
ファイル: Rook.cs プロジェクト: sashomaga/chess_game-C_Sharp
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;
            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return false; //collision same colors
                }
                isCollision = true;
            }

            //road check
            if (nextRow > lastRow) //down
            {
                //x+1
                for (int x = lastRow + 1, y = lastCol; x < nextRow; x++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }

            }
            else if (nextRow < lastRow) //up
            {
                //x-1
                for (int x = lastRow - 1, y = lastCol; x > nextRow; x--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }
            }
            else if (nextCol > lastCol) //left
            {
                //y+1
                for (int x = lastRow, y = lastCol + 1; y < nextCol; y++)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }

            }
            else if (nextCol < lastCol) //right
            {
                //y-1
                for (int x = lastRow, y = lastCol - 1; y > nextCol; y--)
                {
                    if (currentPosition.Position[x, y].Name != Names.None)
                    {
                        return false; //road is not clear
                    }
                }
            }

            //valid move check
            if (lastRow == nextRow || lastCol == nextCol)
            {
                if (isCollision)
                {
                    base.GetFigure(currentPosition, nextRow, nextCol);
                }
                currentPosition.Position[lastRow, lastCol].Name = Names.None;
                currentPosition.Position[nextRow, nextCol].Name = Names.Rook;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return true;
            }
            else
            {
                return false;
            }
        }
コード例 #20
0
        public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
        {
            //check all directions
            //up
            for (int i = row - 1; i >= 0; i--)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return(true); //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //down
            for (int i = row + 1; i <= 7; i++)
            {
                if (currentPosition.Position[i, col].Name != Names.None)
                {
                    if (currentPosition.Position[i, col].Name == Names.King && currentPosition.Position[i, col].Color != currentPlayer.Color)
                    {
                        return(true); //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //left
            for (int i = col - 1; i >= 0; i--)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return(true); //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }
            //right
            for (int i = col + 1; i <= 7; i++)
            {
                if (currentPosition.Position[row, i].Name != Names.None)
                {
                    if (currentPosition.Position[row, i].Name == Names.King && currentPosition.Position[row, i].Color != currentPlayer.Color)
                    {
                        return(true); //chess
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(false);
        }
コード例 #21
0
 public override bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     //left-up
     if (row - 2 >= 0 && col - 1 >= 0)
     {
         if (currentPosition.Position[row - 2, col - 1].Name == Names.King && currentPosition.Position[row - 2, col - 1].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     if (row - 1 >= 0 && col - 2 >= 0)
     {
         if (currentPosition.Position[row - 1, col - 2].Name == Names.King && currentPosition.Position[row - 1, col - 2].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     //right-up
     if (row - 2 >= 0 && col + 1 <= 7)
     {
         if (currentPosition.Position[row - 2, col + 1].Name == Names.King && currentPosition.Position[row - 2, col + 1].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     if (row - 1 >= 0 && col + 2 <= 7)
     {
         if (currentPosition.Position[row - 1, col + 2].Name == Names.King && currentPosition.Position[row - 1, col + 2].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     //left-down
     if (row + 2 <= 7 && col - 1 >= 0)
     {
         if (currentPosition.Position[row + 2, col - 1].Name == Names.King && currentPosition.Position[row + 2, col - 1].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     if (row + 1 <= 7 && col - 2 >= 0)
     {
         if (currentPosition.Position[row + 1, col - 2].Name == Names.King && currentPosition.Position[row + 1, col - 2].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     //right-down
     if (row + 2 <= 7 && col + 1 <= 7)
     {
         if (currentPosition.Position[row + 2, col + 1].Name == Names.King && currentPosition.Position[row + 2, col + 1].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     if (row - 1 >= 0 && col + 2 <= 7)
     {
         if (currentPosition.Position[row + 1, col + 2].Name == Names.King && currentPosition.Position[row + 1, col + 2].Color != currentPlayer.Color)
         {
             return(true); //chess
         }
     }
     return(false);
 }
コード例 #22
0
        public override bool MoveFigure(string coordinates, FigurePositions currentPosition)
        {
            string[] coordinateSplit = coordinates.Split('-');

            int lastCol = int.Parse(coordinateSplit[0]);
            int lastRow = int.Parse(coordinateSplit[1]);
            int nextCol = int.Parse(coordinateSplit[2]);
            int nextRow = int.Parse(coordinateSplit[3]);

            //collision check
            bool isCollision = false;

            if (currentPosition.Position[nextRow, nextCol].Name != Names.None)
            {
                if (currentPosition.Position[nextRow, nextCol].Color == currentPosition.Position[lastRow, lastCol].Color)
                {
                    return(false); //collision same colors
                }
                isCollision = true;
            }

            //road check for two cells move
            if (Math.Abs(lastRow - nextRow) == 2)
            {
                if (nextRow - lastRow < 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.White) //up - white
                {
                    if (currentPosition.Position[lastRow - 1, lastCol].Name != Names.None)
                    {
                        return(false); // blocked road
                    }
                }
                else //down - black
                {
                    if (currentPosition.Position[lastRow + 1, lastCol].Name != Names.None)
                    {
                        return(false); // blocked road
                    }
                }
            }

            //valid move check
            //up - white
            if (isCollision == true && currentPosition.Position[lastRow, lastCol].Color == Colors.White && Math.Abs(nextCol - lastCol) == 1 && nextRow - lastRow == -1)
            {
                base.GetFigure(currentPosition, nextRow, nextCol);
                currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                currentPosition.Position[nextRow, nextCol].Name  = Names.Pawn;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return(true);
            }
            //down - black
            if (isCollision == true && currentPosition.Position[lastRow, lastCol].Color == Colors.Black && Math.Abs(nextCol - lastCol) == 1 && nextRow - lastRow == 1)
            {
                //TODO: collision
                base.GetFigure(currentPosition, nextRow, nextCol);
                currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                currentPosition.Position[nextRow, nextCol].Name  = Names.Pawn;
                currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                return(true);
            }
            if ((lastCol == nextCol && Math.Abs(lastRow - nextRow) == 1) || (lastCol == nextCol && Math.Abs(lastRow - nextRow) == 2 && (lastRow == 1 || lastRow == 6)))
            {
                if (nextRow - lastRow < 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.Black)
                {
                    return(false); //wrong direction
                }
                else if (nextRow - lastRow > 0 && currentPosition.Position[lastRow, lastCol].Color == Colors.White)
                {
                    return(false); //wrong direction
                }

                if (isCollision == false)
                {
                    currentPosition.Position[lastRow, lastCol].Name  = Names.None;
                    currentPosition.Position[nextRow, nextCol].Name  = Names.Pawn;
                    currentPosition.Position[nextRow, nextCol].Color = currentPosition.Position[lastRow, lastCol].Color;
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
            return(false);
        }