Skip to content

PackHg/minesweeper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Minesweeper

A project of JetBrains Academy's Kotlin Core track.

Implementation by pack.heng@gmail.com.

1. Description

Minesweeper is a game of logic where the player is presented with a field full of hidden mines. The goal is to mark the positions of all mines without setting any of them off. It's not a game of wild guessing: it offers hints showing the number of mines around each cell. One wrong move, and game over!

The game starts with an unexplored minefield that has a user-defined number of mines.

The player can:

  • Mark unexplored cells as cells that potentially have a mine, and also remove those marks. Any empty cell can be marked, not just the cells that contain a mine. The mark is removed by marking the previously marked cell.

  • Explore a cell if they think it does not contain a mine.

There are three possibilities after exploring a cell:

  1. If the cell is empty and has no mines around, all the cells around it, including the marked ones, can be explored, and it should be done automatically. Also, if next to the explored cell there is another empty one with no mines around, all the cells around it should be explored as well, and so on, until no more can be explored automatically.
  2. If a cell is empty and has mines around it, only that cell is explored, revealing a number of mines around it.
  3. If the explored cell contains a mine, the game ends and the player loses.

There are two possible ways to win:

  1. Marking all the cells that have mines correctly.
  2. Opening all the safe cells so that only those with unexplored mines are left.

The following symbols represent each cell’s state:

  • . as unexplored cells
  • / as explored free cells without mines around it
  • Numbers from 1 to 8 as explored free cells with 1 to 8 mines around them, respectively
  • X as mines
  • * as unexplored marked cells

2. Examples

The greater-than symbol followed by a space (> ) represents the user input. Note that it's not part of the input.

Example 1: the user loses after exploring a cell that contains a mine

How many mines do you want on the field? > 10

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.........β”‚
2β”‚.........β”‚
3β”‚.........β”‚
4β”‚.........β”‚
5β”‚.........β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 3 2 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.1///1...β”‚
2β”‚.1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 1 1 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚.1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 1 2 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚*1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 8 8 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚*1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.......1.β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 8 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚*1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚......11.β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 6 8 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚*1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.....211.β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 2 7 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1...β”‚
2β”‚*1//12...β”‚
3β”‚11//1....β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.........β”‚
7β”‚.3.......β”‚
8β”‚.....211.β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 5 6 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚11///1X..β”‚
2β”‚X1//12...β”‚
3β”‚11//1X...β”‚
4β”‚////1....β”‚
5β”‚11111....β”‚
6β”‚.X..X....β”‚
7β”‚.3X...X..β”‚
8β”‚.X..X211.β”‚
9β”‚...X.....β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
You stepped on a mine and failed!

Example 2: the user wins by marking all mines correctly

How many mines do you want on the field? > 8

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.........β”‚
2β”‚.........β”‚
3β”‚.........β”‚
4β”‚.........β”‚
5β”‚.........β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 5 5 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚..1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1...β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23.1//111β”‚
8β”‚..21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 2 1 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1...β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23.1//111β”‚
8β”‚..21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 3 7 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1...β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚..21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 2 8 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1...β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚.*21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 1 8 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1...β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 3 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1*..β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 8 3 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2...β”‚
3β”‚/////1*1.β”‚
4β”‚/////11..β”‚
5β”‚//////1..β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 9 3 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2.31β”‚
3β”‚/////1*1/β”‚
4β”‚/////111/β”‚
5β”‚//////111β”‚
6β”‚/111//1..β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 8 6 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2.31β”‚
3β”‚/////1*1/β”‚
4β”‚/////111/β”‚
5β”‚//////111β”‚
6β”‚/111//1*.β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 2 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1...β”‚
2β”‚111//2231β”‚
3β”‚/////1*1/β”‚
4β”‚/////111/β”‚
5β”‚//////111β”‚
6β”‚/111//1*.β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 1 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1*..β”‚
2β”‚111//2231β”‚
3β”‚/////1*1/β”‚
4β”‚/////111/β”‚
5β”‚//////111β”‚
6β”‚/111//1*.β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 9 1 mine

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.*1//1*.*β”‚
2β”‚111//2231β”‚
3β”‚/////1*1/β”‚
4β”‚/////111/β”‚
5β”‚//////111β”‚
6β”‚/111//1*.β”‚
7β”‚23*1//111β”‚
8β”‚**21/////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Congratulations! You found all the mines!

Example 3: the user wins by exploring all safe cells

How many mines do you want on the field? > 5

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚.........β”‚
2β”‚.........β”‚
3β”‚.........β”‚
4β”‚.........β”‚
5β”‚.........β”‚
6β”‚.........β”‚
7β”‚.........β”‚
8β”‚.........β”‚
9β”‚.........β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 5 5 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚..1//1.21β”‚
5β”‚111//1...β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚..1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 1 9 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚..1//1.21β”‚
5β”‚111//1...β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚1.1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 1 4 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚1.1//1.21β”‚
5β”‚111//1...β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚1.1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 4 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚1.1//1121β”‚
5β”‚111//1...β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚1.1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 7 5 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚1.1//1121β”‚
5β”‚111//11..β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚1.1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Set/unset mines marks or claim a cell as free: > 8 5 free

 β”‚123456789β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
1β”‚/////////β”‚
2β”‚/////111/β”‚
3β”‚111//1.1/β”‚
4β”‚1.1//1121β”‚
5β”‚111//112.β”‚
6β”‚/////1.21β”‚
7β”‚/////111/β”‚
8β”‚111//////β”‚
9β”‚1.1//////β”‚
β€”β”‚β€”β€”β€”β€”β€”β€”β€”β€”β€”β”‚
Congratulations! You found all the mines!

About

Minesweeper project with JetBrains Academy

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages