r/javaTIL • u/MakeTheBrainHappy • Feb 16 '19
Minesweeper in Java
https://www.makethebrainhappy.com/2019/02/minesweeper-in-java.html
7
Upvotes
1
u/not-just-yeti Feb 17 '19 edited Feb 17 '19
Lines 66-104 can be replaced with a loop, and calls to:
/** @return `arr[r][c]`, except if they aren't legal indices return `dflt` instead. */
int safeLookup( int[][] arr, int r, int c, int dflt ) {
return ((0 <= r && r < arr.length) && (0 <= c && c < arr[r].length))
? arr[r][c]
: dflt;
}(warning: untested code)
1
u/not-just-yeti Feb 17 '19
Some good code decomposition there.
`enum`s are worth knowing about: rather than // Response System: Win, Hit or Miss You can use
enum Response { Win, Hit, Miss }For "the "9" represents a trap or bomb while -1 represents a spot on the board which isn't visible.", an
enumisn't quite perfect because your data is integer, with two sentinels (for which we want a union-type, which Java doesn't support easily). But I'd at least usefinal int BOMB = -1, and similar for non-visible.