/// <summary> /// Vérifie les mouvements en L du cavalier /// </summary> /// <param name="plateau">plateau</param> /// <param name="destination">destination</param> /// <param name="currentPosition">position</param> /// <returns>Vrai / faux</returns> public override bool IsValidMouvement(ref Case[,] plateau, Point destination, Point currentPosition) { if (plateau[destination.Y, destination.X].UnePiece == null || plateau[destination.Y, destination.X].UnePiece.Couleur != this.Couleur) { if (destination.Y == currentPosition.Y - 2) { if (destination.X + 1 == currentPosition.X || destination.X - 1 == currentPosition.X) return true; } else if (destination.Y == currentPosition.Y + 2) { if (destination.X + 1 == currentPosition.X || destination.X - 1 == currentPosition.X) return true; } else if (destination.X == currentPosition.X + 2) { if (destination.Y + 1 == currentPosition.Y || destination.Y - 1 == currentPosition.Y) return true; } else if (destination.X == currentPosition.X - 2) { if (destination.Y + 1 == currentPosition.Y || destination.Y - 1 == currentPosition.Y) return true; } } return false; }
public override bool IsValidMouvement(ref Case[,] plateau, Point destination, Point currentPosition) { if (plateau[destination.Y, destination.X].UnePiece == null || plateau[destination.Y, destination.X].UnePiece.Couleur != this.Couleur) { if (destination.Y < currentPosition.Y && destination.X > currentPosition.X) { // Diagonale Haut Droite if (TopRightToBottomLeft(destination, currentPosition, ref plateau)) return true; } else if (destination.Y < currentPosition.Y && destination.X < currentPosition.X) { // Diagonale Haut Gauche if (TopLeftToBottomRight(currentPosition, destination, ref plateau)) return true; } else if (destination.Y > currentPosition.Y && destination.X < currentPosition.X) { // Diagonale Bas Gauche if (TopRightToBottomLeft(currentPosition, destination, ref plateau)) return true; } else if (destination.Y > currentPosition.Y && destination.X > currentPosition.X) { // Diagonale Bas droite if (TopLeftToBottomRight(destination,currentPosition,ref plateau)) return true; } } return false; }
/// <summary> /// Vérifie les mouvements verticaux & horizontaux de la tour /// </summary> /// <param name="plateau">plateau</param> /// <param name="destination">destination</param> /// <param name="currentPosition">position</param> /// <returns>Vrai / faux</returns> public override bool IsValidMouvement(ref Case[,] plateau, Point destination, Point currentPosition) { if (plateau[destination.Y, destination.X].UnePiece == null || plateau[destination.Y, destination.X].UnePiece.Couleur != this.Couleur) { if (destination.Y == currentPosition.Y) { if (destination.X > currentPosition.X) { if (MoveHorizontal(currentPosition, destination, ref plateau)) return true; } else { if (MoveHorizontal(destination, currentPosition, ref plateau)) return true; } } else if (destination.X == currentPosition.X) { if (destination.Y > currentPosition.Y) { if (MoveVertical(currentPosition, destination, ref plateau)) return true; } else { if (MoveVertical(destination, currentPosition, ref plateau)) return true; } } } return false; }
private bool MoveHorizontal(Point currentPosition, Point destination, ref Case[,] plateau) { for (int x = destination.X + 1; x < currentPosition.X; x++) { if (plateau[destination.Y, x].UnePiece != null) return false; } return true; }
private bool MoveVertical(Point currentPosition, Point destination, ref Case[,] plateau) { for (int y = currentPosition.Y + 1; y < destination.Y; y++) { if (plateau[y, destination.X].UnePiece != null) return false; } return true; }
/// <summary> /// Verifie sur la diagonale Nord ouest -> Sud Est /// </summary> /// <param name="currentPosition">Position courante</param> /// <param name="destination">Position destination</param> /// <param name="plateau">Plateau de jeu</param> /// <returns>Vrai / Faux</returns> private bool TopLeftToBottomRight(Point currentPosition,Point destination,ref Case[,] plateau) { if (destination.Y - destination.X == currentPosition.Y - currentPosition.X) { int init = currentPosition.Y - destination.Y; for (int i = 0 + 1; i < init; i++) { if (plateau[i + destination.Y, i + destination.X].UnePiece != null) return false; } return true; } return false; }
/// <summary> /// Retourne la case ou se trouve le roi courant /// </summary> /// <returns>Retourne la case</returns> private Case getKingCase(ref Case[,] plateau) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (plateau[y, x].UnePiece != null) { if (plateau[y, x].UnePiece.GetType().Equals(typeof(Roi)) && plateau[y, x].UnePiece.Couleur == playerTour) { return plateau[y, x]; } } } } return null; }
/// <summary> /// Verifie sur la diagonale Nord Est -> Sud Ouest /// </summary> /// <param name="currentPosition">Position courante</param> /// <param name="destination">Position destination</param> /// <param name="plateau">Plateau de jeu</param> /// <returns>Vrai / Faux</returns> private bool TopRightToBottomLeft(Point currentPosition, Point destination, ref Case[,] plateau) { if (destination.Y + destination.X == currentPosition.Y + currentPosition.X) { int x = currentPosition.X - 1; int y = currentPosition.Y + 1; while (x > destination.X && y < destination.Y) { if (plateau[y, x].UnePiece != null) return false; x--; y++; } return true; } return false; }
/// <summary> /// Vérifie si le mouvement de la piéce est valide /// </summary> /// <param name="plateau">Plateau de jeu</param> /// <returns>Retourne vrai / faux</returns> public abstract bool IsValidMouvement(ref Case[,] plateau,Point destination,Point currentPosition);
public Case(Case uneCase) { this.UnePiece = uneCase.UnePiece; this.Position = uneCase.Position; }
public override bool IsValidMouvement(ref Case[,] plateau, Point destination, Point currentPosition) { if (this.Couleur == PlayerColor.Black) { if (plateau[destination.Y, destination.X].UnePiece == null) { if (firstMove) { if (destination.Y == currentPosition.Y + 2 && destination.X == currentPosition.X) { return true; } if (destination.Y == currentPosition.Y + 1 && destination.X == currentPosition.X) { return true; } } else { if (destination.Y == currentPosition.Y + 1 && destination.X == currentPosition.X) return true; } } else if (plateau[destination.Y, destination.X].UnePiece.Couleur == PlayerColor.White) { if (destination.Y == currentPosition.Y + 1 && (destination.X == currentPosition.X - 1 || destination.X == currentPosition.X + 1)) return true; } } else if (this.Couleur == PlayerColor.White) { if (plateau[destination.Y, destination.X].UnePiece == null) { if (firstMove) { if (destination.Y == currentPosition.Y - 2 && destination.X == currentPosition.X) { return true; } if (destination.Y == currentPosition.Y - 1 && destination.X == currentPosition.X) { return true; } } else { if (destination.Y == currentPosition.Y - 1 && destination.X == currentPosition.X) return true; } } else if (plateau[destination.Y, destination.X].UnePiece.Couleur == PlayerColor.Black) { if (destination.Y == currentPosition.Y - 1 && (destination.X == currentPosition.X+1 || destination.X == currentPosition.X-1)) return true; } } return false; }
public void Initialize() { positionPossibilities.Clear(); for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { plateauValeur[y, x] = new Case(); plateauValeur[y, x].Position = new Point(x, y); } } for (int x = 0; x < 8; x++) { Pion pionNoir = new Pion(PlayerColor.Black); plateauValeur[1, x].UnePiece = pionNoir; Pion pionBlanc = new Pion(PlayerColor.White); plateauValeur[6, x].UnePiece = pionBlanc; } for (int y = 0; y < 2; y++) { PlayerColor couleur; if (y % 2 == 1) couleur = PlayerColor.White; else couleur = PlayerColor.Black; plateauValeur[y * 7, 0].UnePiece = new Tour(couleur); plateauValeur[y * 7, 7].UnePiece = new Tour(couleur); plateauValeur[y * 7, 1].UnePiece = new Cavalier(couleur); plateauValeur[y * 7, 6].UnePiece = new Cavalier(couleur); plateauValeur[y * 7, 2].UnePiece = new Fou(couleur); plateauValeur[y * 7, 5].UnePiece = new Fou(couleur); plateauValeur[y * 7, 3].UnePiece = new Reine(couleur); plateauValeur[y * 7, 4].UnePiece = new Roi(couleur); } }
/// <summary> /// Enléve la sélection /// </summary> public void ReleaseSelection() { rectangleSelection.IsHide = true; pieceIsSelected = false; caseSelected = null; positionPossibilities.Clear(); }
/// <summary> /// Vérifie l'état d'échec et mat et termine la partie /// </summary> private bool CheckEchecEtMat() { Case[,] plateauTmp = new Case[8,8]; for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { plateauTmp[y, x] = new Case(plateauValeur[y, x]); } } List<Point> possibilites = new List<Point>(); Case king = getKingCase(ref plateauTmp); TestPossibilities(ref plateauTmp, king, possibilites); foreach (Point position in possibilites) { king.Position = position; if (!CheckEchec(ref plateauTmp)) { return false; } } return true; }
/// <summary> /// Vérifie l'état d'échec de la partie /// </summary> /// <returns>Vrai / Faux</returns> private bool CheckEchec(ref Case[,] plateau) { Case caseRoi = getKingCase(ref plateau); if (caseRoi != null) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (plateau[y, x].UnePiece != null && plateau[y, x].UnePiece.Couleur != playerTour) if (plateau[y, x].UnePiece.IsValidMouvement(ref plateau, caseRoi.Position, plateau[y, x].Position)) return true; } } } return false; }
/// <summary> /// Teste les possibilités /// </summary> public void TestPossibilities(ref Case[,] plateau,Case caseSelectionnee,List<Point> possibilites) { for (int y = 0; y < 8; y++) { for (int x = 0; x < 8; x++) { if (caseSelectionnee.UnePiece.IsValidMouvement(ref plateau, new Point(x, y), caseSelectionnee.Position)) possibilites.Add(new Point(x, y)); } } }
/// <summary> /// Sélectionne la piéce sous le curseur /// </summary> /// <param name="mouseCoord">Coordonnée de la souris</param> public void selectPiece(Point mouseCoord) { int x = mouseCoord.X - (mouseCoord.X % CaseWidth); int y = mouseCoord.Y - (mouseCoord.Y % CaseHeight); if (new Rectangle(x, y, 1, 1).Intersects(plateauRectangle)) { caseSelected = getCase(x / CaseWidth, y / CaseHeight); if (caseSelected.UnePiece != null && caseSelected.UnePiece.Couleur == playerTour) { if (isEchec) { if (!caseSelected.Equals(getKingCase(ref plateauValeur))) { ReleaseSelection(); return; } } rectangleSelection.IsHide = false; rectangleSelection.MoveTo(new Point(x, y)); pieceIsSelected = true; TestPossibilities(ref plateauValeur, caseSelected, positionPossibilities); } } }