public void Run() { Player player = game.Player2; NetworkStream ns = null; TcpClient client = null; try { listener = new TcpListener(IPAddress.Any, 12000); listener.Start(1); client = listener.AcceptTcpClient(); view.AddToLog(String.Format("Подключен клиент {0}\nИграем за белых", client.Client.RemoteEndPoint.ToString())); ns = client.GetStream(); string playername = ReadString(ns); int lose, win; win = ReadInt(ns); lose = ReadInt(ns); player.Name = playername; player.SetStatistic(win, lose); // send player info player = game.Player1; WriteString(ns, player.Name); WriteInt(ns, player.GetWin()); WriteInt(ns, player.GetLose()); view.HideServerBanner(); CommandLoop(ns, view, game); client.Close(); } catch (Exception e) { view.Message(e.Message); } finally { if (ns != null) { ns.Close(); } if (client != null) { client.Close(); } if (listener != null) { // Остановим его listener.Stop(); } } }
public void Run() { TcpClient client = null; NetworkStream ns = null; Player player = game.Player2; try { client = new TcpClient(); client.Connect(new IPEndPoint(IPAddress.Parse(server), port)); ns = client.GetStream(); view.AddToLog(String.Format("Подключен к серверу {0}:{1}\nИграем за черных", server, port)); WriteString(ns, player.Name); WriteInt(ns, player.GetWin()); WriteInt(ns, player.GetLose()); // get first player player = game.Player1; string playername = ReadString(ns); int lose, win; win = ReadInt(ns); lose = ReadInt(ns); player.Name = playername; player.SetStatistic(win, lose); view.Invoke(new Action(() => game.ClientGameView())); CommandLoop(ns, view, game); } catch (Exception e) { view.Message(e.Message); } finally { if (ns != null) { ns.Close(); } if (client != null) { client.Close(); } } }
public void Cell_Click(Position pos) { MyList <Position> moves, attacks, castlings, inmoveattacks; Figure fig = Field.GetFigureAt(pos); if (!isHighlighted()) { if (Hightlight(pos, out moves, out attacks, out castlings, out inmoveattacks)) { foreach (Position move in moves) { view.CellMove(move); } view.CellMove(pos); foreach (Position move in attacks) { view.CellAttack(move); } foreach (Position castle in castlings) { view.CellCastling(castle); } foreach (Position attack in inmoveattacks) { view.CellAttack(attack); } } } else { if (isCorrectMove(pos)) { view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString()); Move(pos); view.DrawField(); } if (isCorrectCastling(pos)) { view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " Рокировка " + pos.ToString()); Castle(pos); view.DrawField(); } if (isCorrectInMoveAttack(pos)) { view.AddToLog(Field.GetFigureAt(highlightedfigurepos).GetImage() + " " + highlightedfigurepos.ToString() + "-" + pos.ToString()); InMoveAttack(pos); view.DrawField(); } // снять выделение if (isHighlightedFigure(pos)) { MyList <Position> needunhighlight = Escape(); foreach (Position unhpos in needunhighlight) { view.CellDefault(unhpos); } } } view.SetTurnText(); view.WhiteCount(player1.GetCount()); view.BlackCount(player2.GetCount()); }