Minesweeper At Work
MATLAB, 94 93 bytes
rng(input(''));x(9,9)=~1;x(randperm(81,10))=1;y=[conv2(+x,ones(3),'s')+48 ''];y(x)=42;disp(y)
Example run (the first line after the code is the input typed by the user):
>> rng(input(''));x(9,9)=~1;x(randperm(81,10))=1;y=[conv2(+x,ones(3),'s')+48 ''];y(x)=42;disp(y)
99
*10001*2*
220001232
*201111*1
*312*1111
12*211000
011211000
0001*1000
000112110
000001*10
Explanation
rng(input(''));
takes an integer and uses it as seed. (This works in modern MATLAB versions. Old versions may need a different syntax.)
x(9,9)=~1;
assigns logical 0
, or false
(obtained by logically negating 1
) to the entry (9,9)
of a matrix x
. The rest of the entries are automatically initiallized to logical 0
too.
x(randperm(81,10))=1;
assigns 1
(autoomatically cast to logical 1
, or true
) to 10
of the
81
entries of x
, chosen randomly without replacement. These entries are the ones that contain bombs.
conv2(+x,ones(3),'s')
is an abbreviation of conv2(+x,ones(3),'same')
. It convolves the matrix x
(which needs to be cast to double
, using +
) with a 3×3 neighbourhood containing 1
. This counts how many bombs are adjacent to each entry. For entries that contain a bomb it includes that bomb, but the value there will be overwritten later.
y=[...+48 ''];
adds 48 to the value, to convert from number to ASCII code. Concatenating with the empty matrix casts these ASCII codes to chars.
y(x)=42;
assigns 42 (ASCII code for '*'
) to the positions of the bombs. These positions are given by x
, which is here used as a logical index.
disp(y)
displays the result.
Javascript (ES6), 204 or 198 bytes
Custom PRNG (204 bytes)
s=>(p=-1,S=n=>(x=p%9-(n+=p)%9)*x-64&&b[n]=='*',T=n=>S(n)+S(-n),b=[...'*00000000'.repeat(9)]).sort(_=>(s=(22695477*s+1)>>>0)&1||-1).map(c=>(p++,c=='0'?T(1)+T(8)+T(9)+T(10):c)).join``.match(/.{9}/g).join`
`
This code is using a linear congruential generator with multiplier 22695477
and increment 1
(this is the Borland C/C++ implementation).
Due to the poor efficiency of the PRNG during its warmup phase, I had to place one bomb per row (instead of 10 at the beginning or 10 at the end of the unshuffled array). So, there are only 9 bombs. I may try to fix that later.
Also, there must be a simpler/shorter way of processing the 'out of board' check (x=p%9-(n+=p)%9)*x-64
but I just can't figure it out right now.
Using Math.random() (198 bytes)
s=>(p=-1,S=n=>(x=p%9-(n+=p)%9)*x-64&&b[n]=='*',T=n=>S(n)+S(-n),b=[...'**********'+'0'.repeat(71)]).sort(_=>Math.random()-.5).map(c=>(p++,c=='0'?T(1)+T(8)+T(9)+T(10):c)).join``.match(/.{9}/g).join`
`
This one includes 10 mines as requested.
Demo
let f =
_=>(p=-1,S=n=>(x=p%9-(n+=p)%9)*x-64&&b[n]=='*',T=n=>S(n)+S(-n),b=[...'**********'+'0'.repeat(71)]).sort(_=>Math.random()-.5).map(c=>(p++,c=='0'?T(1)+T(8)+T(9)+T(10):c)).join``.match(/.{9}/g).join`
`
console.log(f())
Python 2, 269 266 264 bytes
from random import*
seed(input())
z=1,0,-1
n=range(9)
m=[[0]*9 for _ in n]
for x,y in sample([[x,y]for x in n for y in n],10):
m[x][y]=-9
for a in z:
for b in z:
if 0<=x+a<9>0<=y+b<9:m[x+a][y+b]+=1 # it gets displayed as 4 spaces, but the beginning of this line is a single tab
print("\n".join("".join([`c`,'*'][c<0]for c in l)for l in m))
Try it on ideone.com
Saved 2 bytes thanks to Aaron.
Most likely still golfable.
Explanation
random
is imported for using seed
to seed the PRNG and sample
to select ten bomb locations randomly. m
is a 9 x 9 matrix saving the board. For each of the bomb locations, the corresponding entry in m
gets set to -9
and all neighbouring entries get incremented. This way m
ends up containing the count of adjacent bombs for non-bomb cells and a negative number for bomb cells. The final print
prints the whole board by iterating through all lines l
in m
and all cells c
in l
.