diff --git a/go01/CreerPartieReseau_serv.cs b/go01/CreerPartieReseau_serv.cs index 45cb2d6..d86a2e8 100644 --- a/go01/CreerPartieReseau_serv.cs +++ b/go01/CreerPartieReseau_serv.cs @@ -37,7 +37,7 @@ namespace go01 var startTimeSpan = TimeSpan.Zero; var periodTimeSpan = TimeSpan.FromSeconds(5); - socketPlateauBase.ConfigGo_S config = new socketPlateauBase.ConfigGo_S(tbUid.Text, tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text)); + Goban.ConfigGo_S config = new Goban.ConfigGo_S(tbPartName.Text, "", tbNomJoeur.Text, int.Parse(tbTaille.Text), int.Parse(tbPionaAligner.Text), IPAddress.Parse(tbIp.Text), int.Parse(tbPort.Text)); serveur = new socketPlateauSRV(config, null, new socketPlateauSRV.OnJoinServ(ConnectionEtablie)); serveur.AttenteJoueur(); @@ -45,10 +45,10 @@ namespace go01 } private void ConnectionEtablie(object o, socketPlateauServJoinArgs e) { - Console.WriteLine($"{e.ConfigGo.NomJoeurClient} vs {e.ConfigGo.NomJoeurServeur}"); + Console.WriteLine($"{e.ConfigGo.NomJoeurBlanc} vs {e.ConfigGo.NomJoeurNoir}"); serveur.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, new socketPlateauServACKArgs(true))); Console.WriteLine("CONNETION ETABLEIE"); - Plateau p = new Plateau(e.ConfigGo.Taille, e.ConfigGo.PionAligner, e.ConfigGo.NomJoeurServeur, e.ConfigGo.NomJoeurClient); + Plateau p = new Plateau(e.ConfigGo.taille, e.ConfigGo.PionAligner, e.ConfigGo.NomJoeurNoir, e.ConfigGo.NomJoeurBlanc); //this.Hide(); p.ShowDialog(); //this.Close(); diff --git a/go01/Goban.cs b/go01/Goban.cs index 1bd0965..72f1ad5 100644 --- a/go01/Goban.cs +++ b/go01/Goban.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; +using System.Net; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; @@ -12,22 +13,16 @@ namespace go01 { public class Goban { - - - - public PictureBox Conteneur; - int nombreDePionAAligne; int ptBlanc = 0, ptNoir = 0; public Plateau Fenetre; bool win = false; - public int dim; Label LabelPlayerTurn; public string PlayerTurn { get { return this.LabelPlayerTurn.Text; } } - string playerWName, playerBName; + string playerWName { get => cfg.NomJoeurBlanc; } + string playerBName{ get => cfg.NomJoeurNoir; } List pboxList = new List(); - public enum Occupant_E { Vide, Blanc, Noir, LAST } - public enum Joueur_E { Blanc, Noir, LAST } + Joueur_E tour; Joueur_E Tour @@ -52,8 +47,11 @@ namespace go01 } } Occupant_E[,] tableauOccupation; + ConfigGo_S cfg; - + #region struct&enum + public enum Occupant_E { Vide, Blanc, Noir, LAST } + public enum Joueur_E { Blanc, Noir, LAST } [Flags] enum Deplacement_E { @@ -69,20 +67,18 @@ namespace go01 [Serializable] public struct sauvegarde_S { - public string nomBlanc, nomNoir; + public ConfigGo_S cfg; public int ptBlanc, ptNoir; - public int dimGoban, nbAlignementToWin; public bool win; public Goban.Occupant_E[,] tableauOccupation; public Goban.Joueur_E tour; - public sauvegarde_S(string nomBlanc, string nomNoir, int pointBlanc, int pointNoir, int nbAlignementToWin, int dimGoban, Goban.Occupant_E[,] tableauOccupation, Goban.Joueur_E tour, bool win) + public sauvegarde_S(int pointBlanc, int pointNoir, Goban.Occupant_E[,] tableauOccupation, Goban.Joueur_E tour, bool win, ConfigGo_S cfg) { - this.nomBlanc = nomBlanc; this.nomNoir = nomNoir; this.ptBlanc = pointBlanc; this.ptNoir = pointNoir; - this.dimGoban = dimGoban; this.nbAlignementToWin = nbAlignementToWin; this.win = win; this.tableauOccupation = tableauOccupation; this.tour = tour; + this.cfg = cfg; } } struct carrefour_S @@ -95,16 +91,48 @@ namespace go01 SensDeDeplacment = sens; } } + [Serializable] + public struct ConfigGo_S + { + public readonly string Uid; + private string partName; + public string PartName { get { return partName; } } + public string NomJoeurBlanc; + public string NomJoeurNoir; + public readonly int taille; + public readonly int pionaAligner; + public int PionAligner { get { return pionaAligner; } } + public readonly IPAddress IpServeur; + public readonly int port; + + public ConfigGo_S(string PartName, string NomJoeurBlanc, string NomJoeurNoir, int Taille, int PionaAligner, IPAddress ipServeur=null, int port=-1) + { + this.Uid = Guid.NewGuid().ToString(); ; + this.partName = PartName; + this.NomJoeurBlanc = NomJoeurBlanc; + this.NomJoeurNoir = NomJoeurNoir; + this.taille = Taille; + this.pionaAligner = PionaAligner; + this.IpServeur = ipServeur; + this.port = port; + } + public override string ToString() + { + return partName; + } + public static implicit operator string(ConfigGo_S part) + { + return part.ToString(); + } + } + #endregion public Goban(Plateau fenetre, PictureBox conteneur, sauvegarde_S s) { tableauOccupation = s.tableauOccupation; - playerBName = s.nomNoir; - playerWName = s.nomBlanc; - nombreDePionAAligne = s.nbAlignementToWin; - dim = s.dimGoban; + this.cfg = s.cfg; this.Conteneur = conteneur; this.Fenetre = fenetre; @@ -115,29 +143,22 @@ namespace go01 GenerationPlateau(false); //redrawAllpb(); } - public Goban(Plateau fenetre, PictureBox conteneur, socketPlateauBase.ConfigGo_S cfg) : this(fenetre, conteneur, cfg.Taille, cfg.PionAligner, cfg.NomJoeurClient, cfg.NomJoeurServeur) - {} - public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB) + public Goban(Plateau fenetre, PictureBox conteneur, ConfigGo_S cfg) { int x = 0; - nombreDePionAAligne = nbPionAlignn; this.Conteneur = conteneur; - this.dim = dim; this.Fenetre = fenetre; - - playerWName = playerW; - playerBName = playerB; - - fenetre.lblBlanc = playerB + ":0"; - fenetre.lblNoir = playerW + ":0"; - - - + this.cfg = cfg; + fenetre.lblBlanc = playerWName + ":0"; + fenetre.lblNoir = playerBName + ":0"; Tour = Joueur_E.Noir; - //gen des tab - tableauOccupation = new Occupant_E[dim, dim]; + tableauOccupation = new Occupant_E[cfg.taille, cfg.taille]; GenerationPlateau(); + } + public Goban(Plateau fenetre, PictureBox conteneur, int dim, int nbPionAlignn, string playerW, string playerB) : this(fenetre,conteneur, new ConfigGo_S("",playerW,playerB,dim,nbPionAlignn)) + { + } public void PaintPb(object sender, PaintEventArgs e) { @@ -191,7 +212,7 @@ namespace go01 } private int[] GetXY(int i) { - return new int[2] { i % dim, i / dim }; + return new int[2] { i % cfg.taille, i / cfg.taille }; } private int[] GetXYStr(string s) { @@ -200,11 +221,11 @@ namespace go01 } private int GetX(int i) { - return i % dim; + return i % cfg.taille; } private int GetY(int i) { - return i / dim; + return i / cfg.taille; } bool RechercheTeritoir(int x, int y) @@ -213,7 +234,7 @@ namespace go01 int pionAligneDsRecherche = 1; - int[,] tmpParcour = new int[dim, dim]; + int[,] tmpParcour = new int[cfg.taille, cfg.taille]; tmpParcour[origine.X, origine.Y] = 1; ListpointsSuivant = RecherchePointsSuivant(origine,(Deplacement_E)0xff);//Recherche les point autour du pion if (pointsSuivant.Count == 0) @@ -231,7 +252,7 @@ namespace go01 printTab(tmpParcour); } Console.WriteLine(pionAligneDsRecherche); - if (pionAligneDsRecherche >= nombreDePionAAligne*(Math.Ceiling((double)pionAligneDsRecherche/(double)nombreDePionAAligne))) + if (pionAligneDsRecherche >= cfg.pionaAligner*(Math.Ceiling((double)pionAligneDsRecherche/(double)cfg.pionaAligner))) return true; } return false; @@ -248,23 +269,23 @@ namespace go01 { PointsTrouver.Add(new carrefour_S(new Point(p.X,p.Y-1),Deplacement_E.S)); } - if (p.Y - 1 >= 0 && p.X+1 < dim && tableauOccupation[p.X+1,p.Y-1] == occupant && (deplacement & Deplacement_E.SD) == Deplacement_E.SD) + if (p.Y - 1 >= 0 && p.X+1 < cfg.taille && tableauOccupation[p.X+1,p.Y-1] == occupant && (deplacement & Deplacement_E.SD) == Deplacement_E.SD) { PointsTrouver.Add(new carrefour_S(new Point(p.X+1,p.Y-1),Deplacement_E.SD)); } - if (p.X + 1 < dim && tableauOccupation[p.X+1,p.Y] == occupant && (deplacement & Deplacement_E.D) == Deplacement_E.D) + if (p.X + 1 < cfg.taille && tableauOccupation[p.X+1,p.Y] == occupant && (deplacement & Deplacement_E.D) == Deplacement_E.D) { PointsTrouver.Add(new carrefour_S(new Point(p.X+1,p.Y),Deplacement_E.D)); } - if (p.X + 1 < dim && p.Y+1=0 && p.Y + 1 < dim && tableauOccupation[p.X-1,p.Y+1] == occupant && (deplacement & Deplacement_E.IG) == Deplacement_E.IG) + if (p.X-1>=0 && p.Y + 1 < cfg.taille && tableauOccupation[p.X-1,p.Y+1] == occupant && (deplacement & Deplacement_E.IG) == Deplacement_E.IG) { PointsTrouver.Add(new carrefour_S(new Point(p.X-1,p.Y+1),Deplacement_E.IG)); } @@ -285,9 +306,9 @@ namespace go01 void printTab(int[,] Tab) { - for(int y = 0;y { ajoutPartie(cfgGo, ClientEp.Address.ToString()); })); } @@ -104,11 +104,11 @@ namespace go01 } } - public void ajoutPartie(socketPlateauBase.ConfigGo_S cfgGo, string ip) + public void ajoutPartie(Goban.ConfigGo_S cfgGo, string ip) { if(dtListPart.Select($"uid = '{cfgGo.Uid}'").Length==0) - dtListPart.Rows.Add(cfgGo.Uid, cfgGo, cfgGo.NomJoeurServeur, cfgGo.IpServeur, cfgGo.port, DateTime.Now); + dtListPart.Rows.Add(cfgGo.Uid, cfgGo, cfgGo.NomJoeurBlanc==""? cfgGo.NomJoeurNoir: cfgGo.NomJoeurBlanc, cfgGo.IpServeur, cfgGo.port, DateTime.Now); //lock (DgvPart) ; dtListPart.Select($"uid = '{cfgGo.Uid}'")[0]["timeStamp"] = DateTime.Now; DgvPart.DataSource = dtListPart; @@ -124,7 +124,6 @@ namespace go01 dgvPart.Rows.Remove(row); dgvPart.Refresh(); })); - } } } @@ -149,7 +148,7 @@ namespace go01 { if (dgvPart.SelectedRows.Count <= 0) return; - cfg = (socketPlateauBase.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; + cfg = (Goban.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; tbIpServ.Text = cfg.IpServeur.ToString(); tbPort.Text = cfg.port.ToString(); } @@ -158,8 +157,13 @@ namespace go01 { if (dgvPart.SelectedRows.Count <= 0) return; - cfg = (socketPlateauBase.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; - cfg.NomJoeurClient = tbPlayername.Text; + cfg = (Goban.ConfigGo_S)dgvPart.SelectedRows[0].Cells["partName"].Value; + if(cfg.NomJoeurBlanc == "") + cfg.NomJoeurBlanc = tbPlayername.Text; + else + { + cfg.NomJoeurBlanc = tbPlayername.Text; + } socketPlateauCLI sc = new socketPlateauCLI(cfg, null, new socketPlateauCLI.OnConnected(SocketCoonected)); /*sc. sc.senCmd(new socketPlateauBase.CommandeSocket_S(socketPlateauBase.commande_E.Join, cfg));*/ @@ -173,9 +177,11 @@ namespace go01 //################################################################################################ //################################################################################################ Console.WriteLine("CONNECTION ETABLEIE"); - Plateau p = new Plateau(cfg.Taille, cfg.PionAligner, cfg.NomJoeurServeur, cfg.NomJoeurClient); + p = new Plateau(cfg.taille, cfg.PionAligner, cfg.NomJoeurBlanc, cfg.NomJoeurNoir); + p.Show(); + //this.Hide(); - p.ShowDialog(); + // p.ShowDialog(); //this.Close(); } } diff --git a/go01/go01.csproj b/go01/go01.csproj index dcb358b..bee09c3 100644 --- a/go01/go01.csproj +++ b/go01/go01.csproj @@ -84,8 +84,8 @@ - + CreerPartieReseau_serv.cs diff --git a/go01/socketPlateauBase.cs b/go01/socketPlateauBase.cs index 8685659..edd2640 100644 --- a/go01/socketPlateauBase.cs +++ b/go01/socketPlateauBase.cs @@ -30,44 +30,6 @@ namespace go01 data = dt; } } - [Serializable] - public struct ConfigGo_S - { - private string uid; - public string Uid { get { return uid; } } - private string partName; - public string PartName { get { return partName; } } - public string NomJoeurClient; - private string nomJoeurServeur; - public string NomJoeurServeur { get { return nomJoeurServeur; } } - private int taille; - public int Taille { get { return taille; } } - private int pionaAligner; - public int PionAligner { get { return pionaAligner; } } - private IPAddress ipServeur; - public IPAddress IpServeur { get => ipServeur; } - public readonly int port; - - public ConfigGo_S(string Uid, string PartName, string NomJoeurClient, string NomJoeurServeur, int Taille, int PionaAligner, IPAddress ipServeur, int port) - { - this.uid = Uid; - this.partName = PartName; - this.NomJoeurClient = NomJoeurClient; - this.nomJoeurServeur = NomJoeurServeur; - this.taille = Taille; - this.pionaAligner = PionaAligner; - this.ipServeur = ipServeur; - this.port = port; - } - public override string ToString() - { - return partName; - } - public static implicit operator string(ConfigGo_S part) - { - return part.ToString(); - } - } #endregion #region variable protected Socket socServ, socCli; @@ -95,13 +57,12 @@ namespace go01 protected static Boolean connexionEtablie = false; protected Goban plateauDeJeu; - protected ConfigGo_S cfgGo; + protected Goban.ConfigGo_S cfgGo; #endregion public delegate void OnReceived(object myObject, socketPlateauServReception myArgs); public event OnReceived onReceived; - - public socketPlateauBase(TypeSocket_E socF, ConfigGo_S cfgGo, Goban g) + public socketPlateauBase(TypeSocket_E socF, Goban.ConfigGo_S cfgGo, Goban g) { plateauDeJeu = g; adresseIpCourante = cfgGo.IpServeur; @@ -116,25 +77,10 @@ namespace go01 var netStream = new NetworkStream(sArg, true); var binaryFormatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); CommandeSocket_S cmd = (CommandeSocket_S)binaryFormatter.Deserialize(netStream); - try { onReceived(this, new socketPlateauServReception(cmd)); } catch { } - switch (cmd.commande) - { - case commande_E.Join: - break; - case commande_E.Leave: - break; - case commande_E.Place: - break; - case commande_E.WhoTurn: - break; - case commande_E.Win: - break; - } - Received(cmd); + try { onReceived?.Invoke(this, new socketPlateauServReception(cmd)); } catch (Exception e) { Console.WriteLine(e); } InitialiserReception(sArg); } - protected abstract void Received(CommandeSocket_S cmd); - public void senCmd(CommandeSocket_S cmd) + public void senCmd(object cmd) { var netStream = new NetworkStream(socCli, true); diff --git a/go01/socketPlateauCLI.cs b/go01/socketPlateauCLI.cs index 9d7b167..fdc5969 100644 --- a/go01/socketPlateauCLI.cs +++ b/go01/socketPlateauCLI.cs @@ -16,7 +16,7 @@ namespace go01 public delegate void OnJoinACK(object myObject, socketPlateauServACKArgs myArgs); public event OnJoinACK onJoinACK; - public socketPlateauCLI(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Client, cfgGo, g) + public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Client, cfgGo, g) { try { @@ -29,11 +29,11 @@ namespace go01 MessageBox.Show("Connexion impossible : " + ee.Message); } } - public socketPlateauCLI(ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) + public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) { this.onJoinACK = onJoinACK; } - public socketPlateauCLI(ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) + public socketPlateauCLI(Goban.ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) { this.onConnected = onConnected; } @@ -43,17 +43,12 @@ namespace go01 Socket sTmp = (Socket)iAR.AsyncState; if (sTmp.Connected) { connexionEtablie = true; - try { onConnected(this, new socketPlateauConnected(cfgGo)); } catch { } - senCmd(new CommandeSocket_S(commande_E.Join, cfgGo)); + try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); } InitialiserReception(sTmp); } else { MessageBox.Show("Serveur innacessible"); } } - protected override void Received(CommandeSocket_S cmd) - { - throw new NotImplementedException(); - } } } diff --git a/go01/socketPlateauEventArgs.cs b/go01/socketPlateauEventArgs.cs index 3912ae5..b39d007 100644 --- a/go01/socketPlateauEventArgs.cs +++ b/go01/socketPlateauEventArgs.cs @@ -8,8 +8,8 @@ namespace go01 { public class socketPlateauServJoinArgs : EventArgs { - public socketPlateauBase.ConfigGo_S ConfigGo; - public socketPlateauServJoinArgs(socketPlateauBase.ConfigGo_S cfg) + public Goban.ConfigGo_S ConfigGo; + public socketPlateauServJoinArgs(Goban.ConfigGo_S cfg) { this.ConfigGo = cfg; } @@ -40,9 +40,9 @@ namespace go01 } public class socketPlateauConnected : EventArgs { - public socketPlateauBase.ConfigGo_S ConfigGo; + public Goban.ConfigGo_S ConfigGo; - public socketPlateauConnected(socketPlateauBase.ConfigGo_S cfg) + public socketPlateauConnected(Goban.ConfigGo_S cfg) { this.ConfigGo = cfg; } diff --git a/go01/socketPlateauInterface.cs b/go01/socketPlateauInterface.cs deleted file mode 100644 index 2b793b4..0000000 --- a/go01/socketPlateauInterface.cs +++ /dev/null @@ -1,14 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net.Sockets; -using System.Text; -using System.Threading.Tasks; - -namespace go01 -{ - public interface socketPlateauInterface - { - void InitialiserReception(Socket sArg); - } -} diff --git a/go01/socketPlateauSRV.cs b/go01/socketPlateauSRV.cs index 85a5090..63b4678 100644 --- a/go01/socketPlateauSRV.cs +++ b/go01/socketPlateauSRV.cs @@ -27,20 +27,21 @@ namespace go01 public event OnDemandeCo onDemandeCo; public delegate void OnConnected(object myObject, socketPlateauConnected myArgs); public event OnConnected onConnected; + public event EventHandler onReceive; #endregion - public socketPlateauSRV(ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Serveur, cfgGo, g) + public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g) : base(TypeSocket_E.Serveur, cfgGo, g) { } - public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) + public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnJoinACK onJoinACK) : this(cfgGo, g) { this.onJoinACK = onJoinACK; } - public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) + public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnConnected onConnected) : this(cfgGo, g) { this.onConnected = onConnected; } - public socketPlateauSRV(ConfigGo_S cfgGo, Goban g, OnJoinServ onJoinServ) : this(cfgGo, g) + public socketPlateauSRV(Goban.ConfigGo_S cfgGo, Goban g, OnJoinServ onJoinServ) : this(cfgGo, g) { this.onJoinServ = onJoinServ; } @@ -79,12 +80,9 @@ namespace go01 { try { - onDemandeCo(this, new socketPlateauServDemandeCo()); - } - catch - { - + onDemandeCo?.Invoke(this, new socketPlateauServDemandeCo()); } + catch (Exception e) { Console.WriteLine(e); } if (socFlag == TypeSocket_E.Serveur && !connexionEtablie) { Socket sTmp = (Socket)iAR.AsyncState; @@ -94,7 +92,7 @@ namespace go01 Console.WriteLine($"Connexion effectuée par {((IPEndPoint)socCli.RemoteEndPoint).Address}"); #endif connexionEtablie = true; - try { onConnected(this, new socketPlateauConnected(cfgGo)); } catch { } + try { onConnected?.Invoke(this, new socketPlateauConnected(cfgGo)); } catch (Exception e) { Console.WriteLine(e); } timerBroadcast.Dispose(); InitialiserReception(socCli); } @@ -106,23 +104,5 @@ namespace go01 socCli.Close(); } } - - protected override void Received(CommandeSocket_S cmd) - { - switch (cmd.commande) - { - case commande_E.Join: - try{ onJoinServ(this, new socketPlateauServJoinArgs((ConfigGo_S)cmd.data)); } catch { } - break; - case commande_E.Leave: - break; - case commande_E.Place: - break; - case commande_E.WhoTurn: - break; - case commande_E.Win: - break; - } - } } }