Original Address: Write class form with console 2048 small game Yu Wen
2048 is a very simple little game, this is a version of my own implementation.
This version has two features
- This is a console program for a high-profile form program.
- The movement of four directions is simplified into One direction by using a pointer.
It was a pleasure to play when I first came out with 2048 small games. Then according to the rules of the game himself spent an afternoon of time with the console to write a play, no reference source.
At that time only realized the game's mobile logic and the decision-winning logic, the interface is very simple, but already can play.
This is when I just finished the hair space to say that the time.
In May this year, there was a period of idle time, the program has made a large update.
- Show current score in real time
- Moving the original rows into multi-threaded parallel moves, the lock mechanism guarantees that the score will not be increased.
- Block output that mimics the original interface
- Background color and foreground color vary with number
- Clean the interface with spiral style after the game is over
As below, the updated mini-game is still a console program, but it's much like a form program.
The essence of this program is simply to operate on a two-dimensional array with a width of 4, which is not difficult.
Public Static ReadOnly int New int [44];
I think the more interesting is about the mobile, so deliberately pull out alone to say a bit.
Because the movement in four directions is essentially the same, it's just a different direction. If you write a set of methods for four directions, it is not necessary and cumbersome. So I want to move the four-direction movement into One direction.
To achieve this, four sets of pointers are defined
Private StaticDictionary<directionenum,int*[,]> _directiondic =NewDictionary<directionenum,int*[,]>() {{directionenum.up,New int*[4,4]}, {directionenum.down,New int*[4,4]}, {directionenum.left,New int*[4,4]}, {directionenum.right,New int*[4,4]}};
Each set of pointers points to the position of the array rotated 0 degrees, 90 degrees, 180 degrees, 270 degrees respectively.
StaticMatrixhelper () { for(vari =0; I <4; i++) { for(varj =0; J <4; J + +) { fixed(int* Intpoint = &Matrix._cells[i, J]) {_directiondic[directionenum.up][i, J]=Intpoint; _directiondic[directionenum.down][i,3-J] =Intpoint; _directiondic[directionenum.left][j, I]=Intpoint; _directiondic[directionenum.right][3-J,3-I] =Intpoint; } } }}
The movement of the subsequent directions only needs to be converted to the corresponding pointer upward movement. Because each pointer points to the actual direction of rotation, the actual pointer moves up naturally to the left, right, up, and down of the actual array, respectively.
Public Static BOOL Move (Directionenum directionenum) { return MoveUp (_directiondic[directionenum]);}
The MoveUp method is relatively long, it is not posted out.
Another feature is that the console program is made into a form. But it's tedious, but it's nothing complicated, it's just tedious. More troublesome to break through the console program default 16 colors, is from the Internet to find, in Colorsetter this file. Interested in looking for a look. The rest is entirely self-fulfilling.
The program has been uploaded to GitHub with the following address
GitHub Address
Writing class forms with the console 2048 small games