private void button1_Click(object sender, EventArgs e) { this.Invoke(new Action(() => { labelStatus.Text = "Поиск игры"; })); if (gs == null) { if (Connection.wcfClient.AddPlayer(this.name) == "SUCCESS") { gs = Connection.wcfClient.GetGameState(this.name); thread = new Thread(runCheck); thread.IsBackground = true; thread.Start(); this.Invoke(new Action(() => { button1.Text = "Покинуть игру"; })); } } else { if (gs != null && gs.state == 1 || gs.state == 2) { MessageBox.Show("Вы покинули игру, засчитан проигрыш", "Выход", MessageBoxButtons.OK, MessageBoxIcon.Information); } Connection.wcfClient.leaveFromGame(name); resetAll(); thread.Abort(); thread.Join(500); } foreach (Control c in MainTableLayout.Controls.OfType <Panel>()) { MainTableLayout.Invoke(new Action(() => { c.BackgroundImage = null; })); } }
public void runCheck() { while (true) { try { Connection.wcfClient.client.isConnected(); } catch { Connection.MakeConnection(true); break; } gs = Connection.wcfClient.GetGameState(this.name); if (gs == null) { MessageBox.Show("Вы были кикнуты за бездействие", "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Information); resetAll(); break; } if (gs.state == 0) { labelStatus.Invoke(new Action(() => { labelStatus.Text = "Ожидание игрока"; })); } else if (gs.state == 1 || gs.state == 2) { String str = gs.players[gs.idOfPlayer(this.name)].Symbol.ToString(); if (this.labelChar.Text != str) { labelChar.Invoke(new Action(() => { labelChar.Text = str; labelChar.Visible = true; label2.Visible = true; })); } if (gs.players[gs.state - 1].name == name) { if (gs.lastAction != null) { foreach (Control c in MainTableLayout.Controls.OfType <Panel>()) { if (c.Tag.ToString().Contains(gs.lastAction)) { Panel panel = c as Panel; if (panel.BackgroundImage == null) { panel.Invoke(new Action(() => { panel.BackgroundImage = (gs.players[gs.idOfPlayer(this.name)].Symbol != 'X') ? Resource1.X : Resource1.O; })); } break; } } } labelStatus.Invoke(new Action(() => { labelStatus.Text = "Ваш ход"; })); MainTableLayout.Invoke(new Action(() => { MainTableLayout.Enabled = true; })); } else { labelStatus.Invoke(new Action(() => { labelStatus.Text = "Ходит - " + gs.players[gs.state - 1].name; })); MainTableLayout.Invoke(new Action(() => { MainTableLayout.Enabled = false; })); } } else if (gs.state == 3 || gs.state == 4) { if (gs.players[gs.state - 3].name == name) { if (gs.players[(gs.state == 3) ? 1 : 0].name == "") { labelStatus.Invoke(new Action(() => { labelStatus.Text = "Соперник покинул игру"; })); } else { labelStatus.Invoke(new Action(() => { labelStatus.Text = "Вы победили"; })); } MessageBox.Show("Поздравляем с победой!", "Победа", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (gs.lastAction != null) { foreach (Control c in MainTableLayout.Controls.OfType <Panel>()) { if (c.Tag.ToString().Contains(gs.lastAction)) { Panel panel = c as Panel; if (panel.BackgroundImage == null) { panel.Invoke(new Action(() => { panel.BackgroundImage = (gs.players[gs.idOfPlayer(this.name)].Symbol != 'X') ? Resource1.X : Resource1.O; })); } break; } } } labelStatus.Invoke(new Action(() => { labelStatus.Text = "Победил - " + gs.players[gs.state - 3].name; })); MessageBox.Show("Вы проиграли. Повезет в следующий раз!", "Поражение", MessageBoxButtons.OK, MessageBoxIcon.Information); } this.button1_Click(null, null); gs = null; break; } else if (gs.state == 5) { labelStatus.Invoke(new Action(() => { labelStatus.Text = "Ничья"; })); MessageBox.Show("Нет победивших. Конец игры", "Ничья", MessageBoxButtons.OK, MessageBoxIcon.Error); this.button1_Click(null, null); gs = null; break; } Thread.Sleep(1000); } }