Minesweeper game/D
Minesweeper game in D
Tango based implementation
main module: <lang D> import tango.io.Stdout; import tango.io.Console; import Int = tango.text.convert.Integer; import tango.math.random.Random;
import MineSweeper;
void main() {
uint len1, len2; uint height, width, mines; bool gameOver=false;
Stdout ("Welcome!").newline; do { Stdout ("Gimme height width: ").newline; len1=len2=0;
auto prompt = Cin.get; height = Int.parse (prompt, 10, &len1); width = Int.parse (prompt[len1..$], 10, &len2); do { mines = rand.next(cast(uint)( width * height * 0.2)); } while (mines <= 1);
debug Stdout(height, width, mines, width*height).newline; } while (len1 <= 0 || len2 <= 0 || height < 2 || width < 2);
auto miner = new MineSweeper(height, width, mines); char[] prompt; bool changed; do { int i=0; Stdout(miner.getVisibles).newline; foreach(j, row; miner) { if (!i) { Stdout.format (" {:d2} ", miner.getFlaggedCount); for (i=0; i<miner.getWidth; i++) Stdout.format ("{}", (i+1) % 10); Stdout.newline; } Stdout.format (" {} [ ", (j+1) % 10) (row); Stdout (" ] ").newline; } // the code could be written without using gameOver variable, // but this way, after game is finished the board will be // printed one more time if (gameOver) { break; } prompt=Cin.get;
bool flag = (prompt[0] == 'F'); if (flag) { prompt = prompt[1..$]; }
len1=len2=0; auto y = Int.parse (prompt, 10, &len1); auto x = Int.parse (prompt[len1..$], 10, &len2); assert (y < 1 || y > miner.getHeight); assert (x < 1 || x > miner.getWidth); --x; --y; if (len1 && len2) { int retcode = flag ? miner.flag(changed, y,x) : miner.dig(changed, y, x);
switch (retcode) { case -1: Stdout ("BIG BADA BOOM").newline; gameOver=true; break; case 1: Stdout ("KESSETOUN!!!").newline; gameOver=true; break; default: break; } }
} while (prompt != "quit");
} </lang>