Then, if you check the neighborhood of a field for mines, you don't have to check if you are off-limits because you have that safety border, and because these fields are always empty, your count is still correct.Īs a starting point, here is how I would start (without any user interaction or game loop, but with calculation and display done): import java.util. Don't show them, don't allow the user to access them. your 10x10 field would become internally 12x12), which are empty. map = (num > threshold) ? 9 : 0 Īnd last but not least a common trick for board games: Surround your board with an invisible border of one field (e.g. If an empty square E with at least one adjacent mine is revealed, then change it to a digit ( 1 to 8 ) representing the number of adjacent mines. learn to use the ternary operator, e.g.don't write int r, c at the beginning of the method, just initialize the vars in the loop: for(int c=.the Random object should be reused, make it a static member.That may be okay if you are a beginner, but if you know how to initialize objects, you should use that knowledge. This solution is not very object oriented.Random rand_obj= new Random() // use Random class to create a rand_obj If ((col_pick map.length - 1) || (row_pick map.length - 1) ) ("Which row and column to play? ( -1 to quit) ") ShowMap() // show current map (all hidden) Scanner kb = new Scanner(System.in) // keyboard input Int r, c // each 'row' & 'column' in 2-D array Public static int threshold = 6 // low threshold = more mines Public static int col_pick // column picked to play Public static int row_pick // row picked to play Public static boolean played = new boolean // played moves Public static int map = new int // mine field is 10 cells It's not fully completed but it would helpful to know if I am on the right track or completely off base with where I am at. So far I have a code that creates a 20x20 grid of JButtons, but I am unsure of how I can get my bombs randomly assigned to multimple JButtons durring the game.I got my 1 dimensional program to work just fine so I figured I just need a few tweaks to get the 2D to work as well. I'm working on creating a simple mine sweeper game in java using JButtons.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |