using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.Drawing.Drawing2D; using System.Threading; using System.IO; namespace ECHEC { public partial class Form1 : Form { public piece[,] Occupmat; public int xx, yy, xxx, yyy; public bool flag, cflag,sahmat; public Form1() { InitializeComponent(); Occupmat = new piece [8,8]; } private void Form1_FormClosing(object sender, FormClosingEventArgs e) { Close(); } private void nouvelleToolStripMenuItem_Click(object sender, EventArgs e) { for (int i = 0; i < 8; i++) { for (int j = 2; j < 6; j++) Occupmat[i, j] = null; } Occupmat[4, 0] = new roi(true, pictureBox1); Occupmat[3, 0] = new reine(true, pictureBox1); Occupmat[0, 0] = new tour(true, pictureBox1); Occupmat[1, 0] = new cavalier(true, pictureBox1); Occupmat[2, 0] = new fou(true, pictureBox1); Occupmat[5, 0] = new fou(true, pictureBox1); Occupmat[6, 0] = new cavalier(true, pictureBox1); Occupmat[7, 0] = new tour(true, pictureBox1); for (int i = 0; i < 8; i++) { Occupmat[i, 1] = new pion(true, pictureBox1); } for (int i = 0; i < 8; i++) { Occupmat[i, 6] = new pion(false, pictureBox1); } Occupmat[4, 7] = new roi(false, pictureBox1); Occupmat[3, 7] = new reine(false, pictureBox1); Occupmat[0, 7] = new tour(false, pictureBox1); Occupmat[1, 7] = new cavalier(false, pictureBox1); Occupmat[2, 7] = new fou(false, pictureBox1); Occupmat[5, 7] = new fou(false, pictureBox1); Occupmat[6, 7] = new cavalier(false, pictureBox1); Occupmat[7, 7] = new tour(false, pictureBox1); cflag = true; flag = false; sahmat = false; for (int i=0;i<8;i++) { for (int j = 0; j < 8; j++) if (Occupmat[i,j]!= null) Occupmat[i, j].Afficher(i, j); } } private void Enregistrer() { DialogResult rep = boitesauvegarde.ShowDialog(); if (rep == DialogResult.OK) { StreamWriter foc = new StreamWriter(boitesauvegarde.FileName); foc.WriteLine(cflag.ToString()); foc.WriteLine(flag.ToString()); for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (Occupmat[i, j] == null) foc.WriteLine(0.ToString()); if (Occupmat[i, j].coul) { if (Occupmat[i, j] is tour) foc.WriteLine(3.ToString()); if (Occupmat[i, j] is cavalier) foc.WriteLine(5.ToString()); if (Occupmat[i, j] is roi) foc.WriteLine(1.ToString()); if (Occupmat[i, j] is reine) foc.WriteLine(2.ToString()); if (Occupmat[i, j] is fou) foc.WriteLine(4.ToString()); if (Occupmat[i, j] is pion) foc.WriteLine(6.ToString()); } if (!Occupmat[i,j].coul) { if (Occupmat[i, j] is tour) foc.WriteLine(9.ToString()); if (Occupmat[i, j] is cavalier) foc.WriteLine(11.ToString()); if (Occupmat[i, j] is roi) foc.WriteLine(7.ToString()); if (Occupmat[i, j] is reine) foc.WriteLine(8.ToString()); if (Occupmat[i, j] is fou) foc.WriteLine(10.ToString()); if (Occupmat[i, j] is pion) foc.WriteLine(12.ToString()); } } } foc.Close(); } } public class piece { public PictureBox pb; public bool coul; public piece(bool coul_, PictureBox pb_) { pb = pb_; coul = coul_; } public virtual void Afficher(int x, int y) { } public virtual bool Deplacement(int a, int b, int c, int d,piece[,] Matrix) { return false; } } public class tour : piece { private Bitmap img; public tour(bool coul_, PictureBox pb_) : base(coul_, pb_) { if (coul) img = new Bitmap ("tower.png"); else img = new Bitmap("wtower.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx=7+x*75; pty=8+y*75; gr.DrawImage(img, ptx, pty,60,60); gr.Dispose(); } public override bool Deplacement(int a, int b, int c, int d,piece[,] Matrix) { int e, f; int k; e = c - a; f = d - b; k = 1; if ((e== 0) || (f == 0)) { if ((e > 0)&&(f==0)) { while ((Matrix[a + k, b] == null)&&(a+k 0)&&(e==0)) { while (Matrix[a, b + k] == null) { k++; } if ((a + k == c) && (Matrix[a, b].coul != Matrix[c, d].coul)||(Matrix[c,d]==null)) return true; else return false; } if ((f < 0)&&(e==0)) { while (Matrix[a, b - k] == null) { k--; } if ((a + k == c) && (Matrix[a, b].coul != Matrix[c, d].coul) || (Matrix[c, d] == null)) return true; else return false; } } return false; } } public class roi : piece { private Bitmap img; public roi(bool coul_, PictureBox pb_):base(coul_,pb_) { if (coul) img = new Bitmap("king.png"); else img = new Bitmap("wking.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx = 7 + x * 75; pty = 8 + y * 75; gr.DrawImage(img, ptx, pty, 60, 60); gr.Dispose(); } public override bool Deplacement(int a, int b, int c, int d,piece[,] Matrix) { if (((Math.Abs(a - c) < 2) && (Math.Abs(b - d) < 2)) && ((Matrix[a, b].coul != Matrix[b, d].coul) || (Matrix[b, d] == null))) { return true; } else { return false; } } } public class reine : piece { private Bitmap img; public reine(bool coul_, PictureBox pb_):base(coul_,pb_) { if (coul) img = new Bitmap("queen.png"); else img = new Bitmap("wqueen.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx = 7 + x * 75; pty = 8 + y * 75; gr.DrawImage(img, ptx, pty, 60, 60); gr.Dispose(); } public override bool Deplacement(int a, int b, int c, int d,piece[,] Matrix) { int e, f; int k; e = c - a; f = d - b; k = 1; if ((e == 0) || (f == 0)) { if ((e > 0) && (f == 0)) { while ((Matrix[a + k, b] == null) && (a + k < c)) { k++; } if ((a + k == c) && (Matrix[a, b].coul != Matrix[c, d].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((e < 0) && (f == 0)) { while (Matrix[a - k, b] == null) { k++; } if ((a - k == c) && (Matrix[a, b].coul != Matrix[c, d].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((f == 0) && (e == 0)) return false; if ((f > 0) && (e == 0)) { while (Matrix[a, b + k] == null) { k++; } if ((a + k == c) && (Matrix[a, b].coul != Matrix[c, d].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((f < 0) && (e == 0)) { while (Matrix[a, b - k] == null) { k--; } if ((a + k == c) && (Matrix[a, b].coul != Matrix[c, d].coul) || (Matrix[c, d] == null)) return true; else return false; } } if (Math.Abs(e) == Math.Abs(f)) { if ((e > 0) && (f > 0)) { while ((Matrix[a + k, b + k] == null) && (a + k < c)) { k++; } if ((a + k == c) && ((Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null))) return true; else return false; } if ((e > 0) && (f < 0)) { while ((Matrix[a + k, b - k] == null) && (a + k < c)) { k++; } if ((a + k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((e < 0) && (f > 0)) { while ((Matrix[a - k, b + k] == null) && (a + k < c)) { k++; } if ((a - k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((e < 0) && (f < 0)) { while ((Matrix[a - k, b - k] == null) && (a + k < c)) { k++; } if ((a - k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } } return false; } } public class pion : piece { private Bitmap img; public pion (bool coul_, PictureBox pb_): base(coul_,pb_) { if (coul) img = new Bitmap("pawn.png"); else img = new Bitmap("wpawn.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx = 7 + x * 75; pty = 8 + y * 75; gr.DrawImage(img, ptx, pty, 60, 60); gr.Dispose(); } public override bool Deplacement(int a, int b, int c, int d, piece[,] Matrix) { /* int e, f; e = c - a; f = d - b; if (f==0) return false; if ((Math.Abs(e)>1)||(Math.Abs(f)>2)) return false; else { if ((f > 0) && (Matrix[a, b].coul == false)) { if (e == 0) { if((Matrix[c, d] == null) && (f == 1)) return true; if ((a == 1) && (Matrix[a, b + 1] == null) && (Matrix[c, d] == null)) return true; else return false; } } if ((f<0)&&(Matrix[a,b].coul==true)) { } }*/ return true; } } public class fou : piece { private Bitmap img; public fou(bool coul_, PictureBox pb_) : base(coul_, pb_) { if (coul) img = new Bitmap("bishop.png"); else img = new Bitmap("wbishop.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx = 7 + x * 75; pty = 8 + y * 75; gr.DrawImage(img, ptx, pty, 60, 60); gr.Dispose(); } public override bool Deplacement(int a, int b, int c, int d, piece[,] Matrix) { int e, f, k; e = c - a; f = d - b; k=1; if (Math.Abs(e) == Math.Abs(f)) { if ((e>0)&&(f>0)) { while ((Matrix[a + k, b + k] == null)&&(a+k 0) && (f < 0)) { while ((Matrix[a + k, b - k] == null) && (a + k < c)) { k++; } if ((a + k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((e < 0) && (f > 0)) { while ((Matrix[a - k, b + k] == null) && (a + k < c)) { k++; } if ((a - k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } if ((e < 0) && (f < 0)) { while ((Matrix[a - k, b - k] == null) && (a + k < c)) { k++; } if ((a - k == c) && (Matrix[c, d].coul != Matrix[a, b].coul) || (Matrix[c, d] == null)) return true; else return false; } } return false; } } public class cavalier : piece { private Bitmap img; public cavalier(bool coul_, PictureBox pb_) : base(coul_, pb_) { if (coul) img = new Bitmap("knight.png"); else img = new Bitmap("wknight.png"); } public override void Afficher(int x, int y) { int ptx, pty; Graphics gr = pb.CreateGraphics(); ptx = 7 + x * 75; pty = 8 + y * 75; gr.DrawImage(img, ptx, pty, 60, 60); gr.Dispose(); } public override bool Deplacement(int a,int b, int c, int d,piece[,] Matrix) { if (((((Math.Abs(c-a))==1)&&((Math.Abs(d-b))==2))|| ((Math.Abs(c-a)==2)&&(Math.Abs(d-b)==1)))&& ((Matrix[a,b].coul!=Matrix[c,d].coul)||(Matrix[c,d]==null))) return true; else return false; } } private void pictureBox1_MouseDown(object sender, MouseEventArgs e) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) if (Occupmat[i, j] != null) Occupmat[i, j].Afficher(i,j); } if (flag == false) { xx = e.X * 8 / pictureBox1.ClientSize.Width; yy = e.Y * 8 / pictureBox1.ClientSize.Height; if (this.Occupmat[xx, yy] != null) { if (this.Occupmat[xx, yy].coul != cflag) { flag = true; } } } if (flag == true) { xxx = e.X * 8 / pictureBox1.ClientSize.Width; yyy = e.Y * 8 / pictureBox1.ClientSize.Height; if (Occupmat[xx, yy].Deplacement(xx, yy, xxx, yyy,Occupmat) == false) { flag = false; } else { if (Occupmat[xxx, yyy] is roi) sahmat = true; { this.Occupmat[xxx, yyy] = this.Occupmat[xx, yy]; this.Occupmat[xx, yy] = null; } for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) if (Occupmat[i, j] != null) Occupmat[i, j].Afficher(i,j); } cflag = (!cflag); } } } private void sauvegarderToolStripMenuItem_Click(object sender, EventArgs e) { Enregistrer(); } private void pictureBox1_Paint(object sender, PaintEventArgs e) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { if (Occupmat[i,j] != null) this.Occupmat[i, j].Afficher(i, j); } } } } }