A project of JetBrains Academy's Kotlin Core track.
Implementation by pack.heng@gmail.com.
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:
- 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.
- If a cell is empty and has mines around it, only that cell is explored, revealing a number of mines around it.
- If the explored cell contains a mine, the game ends and the player loses.
There are two possible ways to win:
- Marking all the cells that have mines correctly.
- 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
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!