using System; 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; namespace go01 { public class Goban { public PictureBox Conteneur; int ptBlanc = 0, ptNoir = 0; public Plateau Fenetre; bool win = false; Label LabelPlayerTurn; public string PlayerTurn { get { return this.LabelPlayerTurn.Text; } } string playerWName { get => cfg.NomJoeurBlanc; } string playerBName{ get => cfg.NomJoeurNoir; } List pboxList = new List(); Joueur_E tour; Joueur_E Tour { get { return tour; } set { tour = value; try { this.LabelPlayerTurn.Text = (tour == Joueur_E.Blanc) ? playerWName : playerBName; this.LabelPlayerTurn.ForeColor = (tour == Joueur_E.Blanc) ? Color.Gray : Color.Black; this.LabelPlayerTurn.Location = new Point(Conteneur.Location.X + Conteneur.Width + ((Fenetre.ClientSize.Width - Conteneur.Location.X - Conteneur.Width - LabelPlayerTurn.Width) / 2), Conteneur.Location.Y); } catch { Console.WriteLine("ERROR GOBAN LabelPlayerTurn "); } } } 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 { SG = 0b00000001, S = 0b00000010, SD = 0b00000100, G = 0b00001000, D = 0b00010000, IG = 0b00100000, I = 0b01000000, ID = 0b10000000 } [Serializable] public struct sauvegarde_S { public ConfigGo_S cfg; public int ptBlanc, ptNoir; public bool win; public Goban.Occupant_E[,] tableauOccupation; public Goban.Joueur_E tour; public sauvegarde_S(int pointBlanc, int pointNoir, Goban.Occupant_E[,] tableauOccupation, Goban.Joueur_E tour, bool win, ConfigGo_S cfg) { this.ptBlanc = pointBlanc; this.ptNoir = pointNoir; this.win = win; this.tableauOccupation = tableauOccupation; this.tour = tour; this.cfg = cfg; } } struct carrefour_S { public Point point; public Deplacement_E SensDeDeplacment; public carrefour_S(Point p, Deplacement_E sens) { point = p; 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; this.cfg = s.cfg; this.Conteneur = conteneur; this.Fenetre = fenetre; fenetre.lblBlanc = playerWName + ":0"; fenetre.lblNoir = playerBName + ":0"; GenerationPlateau(false); //redrawAllpb(); } public Goban(Plateau fenetre, PictureBox conteneur, ConfigGo_S cfg) { int x = 0; this.Conteneur = conteneur; this.Fenetre = fenetre; this.cfg = cfg; fenetre.lblBlanc = playerWName + ":0"; fenetre.lblNoir = playerBName + ":0"; Tour = Joueur_E.Noir; //gen des tab 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) { PictureBox pb = (PictureBox)sender; String[] xy = ((PictureBox)sender).Name.Split(';'); Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}"); int x = int.Parse(xy[0]), y = int.Parse(xy[1]); if (tableauOccupation[x, y] != Occupant_E.Vide && !win) { //tableauOccupation[x, y] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir; Graphics gr = e.Graphics; SolidBrush b = (tableauOccupation[x, y] == Occupant_E.Blanc) ? new SolidBrush(Color.White) : new SolidBrush(Color.Black); ; if (b != null) gr.FillEllipse(b, 1, 0, pb.ClientSize.Width - 2, pb.ClientSize.Height - 1); else gr.DrawImage(pb.Image, 0, 0, pb.Width, pb.Height); } } private void pictureBox1_Click(object sender, EventArgs e) { PictureBox pb = (PictureBox)sender; String[] xy = ((PictureBox)sender).Name.Split(';'); Console.WriteLine($"Click sur {xy[0]};{xy[1]} par {Tour}"); int x = int.Parse(xy[0]), y = int.Parse(xy[1]); if (tableauOccupation[x, y] == Occupant_E.Vide && !win) { tableauOccupation[x, y] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir; using (Graphics gr = Graphics.FromHwnd((pb.Handle))) { SolidBrush b = (Tour == Joueur_E.Blanc)?new SolidBrush(Color.White):new SolidBrush(Color.Black);; if (b != null) gr.FillEllipse(b, 1, 0, pb.ClientSize.Width - 2, pb.ClientSize.Height - 1); else gr.DrawImage(pb.Image, 0, 0, pb.Width, pb.Height); } bool fin = win = RechercheTeritoir(x, y); if (fin) { x=((Tour == Joueur_E.Blanc)) ? ptNoir++:ptBlanc++; Win(true); MessageBox.Show("YOUR WIN" + ((Tour == Joueur_E.Blanc) ? playerWName : playerBName)); } Console.WriteLine(fin); if(!win) Tour = (Tour + 1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour + 1; } } private int[] GetXY(int i) { return new int[2] { i % cfg.taille, i / cfg.taille }; } private int[] GetXYStr(string s) { String[] xy = s.Split(';'); return new int[2] { int.Parse(xy[0]), int.Parse(xy[1]) }; } private int GetX(int i) { return i % cfg.taille; } private int GetY(int i) { return i / cfg.taille; } bool RechercheTeritoir(int x, int y) { Point origine = new Point(x, y); int pionAligneDsRecherche = 1; 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) return false; foreach (carrefour_S point in pointsSuivant) { pionAligneDsRecherche = 2; tmpParcour[point.point.X, point.point.Y] = 1; List parcour = RecherchePointsSuivant(point.point, point.SensDeDeplacment); while(parcour.Count > 0) { pionAligneDsRecherche++; tmpParcour[parcour[0].point.X, parcour[0].point.Y] = 1; parcour = RecherchePointsSuivant(parcour[0].point, parcour[0].SensDeDeplacment); printTab(tmpParcour); } Console.WriteLine(pionAligneDsRecherche); if (pionAligneDsRecherche >= cfg.pionaAligner*(Math.Ceiling((double)pionAligneDsRecherche/(double)cfg.pionaAligner))) return true; } return false; } bool RechercheTeritoir(Point p) { return RechercheTeritoir(p.X, p.Y); } List RecherchePointsSuivant(Point p, Deplacement_E deplacement) { List PointsTrouver = new List(); Occupant_E occupant = (Tour == Joueur_E.Blanc)?Occupant_E.Blanc:Occupant_E.Noir; if (p.Y-1>=0 && tableauOccupation[p.X,p.Y-1] == occupant && (deplacement & Deplacement_E.S) == Deplacement_E.S) { PointsTrouver.Add(new carrefour_S(new Point(p.X,p.Y-1),Deplacement_E.S)); } 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 < 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 < cfg.taille && p.Y+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)); } if (p.X - 1 >= 0 && tableauOccupation[p.X-1,p.Y] == occupant && (deplacement & Deplacement_E.G) == Deplacement_E.G) { PointsTrouver.Add(new carrefour_S(new Point(p.X-1,p.Y),Deplacement_E.G)); } if (p.Y - 1 >= 0 && p.X - 1 >= 0 && tableauOccupation[p.X-1,p.Y-1] == occupant && (deplacement & Deplacement_E.SG) == Deplacement_E.SG) { PointsTrouver.Add(new carrefour_S(new Point(p.X-1,p.Y-1),Deplacement_E.SG)); } return PointsTrouver; } Boolean ptEgal(Point ptBlanc, Point ptNoir) { return ptBlanc.X == ptNoir.X && ptBlanc.Y == ptNoir.Y; } void printTab(int[,] Tab) { for(int y = 0;y