using System; using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; 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; 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 { 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; [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 string nomBlanc, nomNoir; 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) { 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; } } struct carrefour_S { public Point point; public Deplacement_E SensDeDeplacment; public carrefour_S(Point p, Deplacement_E sens) { point = p; SensDeDeplacment = sens; } } 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.Conteneur = conteneur; this.Fenetre = fenetre; fenetre.lblBlanc = playerWName + ":0"; fenetre.lblNoir = playerBName + ":0"; 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) { 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"; Tour = Joueur_E.Noir; //gen des tab tableauOccupation = new Occupant_E[dim, dim]; GenerationPlateau(); } 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 % dim, i / dim }; } 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 % dim; } private int GetY(int i) { return i / dim; } bool RechercheTeritoir(int x, int y) { Point origine = new Point(x, y); int pionAligneDsRecherche = 1; int[,] tmpParcour = new int[dim, dim]; 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 >= nombreDePionAAligne*(Math.Ceiling((double)pionAligneDsRecherche/(double)nombreDePionAAligne))) 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 < dim && 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) { 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) { 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