private void mnGame_Click(object sender, EventArgs e) { if (this.InGame) { return; } //На всякий случай :) try { frmLogin loginForm = new frmLogin(); if (loginForm.ShowDialog() == System.Windows.Forms.DialogResult.OK) { this.cFactory = new ChannelFactory<TicTacService>("ENDPOINT"); this.ticTacClient = cFactory.CreateChannel(); if (this.ticTacClient.Login(loginForm.UserLogin) == true) { this.InGame = true; this.login = loginForm.UserLogin; this.Text = "TicTac - " + this.login; GameState gameState = this.ticTacClient.GetState(this.login); this.character = (gameState.firstPlayer == this.login) ? GameState.FIELD_STATE.CROSS : GameState.FIELD_STATE.ZERO; this.getGameThr = new Thread(GetGame); this.getGameThr.IsBackground = true; this.getGameThr.Start(); } else { string message = "Не удалось присоединиться к игре. Возможно уже существует такой логин. Возможно другие проблемы с сервером. Попробуйте позже"; MessageBox.Show(message, "TicTac", MessageBoxButtons.OK, MessageBoxIcon.Information); } } } catch (Exception exc) { Console.WriteLine(exc.Message); Console.WriteLine(exc.StackTrace); MessageBox.Show(exc.Message, "Соединение с сервером", MessageBoxButtons.OK, MessageBoxIcon.Warning); } }
private void SetGameState() { try { GameState gs; lock (this) { gs = this.ticTacClient.GetState(this.login); } //Игровое поле Image img; for (int i = 0; i < gs.field.Length; i++) { if (gs.field[i] == GameState.FIELD_STATE.FREE) { this.Invoke(new Action(() => this.pbBoxes[i].Image = null)); } else { img = (gs.field[i] == GameState.FIELD_STATE.CROSS) ? Resources.cross : Resources.zero; this.Invoke(new Action(() => this.pbBoxes[i].Image = img)); } } //Окончание игры if (gs.state == GameState.GAME_STATE.END_GAME) { Console.WriteLine("GAME OVER"); this.ticTacClient.ExitPlayer(this.login); this.InGame = false; this.IsTurn = false; this.ticTacClient = null; this.cFactory = null; this.Invoke(new Action(() => this.sbStatus.Text = "Игра завершена")); this.Invoke(new Action(() => this.Text = "TicTac")); string message; if (gs.currentPlayer == null) { message = "Ничья!"; } else { message = (gs.currentPlayer == this.login) ? "Вы выйграли :)" : "Вы проиграли :("; } MessageBox.Show(message, "Результат", MessageBoxButtons.OK, MessageBoxIcon.Information); return; } //Текущее состояние if (gs.state == GameState.GAME_STATE.WAIT_SECOND_PLAYER) { this.Invoke(new Action(() => this.sbStatus.Text = "Ожидаем соперника...")); return; } if (gs.currentPlayer == this.login) { this.Invoke(new Action(() => this.sbStatus.Text = "Ваш ход")); this.IsTurn = true; } else { this.Invoke(new Action(() => this.sbStatus.Text = "Ожидаем ход соперника...")); this.IsTurn = false; } } catch (Exception e) { Console.WriteLine(e.Message); Console.WriteLine(e.StackTrace); } }