morpionReseau/go01/Goban.cs

444 lines
17 KiB
C#
Raw Normal View History

2018-12-14 14:08:07 +01:00
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
{
2018-12-14 14:08:07 +01:00
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<PictureBox> pboxList = new List<PictureBox>();
public enum Occupant_E { Vide, Blanc, Noir, LAST }
public enum Joueur_E { Blanc, Noir, LAST }
2018-12-14 14:08:07 +01:00
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
2018-12-14 14:08:07 +01:00
}
[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;
2018-12-14 14:08:07 +01:00
}
}
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)
2018-12-14 14:08:07 +01:00
{
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();
}
2019-01-03 11:20:16 +01:00
public Goban(Plateau fenetre, PictureBox conteneur, socketPlateauBase.ConfigGo_S cfg) : this(fenetre, conteneur, cfg.Taille, cfg.PionAligner, cfg.NomJoeurClient, cfg.NomJoeurServeur)
{}
2018-12-14 14:08:07 +01:00
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;
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 >= nombreDePionAAligne*(Math.Ceiling((double)pionAligneDsRecherche/(double)nombreDePionAAligne)))
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 < 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<dim && 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 < dim && 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 < 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<dim;y++)
{
for (int x = 0; x < dim; 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 + dim* y;
}
public void redrawAllpb()
{
for(int i=0;i<dim*dim;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 < dim; x++)
for (int y = 0; y < dim; 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(playerWName, playerBName, ptBlanc,ptNoir, nombreDePionAAligne, dim, tableauOccupation, tour, win);
return s;
}
public static sauvegarde_S sauvegarder(Goban g)
{
sauvegarde_S s = new sauvegarde_S(g.playerWName, g.playerBName, g.ptBlanc, g.ptNoir, g.nombreDePionAAligne, g.dim, g.tableauOccupation, g.tour, g.win);
return s;
}
public void GenerationPlateau(bool initTableauOccupation)
{
//label qui indique à qui le tour
LabelPlayerTurn = new System.Windows.Forms.Label();
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 = "playerW";
Fenetre.Controls.Add(this.LabelPlayerTurn);
for (int i = 0; i < dim * dim; i++) //Genération du goban
{
pboxList.Add(new PictureBox());
//pboxList[i].Paint += new PaintEventHandler(this.PaintPb);
pboxList[i].Location = new Point((i % dim) * (Conteneur.Width / dim), (i / dim) * (Conteneur.Height / dim));
pboxList[i].Width = Conteneur.Width / dim;
pboxList[i].Height = Conteneur.Height / dim;
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) == dim - 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) == dim - 1)
pboxList[i].Image = global::go01.Properties.Resources.tr;
else
pboxList[i].Image = global::go01.Properties.Resources.t;
else if (GetY(i) == dim - 1)
if (GetX(i) == dim - 1)
pboxList[i].Image = global::go01.Properties.Resources.br;
else
pboxList[i].Image = global::go01.Properties.Resources.b;
else if (GetX(i) == dim - 1)
pboxList[i].Image = global::go01.Properties.Resources.r;
else
pboxList[i].Image = global::go01.Properties.Resources.m;
if (initTableauOccupation)
{
tableauOccupation[i % dim, i / dim] = Occupant_E.Vide;
}
pboxList[i].Name = (i % dim).ToString() + ";" + (i / dim).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;
}
}
}