private void BTN_Attaquer_Click(object sender, EventArgs e) { DGV_Attaque.ClearSelection(); DGV_Perso.ClearSelection(); if (_cibleSelectionnee) { EnvoyerAttaque(); _cibleSelectionnee = false; } }
private void RendreToutVisible(bool b) { LB_Demarrer.Visible = !b; BTN_Demarrer.Visible = !b; DGV_Attaque.Visible = b; DGV_Perso.Visible = b; label1.Visible = b; LB_GrilleAttaque.Visible = b; LB_GrillePerso.Visible = b; BTN_Attaquer.Visible = b; LB_MsgAttaque.Visible = b; panel1.Visible = b; DGV_Attaque.ClearSelection(); DGV_Perso.ClearSelection(); }
private void DGV_Attaque_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) { if (_monTour) { if (e.ColumnIndex >= 0 && e.RowIndex >= 0) { if (DGV_Attaque.Rows[e.RowIndex].Cells[e.ColumnIndex].Style.BackColor == orgStyle) { _cibleSelectionnee = true; DGV_Attaque.ClearSelection(); DGV_Attaque.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected = true; } else { _cibleSelectionnee = false; DGV_Attaque.ClearSelection(); } } } }
//Reçoit le résultat de son coup private void RecevoirReponse() { DataGridViewCellStyle toucheStyle = new DataGridViewCellStyle(); toucheStyle.BackColor = Color.Red; toucheStyle.ForeColor = Color.Red; DataGridViewCellStyle manqueStyle = new DataGridViewCellStyle(); manqueStyle.BackColor = Color.Blue; manqueStyle.ForeColor = Color.Blue; DialogResult dialogResult; string reponse; byte[] buffer = new byte[socket.SendBufferSize]; int byteLecture = socket.Receive(buffer); byte[] byteFormatter = new byte[byteLecture]; for (int i = 0; i < byteLecture; ++i) { byteFormatter[i] = buffer[i]; } reponse = Encoding.ASCII.GetString(byteFormatter); string[] tabreponse = reponse.Split(' '); //Si le coup touche un bateau if (tabreponse[0] == "True") { if (_monTour) { if (LB_MsgAttaque.InvokeRequired)//pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => LB_MsgAttaque.Text = "Touché!"; LB_MsgAttaque.Invoke(act1); Action act2 = () => LB_MsgAttaque.ForeColor = Color.Green; LB_MsgAttaque.Invoke(act2); Action act3 = () => DGV_Attaque.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = toucheStyle; DGV_Attaque.Invoke(act3); } else { LB_MsgAttaque.Text = "Touché!"; LB_MsgAttaque.ForeColor = Color.Green; DGV_Attaque.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = toucheStyle; } } else { if (LB_MsgAttaque.InvokeRequired)//pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => LB_MsgAttaque.Text = "Touché!"; LB_MsgAttaque.Invoke(act1); Action act2 = () => LB_MsgAttaque.ForeColor = Color.Red; LB_MsgAttaque.Invoke(act2); Action act3 = () => DGV_Perso.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = toucheStyle; DGV_Perso.Invoke(act3); } else { LB_MsgAttaque.Text = "Touché!"; LB_MsgAttaque.ForeColor = Color.Red; DGV_Perso.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = toucheStyle; } } if (tabreponse.Length > 3) //si tabreponse[3] existe, il contient le bateau coulé { if (LB_MsgAttaque.InvokeRequired) //pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => LB_MsgAttaque.Text = tabreponse[3] + " coulé!"; LB_MsgAttaque.Invoke(act1); } else { LB_MsgAttaque.Text = tabreponse[3] + " coulé!"; } if (tabreponse.Length > 4)//si tabreponse[4] existe, quelqu'un à gagné, le match est fini { if (tabreponse[4] == "1") { dialogResult = MessageBox.Show("Vous avez gagné \n Play Again ?", "Fini", MessageBoxButtons.YesNo); } else { dialogResult = MessageBox.Show("Vous avez perdu \n Play Again ?", "Fini", MessageBoxButtons.YesNo); } if (dialogResult == DialogResult.Yes) { envoyerMessageNouvellePartie("NouvellePartie"); System.Diagnostics.Process.Start(Application.ExecutablePath); // to start new instance of application if (this.InvokeRequired) { Action act = () => this.Dispose(); this.Invoke(act); //to turn off current app } else { Dispose(); } } else if (dialogResult == DialogResult.No) { envoyerMessageNouvellePartie("Non"); if (this.InvokeRequired) { Action act = () => this.Dispose(); this.Invoke(act); //to turn off current app } else { Dispose(); } } } } } else { if (_monTour) { if (LB_MsgAttaque.InvokeRequired)//pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => LB_MsgAttaque.Text = "Manqué!"; LB_MsgAttaque.Invoke(act1); Action act2 = () => LB_MsgAttaque.ForeColor = Color.Red; LB_MsgAttaque.Invoke(act2); Action act3 = () => DGV_Attaque.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = manqueStyle; DGV_Attaque.Invoke(act3); } else { LB_MsgAttaque.Text = "Manqué!"; LB_MsgAttaque.ForeColor = Color.Red; DGV_Attaque.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = manqueStyle; } } else { if (LB_MsgAttaque.InvokeRequired)//pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => LB_MsgAttaque.Text = "Manqué!"; LB_MsgAttaque.Invoke(act1); Action act2 = () => LB_MsgAttaque.ForeColor = Color.Green; LB_MsgAttaque.Invoke(act2); Action act3 = () => DGV_Perso.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = manqueStyle; DGV_Perso.Invoke(act3); } else { LB_MsgAttaque.Text = "Manqué!"; LB_MsgAttaque.ForeColor = Color.Green; DGV_Perso.Rows[Int32.Parse(tabreponse[2])].Cells[Int32.Parse(tabreponse[1])].Style = manqueStyle; } } } if (BTN_Attaquer.InvokeRequired)//pour exécuter un délégué qui met à jour le thread d'interface utilisateur { Action act1 = () => BTN_Attaquer.Enabled = !BTN_Attaquer.Enabled; BTN_Attaquer.Invoke(act1); Action act2 = () => DGV_Attaque.Refresh(); DGV_Attaque.Invoke(act2); Action act3 = () => this.Refresh(); this.Invoke(act3); } else { BTN_Attaquer.Enabled = !BTN_Attaquer.Enabled; DGV_Attaque.Refresh(); this.Refresh(); } _monTour = !_monTour; if (!_monTour) { RecevoirReponse(); } }