Esempio n. 1
0
        public Games MakeMove(int id, string move)
        {
            Games game = GetGame(id);

            if (game == null)
            {
                return(game);
            }

            if (game.Statuse != "play")
            {
                return(game);
            }

            Chess.Chess chess = new Chess.Chess(game.FEN);

            var chessNext = chess.Move(move);

            if (chessNext.fen == game.FEN)
            {
                return(game);
            }

            game.FEN = chessNext.fen;
            if (chessNext.IsCheckAndMate())
            {
                game.Statuse = "done";
            }

            db.Entry(game).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            return(game);
        }
Esempio n. 2
0
        //метод для перевода шахмат в аски формат
        static string ChessToAscii(Chess.Chess chess)
        {
            // рисуем начало доски 16+1 по 2 на каждые 8 клеток
            string text = "  +" + new string('-', 17) + "+\n";

            // цикл рисования всех внутренних линий
            for (int y = 7; y >= 0; y--)
            {
                //добавляем номер строки
                text += y + 1;
                // добавляем черту
                text += " | ";
                //
                for (int x = 0; x < 8; x++)
                {
                    // добавляем фигуру получая символ фигуры + пробел
                    text += chess.GetFigureAt(x, y) + " ";
                }
                //
                text += "|\n";
            }
            //закрываем доску
            text += "  +" + new string('-', 17) + "+\n";
            //добавляем надписи
            text += "    a b c d e f g h\n";
            return(text);
        }
