示例#1
0
 public void MakeMove(Board board, Move lastMove, Action<Move> continuation)
 {
     // Logik wird von GameWindow ge-highjacked.
     throw new NotImplementedException();
 }
示例#2
0
 void IPlayer.MakeMove(Board board, Move lastMove, Action<Move> continuation)
 {
     continuation(MakeMove(board));
 }
示例#3
0
        void Move(Move move)
        {
            TimeSpan elapsed = moveWatch.Elapsed;
            if (board.CurrentPlayer == player0)
            {
                if (elapsed > player0MaxMove)
                    p0Series.Title = string.Format("{0} ({1})", player0.Name, player0MaxMove = elapsed);
            } else
                if (elapsed > player1MaxMove)
                    p1Series.Title = string.Format("{0} ({1})", player1.Name, player1MaxMove = elapsed);

            if (move == null)
            {
                Result = string.Format("{0} hat aufgegeben!", board.CurrentPlayer.Name);
                CheckForTimeout(player0, player0MaxMove);
                CheckForTimeout(player1, player1MaxMove);
                Close();
                return;
            }

            Debug.WriteLine(board.CurrentPlayer.Name + " : " + move.From + " - " + move.To);
            if (board.OpposingPlayer is Server.NetworkEnemy)
                ((Server.NetworkEnemy)board.OpposingPlayer).ReportEnemyMove(move);

            IPlayer current = board.CurrentPlayer;
            board = board.MovePiece(move.From, move.To);
            p0Points.Add(new Point(moves, board.GetPoints(player0)));
            p1Points.Add(new Point(moves, board.GetPoints(player1)));
            moves++;
            ShowBoard();
            Delay(TimeSpan.FromMilliseconds(800) - elapsed, () => {
                if (!board.IsFilled && board.GetPoints(player0) > 0 && board.GetPoints(player1) > 0)
                {
                    moveWatch.Restart();
                    if (!(board.CurrentPlayer is HumanPlayer))
                        board.CurrentPlayer.MakeMove(board, board.CurrentPlayer != current ? move : null, Move);
                }
                else
                {
                    Result = string.Format("{0} hat gewonnen! {1}", (board.GetPointDelta(player0) > 0 ? player0 : player1).Name, Title);
                    CheckForTimeout(player0, player0MaxMove);
                    CheckForTimeout(player1, player1MaxMove);
                    Close();
                }
            });
        }
        /// <summary>
        ///     Starts this session
        /// </summary>
        public void Start()
        {
            // initilaize the tcp end point
            IPEndPoint ep = new IPEndPoint(IPAddress.Parse(ConfigurationManager.AppSettings[0]),
                                           int.Parse(ConfigurationManager.AppSettings[1]));
            string answer = "";
            string msg;
            string commandLine = "";

            while (true)
            {
                // connect to the server
                TcpClient client = new TcpClient();
                client.Connect(ep);
                Console.WriteLine("You are connected");
                _stream = client.GetStream();
                _reader = new MessageReader(new StreamReader(_stream));
                _writer = new MessageWriter(new StreamWriter(_stream));
                Task write = null;
                // task for read answers from the server
                Task read = new Task(() =>
                {
                    do
                    {
                        answer = _reader.ReadMessage();
                        try
                        {
                            // check if it's a move
                            Move move = Move.FromJson(answer);
                            if (move.ClientId != clientId)
                            {
                                Console.WriteLine(move.ToString());
                            }
                        }
                        catch
                        {
                            if (!answer.Contains("close"))
                            {
                                Console.WriteLine(answer);
                            }
                            // if game end
                            else
                            {
                                Console.WriteLine("Game ended!");
                            }
                            // if it's not multiple game break
                            if (!CheckIfMultiple(commandLine, answer))
                            {
                                break;
                            }
                        }
                    } while (!answer.Contains("close"));
                });
                // task for write a command to the server
                write = new Task(() =>
                {
                    while (true)
                    {
                        commandLine = Console.ReadLine();
                        _writer.WriteMessage(commandLine);
                    }
                });
                write.Start();
                read.Start();
                // wait to answer from server
                read.Wait();
                _stream.Close();
                _reader.Close();
                _writer.Close();
                client.Close();
            }
        }