public void NewGame(Client first, Client second) { Random random = new Random(); int order = random.Next(0, 2); first.SendMessage(protocol.ToMessage(new Command(Command.Commands.StartGame, order.ToString()))); second.SendMessage(protocol.ToMessage(new Command(Command.Commands.StartGame, (order ^ 1).ToString()))); Game game = new Game(first, second, order); game.GameFinishEvent += GameFinish; gameList.Add(game); //move to game new Thread(delegate() { Thread.Sleep(500); if (order == 1) { first.SendMessage(protocol.ToMessage(new Command(Command.Commands.YourStep, game.StepPlayerGameField))); second.SendMessage(protocol.ToMessage(new Command(Command.Commands.OpponentStep, game.NextPlayerGameField))); } else { first.SendMessage(protocol.ToMessage(new Command(Command.Commands.OpponentStep, game.StepPlayerGameField))); second.SendMessage(protocol.ToMessage(new Command(Command.Commands.YourStep, game.NextPlayerGameField))); } }).Start(); }
//methods public void RiseEvent(Game game, Client client, Command command) { switch (command.CurrentCommand) { case "Confirm": ConfirmEvent(game, client); break; case "Reject": RejectEvent(game, client); break; case "Move": MoveEvent(game, client, command.Data); break; case "Exit": ExitEvent(game, client); break; } }
public GameOverEventArgs(Game game) { this.game = game; }
private void Reject(Game game, Client client) { gameList.Remove(game); game.Second.SendMessage(protocol.ToMessage(new Command(Command.Commands.Exit, ""))); game.First.SendMessage(protocol.ToMessage(new Command(Command.Commands.Exit, ""))); FreeUserEvent(client); }
private void Move(Game game, Client client, string message) { string messageToStepPlayer, messageToNextStepPlayer; if (game.Move(message, client.Name, out messageToStepPlayer, out messageToNextStepPlayer)) { game.StepPlayer.SendMessage(protocol.ToMessage(new Command(Command.Commands.YourStep, messageToStepPlayer))); game.NextStepPlayer.SendMessage(protocol.ToMessage(new Command(Command.Commands.OpponentStep, messageToNextStepPlayer))); } else { Stop(); } }
private void GameFinish(Game game, object result) { switch (result as string) { case "FirstWin": game.First.SendMessage(protocol.ToMessage(new Command(Command.Commands.Win, ""))); game.Second.SendMessage(protocol.ToMessage(new Command(Command.Commands.Lose, ""))); break; case "SecondWin": game.First.SendMessage(protocol.ToMessage(new Command(Command.Commands.Lose, ""))); game.Second.SendMessage(protocol.ToMessage(new Command(Command.Commands.Win, ""))); break; case "Tie": game.First.SendMessage(protocol.ToMessage(new Command(Command.Commands.Tie, ""))); game.Second.SendMessage(protocol.ToMessage(new Command(Command.Commands.Tie, ""))); break; } GameOverEvent(null, new GameOverEventArgs(game)); new Thread(delegate() { Thread.Sleep(300); game.First.SendMessage(protocol.ToMessage(new Command(Command.Commands.Continue, ""))); game.Second.SendMessage(protocol.ToMessage(new Command(Command.Commands.Continue, ""))); }).Start(); }
private void Confirm(Game game, Client client) { if (game.IsClosed) { return; } if (game.IsFinished) { game.ContinueFlag++; } if (game.ContinueFlag == 2) { gameList.Remove(game); Client first = game.First; Client second = game.Second; Thread.Sleep(50); NewGame(first, second); } }