417 lines
19 KiB
C#
417 lines
19 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
|
|
namespace go01
|
|
{
|
|
class Goban
|
|
{
|
|
public PictureBox Conteneur;
|
|
public Form Fenetre;
|
|
public int dim;
|
|
Label LabelPlayerTurn;
|
|
struct carrefour_S
|
|
{
|
|
public Point pointCourant, pointSuivant;
|
|
public carrefour_S(Point pointCourant, Point pointSuivant)
|
|
{
|
|
this.pointCourant = pointCourant;
|
|
this.pointSuivant = pointSuivant;
|
|
}
|
|
}
|
|
string playerWName, playerBName;
|
|
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;
|
|
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);
|
|
/*using (Graphics gr = Graphics.FromHwnd(Fenetre.Handle))
|
|
{
|
|
Font fbg = new System.Drawing.Font("Matura MT Script Capitals", 40F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
|
Font f = new System.Drawing.Font("Matura MT Script Capitals", 38F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
|
|
//gr.DrawString((tour == Joueur_E.Blanc) ? playerWName : playerBName, fbg, new SolidBrush(Color.Black), 0, 0);
|
|
SizeF stringSizebg = new SizeF();
|
|
SizeF stringSize = new SizeF();
|
|
stringSizebg = gr.MeasureString((tour == Joueur_E.Blanc) ? playerWName : playerBName, fbg);
|
|
stringSize = gr.MeasureString((tour == Joueur_E.Blanc) ? playerWName : playerBName, f);
|
|
|
|
gr.DrawString((tour == Joueur_E.Blanc) ? playerWName : playerBName, fbg, new SolidBrush(Color.Black), new Point((int)(Conteneur.Location.X + Conteneur.Width + ((Fenetre.ClientSize.Width - Conteneur.Location.X - Conteneur.Width - stringSizebg.Width) / 2)), Conteneur.Location.Y));
|
|
gr.DrawString((tour == Joueur_E.Blanc) ? playerWName : playerBName, f, new SolidBrush(Color.White), new Point((int)(Conteneur.Location.X + Conteneur.Width + ((Fenetre.ClientSize.Width - Conteneur.Location.X - Conteneur.Width - stringSizebg.Width) / 2) + (stringSizebg.Width - stringSize.Width) / 2), (int)(Conteneur.Location.Y + ((stringSizebg.Height - stringSize.Height) / 2))));
|
|
}*/
|
|
}
|
|
}
|
|
|
|
Occupant_E[,] tableauOccupation;
|
|
Boolean[,] tabTeritoirBlanc, tabTeritoirNoir;
|
|
List<PictureBox> pboxList = new List<PictureBox>();
|
|
public Goban(Form fenetre, PictureBox conteneur, int dim, string playerW, string playerB)
|
|
{
|
|
this.Conteneur = conteneur;
|
|
this.dim = dim;
|
|
this.Fenetre = fenetre;
|
|
|
|
playerWName = playerW;
|
|
playerBName = playerB;
|
|
|
|
//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);
|
|
Tour = Joueur_E.Noir;
|
|
|
|
//gen des tab
|
|
tableauOccupation = new Occupant_E[dim, dim];
|
|
tabTeritoirBlanc = new bool[dim, dim];
|
|
tabTeritoirNoir = new bool[dim, dim];
|
|
for (int i = 0; i < dim * dim; i++) //Genération du goban
|
|
{
|
|
pboxList.Add(new PictureBox());
|
|
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) == 3 && GetY(i) == 3 || GetX(i) == dim / 2 && GetY(i) == 3 || GetX(i) == dim - 4 && GetY(i) == 3 ||
|
|
GetX(i) == 3 && GetY(i) == dim / 2 || GetX(i) == dim / 2 && GetY(i) == dim / 2 || GetX(i) == dim - 4 && GetY(i) == dim / 2 ||
|
|
GetX(i) == 3 && GetY(i) == dim - 4 || GetX(i) == dim / 2 && GetY(i) == dim - 4 || GetX(i) == dim - 4 && GetY(i) == dim - 4)
|
|
pboxList[i].Image = global::go01.Properties.Resources.hoshi;
|
|
else 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;
|
|
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);
|
|
conteneur.Controls.Add(pboxList[i]);
|
|
}
|
|
}
|
|
|
|
private void pictureBox1_Click(object sender, EventArgs e)
|
|
{
|
|
PictureBox pb = (PictureBox)sender;
|
|
String[] xy = ((PictureBox)sender).Name.Split(';');
|
|
Console.WriteLine($"{xy[0]};{xy[1]}");
|
|
|
|
|
|
|
|
if (tableauOccupation[int.Parse(xy[0]), int.Parse(xy[1])] == Occupant_E.Vide)
|
|
{
|
|
tableauOccupation[int.Parse(xy[0]), int.Parse(xy[1])] = (Tour == Joueur_E.Blanc) ? Occupant_E.Blanc : Occupant_E.Noir;
|
|
//Tour = (Tour+1 == Joueur_E.LAST) ? Joueur_E.Blanc : Tour+1;
|
|
using (Graphics gr = Graphics.FromHwnd((pb.Handle)))
|
|
{
|
|
SolidBrush b = null;
|
|
switch (tableauOccupation[int.Parse(xy[0]), int.Parse(xy[1])])
|
|
{
|
|
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, pb.ClientSize.Width - 2, pb.ClientSize.Height - 1);
|
|
else
|
|
gr.DrawImage(pb.Image, 0, 0, pb.Width, pb.Height);
|
|
}
|
|
|
|
RechercheTeritoir(int.Parse(xy[0]), int.Parse(xy[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;
|
|
}
|
|
|
|
void RechercheTeritoir(int x, int y)
|
|
{
|
|
List<carrefour_S> carrefour = new List<carrefour_S>();
|
|
Point origine = new Point(x, y);
|
|
Point pointCourant = new Point(x, y);
|
|
Point pointSuivant, pointPrec;
|
|
Point min = new Point(x, y), max = new Point(x, y);
|
|
bool[,] tmpParcour = new bool[dim, dim];
|
|
tmpParcour[origine.X, origine.Y] = true;
|
|
pointSuivant = RecherchePointSuivant(pointCourant, new Point(-1,-1), tmpParcour, carrefour);
|
|
pointPrec = pointCourant;
|
|
pointCourant = pointSuivant;
|
|
if (pointSuivant.X != -1)
|
|
{
|
|
tmpParcour[pointCourant.X, pointCourant.Y] = true;
|
|
while ((pointSuivant = RecherchePointSuivant(pointCourant, pointPrec, tmpParcour, carrefour)).X != -1 || carrefour.Count > 0)
|
|
{
|
|
using (Graphics gr = Graphics.FromHwnd((pboxList[xyToi(pointCourant.X, pointCourant.Y)].Handle)))
|
|
{
|
|
SolidBrush b = new SolidBrush(Color.Red);
|
|
|
|
if (b != null)
|
|
gr.FillEllipse(b, 1, 0, pboxList[xyToi(pointCourant.X, pointCourant.Y)].ClientSize.Width - 2, pboxList[xyToi(pointCourant.X, pointCourant.Y)].ClientSize.Height - 1);
|
|
else
|
|
gr.DrawImage(pboxList[xyToi(pointCourant.X, pointCourant.Y)].Image, 0, 0, pboxList[xyToi(pointCourant.X, pointCourant.Y)].Width, pboxList[xyToi(pointCourant.X, pointCourant.Y)].Height);
|
|
}
|
|
foreach(carrefour_S c in carrefour)
|
|
{
|
|
using (Graphics gr = Graphics.FromHwnd((pboxList[xyToi(c.pointSuivant.X, c.pointSuivant.Y)].Handle)))
|
|
{
|
|
SolidBrush b = new SolidBrush(Color.Orange);
|
|
|
|
if (b != null)
|
|
gr.FillEllipse(b, 1, 0, pboxList[xyToi(c.pointSuivant.X, c.pointSuivant.Y)].ClientSize.Width - 2, pboxList[xyToi(c.pointSuivant.X, c.pointSuivant.Y)].ClientSize.Height - 1);
|
|
else
|
|
gr.DrawImage(pboxList[xyToi(pointCourant.X, pointCourant.Y)].Image, 0, 0, pboxList[xyToi(pointCourant.X, pointCourant.Y)].Width, pboxList[xyToi(pointCourant.X, pointCourant.Y)].Height);
|
|
}
|
|
}
|
|
System.Threading.Thread.Sleep(500);
|
|
Console.WriteLine("Debut Boucle");
|
|
printTab(tmpParcour);
|
|
if (pointSuivant.X == -1)
|
|
{
|
|
pointSuivant = new Point(carrefour[carrefour.Count - 1].pointSuivant.X, carrefour[carrefour.Count - 1].pointSuivant.Y);
|
|
tmpParcour[pointSuivant.X, pointSuivant.Y] = true;
|
|
pointPrec = new Point(carrefour[carrefour.Count - 1].pointCourant.X, carrefour[carrefour.Count - 1].pointCourant.Y);
|
|
pointCourant = pointSuivant;
|
|
carrefour.RemoveAt(carrefour.Count - 1);
|
|
}
|
|
else if (tmpParcour[pointSuivant.X, pointSuivant.Y] || ptEgal(pointSuivant, pointPrec))
|
|
{
|
|
if (carrefour.Count > 0 && !ptEgal(pointSuivant, origine))
|
|
{
|
|
pointSuivant = new Point(carrefour[carrefour.Count - 1].pointSuivant.X, carrefour[carrefour.Count - 1].pointSuivant.Y);
|
|
tmpParcour[pointSuivant.X, pointSuivant.Y] = true;
|
|
pointPrec = new Point(carrefour[carrefour.Count - 1].pointCourant.X, carrefour[carrefour.Count - 1].pointCourant.Y);
|
|
pointCourant = pointSuivant;
|
|
carrefour.RemoveAt(carrefour.Count - 1);
|
|
|
|
if (ptEgal(pointSuivant, origine))
|
|
break;
|
|
}
|
|
else if (ptEgal(pointSuivant, origine))
|
|
{
|
|
Console.WriteLine("teritoir trouver");
|
|
printTab(tmpParcour);
|
|
break;
|
|
}
|
|
else
|
|
{
|
|
Console.WriteLine("teritoir non trouver");
|
|
printTab(tmpParcour);
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
tmpParcour[pointSuivant.X, pointSuivant.Y] = true;
|
|
pointPrec = pointCourant;
|
|
pointCourant = pointSuivant;
|
|
}
|
|
printTab(tmpParcour);
|
|
|
|
}
|
|
redrawAllpb();
|
|
}
|
|
}
|
|
Point RecherchePointSuivant(Point p, Point ptPrec, bool[,] parcout, List<carrefour_S> carrefour)
|
|
{
|
|
Occupant_E occupant = (Tour == Joueur_E.Blanc)?Occupant_E.Blanc:Occupant_E.Noir;
|
|
bool trouver = false;
|
|
Point pRet = new Point(-1, -1);
|
|
if (tableauOccupation[p.X,p.Y-1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X,p.Y-1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X,p.Y), new Point(p.X,p.Y-1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X,p.Y-1)))
|
|
{
|
|
pRet = new Point(p.X,p.Y-1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X+1,p.Y-1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X+1,p.Y-1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X+1,p.Y-1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X+1,p.Y-1)))
|
|
{
|
|
pRet = new Point(p.X+1,p.Y-1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X+1,p.Y] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X+1,p.Y])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X+1,p.Y)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X+1,p.Y)))
|
|
{
|
|
pRet = new Point(p.X+1,p.Y);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X+1,p.Y+1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X+1,p.Y+1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X+1,p.Y+1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X+1,p.Y+1)))
|
|
{
|
|
pRet = new Point(p.X+1,p.Y+1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X,p.Y+1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X,p.Y+1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X,p.Y+1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X,p.Y+1)))
|
|
{
|
|
pRet = new Point(p.X,p.Y+1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X-1,p.Y+1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X-1,p.Y+1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X-1,p.Y+1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X-1,p.Y+1)))
|
|
{
|
|
pRet = new Point(p.X-1,p.Y+1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X-1,p.Y] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X-1,p.Y])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X-1,p.Y)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X-1,p.Y)))
|
|
{
|
|
pRet = new Point(p.X-1,p.Y);
|
|
trouver = true;
|
|
}
|
|
}
|
|
if (tableauOccupation[p.X-1,p.Y-1] == occupant)
|
|
{
|
|
if(trouver && !parcout[p.X-1,p.Y-1])
|
|
carrefour.Add(new carrefour_S(new Point(p.X, p.Y), new Point(p.X-1,p.Y-1)));
|
|
else if(!ptEgal(ptPrec, new Point(p.X-1,p.Y-1)))
|
|
{
|
|
pRet = new Point(p.X-1,p.Y-1);
|
|
trouver = true;
|
|
}
|
|
}
|
|
return pRet;
|
|
}
|
|
Boolean ptEgal(Point pt1, Point pt2)
|
|
{
|
|
return pt1.X == pt2.X && pt1.Y == pt2.Y;
|
|
}
|
|
|
|
void printTab(bool[,] Tab)
|
|
{
|
|
for(int y = 0;y<dim;y++)
|
|
{
|
|
for (int x = 0; x < dim; x++)
|
|
{
|
|
Console.ForegroundColor = Tab[x, y] ? ConsoleColor.Green : ConsoleColor.White;
|
|
Console.Write(Tab[x, y] ? '1' : '0');
|
|
}
|
|
Console.WriteLine();
|
|
}
|
|
Console.WriteLine();
|
|
Console.WriteLine();
|
|
Console.WriteLine();
|
|
}
|
|
int xyToi(int x, int y)
|
|
{
|
|
return x + dim* y;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|