コード例 #1
0
ファイル: Screen.cs プロジェクト: apacpg/Console_chess
        public static void DisplayCapturedPieces(ChessMatch match)
        {
            var blackPieces = match.CapturedPieces(Color.Black);
            var withePieces = match.CapturedPieces(Color.White);

            ConsoleColor basic = Console.ForegroundColor;


            Console.Write("White: ");
            foreach (Piece piece in withePieces)
            {
                Console.Write(piece.ToString());
            }
            Console.WriteLine();

            Console.ForegroundColor = ConsoleColor.Green;
            Console.Write("Black: ");
            foreach (Piece piece in blackPieces)
            {
                Console.Write(piece.ToString());
            }
            Console.WriteLine();

            Console.ForegroundColor = basic;

            Console.WriteLine();
        }
コード例 #2
0
        public static void PrintMatch(ChessMatch chessMatch)
        {
            PrintBoard(chessMatch.Board);
            Console.WriteLine();

            PrintCapturedPieces(chessMatch);
            Console.WriteLine();

            Console.WriteLine("Turn: " + chessMatch.Turn);

            if (!chessMatch.Ended)
            {
                Console.WriteLine("Waiting for: " + chessMatch.ActualPlayer);
                if (chessMatch.Check)
                {
                    Console.WriteLine("CHECK!");
                }
            }
            else
            {
                Console.WriteLine();
                Console.WriteLine("CHECKMATE!");
                Console.WriteLine("Winner: " + chessMatch.ActualPlayer);
            }
        }
コード例 #3
0
        public async Task <ChessMatch> AcceptChallenge(ulong channel, ulong player)
        {
            if (await PlayerIsInGame(channel, player))
            {
                throw new ChessException($"{player.Mention()} is currently in a game.");
            }

            var challenge = _challenges.Where(x => x.Channel == channel && x.Challenged == player).OrderBy(x => x.ChallengeDate).FirstOrDefault();

            if (challenge == null)
            {
                throw new ChessException($"No challenge exists for you to accept.");
            }

            if (await PlayerIsInGame(channel, challenge.Challenger))
            {
                throw new ChessException($"{challenge.Challenger.Mention()} is currently in a game.");
            }

            var chessGame  = new ChessGame();
            var chessMatch = new ChessMatch {
                Channel = channel, Game = chessGame, Challenger = challenge.Challenger, Challenged = challenge.Challenged
            };

            _challenges.Remove(challenge);
            _chessMatches.Add(chessMatch);

            return(await Task.FromResult <ChessMatch>(chessMatch));
        }
コード例 #4
0
        static void Main(string[] args)
        {
            try
            {
                ChessMatch chessMatch = new ChessMatch();

                while (!chessMatch.GameOver)
                {
                    Console.Clear();
                    View.PrintBoard(chessMatch.Board);

                    Console.Write("\nOrigem: ");
                    Position origin = View.ReadPositionChess().ToPosition();

                    Console.Write("Destino: ");
                    Position destination = View.ReadPositionChess().ToPosition();

                    chessMatch.PerformMovement(origin, destination);
                }
            }
            catch (BoardException e)
            {
                Console.WriteLine(e.Message);
            }
        }
コード例 #5
0
        public static void PrintMatch(ChessMatch chessMatch)
        {
            PrintBoard(chessMatch.board);
            printCapturedPieces(chessMatch);
            Console.WriteLine();
            Console.WriteLine("Turn: " + chessMatch.turn);
            if (!chessMatch.finished)
            {
                Console.Write("Waiting player: ");
                ConsoleColor aux = Console.ForegroundColor;

                if (chessMatch.turnPlayer == Color.Black)
                {
                    Console.ForegroundColor = ConsoleColor.Yellow;
                    Console.WriteLine(chessMatch.turnPlayer);
                }
                else
                {
                    Console.ForegroundColor = aux;
                    Console.WriteLine(chessMatch.turnPlayer);
                }
                Console.ForegroundColor = aux;
                if (chessMatch.check)
                {
                    Console.WriteLine(" - CHECK");
                }
            }
            else
            {
                Console.WriteLine("CHECKMATE!");
                Console.WriteLine("Winner: " + chessMatch.turnPlayer);
                Console.ReadLine();
            }
        }
