Saturday 6 February 2016

15 Puzzle Game In C

Note:- This is the first game that I wrote and is definitely poorly written code.This is a code written by me in two days after reading a C book for five days after my 12th with absolutely no formal training in programing. In fact I would be commenting on the mistakes that I made during coding of this game so that you don't make them. Is this code worth reading then? Yes definitely. The text based graphics and scan key events might be worth reading!



15 Puzzle Game

Wikipedia Link:-wiki

Code:-

The code had been written using turbo c IDE under 32 Windows x. So you might have to recompile it if you were planning to test the exe directly. This code also allows you to create your own 15 puzzle game. However I later came to know that only 50% of the randomly generated 15 puzzle games are solvable. Just add a function to implement this(check if the puzzle is solvable or not) if you are planning to make this game.

IDE:- Turbo C (These days I can't imagine coding without eclipse)
OS:- Windows 32 bit
Language:-C
Lines:- 400+ lines of code( download it from mediafire link given below)

Things to Avoid


  • Goto Statements:- There are about 12 goto statements. See for yourself if yourself if you find it hard to understand the code. This  code starts looking like Assembly Code jmp instructions here and there. Every programing book asks you not to use goto statements.

  • Lack of Functions:- What should have been written in functions was unfortunately written in main function. So function calls were replaced by goto statements. This makes the code hard to edit and reduces the re-usability of the code.

  • Use of 1-D array:- This code should have been implemented using a 2-D array however  I somehow managed to get this code working using a 1-D array. Trust me, this is a truly a wonder. You are better off using 2-D arrays. The logic becomes a lot simpler.

Things toLearn

  • Comment your code:- I somehow managed to get this shitty code running. So how did I manage to debug this shitty code? The answer is comments. Whenever I wrote code I wrote comments explaining what it did in short. If today, by any chance you understand this code if believe it would be attributed to comments. 5 days of reading a programming book I understood the importance of Comments. Do you comment your code?

  • Text Based Graphics:- The graphics were designed using ascii characters. I would sit for hours holding a page in my hand, an ascii chart and start plotting the text graphics on the paper roaming around my house. If you ever design text based graphics say on any terminal based program my suggestion is , plot it on paper before you code. You are less likely to make errors then.

  • Scan Keys:- If you do run this code you might find that I capture directional keys. This is possible using scan key codes. I directly copied this function code then and had absolutely no idea how it worked. I found out scan keys for various keys that I required ,actually I wrote another small program that would print the scan code value for keyboard key when pressed.

Bugs:-

There's a bug in version 1 see if you figure it out. I corrected it in the second version. Find the files for both second and first version at the mediafire link. See if you can find the bug.

"To bug is human, to debug divine." 


Lastly

Please ignore free trial, premium version n paid version available. I really thought then that this was awesome code.Also ignore Bull's eye productions it felt cool then. Do not report any bugs to me I am likely to ignore them now since this code really sucks.Judge me by this article and not this code .I have improved my coding abilities since then.

Download Link

The link contains the source code for both the buggy version 1.0 and the working version 2.0, executable files for both the versions. The executable file might not work if you don't have a 32 bit Windows machine. So recompile it.

Link:-Mediafire





No comments:

Post a Comment