Building a Mine Sweeper Game using Dart's Flutter Framework (Part 3)

in #utopian-io6 years ago (edited)

What Will I Learn?

  • You will learn how to programmatically reveal parts of our game board
  • You will learn how to change the state for our flags and our covered tiles
  • You will learn how to give tiles numbers based their layout on the board and how many mines are nearby
  • You will learn how to structure game logic and state in a flutter application

Requirements

Difficulty

  • Intermediate

Description

In this third video tutorial, we build out most of the internal logic that drives the game state for our minesweeper game. This includes building functions that allow us to manipulate the elements on the board and the board itself. We also use the flutter framework's widget pattern to our advantage to make this application function in a way that is intuitive and preformant.

We create two helper functions. The first helper function allows us to determine if a tile is on the board or not. This is important for some of the other functions to work properly. We also build a function called bombs which lets use return a one or zero depending upon whether or not there is a bomb in a tile. We use this bomb function inside of our next function which is called mine count. The mine count function counts the mines that are adjacent to the tiles on our board and then outputs this amount. We use this function to generate the numbers for our tiles.

The next function we create is called flag. This function handles the state change from a covered tile to a flagged tile. This allows a user to flag a covered tile and remove the flag from a flagged tile. The open function is probably the most interesting function in this tutorial. It allows us to check to see if a tile is on the board, then it checks to see what that tiles state is. Afterwards, it checks to see if the tile has a mine count that is above zero. After these checks are executed, the function then recursively calls itself on every tile around the tile that was selected by the user. This is the function that allows the user to tap on a tile that isn't near a mine and open up the board. Finally, we add a function that allows us to check to see if the user has clicked on a mine or not. If not, then we run the open function.

We then wire all of these functions up to our board by putting them into our gesture detector. The counter is also put at the top of our build board function so that it is run on each tile as it is generated. While we do not have a failure state or a win state for our game just yet, we can now click on any tile and reveal the numbers, mines or blank tiles underneath of them. We can also flag the tiles when they are still covered.

The source code for this project can be found here

The freeware minesweeper game that we keep referring too can be found here

Video Tutorial

Curriculum

Related Series Tutorials

Projects and Series

Stand Alone Projects:
Building a Calculator
Movie Searcher Application

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @tensor I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.16
TRX 0.13
JST 0.027
BTC 59200.90
ETH 2601.74
USDT 1.00
SBD 2.41