public void DrawGrid() { squares = new Square[row, col]; for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) { Square sqr = new Square(this, i, j); //this is sending the game, probably can send the panel from game.Panel squares[i, j] = sqr; } }
private void QueueExposeMoves(Square analyzedSquare) { if ((int)analyzedSquare.type >= 1) { SquareInfo info = GetAdjacentSquaresInfo(analyzedSquare.x, analyzedSquare.y); if ((int)analyzedSquare.type == info.adjacentFlagCount) { List <Square> adjacentHiddenSquares = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y, SquareType.Hidden); foreach (Square sq in adjacentHiddenSquares) { if (!moveQueue.Contains(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose })) { moveQueue.Enqueue(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose }); } } } if ((((int)analyzedSquare.type - info.adjacentFlagCount) == 1) && (info.adjacentHiddenCount == 2)) { List <Square> adjacentSquares = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y); foreach (Square adjacentSquare in adjacentSquares) { SquareInfo infoAboutAdjacent = GetAdjacentSquaresInfo(adjacentSquare.x, adjacentSquare.y); if ((((int)adjacentSquare.type - infoAboutAdjacent.adjacentFlagCount) == 1) && (infoAboutAdjacent.adjacentHiddenCount == 3)) { List <Square> hiddenAroundAnalyzed = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y, SquareType.Hidden); List <Square> hiddenAroundAdjacent = GetAdjacentSquares(adjacentSquare.x, adjacentSquare.y, SquareType.Hidden); List <Square> result = new List <Square>(); foreach (Square sq in hiddenAroundAdjacent) { if (!hiddenAroundAnalyzed.Contains(sq)) { result.Add(sq); } } if (result.Count == 1) { Square sq = result[0]; if (!moveQueue.Contains(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose })) { moveQueue.Enqueue(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose }); } } } } } if ((((int)analyzedSquare.type - info.adjacentFlagCount) == 2) && (info.adjacentHiddenCount == 3)) { List <Square> adjacentCrossSquares = GetAdjacentCrossSquares(analyzedSquare.x, analyzedSquare.y); foreach (Square adjacentSquare in adjacentCrossSquares) { SquareInfo infoAboutAdjacent = GetAdjacentSquaresInfo(adjacentSquare.x, adjacentSquare.y); if ((((int)adjacentSquare.type - infoAboutAdjacent.adjacentFlagCount) == 1) && (infoAboutAdjacent.adjacentHiddenCount == 3)) { List <Square> hiddenAroundAnalyzed = GetAdjacentSquares(analyzedSquare.x, analyzedSquare.y, SquareType.Hidden); List <Square> hiddenAroundAdjacent = GetAdjacentSquares(adjacentSquare.x, adjacentSquare.y, SquareType.Hidden); foreach (Square sq in hiddenAroundAdjacent) { if (!hiddenAroundAnalyzed.Contains(sq)) { if (!moveQueue.Contains(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose })) { moveQueue.Enqueue(new Move { X = sq.x, Y = sq.y, Type = MoveType.Expose }); } } } } } } } }
public int Click() { if (is_marked) { return(9); } else if (is_clicked) { int count = 0; foreach (Square s in this.GetNeighbors()) { if (s.is_marked) { count++; } } int min_val = 9; if (this.num_neighbor_mines == count) { foreach (Square s in this.GetNeighbors()) { if (!s.is_marked && !s.is_clicked) { min_val = Math.Min(min_val, s.Click()); } } } return(min_val); } else if (is_mine) { button.BackColor = Color.Red; is_clicked = true; return(-1); } else { int count = ClickAndCount(); if (count == 0) { List <Square> queue = new List <Square>(); queue.Add(this); while (queue.Count > 0) { Square s = queue[0]; queue.RemoveAt(0); int c = s.ClickAndCount(); if (c == 0) { foreach (Square n in s.GetNeighbors()) { if (!n.is_clicked) { queue.Add(n); } } } } } return(count); } }
public void AddNeighbor(Square n) { neighbors.Add(n); }
public Game( int width, int height, Difficulty d ) { gridHeight = height; gridWidth = width; // instantiate minefield grid = new Square[ width, height ]; for( int x = 0; x < width; x++ ) { for( int y = 0; y < height; y++ ) { grid[ x, y ] = new Square( x, y, d ); grid[ x, y ].TriggerSurroundingSquares += new Square.TriggerSurroundingSquaresHandler( Game_TriggerSurroundingSquares ); grid[ x, y ].SquareTriggered += new Square.TriggerSurroundingSquaresHandler( Game_SquareTriggered ); grid[ x, y ].MineTriggered += new EventHandler( Game_MineTriggered ); this.Controls.Add( grid[ x, y ] ); } } // get surrounding counts for( int x = 0; x < width; x++ ) { for( int y = 0; y < height; y++ ) { int surroundingCount = 0; if( x != 0 ) { if( grid[ x - 1, y ].isMine ) surroundingCount++; } if( y != 0 ) { if( grid[ x, y - 1 ].isMine ) surroundingCount++; } if( y != 0 && x != 0 ) { if( grid[ x - 1, y - 1 ].isMine ) surroundingCount++; } if( ( y + 1 ) != height ) { if( grid[ x, y + 1 ].isMine ) surroundingCount++; } if( ( x + 1 ) != width ) { if( grid[ x + 1, y ].isMine ) surroundingCount++; } if( ( x + 1 ) != width && y != 0 ) { if( grid[ x + 1, y - 1 ].isMine ) surroundingCount++; } if( ( y + 1 ) != height && x != 0 ) { if( grid[ x - 1, y + 1 ].isMine ) surroundingCount++; } if( ( x + 1 ) != width && ( y + 1 ) != height ) { if( grid[ x + 1, y + 1 ].isMine ) surroundingCount++; } grid[ x, y ].surroundingCount = surroundingCount; } } //TriggerAll( ); int mc = mineCount( ); int sc = gridHeight * gridWidth; t.Interval = 1000; }
void Game_TriggerSurroundingSquares( object sender, Square.MineSquareEventArgs e ) { int x = e.X; int y = e.Y; int height = this.gridHeight; int width = this.gridWidth; if( x != 0 ) { if( !grid[ x - 1, y ].Checked ) grid[ x - 1, y ].Trigger( ); } if( y != 0 ) { if( !grid[ x, y - 1 ].Checked ) grid[ x, y - 1 ].Trigger( ); } if( y != 0 && x != 0 ) { if( !grid[ x - 1, y - 1 ].Checked ) grid[ x - 1, y - 1 ].Trigger( ); } if( ( y + 1 ) != height ) { if( !grid[ x, y + 1 ].Checked ) grid[ x, y + 1 ].Trigger( ); } if( ( x + 1 ) != width ) { if( !grid[ x + 1, y ].Checked ) grid[ x + 1, y ].Trigger( ); } if( ( x + 1 ) != width && y != 0 ) { if( !grid[ x + 1, y - 1 ].Checked ) grid[ x + 1, y - 1 ].Trigger( ); } if( ( y + 1 ) != height && x != 0 ) { if( !grid[ x - 1, y + 1 ].Checked ) grid[ x - 1, y + 1 ].Trigger( ); } if( ( x + 1 ) != width && ( y + 1 ) != height ) { if( !grid[ x + 1, y + 1 ].Checked ) grid[ x + 1, y + 1 ].Trigger( ); } }
void Game_SquareTriggered( object sender, Square.MineSquareEventArgs e ) { if( gameOver == true ) return; int untriggeredCount=0; // get count of total untriggered squares foreach( Square item in grid ) { if( item.Checked == false ) untriggeredCount++; } // get count of mines // this.mineCount(); // if untriggered square count <= mine count, game is won. if( untriggeredCount == mineCount( ) ) { gameOver = true; // stop timer t.Stop( ); // fetch time int time = ((GameForm)Parent.Parent.Parent).Time; foreach( Square item in grid ) { item.autoFlag( this, new EventArgs( ) ); } // WOOOOOOOO!!!!! MessageBox.Show( string.Format( "Yay! You won the game!\n\nYour final time was: {0}", time.ToString( ) ) ); } }