// (C) /// <summary> /// Positionne la pièce de jeu au centre de la surface de jeu et en haut de celle-ci. /// </summary> /// /// <param name="_type"> /// Le type de bloc qui sera placé. /// </param> public Piece(PieceType _type = PieceType.Dead) { // Debug if (_type == PieceType.Dead) { // Chooses a random piece. type = (PieceType)Enum.GetValues(typeof(PieceType)).GetValue(RandomTetris.rnd.Next(7)); } else { type = _type; } pieceArray = StaticVars.GetPieceArray(type); // Centers the piece and place it at the top of the grid. if (type != PieceType.O) { position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), 0); } else { position = new Vector2i(5 - (int)Math.Ceiling((decimal)pieceArray.GetLength(1) / 2), -1); } }
// (2) /// <summary> /// Fais tourner la pièce active en sens horaire, ou sens antihoraires. /// </summary> /// /// <param name="piece"> /// La pièce active qui sera tournée. /// </param> /// /// <param name="directionOfRotation"> /// Détermine si la pièce sera tournée dans le sens horaire (0), ou antihoraire (1). /// </param> /// /// <returns> /// Retourne un tableau avec la nouvelle pièce tournée. /// </returns> private PieceType[,] RotatePieceArray(Piece piece, int directionOfRotation) { PieceType[,] pieceArray = piece.GetPieceArray(); int iLength = pieceArray.GetLength(0); int jLength = pieceArray.GetLength(1); PieceType[,] newPieceArray = new PieceType[iLength, jLength]; for (int i = iLength - 1; i >= 0; i--) { for (int j = 0; j < jLength; j++) { if (directionOfRotation == 0) { newPieceArray[j, iLength - 1 - i] = pieceArray[i, j]; } else { newPieceArray[i, j] = pieceArray[j, iLength - 1 - i]; } } } return(newPieceArray); }
public static bool CheckWin(PieceType[,] board, int x, int y) { //skip empty slots PieceType type = board[x, y]; if (type == PieceType.Empty) { return(false); } //prevent out of bounds int range = ConnectFourController.piecesToWin - 1; bool insideRangeX = x + range < board.GetLength(0); bool insideRangeY1 = y + range < board.GetLength(1); bool insideRangeY2 = y - range >= 0; //win bool winHorizontal = insideRangeX; bool winVertical = insideRangeY1; bool winDiagonal1 = insideRangeX && insideRangeY1; bool winDiagonal2 = insideRangeX && insideRangeY2; //check conditions for (int w = 1; w < ConnectFourController.piecesToWin; w++) { winHorizontal = winHorizontal && (type == board[x + w, y]); winVertical = winVertical && (type == board[x, y + w]); winDiagonal1 = winDiagonal1 && (type == board[x + w, y + w]); winDiagonal2 = winDiagonal2 && (type == board[x + w, y - w]); } //return true if any condition was met return(winHorizontal || winVertical || winDiagonal1 || winDiagonal2); }
public PieceType[,] GetDrawable() { for (int i = 0; i < returnArray.GetLength(0); i++) { for (int j = 0; j < returnArray.GetLength(1); j++) { //i + 2 since we only want the grid where we actually see the pieces returnArray[i, j] = grid[i + 2, j]; } } return(returnArray); }
public Simulation(bool isPlayersTurn, PieceType[,] board) { this.isPlayersTurn = isPlayersTurn; simulatedBoard = new PieceType[board.GetLength(0), board.GetLength(1)]; for (int x = 0; x < board.GetLength(0); x++) { for (int y = 0; y < board.GetLength(1); y++) { simulatedBoard[x, y] = board[x, y]; } } }
public void RemovePiece(Piece piece) { PieceType[,] pieceArray = piece.GetPieceArray(); for (int i = 0; i < pieceArray.GetLength(0); i++) { for (int j = 0; j < pieceArray.GetLength(1); j++) { if (NotOutOfRange(piece.position.Y + i, piece.position.X + j) && grid[piece.position.Y + i, piece.position.X + j] == piece.type) { grid[piece.position.Y + i, piece.position.X + j] = PieceType.Empty; } } } }
void DrawActivePieceHardDrop() { PieceType[,] pieceArray = activePiece.GetPieceArray(); for (int i = 0; i < pieceArray.GetLength(0); i++) { for (int j = 0; j < pieceArray.GetLength(1); j++) { if (pieceArray[i, j] == activePiece.type) { StaticVars.drawGrid[Math.Max(0, activePiece.position.Y + i - 2), activePiece.position.X + j].Texture = StaticVars.blockTextures[(int)activePiece.type]; } } } }
public static int CheckWin(PieceType[,] board, bool isPlayersTurn) { for (int i = 0; i < board.GetLength(0); i++) { for (int j = 0; j < board.GetLength(1); j++) { bool win = CheckWin(board, i, j); if (win) { return(isPlayersTurn ? 1 : -1); } } } return(0); }
// (2) /// <summary> /// Ajoute la nouvelle pièce tournée dans le tableau de jeu et remplace le tableau /// partagé qui stocke la pièce avec le nouveau tableau tourner. /// </summary> /// /// <param name="piece"> /// La pièce active qui sera tournée. /// </param> /// /// <param name="kickBackPosition"> /// Décalage de la pièce active sur le tableau de jeu après avoir tourné. /// </param> /// /// <param name="directionOfRotation"> /// Détermine si la pièce sera tournée dans le sens horaire (0), ou antihoraire (1). /// </param> public void AddRotatedPiece(Piece piece, int[] kickBackPosition, int directionOfRotation) { PieceType[,] pieceArray = RotatePieceArray(piece, directionOfRotation); for (int i = 0; i < pieceArray.GetLength(0); i++) { for (int j = 0; j < pieceArray.GetLength(1); j++) { if (NotOutOfRange(piece.position.Y + i + kickBackPosition[0], piece.position.X + j + kickBackPosition[1]) && pieceArray[i, j] == piece.type) { grid[piece.position.Y + i + kickBackPosition[0], piece.position.X + j + kickBackPosition[1]] = pieceArray[i, j]; } } } Vector2i position = new Vector2i(kickBackPosition[1], kickBackPosition[0]); piece.SetPieceArray(pieceArray); piece.position += position; }
// (C) /// <summary> /// Déplace la pièce active dans le tableau 2D du jeu dans une direction passée en paramètre. /// </summary> /// /// <param name="piece"> /// La pièce active qui sera déplacée. /// </param> /// /// <param name="position"> /// La direction dans laquelle la pièce active sera déplacée. /// </param> public void AddPiece(Piece piece, Vector2i position) { PieceType[,] pieceArray = piece.GetPieceArray(); for (int i = 0; i < pieceArray.GetLength(0); i++) { for (int j = 0; j < pieceArray.GetLength(1); j++) { if (NotOutOfRange(piece.position.Y + position.Y + i, piece.position.X + position.X + j) && pieceArray[i, j] == piece.type) { grid[piece.position.Y + position.Y + i, piece.position.X + position.X + j] = pieceArray[i, j]; } } } piece.position += position; }
public static List <Vector2Int> GetPossibleMovements(PieceType[,] board) { List <Vector2Int> possibleMovements = new List <Vector2Int>(); for (int i = 0; i < board.GetLength(1); i++) { for (int j = 0; j < board.GetLength(0); j++) { if (board[j, i] == PieceType.Empty) { possibleMovements.Add(new Vector2Int(j, i)); break; } } } return(possibleMovements); }
// (2) /// <summary> /// Utilisé en union avec la fonction « CanRotatePiece », vérifie si la pièce active peut être /// placée à la position passer en paramètre. /// </summary> /// /// <param name="piece"> /// La pièce active qui sera tournée. /// </param> /// /// <param name="kickBackPosition"> /// Décalage de la pièce active sur le tableau de jeu après avoir tourné. /// </param> /// /// <param name="newPieceArray"> /// Tableau 2D contenant la pièce à vérifier. /// </param> /// /// <returns> /// Retourne « vrai » si la pièce peut être décalée à la position passée en paramètre, ou « faux » si elle ne peut pas. /// </returns> private bool CheckPiecePositionValid(Piece piece, PieceType[,] newPieceArray, int[] kickBackPosition) { for (int i = 0; i < newPieceArray.GetLength(0); i++) { for (int j = 0; j < newPieceArray.GetLength(1); j++) { int newYPos = piece.position.Y + i + kickBackPosition[0]; int newXPos = piece.position.X + j + kickBackPosition[1]; if (newPieceArray[i, j] == piece.type) { if (!NotOutOfRange(newYPos, newXPos) || grid[newYPos, newXPos] == PieceType.Dead) { return(false); } } } } return(true); }
// (1) /// <summary> /// Vérifie si la pièce active peut être déplacée dans une direction passée en paramètre. /// </summary> /// /// <param name="piece"> /// La pièce active qui sera déplacée. /// </param> /// /// <param name="position"> /// La direction dans laquelle on veut déplacer la pièce active. /// </param> /// /// <returns> /// Retourne « vrai » si la pièce peut bouger et « faux » si elle ne peut pas. /// </returns> public bool CanPlacePiece(Piece piece, Vector2i position) { PieceType[,] pieceArray = piece.GetPieceArray(); for (int i = 0; i < pieceArray.GetLength(0); i++) { for (int j = 0; j < pieceArray.GetLength(1); j++) { if (pieceArray[i, j] != PieceType.Empty) { int newYPos = piece.position.Y + position.Y + i; int newXPos = piece.position.X + position.X + j; if (!NotOutOfRange(newYPos, newXPos) || grid[newYPos, newXPos] == PieceType.Dead) { return(false); } } } } return(true); }
public LevelSolver Clone() { LevelSolver n = new LevelSolver(board.GetLength(0), board.GetLength(1), board.Cast <PieceType>().ToArray()); n.SetGoals(goals.ToArray()); return(n); }
public List <int> CheckFullRows() { idxFullRows.Clear(); for (int i = 0; i < grid.GetLength(0); i++) { bool isFull = true; for (int j = 0; j < grid.GetLength(1); j++) { if (grid[i, j] != PieceType.Dead) { isFull = false; break; } } if (isFull) { idxFullRows.Add(i); //RemoveRow(i); } } return(idxFullRows); }
/// <summary> /// Draw code of the program /// </summary> private void Draw() { window.Clear(); //TODO: make this change the textures of the sprites that has moved if (gameState == GameState.Playing || gameState == GameState.Pause) { //Draw the background first, so it's in the back window.Draw(StaticVars.backDrop); window.Draw(StaticVars.holdSprite); window.Draw(StaticVars.queueSprite); window.Draw(StaticVars.drawGridSprite); #region Draw main grid //Get a temp grid to stop calling a function in a class, more FPS PieceType[,] drawArray = grid.GetDrawable(); //Loop through the drawable grid elements for (int i = 0; i < drawArray.GetLength(0); i++) { for (int j = 0; j < drawArray.GetLength(1); j++) { //Slower code? /* * switch (drawArray[i, j]) * { * * case PieceType.Dead: * //drawGrid[i, j].Texture = Color.White; * break; * * default: * drawGrid[i, j].Texture = blockTextures[(int)drawArray[i, j]]; * break; * * } */ //Update the textures except dead pieces, since we want them to keep their original colors if (drawArray[i, j] != PieceType.Dead) { //Associated the blockTextures with the same indexes as the Enum //Look at both and you will understand StaticVars.drawGrid[i, j].Texture = StaticVars.blockTextures[(int)drawArray[i, j]]; } //Finally draw to the screen the final results window.Draw(StaticVars.drawGrid[i, j]); } } #endregion //Draw the queue #region Queue List <PieceType> queueArray = pieceQueue.GetList(); for (int i = 0; i < queueArray.Count; i++) { PieceType[,] queuePieceToDraw = StaticVars.GetPieceArray(queueArray[i]); if (queueArray[i] == PieceType.I) { for (int j = 0; j < queueSpriteArray1x4[0].GetLength(0); j++) { for (int k = 0; k < queuePieceToDraw.GetLength(1); k++) { if (queuePieceToDraw[j + 1, k] != PieceType.Empty) { queueSpriteArray1x4[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j + 1, k]]; window.Draw(queueSpriteArray1x4[i][j, k]); } } } } else if (queuePieceToDraw.GetLength(1) == 4) { for (int j = 0; j < queuePieceToDraw.GetLength(0); j++) { for (int k = 0; k < queuePieceToDraw.GetLength(1); k++) { if (queuePieceToDraw[j, k] != PieceType.Empty) { queueSpriteArray4x4[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j, k]]; window.Draw(queueSpriteArray4x4[i][j, k]); } } } } else { for (int j = 0; j < queuePieceToDraw.GetLength(0); j++) { for (int k = 0; k < queuePieceToDraw.GetLength(1); k++) { if (queuePieceToDraw[j, k] != PieceType.Empty) { queueSpriteArray3x3[i][j, k].Texture = StaticVars.blockTextures[(int)queuePieceToDraw[j, k]]; window.Draw(queueSpriteArray3x3[i][j, k]); } } } } } #endregion #region Hold PieceType[,] pieceToDraw = StaticVars.GetPieceArray(holdManager.currentPiece); if (holdManager.currentPiece == PieceType.I) { for (int j = 0; j < holdSprite1x4.GetLength(0); j++) { for (int k = 0; k < pieceToDraw.GetLength(1); k++) { if (pieceToDraw[j + 1, k] != PieceType.Empty) { holdSprite1x4[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j + 1, k]]; window.Draw(holdSprite1x4[j, k]); } } } } else if (pieceToDraw.GetLength(1) == 4) { for (int j = 0; j < pieceToDraw.GetLength(0); j++) { for (int k = 0; k < pieceToDraw.GetLength(1); k++) { if (pieceToDraw[j, k] != PieceType.Empty) { holdSprite4x4[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j, k]]; window.Draw(holdSprite4x4[j, k]); } } } } else { for (int j = 0; j < pieceToDraw.GetLength(0); j++) { for (int k = 0; k < pieceToDraw.GetLength(1); k++) { if (pieceToDraw[j, k] != PieceType.Empty) { holdSprite3x3[j, k].Texture = StaticVars.blockTextures[(int)pieceToDraw[j, k]]; window.Draw(holdSprite3x3[j, k]); } } } } #endregion window.Draw(score); window.Draw(level); window.Draw(realTime); window.Draw(StaticVars.statsSprite); window.Draw(StaticVars.controlsText); foreach (var item in stats.GetDrawable()) { window.Draw(item); } if (gameState == GameState.Pause) { window.Draw(StaticVars.pauseText); } } else if (gameState == GameState.End) { //Draw the background first, so it's in the back window.Draw(StaticVars.backDrop); window.Draw(StaticVars.holdSprite); window.Draw(StaticVars.queueSprite); window.Draw(StaticVars.drawGridSprite); window.Draw(endText); window.Draw(score); window.Draw(level); window.Draw(realTime); window.Draw(StaticVars.statsSprite); window.Draw(StaticVars.controlsText); foreach (var item in stats.GetDrawable()) { window.Draw(item); } } else if (gameState == GameState.Menu) { //window.Draw(StaticVars.menuBackdrop); foreach (var item in menu.GetDrawable()) { window.Draw(item); } } else if (gameState == GameState.Options) { foreach (var item in optionsMenu.GetDrawable()) { window.Draw(item); } } window.Display(); }