コード例 #1
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;
        }
コード例 #2
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;
        }
コード例 #3
0
 public virtual bool MakesChess(int row, int col, Player currentPlayer, FigurePositions currentPosition)
 {
     return false;
 }
コード例 #4
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;
        }
コード例 #5
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;
 }
コード例 #6
0
        static void Main()
        {
            //console settings
            Console.Title = "Just Star Chess";
            Console.BufferHeight = 50;
            Console.BufferWidth = 102;
            Console.SetWindowSize(102, 50);

            string menu = Menu.Initialise();
            bool hasLoaded = false;

            Board board = new Board();

            InitialBoard();

            WhitePlayer = new Player("None", Colors.White);
            BlackPlayer = new Player("None", Colors.Black);

            switch (menu)
            {
                case "LOAD":
                    try
                    {
                        Menu.Load();
                        hasLoaded = true;
                    }
                    catch (Exception)
                    {
                        System.Console.SetCursorPosition(33, 27);
                        System.Console.Write("No file to load! Starting a new game...");
                        System.Threading.Thread.Sleep(2500);
                        hasLoaded = false;
                    }
                    break;
                case "HELP":
                    Menu.Help();
                    break;
                case "EXIT":
                    Console.SetCursorPosition(0, 0);
                    return;
            }

            bool hasCheck = false;

            //check for loading
            if (!hasLoaded)
            {
                PlayerInitialisation();
            }

            /*
            //TEST EVENTS
            //Publisher pub = new Publisher();
            Publisher pub = new Publisher();
            Subscriber sub1 = new Subscriber("Game Over, Player 1 Wins", pub);
            Subscriber sub2 = new Subscriber("sub2", pub);

            // Call the method that raises the event.
            pub.DoSomething();
            */

            /*
            Console.Write("Enter Player 1 name: ");
            string name = Console.ReadLine();
            */

            while (true)
            {
                whiteTimer.StartCounting = DateTime.Now;
                blackTimer.StartCounting = DateTime.Now;

                Console.SetCursorPosition(37, 2);

                Console.BackgroundColor = ConsoleColor.Black;

                board.PrintBoard(BoardFigures);

                Console.BackgroundColor = ConsoleColor.Black;

                //if check
                if (hasCheck)
                {
                    Console.SetCursorPosition(0, 0);
                    Console.BackgroundColor = ConsoleColor.Black;
                    Console.WriteLine("CHECK!!!");
                }

                ShowHistory();

                ShowTakenFigures();

                Console.SetCursorPosition(36, 2);

                Console.Write("                                                  "); //clears old symbols on console

                Console.SetCursorPosition(36, 2);

                if (CurrentPlayer == "white")
                {
                    Console.Write("{0}{1}: ", WhitePlayer.Name[0].ToString().ToUpper(), WhitePlayer.Name.Substring(1));
                }
                else
                {
                    Console.Write("{0}{1}: ", BlackPlayer.Name[0].ToString().ToUpper(), BlackPlayer.Name.Substring(1));
                }

                string input = Console.ReadLine();

                if (input.ToUpper() == "EXIT")
                {
                    Console.SetCursorPosition(0, 0);
                    return;
                }
                else if (input.ToUpper() == "LOAD")
                {
                    Menu.Load();
                    Console.SetCursorPosition(34, 2);
                    Console.Write("   Game loaded");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                         ");
                    continue;
                }
                else if (input.ToUpper() == "SAVE")
                {
                    Menu.Save();
                    Console.SetCursorPosition(34, 2);
                    Console.Write("   Game saved");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                         ");
                    continue;
                }

                Input currentInput = new Input(input);

                try
                {
                    currentInput.ValidateInput();
                }
                catch
                {
                    Console.SetCursorPosition(31, 2);
                    Console.Write("   Invalid command!");
                    System.Threading.Thread.Sleep(1000);
                    Console.SetCursorPosition(31, 2);
                    Console.Write("                          ");
                    continue;
                }

                {
                    string forHistory = input;

                    input = currentInput.GetFieldCoordinates();

                    string[] coordinateSplit = input.Split('-');

                    int lastRow = int.Parse(coordinateSplit[0]);
                    int lastCol = int.Parse(coordinateSplit[1]);

                    //for check and mate
                    int nextCol = int.Parse(coordinateSplit[2]);
                    int nextRow = int.Parse(coordinateSplit[3]);

                    FigureProperties figure = BoardFigures.Position[lastCol, lastRow];

                    //check player color
                    if (figure.Name != Names.None && figure.Color != CurrentColor)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("Figure is not your color!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }

                    //create figures
                    Pawn pawn = new Pawn(figure.Color, figure.Name);
                    Knight knight = new Knight(figure.Color, figure.Name);
                    Bishop bishop = new Bishop(figure.Color, figure.Name);
                    Rook rook = new Rook(figure.Color, figure.Name);
                    Queen queen = new Queen(figure.Color, figure.Name);
                    King king = new King(figure.Color, figure.Name);

                    if (figure.Name == Names.None)
                    {
                        Console.SetCursorPosition(31, 2);
                        Console.Write("     Empty field!");
                        System.Threading.Thread.Sleep(1000);
                        Console.SetCursorPosition(31, 2);
                        Console.Write("                         ");
                        continue;
                    }
                    else if (figure.Name == Names.Pawn)
                    {
                        if (!pawn.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Pawn cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = pawn.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Knight)
                    {
                        if (!knight.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Knight cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = knight.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Bishop)
                    {
                        if (!bishop.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Bishop cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = bishop.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Rook)
                    {
                        if (!rook.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Rook cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = rook.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.Queen)
                    {
                        if (!queen.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("Queen cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = queen.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }
                    else if (figure.Name == Names.King)
                    {
                        if (!king.MoveFigure(input, BoardFigures))
                        {
                            Console.SetCursorPosition(31, 2);
                            Console.Write("King cannot move this way!");
                            System.Threading.Thread.Sleep(1000);
                            Console.SetCursorPosition(31, 2);
                            Console.Write("                          ");
                            continue;
                        }
                        else
                        {
                            if (CurrentPlayer == "white")
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, WhitePlayer, BoardFigures);
                            }
                            else
                            {
                                hasCheck = king.MakesChess(nextRow, nextCol, BlackPlayer, BoardFigures);
                            }
                        }
                    }

                    //add move into history
                    if (CurrentPlayer == "white")
                    {
                        WhiteHistory.Manage(forHistory.ToUpper());
                    }
                    else
                    {
                        BlackHistory.Manage(forHistory.ToUpper());
                    }

                    //change players
                    if (CurrentPlayer == "white")
                    {
                        CurrentPlayer = "black";
                        CurrentColor = Colors.Black;
                    }
                    else
                    {
                        CurrentPlayer = "white";
                        CurrentColor = Colors.White;
                    }
                }
            }
        }
コード例 #7
0
        static void PlayerInitialisation()
        {
            //player initialisation
            Console.Clear();
            System.Console.SetCursorPosition(42, 18);
            System.Console.WriteLine("JUST STAR CHESS\n\r");

            System.Console.SetCursorPosition(38, 20);
            Console.Write("Enter Player 1 Name: ");
            string name = Console.ReadLine();
            WhitePlayer = new Player(name, Colors.White);

            Console.Clear();
            System.Console.SetCursorPosition(42, 18);
            System.Console.WriteLine("JUST STAR CHESS\n\r");
            System.Console.SetCursorPosition(38, 20);
            Console.Write("Enter Player 2 Name: ");
            name = Console.ReadLine();
            while (name == WhitePlayer.Name)
            {
                System.Console.SetCursorPosition(38, 20);
                Console.Write("Enter Player 2 Name: Name already taken!");
                System.Threading.Thread.Sleep(1000);
                System.Console.SetCursorPosition(38, 20);
                Console.Write("Enter Player 2 Name:                    ");
                System.Console.SetCursorPosition(59, 20);
                name = Console.ReadLine();
            }

            BlackPlayer = new Player(name, Colors.Black);
        }