![]() Note that, we will use the same counter variable ' r' as our array index later in the game code. In a Bash for loop, using the seq command to increment from 0 to 9, I print a digit ( d%) to represent the row number ($row, which is defined by seq): r=0 # our counter Next, I establish a counter variable, called r, to keep track of how many horizontal rows have been populated. Lets call it a function, plough, we print the header first: two blank lines, the column headings, and a line to outline the top of the playing field: printf '\n\n' (M is a 100-value array with indexes 0-99.) If you want to know more about Bash arrays, read You don't know Bash: An introduction to Bash arrays. Next, I print my board with columns (0-9) and rows (a-j), forming a 10x10 matrix to serve as the minefield for the game. G="1 4 6 9 10 15 20 25 30 -30 -24 -11 -10 -9 -8 -7"ĭeclare -a room # declare an array room, it will represent each cell/field of our mine. # variables below will be used to randomly get the extract-able cells/fields from our mine.į="1 2 3 35 30 20 22 10 0 -10 -20 -25 -30 -35 -3 -2 -1" Score=0 # will be used to store the score of the game The variables below are some default variables, declared to call randomly for field placement, like the variables a-g, we will use them to calculate our extractable mines: # variables (I wrote this for fun, but I would happily welcome your contributions to make it look better.) By limiting the number of locations, it will be easy to build on top of this. The logic could be better, but I wanted to keep the game looking simple and a bit immature. These are the locations that mines could be placed on the board. The player's objective is to reveal cells that contain no mine, and to never reveal a mine. Bash version of the game uses a 10x10 matrix, implemented using simple bash arrays.įirst, I assign some random variables. Each cell may or may not contain an explosive mine. ![]() In Minesweeper, the game world is a 2D array (columns and rows) of concealed cells. Keep count of available and discovered (extracted) mines.Create logic to determine the available minefield.Getting readyīefore I started writing any code, I outlined the ingredients I needed to create my game: The complete source code is found in this GitHub repository. If you are an experienced Bash programmer and want to hone your skills while having fun, follow along to write your own version of Minesweeper in the terminal. For example, when I wanted to get better at shell scripting, I decided to practice by programming a version of the Minesweeper game in Bash. You can either try the square marked with '?', or make your own guess.I am no expert on teaching programming, but when I want to get better at something, I try to find a way to have fun with it. For your next move, you will have to make a guess. ![]() ![]() It will then mark one square that has the highest chance of being safe with a green '?' symbol. ![]() If the solver cannot find a definite solution, it will calculate for all the remaining squares the probability of being safe.If a mine is found it will be marked with a red flag.If a safe square is found it will be marked on the board with a green flag.Click on 'Find next move' or 'Find all moves' and the solver will check for possible moves.Then click the square on the board to set it. Then, fill the board with numbers for the squares you know.First, select the required board size and the number of mines. ![]()
0 Comments
Leave a Reply. |