Esempio n. 3
0
        public Games CreateOnce(string name)
        {
            Users u = db.Users.Where(g => g.Name == name).FirstOrDefault();

            if (u == null)
            {
                return(null);
            }
            Games userGameWhite = u.Games.Where(g => g.Statuse == "play" || g.Statuse == "wait" || g.Statuse == "offerDraw").FirstOrDefault();
            Games userGameBlack = u.Games1.Where(g => g.Statuse == "play" || g.Statuse == "wait" || g.Statuse == "offerDraw").FirstOrDefault();

            Chess.Chess chess = new Chess.Chess();
            if (userGameBlack != null)
            {
                return(userGameBlack);
            }
            if (userGameWhite != null)
            {
                return(userGameWhite);
            }
            if (userGameWhite == null)
            {
                Games waitGame = new Games {
                    FEN = chess.fen, Statuse = "wait", White = u.id, ColorMove = "white"
                };
                db.Games.Add(waitGame);
                db.SaveChanges();
                return(waitGame);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Chess.Chess   chess = new Chess.Chess();
            List <string> list;

            while (true)
            {
                list = chess.GetAllMoves();
                Console.WriteLine(chess.fen);
                Print(ChessToAscii(chess));
                Console.WriteLine(chess.IsCheck() ? "CHECK" : "-");
                foreach (string moves in list)
                {
                    Console.Write(moves + "\t");
                }

                Console.WriteLine();
                Console.Write("> ");

                string move = Console.ReadLine();
                if (move == "q")
                {
                    break;
                }
                if (move == "")
                {
                    move = list[random.Next(list.Count)];
                }
                chess = chess.Move(move);
            }
        }
Esempio n. 5
0
        public Games CreateNewGame()
        {
            Chess.Chess chess = new Chess.Chess();
            Games       game  = new Games {
                FEN = chess.fen, Statuse = "play"
            };

            db.Games.Add(game);
            db.SaveChanges();
            return(game);
        }
Esempio n. 6
0
        public Games CreateGame(string name)
        {
            Users u = db.Users.Where(g => g.Name == name).FirstOrDefault();

            if (u == null)
            {
                return(null);
            }
            Games userGameWhite = u.Games.Where(g => g.Statuse == "play").FirstOrDefault();
            Games userGameBlack = u.Games1.Where(g => g.Statuse == "play" || g.Statuse == "offerDraw").FirstOrDefault();

            Chess.Chess chess = new Chess.Chess();

            if (userGameBlack != null)
            {
                return(null);
            }
            if (userGameWhite == null)
            {
                Games waitGame = db.Games.Where(g => g.Statuse == "wait").FirstOrDefault();

                if (waitGame == null)
                {
                    waitGame = new Games {
                        FEN = chess.fen, Statuse = "wait", White = u.id, ColorMove = "white"
                    };
                    db.Games.Add(waitGame);
                    db.SaveChanges();
                    return(waitGame);
                }
                else
                {
                    if (waitGame.Users.Name == name)
                    {
                        return(waitGame);
                    }

                    waitGame.Black           = u.id;
                    waitGame.Statuse         = "play";
                    db.Entry(waitGame).State = System.Data.Entity.EntityState.Modified;
                    db.SaveChanges();
                    return(waitGame);
                }
            }
            else
            {
                return(null);
            }
        }
Esempio n. 7
0
 static void Main(string[] args)
 {
     Chess.Chess chess = new Chess.Chess();
     while (true)
     {
         Console.WriteLine(chess.fen);
         Console.WriteLine(ChessToAscii(chess));
         string move = Console.ReadLine();
         if (move == "")
         {
             break;
         }
         chess = chess.Move(move);
     }
 }
        static void Main(string[] args)
        {
            Random random = new Random();

            Chess.Chess   chess = new Chess.Chess();
            List <String> list;

            while (true)
            {
                list = chess.GetAllMoves();
                Console.Clear();


                Console.WriteLine(chess.fen);
                //Console.WriteLine(ChessToAscii(chess));
                Print(ChessToAscii(chess));
                Console.WriteLine("Press 'q' to quit \n Press Enter and computer will make a move, or enter one of the following moves");
                if (list.Count == 0)
                {
                    Console.WriteLine("Mate! You loose.");
                    Console.WriteLine("press Enter to quit");
                    Console.ReadLine();
                }
                else
                {
                    Console.WriteLine(chess.IsCheck() ? "CHECK" : "");

                    foreach (string moves in list)
                    {
                        Console.Write(moves + "\t");
                    }

                    Console.WriteLine();

                    string move = Console.ReadLine();
                    if (move == "q")
                    {
                        break;
                    }
                    if (move == "")
                    {
                        move = list[random.Next(list.Count)];
                    }

                    chess = chess.Move(move);
                }
            }
        }
Esempio n. 9
0
        private void StartGame(string side, string enemy, int SessionID)
        {
            OpponentName   = enemy;
            this.SessionID = SessionID;
            this.side      = side;

            state = ClientState.play;
            labelServer.Invoke(new Action(() => RefreshServerControlTools()));

            wait   = true;
            active = side == "white" ? true : false;
            chess  = new Chess.Chess();
            panelBoard.Invoke(new Action(() => ShowPosition()));

            Thread ServerThread = new Thread(new ThreadStart(ProcessServer));

            ServerThread.Start();
        }
Esempio n. 10
0
        static string ChessToAscii(Chess.Chess chess)
        {
            string text = "  +-----------------+\n";

            for (int y = 7; y >= 0; y--)
            {
                text += y + 1;
                text += " | ";
                for (int x = 0; x < 8; x++)
                {
                    text += chess.GetFigureAt(x, y) + " ";
                }
                text += "|\n";
            }
            text += "  +-----------------+\n";
            text += "    a b c d e f g h\n";
            return(text);
        }
Esempio n. 11
0
        private void panel_MouseClick(object sender, MouseEventArgs e)
        {
            if (active && state == ClientState.play)
            {
                string xy = ((Panel)sender).Name.Substring(1);
                int    x  = xy[0] - '0';
                int    y  = xy[1] - '0';

                if (wait)
                {
                    wait  = false;
                    xFrom = x;
                    yFrom = y;
                }
                else
                {
                    wait = true;
                    string figure = chess.GetFigureAt(xFrom, yFrom).ToString();
                    string move   = figure +
                                    ((char)('a' + xFrom)).ToString() + ((char)('1' + yFrom)).ToString() +
                                    ((char)('a' + x)).ToString() + ((char)('1' + y)).ToString();
                    move += chess.isPromotion(move);
                    if (!chess.Equals(chess.Move(move)))
                    {
                        chess = chess.Move(move);
                        UpdateMoveTable(move);
                        if (chess.isCheckmate())
                        {
                            SendMessage($"MOVEWIN {UserName} {move}");
                        }
                        else if (chess.isStalemate())
                        {
                            SendMessage($"MOVEDRAW {UserName} {move}");
                        }
                        else
                        {
                            SendMessage($"MOVE {UserName} {move}");
                        }
                        active = false;
                    }
                }
                ShowPosition();
            }
        }
Esempio n. 12
0
        public Games MakeMove(string name, string move)
        {
            Games game = GetGame(name);

            Users curUser     = game.Users.Name == name ? game.Users : game.Users1;;
            Users opositeUser = game.Users.Name == name ? game.Users1 : game.Users;

            string userColor = UserColor(name);

            Chess.Chess chess = new Chess.Chess(game.FEN);

            string[] parts    = game.FEN.Split();
            string   curColor = parts[1];

            if (curColor[0] != userColor[0])
            {
                return(game);
            }

            var chessNext = chess.Move(move);

            if (chessNext.fen == game.FEN)
            {
                return(game);
            }

            game.FEN       = chessNext.fen;
            game.ColorMove = FlipColor(userColor);
            if (chessNext.IsCheckAndMate())
            {
                game.Statuse        = "done";
                curUser.Rating     += 1;
                opositeUser.Rating -= 1;
            }


            db.Entry(game).State = System.Data.Entity.EntityState.Modified;
            db.SaveChanges();

            return(game);
        }
Esempio n. 13
0
        private void ProcessServer()
        {
            while (true)
            {
                string msg = GetServerResponse();
                if (msg == "")
                {
                    return;
                }
                if (msg == "DESTROY")
                {
                    ProcessDestroyCommand($"game was rejected");
                    return;
                }
                else if (msg == "DRAW")
                {
                    if (MessageBox.Show("game has been ended with draw", "info", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        JoinedGameID = -1;
                        state        = ClientState.connect;
                        menuStrip1.Invoke(new Action(() => RefreshServerControlTools()));
                    }
                    return;
                }
                else if (msg == "WIN")
                {
                    if (MessageBox.Show($"you have defeated {OpponentName}", "info",
                                        MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        JoinedGameID = -1;
                        state        = ClientState.connect;
                        menuStrip1.Invoke(new Action(() => RefreshServerControlTools()));
                    }
                    return;
                }

                string request = msg.Split(' ')[0];
                if (request == "MOVE" || request == "DEFEATED" || request == "DRAW")
                {
                    string move = msg.Split(' ')[2];
                    chess  = chess.Move(move);
                    active = true;
                    lvMoves.Invoke(new Action(() => UpdateMoveTable(move)));
                    panelBoard.Invoke(new Action(() => ShowPosition()));
                }
                if (request == "DEFEATED")
                {
                    JoinedGameID = -1;
                    state        = ClientState.connect;
                    if (MessageBox.Show($"{OpponentName} has won", "info", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        menuStrip1.Invoke(new Action(() => RefreshServerControlTools()));
                    }
                    return;
                }
                if (request == "DRAW")
                {
                    JoinedGameID = -1;
                    state        = ClientState.connect;
                    if (MessageBox.Show("game has been ended with draw", "info", MessageBoxButtons.OK, MessageBoxIcon.Information) == DialogResult.OK)
                    {
                        menuStrip1.Invoke(new Action(() => RefreshServerControlTools()));
                    }
                    return;
                }
            }
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Random random = new Random();

            Chess.Chess   chess = new Chess.Chess();
            List <String> list;
            Net           connection = new Net();


            connection.Start();

            string command = String.Empty;

            if (!connection.isConnect)
            {
                Console.WriteLine("No connection to the server!");
            }
            else
            {
                while (true)
                {
                    if (command == "Start")
                    {
                        while (true)
                        {
                            list = chess.GetAllMoves();
                            Console.Clear();


                            Console.WriteLine(chess.fen);
                            //Console.WriteLine(ChessToAscii(chess));
                            Print(ChessToAscii(chess));
                            Console.WriteLine(
                                "Press 'q' to quit \n Press Enter and computer will make a move, or enter one of the following moves");
                            if (list.Count == 0)
                            {
                                Console.WriteLine("Mate!");
                                Console.WriteLine("press Enter to quit");
                                Console.ReadLine();
                            }
                            else
                            {
                                Console.WriteLine(chess.IsCheck() ? "CHECK" : "");

                                foreach (string moves in list)
                                {
                                    Console.Write(moves + "\t");
                                }

                                Console.WriteLine();

                                string move = Console.ReadLine();
                                if (move == "q")
                                {
                                    break;
                                }
                                if (move == "")
                                {
                                    move = list[random.Next(list.Count)];
                                }

                                chess = chess.Move(move);
                                connection.SendMessage("4:" + move + ":" + chess.fen);
                            }
                        }
                        command = "Quit";
                    }
                    else if (command == "GetInfo")
                    {
                        Console.Write("Input ID: ");
                        string Id = Console.ReadLine();
                        connection.SendMessage("5:" + Id);
                    }
                    else if (command == "Quit")
                    {
                        break;
                    }
                    else
                    {
                        Console.Clear();
                        Console.WriteLine("The connection to the server is established!");
                        Console.WriteLine("Commands:");
                        Console.WriteLine("'Start' - Start Game");
                        Console.WriteLine("'GetInfo' - Get information about game");
                        Console.WriteLine("'Quit' - Close application");
                        command = Console.ReadLine();
                    }
                }
            }
        }