morpionReseau/go01/Goban.cs

491 lines
20 KiB
C#

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<PictureBox> pboxList = new List<PictureBox>();
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;
private socketPlateauBase sck = null;
#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";
LabelPlayerTurn = new System.Windows.Forms.Label();
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];
LabelPlayerTurn = new System.Windows.Forms.Label();
GenerationPlateau();
}
public Goban(Plateau fenetre, PictureBox conteneur, ConfigGo_S cfg, socketPlateauBase sck) : this(fenetre,conteneur,cfg)
{
this.sck = sck;
sck.onReceived += new socketPlateauBase.OnReceived(reception);
}
public void reception(object e, socketPlateauServReception arg)
{
Console.WriteLine("ho");
string str = (string) arg.cmd;
str.StartsWith("click");
str = str.Substring(6);
int[] xy = GetXYStr(str);
placerPion(xy[0], xy[1], pboxList[xy[0]+xy[1]*cfg.taille]);
}
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($"PaintPb {xy[0]};{xy[1]}");
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]);
placerPion(x, y, pb);
if (sck != null)
sck.senCmd($"click {x};{y}");
}
private void placerPion(int x, int y, PictureBox pb)
{
pb.Invoke((MethodInvoker)(() =>
{
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;
List<carrefour_S>pointsSuivant = 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<carrefour_S> 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<carrefour_S> RecherchePointsSuivant(Point p, Deplacement_E deplacement)
{
List<carrefour_S> PointsTrouver = new List<carrefour_S>();
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<cfg.taille && tableauOccupation[p.X+1,p.Y+1] == occupant && (deplacement & Deplacement_E.ID) == Deplacement_E.ID)
{
PointsTrouver.Add(new carrefour_S(new Point(p.X+1,p.Y+1),Deplacement_E.ID));
}
if (p.Y + 1 < cfg.taille && tableauOccupation[p.X,p.Y+1] == occupant && (deplacement & Deplacement_E.I) == Deplacement_E.I)
{
PointsTrouver.Add(new carrefour_S(new Point(p.X,p.Y+1),Deplacement_E.I));
}
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));
}
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<cfg.taille;y++)
{
for (int x = 0; x < cfg.taille; x++)
{
Console.ForegroundColor = (Tab[x, y] !=0 ) ? ConsoleColor.Green : ConsoleColor.White;
Console.Write(Tab[x, y]);
}
Console.WriteLine();
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine();
}
int xyToi(int x, int y)
{
return x + cfg.taille* y;
}
public void redrawAllpb()
{
for(int i=0;i<cfg.taille*cfg.taille;i++)
{
if (tableauOccupation[GetX(i), GetY(i)] != Occupant_E.Vide)
{
//Tour = (Tour+1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour+1;
using (Graphics gr = Graphics.FromHwnd((pboxList [i].Handle)))
{
SolidBrush b = null;
switch (tableauOccupation[GetX(i), GetY(i)])
{
case Occupant_E.Blanc:
b = new SolidBrush(Color.White);
break;
case Occupant_E.Noir:
b = new SolidBrush(Color.Black);
break;
case Occupant_E.Vide:
b = null;
break;
}
if (b != null)
gr.FillEllipse(b, 1, 0, pboxList[i].ClientSize.Width - 2, pboxList[i].ClientSize.Height - 1);
else
gr.DrawImage(pboxList[i].Image, 0, 0, pboxList[i].Width, pboxList[i].Height);
}
}
}
}
public void resetGoban()
{
for (int x = 0; x < cfg.taille; x++)
for (int y = 0; y < cfg.taille; y++)
tableauOccupation[x, y] = Occupant_E.Vide;
redrawAllpb();
win = false;
Fenetre.pictureBox2WinVisible(false);
}
public void ContinuerAjouer()
{
win = false;
Fenetre.pictureBox2WinVisible(false);
Fenetre.btnContinuerEnable = false;
Tour = (Tour + 1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour + 1;
}
public sauvegarde_S sauvegarder()
{
sauvegarde_S s = new sauvegarde_S(ptBlanc,ptNoir, tableauOccupation, tour, win, cfg);
return s;
}
public static sauvegarde_S sauvegarder(Goban g)
{
sauvegarde_S s = new sauvegarde_S(g.ptBlanc, g.ptNoir, g.tableauOccupation, g.tour, g.win, g.cfg);
return s;
}
public void GenerationPlateau(bool initTableauOccupation)
{
//label qui indique à qui le tour
this.LabelPlayerTurn.AutoSize = true;
this.LabelPlayerTurn.Font = new System.Drawing.Font("Matura MT Script Capitals", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.LabelPlayerTurn.ForeColor = System.Drawing.SystemColors.ControlText;
this.LabelPlayerTurn.Location = new System.Drawing.Point(529, 13);
this.LabelPlayerTurn.Name = "LabelPlayerTurn";
this.LabelPlayerTurn.TabIndex = 1;
this.LabelPlayerTurn.Text = (tour==Joueur_E.Blanc)?cfg.NomJoeurBlanc:cfg.NomJoeurNoir;
Fenetre.Controls.Add(this.LabelPlayerTurn);
for (int i = 0; i < cfg.taille * cfg.taille; i++) //Genération du goban
{
pboxList.Add(new PictureBox());
//pboxList[i].Paint += new PaintEventHandler(this.PaintPb);
pboxList[i].Location = new Point((i % cfg.taille) * (Conteneur.Width / cfg.taille), (i / cfg.taille) * (Conteneur.Height / cfg.taille));
pboxList[i].Width = Conteneur.Width / cfg.taille;
pboxList[i].Height = Conteneur.Height / cfg.taille;
pboxList[i].SizeMode = PictureBoxSizeMode.StretchImage;
if (GetX(i) == 0)
if (GetY(i) == 0)
pboxList[i].Image = global::go01.Properties.Resources.tl;
else if (GetY(i) == cfg.taille - 1)
pboxList[i].Image = global::go01.Properties.Resources.bl;
else
pboxList[i].Image = global::go01.Properties.Resources.l;
else if (GetY(i) == 0)
if (GetX(i) == cfg.taille - 1)
pboxList[i].Image = global::go01.Properties.Resources.tr;
else
pboxList[i].Image = global::go01.Properties.Resources.t;
else if (GetY(i) == cfg.taille - 1)
if (GetX(i) == cfg.taille - 1)
pboxList[i].Image = global::go01.Properties.Resources.br;
else
pboxList[i].Image = global::go01.Properties.Resources.b;
else if (GetX(i) == cfg.taille - 1)
pboxList[i].Image = global::go01.Properties.Resources.r;
else
pboxList[i].Image = global::go01.Properties.Resources.m;
if (initTableauOccupation)
{
tableauOccupation[i % cfg.taille, i / cfg.taille] = Occupant_E.Vide;
}
pboxList[i].Name = (i % cfg.taille).ToString() + ";" + (i / cfg.taille).ToString();
pboxList[i].Click += new System.EventHandler(this.pictureBox1_Click);
pboxList[i].Paint += new PaintEventHandler(this.PaintPb);
Conteneur.Controls.Add(pboxList[i]);
//pboxList[i].Invalidate();
}
//Conteneur.Invalidate();
//redrawAllpb();
}
public void GenerationPlateau()
{
GenerationPlateau(true);
}
public void Win(bool status)
{
Fenetre.pictureBox2WinVisible(status);
Fenetre.lblBlanc = playerBName + ":"+(ptBlanc).ToString();
Fenetre.lblNoir = playerWName + ":"+ptNoir.ToString();
Fenetre.btnContinuerEnable = status;
}
}
}