1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
|
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WinCheckers
{
public partial class Form1 : Form
{
private int sizePion = 64;
private int columns = 8;
private int rows = 8 ;
private Panel[,] Board = new Panel[8, 8] ; //board ou grid
private bool p1turn = true; // flag du tour
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
NewGame();
this.Size = new Size(columns * sizePion , rows *sizePion);
this.StartPosition = FormStartPosition.CenterScreen;
this.Text = "Jeux de Dames";
}
private void NewGame()
{
this.Controls.Clear();
Load_Board();
Load_Checkers();
p1turn = true;
}
// dessin grille de panels
private void Load_Board()
{
for (int x = 0; x < Board.GetUpperBound(0)+1; x++) //ligne
{
for (int y = 0; y < Board.GetUpperBound(1)+1; y++) // colonne
{
//panel container du pion
Panel pnl = new Panel();
//coloriage alternatif des panels de la grille suivant pair-impair
if (x % 2 == 0)
{
pnl.BackColor =y % 2==0? Color.SaddleBrown:Color.Peru;
}
else
{
pnl.BackColor =y % 2==0? Color.Peru:Color.SaddleBrown;
}
pnl.BorderStyle = BorderStyle.FixedSingle;
pnl.Location = new Point(x * sizePion , y * sizePion );
pnl.Size = new Size(sizePion, sizePion);
// ajout au form
Board[x, y] = pnl;
this.Controls.Add(pnl);
// click event handler
pnl.Click += new EventHandler(pnl_Click);
}
}
}
// charge kes pions
private void Load_Checkers()
{
int startRow = 0;
int endRow =-1 + rows / 2;
for (int y = startRow; y < endRow ; y++) //loop des 3 rangees du haut
{
for (int x = 0 ;x< columns ; x ++)
{
//ajoute un nveau pion et sette ses props
Pion check = new Pion();
check.BackColor = Color.Black;
check.IsKing = false;
check.IsSelected = false;
check.Location = new Point(2, 2);
check.Player = 2;
check.Size = new Size(50, 50);
// test parite de la ligne-colonne
if(x % 2==0 && y % 2==0)
{
Board[x, y].Controls.Add(check);
check.Click += new EventHandler(check_Click); //handler du click
}
else if (x % 2 !=0 && y % 2 !=0)
{
Board[x, y].Controls.Add(check);
check.Click += new EventHandler(check_Click);
}
}
}
startRow =1+ rows / 2;
endRow = rows;
for (int y = startRow; y < endRow; y++) //loop des 3 rangees du bas
{
for (int x = 0; x < columns; x++)
{
Pion check = new Pion();
check.BackColor = Color.White ;
check.IsKing = false;
check.IsSelected = false;
check.Location = new Point(2, 2);
check.Player = 1;
check.Size = new Size(50, 50);
if (x % 2 == 0 && y % 2 == 0)
{
Board[x, y].Controls.Add(check);
check.Click += new EventHandler(check_Click);
}
else if (x % 2 != 0 && y % 2 != 0)
{
Board[x, y].Controls.Add(check);
check.Click += new EventHandler(check_Click);
}
}
}
}
private Pion selected_checker;
void pnl_Click(object sender, EventArgs e)
{
Panel pnl_clicked = sender as Panel;
if( selected_checker != null && CheckMove(selected_checker, pnl_clicked) )
MoveChecker(selected_checker, pnl_clicked);
}
void check_Click(object sender, EventArgs e)
{
Pion checker_clicked =sender as Pion;
if( selected_checker != null) // il existe deja une Selection
{
if( checker_clicked == selected_checker)
{
// checker clique deja selectionne => Annuler Selection
selected_checker.IsSelected = false;
selected_checker = null;
}
else
{
//'checker clique non selectionne => le selectionner
selected_checker = checker_clicked;
checker_clicked.IsSelected = true;
}
}
else // Aucune Selection en cours
{
//selection du checker clique
selected_checker = checker_clicked;
checker_clicked.IsSelected = true;
}
}
private bool CheckMove(Pion moving_checker, Panel pnl_to_move_to)
{
// si Panel "destination" clique possede un pion , ignorer ce clic
// Sinon verifier aussi s'il ya un pion selectionne
if (pnl_to_move_to.Controls.Count == 0 && moving_checker != null)
{
// recuperer les coords des 2 panes : panel "destination vide" &
// panel "parent" du pion selectionne
int x_sel = 0;
int y_sel = 0;
int x_cur = 0;
int y_cur = 0;
for (int col = 0; col < Board.GetUpperBound(0) + 1; col++)
{
for (int row = 0; row < Board.GetUpperBound(1) + 1; row++)
{
if (Board[col, row] == pnl_to_move_to)
{
x_sel = col;
y_sel = row;
}
if (Board[col, row] == moving_checker.Parent as Panel)
{
x_cur = col;
y_cur = row;
}
}
}
// verifier si mvt est "legal" ou autorise
if (moving_checker.Player == 1 && p1turn) // tour player 1 (les blancs)
{
if (x_sel - x_cur == -1 && y_sel - y_cur == -1)
{
// verifie si case gauche-haut (NE)
return true;
}
else if (x_sel - x_cur == -2 && y_sel - y_cur == -2)
{
Panel pnl = Board[x_sel + 1, y_sel + 1] as Panel;
if (pnl.Controls.Count > -1 && ((Pion)pnl.Controls[0]).Player == 2)
{
// verifie si 2 cases gauche-haut (NE) et vis-à-vis player 2(Noir)
pnl.Controls.Clear(); // prise
return true;
}
else
{
return false;
}
}
else if (x_sel - x_cur == 1 && y_sel - y_cur == -1)
{
// verifie si case droite-haut (NW)
return true;
}
else if (x_sel - x_cur == 2 && y_sel - y_cur == -2)
{
Panel pnl = Board[x_sel - 1, y_sel + 1] as Panel;
if (pnl.Controls.Count > -1 && ((Pion)pnl.Controls[0]).Player == 2)
{
// verifie si 2 cases droite-haut (Nw) et vis-à-vis player 2(Noir)
pnl.Controls.Clear();
return true;
}
else
{
return false;
}
}
}
else if (moving_checker.Player == 2 && !p1turn)// tour player 2 (les noirs)
{
if (x_sel - x_cur == -1 && y_sel - y_cur == 1)
{
// verifie si case gauche-bas (SW)
return true;
}
else if (x_sel - x_cur == -2 && y_sel - y_cur == 2)
{
Panel pnl = Board[x_sel + 1, y_sel - 1] as Panel;
if (pnl.Controls.Count > -1 && ((Pion)pnl.Controls[0]).Player == 1)
{
// verifie si 2 cases gauche-bas (SW) et vis-à-vis player 1(Blanc)
pnl.Controls.Clear();
return true;
}
else
{
return false;
}
}
else if (x_sel - x_cur == 1 && y_sel - y_cur == 1)
{
// verifie si case droite-bas (SE)
return true;
}
else if (x_sel - x_cur == 2 && y_sel - y_cur == 2)
{
Panel pnl = Board[x_sel - 1, y_sel - 1] as Panel;
if (pnl.Controls.Count > -1 && ((Pion)pnl.Controls[0]).Player == 1)
{
// verifie si 2 cases droite-bas (SE) et vis-à-vis player 1(Blanc)
pnl.Controls.Clear();
return true;
}
else
{
return false;
}
}
}
}
return false;
}
private void MoveChecker(Pion moving_checker, Panel pnl_to_move_to)
{
// deselectionne le pion à deplacer
moving_checker.IsSelected = false;
// le supprime de l'ancien Panel
Panel moving_from_pnl = moving_checker.Parent as Panel;
moving_from_pnl.Controls.Clear();
// l'ajoute au Panel destination
pnl_to_move_to.Controls.Add(moving_checker);
// verifie s'il peut etre "couronne"
for (int x = 0 ; x < Board.GetUpperBound(0)+1 ; x++)
{
for( int y = 0 ;y< Board.GetUpperBound(1)+1; y++)
{
if( Board[x, y] == pnl_to_move_to )
{
if( moving_checker.Player == 1 && y == 0)
moving_checker.IsKing = true;
else if (moving_checker.Player == 2 && y == 7)
moving_checker.IsKing = true;
break;
}
}
}
// change le tour .on inverse le drapeau du tour
p1turn = ! p1turn;
// ICI VIENT LE CODE DU LABEL COLORE
// if (p1turn)
// label.backolor = blanc...
// else
// label.backolor= noir...
// sette le pion selectionne à null
selected_checker = null;
// veifie le decompte des points et s'il y a un "winner"
CheckWin();
}
private void CheckWin()
{
int p1_count = 0;
int p2_count = 0;
for (int x = 0 ; x< Board.GetUpperBound(0)+1;x++)
{
for (int y = 0 ; y< Board.GetUpperBound(1)+1; y++)
{
// panel a-t-il un pion ?
if ( Board[x, y].Controls.Count > 0 )
{
// pion appartient à p1 ou p2
Pion check = Board[x, y].Controls[0] as Pion;
if ( check.Player == 1)
p1_count += 1;
else if (check.Player == 2)
p2_count += 1;
}
}
}
// verifie le nbre de pions de p1 et p2
if ( p1_count == 0)
{
MessageBox.Show("Player2 wins!"); //p2 a gagne
NewGame(); //nouvelle partie
}
else if (p2_count == 0 )
{
MessageBox.Show("Player1 wins!"); // p1 a gagne
NewGame();//nouvelle partie
}
}
}
} |