Follow-Up on 2048, my first C++ game

Follow-Up on 2048, my first C++ game

This follow-up is on my latest post regarding my 2048 game (If you haven't seen it yet, check it out here).

Although the game is - currently - 4×4, the code actually supports any >2 value. The only way to play it in another size (10×10 for example) is by changing a certain constant integer's value before running the code, which makes this feature limited to those who have the code (and by "those" I mean myself XD). This is because the game's grid is actually a global array, which means that it doesn't have to be sent as a parameter for nearly every single function.

The alternative will be giving the array any arbitrary value (20 for example) and changing the access of the program to that array depending on the user's input, but that's a waste of resources especially if you'll stick with small grids.

The game can also be played with either the default arrow keys or those embedded in the numpad (if your keyboard includes one). The reason why I mention this is because each of them returns a 2-part scan code upon being pressed: the first is 224 (default) or 0 (numpad), while the second is the scan code value they both share.

Initially, the game supported WASD keys too, but I got rid of that afterwards as this game isn't suitable for gamers (The amazing UI would definitely give them a heart attack XD).

Speaking of which, that UI - as simple as it might look - actually took nearly half the amount of time it took to write the rest of the program! I've also changed it a little bit and made it grow with respect to the numbers in it, giving it a simpler & much cleaner look (Check out version 2 from here).

Tip N' Trick
Put the terminal into full-screen mode then zoom in (You'll thank me later)

Another thing I'd like to mention is that the highest number you can possibly get in this game (while in 4×4 mode) is 131072. This can only be achieved if you're a lucky grandmaster that not only managed to align the numbers in this order but was also lucky enough to get a 4 generated instead of a 2:

No alt text provided for this image

Finally, some stats (because why not):

  • 5 Libraries
  • 6 Global Variables (1 Boolean - 2 Arrays of Integers - 3 Integers)
  • 12 Functions (2 Boolean - 10 Void)
  • 61 Local Variables (1 Struct - 1 Array of Structs - 1 Array of Integers - 4 Boolean - 54 Integers)
  • 425 Lines (even after applying excessive bracket-removal techniques)

*Surprisingly, the "Main" function is actually 12 lines (including 3 lines for brackets and definition), making it the shortest function in the whole program!

Ahmed Shaikh

Piping Engineer | Piping GAD/Isometric| MTO |Piping Supports | E3D | Autocad 2D | Autodesk Inventor 3D |

3 年

Nice

回复

要查看或添加评论,请登录

Yahya T Khedr的更多文章

  • Journey to the CSWPA-MM

    Journey to the CSWPA-MM

    Since this is - probably - my last Journey to the Certs article (unless I got into Simulation), this is the only exam…

    23 条评论
  • Journey to the CSWE-MD

    Journey to the CSWE-MD

    So you've passed the CSWP-MD exam and any 4 of the CSWPAs, and now you're going for the big boss, the real challenge…

    56 条评论
  • Journey to the CSWPA-SU

    Journey to the CSWPA-SU

    The 4th and last CSWPA exam I took was the Surfacing exam, which I'll give an 8 on my CSWPA Difficulty Scale. Despite…

    2 条评论
  • Journey to the CSWPA-DT

    Journey to the CSWPA-DT

    The drawing tools exam was the 3rd CSWPA exam I took, which I'll give a 3 on my CSWPA Difficulty Scale. Not only…

    4 条评论
  • Journey to the CSWPA-SM

    Journey to the CSWPA-SM

    The sheet metal exam was the 2nd CSWPA exam I took, which I'll give a solid 5 on my CSWPA Difficulty Scale. That's…

  • Journey to the CSWPA-WD

    Journey to the CSWPA-WD

    The weldments exam was the 1st CSWPA exam I took, which I'll give a 6 on my CSWPA Difficulty Scale. This might seem a…

    16 条评论
  • Journey to the CSWP-MD

    Journey to the CSWP-MD

    First of all, you should know that the purchased CSWP exam is divided into 3 segments, allowing you to take one at a…

    11 条评论
  • Journey to the CSWA-MD

    Journey to the CSWA-MD

    First of all, if you're already going to take the CSWP soon after you take the CSWA, I suggest skipping the associate…

    9 条评论

社区洞察

其他会员也浏览了