コード例 #6
0
        public static void Main(string[] args)
        {
            Console.BackgroundColor = ConsoleColor.White;

            try {
                ChessMatch match = new ChessMatch();

                while (!match.finishedMatch)
                {
                    Console.Clear();
                    Screen.printBoard(match.board);

                    Console.Write("Origin: ");
                    Position origin = Screen.getChessPosition().toPosition();

                    bool[,] possiblePositions = match.board.piece(origin).possibleMovements();

                    Console.Clear();
                    Screen.printBoard(match.board, possiblePositions);

                    Console.Write("Destiny: ");
                    Position destiny = Screen.getChessPosition().toPosition();

                    match.doMovement(origin, destiny);
                }
            } catch (BoardException e) {
                Console.WriteLine(e.Message);
            }
        }
コード例 #7
0
        public async Task <IActionResult> PutChessMatch([FromRoute] int id, [FromBody] ChessMatch chessMatch)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (id != chessMatch.ChessMatchId)
            {
                return(BadRequest());
            }

            _context.Entry(chessMatch).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!ChessMatchExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }
コード例 #8
0
        static void Main(string[] args)
        {
            try {
                ChessMatch match = new ChessMatch();

                while (!match.finished)
                {
                    Console.Clear();
                    Screen.printBoard(match.board);

                    Console.WriteLine();
                    Console.Write("Origin: ");
                    Position origin = Screen.readChessPosition().toPosition();

                    bool[,] possiblePositions = match.board.piece(origin).possibleMovements();

                    Console.Clear();
                    Screen.printBoard(match.board, possiblePositions);

                    Console.WriteLine();
                    Console.Write("Destiny: ");
                    Position destiny = Screen.readChessPosition().toPosition();

                    match.executeMovement(origin, destiny);
                }

                Screen.printBoard(match.board);
            }
            catch (BoardException e) {
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: joedav/ConsoleChess
        static void Main(string[] args)
        {
            try
            {
                ChessMatch match = new ChessMatch();

                while (!match.FinishedMatch)
                {
                    try
                    {
                        Canvas.PrintMatch(match);
                    }
                    catch (BoardException ex)
                    {
                        Console.WriteLine(ex.Message);
                        Console.WriteLine("Try again.");
                        Console.ReadLine();
                        Console.Clear();
                    }
                }
            }
            catch (BoardException ex)
            {
                Console.WriteLine(ex.Message);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
コード例 #10
0
        public async Task <IActionResult> GetChessMatch([FromRoute] int id)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            //ChessMatch chessMatch = await _context.Matches.Include("MatchPlayers.ChessPieces.ChessPieceType").SingleOrDefaultAsync(m => m.ChessMatchId == id);

            ChessMatch matchPopulated =
                await _context.Matches
                .Include(chessMatch => chessMatch.MatchPlayers)
                .ThenInclude(matchPlayer => matchPlayer.ChessPieces)
                .ThenInclude(chessPiece => chessPiece.ChessPieceType)
                .Include(chessMatch => chessMatch.MatchPlayers)
                .ThenInclude(matchPlayer => matchPlayer.PlayerType)
                .SingleOrDefaultAsync(m => m.ChessMatchId == id);


            if (matchPopulated == null)
            {
                return(NotFound());
            }

            return(Ok(matchPopulated));
        }
コード例 #11
0
        public static void PrintCapturedPieces(ChessMatch chessMatch)
        {
            Console.WriteLine("\nCaptured pieces: ");
            Console.WriteLine("White:");
            PrintSetPieces(chessMatch.GetCapturedPieces(Color.White));
            Console.WriteLine("Black:");
            ConsoleColor aux = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Red;
            PrintSetPieces(chessMatch.GetCapturedPieces(Color.Black));
            Console.ForegroundColor = aux;
            if (!chessMatch.Finished)
            {
                if (chessMatch.Check)
                {
                    Console.WriteLine("You are on check!");
                }
                Console.WriteLine($"\nTurn: {chessMatch.Turn}\nWaiting for >> {chessMatch.CurrentPlayer}'s <<");
            }
            else
            {
                Console.WriteLine("Check Mate!");
                Console.WriteLine("Winner: " + chessMatch.CurrentPlayer);
            }
        }
コード例 #12
0
        public static void PrintMatch(ChessMatch match)
        {
            PrintBoard(match.Brd);
            Console.WriteLine("");
            PrintCapturedPieces(match);
            Console.WriteLine("");
            Console.WriteLine("Turn: " + match.Turn);

            if (!match.Finished)
            {
                if (match.Quit)
                {
                    Console.WriteLine("Thanks for playing Console Chess!");
                }
                else
                {
                    Console.WriteLine("Waiting play: " + match.CurrentPlayer);
                    if (match.Check)
                    {
                        Console.WriteLine("CHECK!");
                    }
                }
            }
            else
            {
                Console.WriteLine("CHECKMATE!");
                Console.WriteLine("Winner: " + match.CurrentPlayer);
            }
        }
コード例 #13
0
        public static void PrintMatchDestinationPlay(ChessMatch chessMatch, bool[,] possiblePositions)
        {
            ConsoleColor defaultColor = Console.ForegroundColor;

            Console.WriteLine("CONSOLE CHESS");

            Console.WriteLine();
            PrintBoard(chessMatch.Board, possiblePositions);

            Console.WriteLine();
            PrintCapturedPieces(chessMatch);

            Console.WriteLine();
            Console.Write(PrintHowToPlay());

            Console.WriteLine();
            Console.WriteLine("Turn #" + chessMatch.Turn);

            Console.Write("Awaiting Player: ");
            PrintInPlayerColor(chessMatch, defaultColor);
            Console.WriteLine(chessMatch.CurrentPlayer);
            ReturnToDefaultColor(defaultColor);

            Console.WriteLine();
            PrintInPlayerColor(chessMatch, defaultColor);
            Console.Write("Destination: ");
            ReturnToDefaultColor(defaultColor);
        }
コード例 #14
0
        public static PositionChess getMoviment(ChessMatch match, ChessBoard chessBoard)
        {
            string move = "  ";

            Console.WriteLine("\nInforme a Posição da Da Peça que deseja mover:");
            move = Console.ReadLine();
            return(new PositionChess(move));
        }
コード例 #15
0
 public static void printCapturedPieces(ChessMatch chessMatch)
 {
     Console.WriteLine("Captured Pieces:");
     Console.Write("White: ");
     printHashset(chessMatch.capturedPieces(Color.White));
     Console.Write("\nBlack: ");
     printHashset(chessMatch.capturedPieces(Color.Black));
 }
コード例 #16
0
        /// <summary>
        /// Prints on the console the result of the <paramref name="match"/>.
        /// </summary>
        /// <param name="match">The match to print its result.</param>
        public static void PrintMatchResults(ChessMatch match)
        {
            var titleLength = match.GameStatus.ToString().Length;
            var winner      = match.TeamPlaying == Team.Black ? Team.White : Team.Black;
            var colors      = GetTeamColors(winner);

            if (match.GameStatus is Chess.Enums.GameStatus.Stalemate)
            {
                colors = (ConsoleColor.DarkBlue, ConsoleColor.DarkCyan);
            }

            var tmp = (Console.BackgroundColor, Console.ForegroundColor);

            Console.BackgroundColor = colors.BackgroundColor;
            Console.ForegroundColor = colors.ForegroundColor;

            var spaces = new string(' ', (int)Math.Floor((18 - titleLength) / 2d));

            Console.WriteLine();
            Console.CursorLeft = 1;
            Console.WriteLine(new string(' ', 18));
            Console.CursorLeft = 1;
            Console.WriteLine($"{new string(' ', (18 - titleLength) % 2)}{spaces}{match.GameStatus.ToString().ToUpper()}{spaces}");
            Console.CursorLeft = 1;
            Console.WriteLine(new string(' ', 18));
            Console.WriteLine();

            Console.BackgroundColor = tmp.BackgroundColor;
            Console.ForegroundColor = tmp.ForegroundColor;

            PrintBoard(match.Board, 4);

            Console.BackgroundColor = colors.BackgroundColor;
            Console.ForegroundColor = colors.ForegroundColor;

            string message = String.Empty;

            switch (match.GameStatus)
            {
            case Chess.Enums.GameStatus.Checkmate:
                message = $"{winner} wins!";
                break;

            case Chess.Enums.GameStatus.Stalemate:
                message = "Game ends in a tie";
                break;
            }
            var messageLength = message.Length;

            spaces = new string(' ', (int)Math.Floor((18 - messageLength) / 2d));
            Console.WriteLine();
            Console.CursorLeft = 1;
            Console.WriteLine($"{new string(' ', (18 - messageLength) % 2)}{spaces}{message}{spaces}");

            Console.BackgroundColor = tmp.BackgroundColor;
            Console.ForegroundColor = tmp.ForegroundColor;
        }
コード例 #17
0
 public static void PrintMatch(ChessMatch match, bool[,] possiblePositions)
 {
     PrintBoard(match.Board, possiblePositions);
     Console.WriteLine();
     PrintCaptured(match);
     Console.WriteLine();
     Console.WriteLine("Turn #" + match.Turn);
     Console.WriteLine("Now Playing: " + match.NowPlaying + "s");
 }
コード例 #18
0
 public static void PrintMatch(ChessMatch match)
 {
     Screen.PrintTable(match.Tab);
     Console.WriteLine();
     PrintCapturedPieces(match);
     Console.WriteLine();
     Console.WriteLine("Turno: " + match.Turn);
     Console.WriteLine("Aguardando jogada: " + match.ActivePlayer);
 }
コード例 #19
0
ファイル: View.cs プロジェクト: abrandemburg/xadrez-csharp
 private static void printCapturedPieces(ChessMatch match)
 {
     Console.WriteLine("Captured pieces: ");
     Console.Write("White: ");
     printCollection(match.capturedPiecesByColor(Color.White));
     Console.WriteLine();
     Console.Write("Black: ");
     printCollection(match.capturedPiecesByColor(Color.Black));
 }
コード例 #20
0
        static void Main(string[] args)
        {
            try
            {
                const byte boardChessDimension = 8;
                ChessBoard chessBoard          = new ChessBoard(boardChessDimension, boardChessDimension); //cria o tabuleiro
                ChessBoard.OrganizeBoard(ref chessBoard);                                                  //coloca as peças
                ChessMatch match  = new ChessMatch(chessBoard);                                            // inicia os parametros de uma partida
                Queen      cavalo = new Queen(Color.White, chessBoard);
                chessBoard.SetPiece(cavalo, new Position(3, 4));


                do
                {
                    Console.Clear();
                    Screen.PrintBoard(chessBoard);
                    /*------------------------------------------------------------------*/
                    Console.WriteLine("\nInforme a Posição da Da Peça que deseja mover:");
                    String move = Console.ReadLine();
                    Console.WriteLine(" show " + move[1] + " " + move[0] + " " + (int)move[0]);
                    PositionChess p1 = new PositionChess(move);
                    Console.Clear();
                    bool[,] mat = chessBoard.GetPiece(match.ConvertPosition(p1)).MovimentValidate();
                    Screen.PrintBoard(chessBoard, mat);
                    /*------------------------------------------------------------------*/
                    Console.WriteLine("\nPara onde deseja mover:");
                    string set_piece = Console.ReadLine();
                    match.MovePiece(new PositionChess(move), new PositionChess(set_piece));
                    Console.ReadKey();
                } while (!match.Winner);
            }
            catch (BoardException e) {
                Console.WriteLine(e);
            }


            /*principais desafios de implementar o jogo de xadrez , refatorar modificações com forme a necessidade
             * -->movimentos especiais EN PASSENT, ROQUE
             * -->jogadas especiais CHEQUE, CHEQUE MATE
             * -->Movimento e captura do Peão, e movimentos de não cheque do Rei
             */
            //AO FINALIZAR ESTE PROJETO SERÁ TOTALMENTE REFATORADO

            /*será aplicado o SOLID, e posteriormente uma IA, que trabalharei separadamente
             * --> provavelmente em algoritimo genetico
             * --> investigar qual a melhor IA para este caso, divergente do tic tac toe, a valiação do xadres
             * variando das jogadas possiveis e validas é de (~e^120) então uma implementação de IA Burra está fora de
             * cogitação
             *
             *
             * --> as classes foram geradas sem um previo planejamento, por tanto podem conter metodos não muito pertinentes
             * ao escopo ideal da classe, e por sua vez quebrando o conceito SRP.
             *
             *
             */
        }
コード例 #21
0
        static void Main(string[] args)
        {
            ChessMatch chessMatch = new ChessMatch();

            while (!chessMatch.EndMatch)
            {
                try
                {
                    Console.Clear();
                    Screen.RenderMatch(chessMatch);

                    Console.Write("Do you wish to pass the turn (y/n) ? ");
                    char pass = char.Parse(Console.ReadLine());
                    if (pass == 'y')
                    {
                        chessMatch.PassTurn();
                    }
                    else
                    {
                        Console.WriteLine();
                        Console.Write("Input origin position: ");

                        Position origin = Screen.ReadPosition().ToPosition();

                        chessMatch.ValidateOriginPosition(origin);

                        bool[,] possiblePositions = chessMatch.Board.GetPieceAt(origin).PossibleMovements();

                        Console.Clear();
                        Screen.RenderBoard(chessMatch.Board, possiblePositions);

                        Console.WriteLine();
                        Console.Write("Input destiny position: ");
                        Position destiny = Screen.ReadPosition().ToPosition();

                        chessMatch.ValidateDestinyPosition(origin, destiny);

                        chessMatch.PerformMove(origin, destiny);
                    }
                }
                catch (ChessException exception)
                {
                    Console.WriteLine(exception.Message);
                    Console.ReadLine();
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception.Message);
                }
            }

            Console.Clear();
            Screen.RenderMatch(chessMatch);

            Console.ReadLine();
        }
コード例 #22
0
        private async void RemoveUndoRequest(ChessMatch match, Action <ChessMatch> onTimeout)
        {
            await Task.Delay(_confirmationsTimeout);

            if (match.UndoRequest != null)
            {
                match.UndoRequest = null;
                onTimeout(match);
            }
        }
コード例 #23
0
        static void Main(string[] args)
        {
            try
            {
                ChessMatch play = new ChessMatch();

                while (!play.GameOver)
                {
                    try
                    {
                        Console.Clear();
                        Screen.PrintGamePlay(play);



                        Console.WriteLine();
                        Console.Write("Move piece From: ");
                        Position from = Screen.ReadChessBoardPosition().ToPosition();
                        play.ValidatePositionFrom(from);

                        bool[,] possibleMoves = play.Board.Piece(from).PossibleMoves();

                        Console.Clear();

                        Screen.PrintBoard(play.Board, possibleMoves);


                        Console.Write("to: ");
                        Position to = Screen.ReadChessBoardPosition().ToPosition();
                        play.ValidatePositionTo(from, to);

                        play.ExecutePlay(from, to);
                    }
                    catch (BoardException error)
                    {
                        Console.WriteLine(error.Message);
                        Console.ReadLine();
                    }
                }

                Console.Clear();
                Screen.PrintGamePlay(play);
            }

            catch (BoardException e)
            {
                Console.WriteLine(e.Message);
            }

            Console.WriteLine();


            Console.ReadLine();
        }
コード例 #24
0
 public static void printCapturedPieces(ChessMatch match){
     Console.WriteLine("Captured Pieces: ");
     Console.Write("White: ");
     printHashSet(match.capturedPieces(Color.White));
     Console.Write("\nBlack: ");
     ConsoleColor aux = Console.ForegroundColor;
     Console.ForegroundColor = ConsoleColor.Yellow;
     printHashSet(match.capturedPieces(Color.Black));
     Console.ForegroundColor = aux;
     Console.WriteLine();
 }
コード例 #25
0
 private static void PrintInPlayerColor(ChessMatch chessMatch, ConsoleColor defaultColor)
 {
     if (chessMatch.CurrentPlayer == PieceColor.Black)
     {
         Console.ForegroundColor = ConsoleColor.Yellow;
     }
     else
     {
         Console.ForegroundColor = defaultColor;
     }
 }
コード例 #26
0
ファイル: Window.cs プロジェクト: MoreiraJorge/Chess-Console
 private static string getTheColor(ChessMatch match)
 {
     if (match.currentPlayer == Color.BLACK)
     {
         return("Preta");
     }
     else
     {
         return("Branca");
     }
 }
コード例 #27
0
ファイル: Program.cs プロジェクト: otaviopaesl/ConsoleChess
        static void Main(string[] args)
        {
            try
            {
                ChessMatch match = new ChessMatch();

                while (!match.Finished)
                {
                    try
                    {
                        Console.Clear();
                        Screen.PrintMatch(match);

                        Console.WriteLine("");
                        Console.WriteLine("Write an Origin or press q to quit.");
                        Console.Write("Origin: ");
                        Position origin = Screen.ReadChessPosition().ToPosition();
                        match.ValidateOriginPosition(origin);

                        bool[,] possiblePositions = match.Brd.Piece(origin).PossibleMovements();
                        Console.Clear();
                        Screen.PrintBoard(match.Brd, possiblePositions);

                        Console.WriteLine("");
                        Console.Write("Destination: ");
                        Position destination = Screen.ReadChessPosition().ToPosition();
                        match.ValidadeDestinationPosition(origin, destination);

                        match.MakeThePlay(origin, destination);
                    }

                    catch (BoardException e)
                    {
                        Console.Write(e.Message);
                        Console.ReadLine();
                    }

                    catch (ApplicationException)
                    {
                        match.Quit = true;
                        break;
                    }
                }

                Console.Clear();
                Screen.PrintMatch(match);
            }

            catch (BoardException e)
            {
                Console.Write(e.Message);
            }
        }
コード例 #28
0
        public static void PrintCapturedPieces(ChessMatch match)
        {
            Console.WriteLine("Peças capturadas:");
            Console.Write("Brancas: ");
            PrintSet(match.CapturedPiecesSet(Color.Branca));
            ConsoleColor aux = Console.ForegroundColor;

            Console.ForegroundColor = ConsoleColor.Yellow;
            Console.Write("Pretas: ");
            PrintSet(match.CapturedPiecesSet(Color.Preta));
            Console.ForegroundColor = aux;
        }
コード例 #29
0
        public async Task <IActionResult> PostChessMatch([FromBody] ChessMatch chessMatch)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            _context.Matches.Add(chessMatch);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetChessMatch", new { id = chessMatch.ChessMatchId }, chessMatch));
        }
コード例 #30
0
 public static void PrintCapturedPieces(ChessMatch match)
 {
     Console.WriteLine("Captured pieces:");
     Console.Write("White: ");
     PrintSet(match.CapturedPieces(Color.White));
     Console.WriteLine();
     Console.Write("Black: ");
     SetConsoleForegroundColorToBlackPieces();
     PrintSet(match.CapturedPieces(Color.Black));
     SetConsoleForegroundColorToWhitePieces();
     Console.WriteLine();
 }