Forum Moderators: coopster
Basically, as an exercise, I wanted to try creating a few games using PHP. So, for example, if I wanted to create a Checkers game with PHP, how would I go about "teaching" PHP the rules?
From what I've learned so far, a lot of if-then statements will be involved. But still, how does one go about programming into the system that a normal checker-piece can move diagonally, and jump other pieces of the other color, but can't go backward, but when it reaches the opposite row it becomes a king, and a king can go backward? And etc., etc., etc.
Remember, I'm doing this as an exercise to help develop my skills, so I don't want all-out "answers." ;) But if someone could point me in the right direction, I'd sure appreciate it!
Thanks,
Matthew
After a few days' study, I'm fairly comfortable using PHP and MySQL together. I can build content management systems, user registrations and authentication, etc
In answer to your post, I imagine the rules might be stored in a database and the pieces just check against that. So each piece would have a row detailing where it can move and what happens when it reaches the opposite row on the board.
Eg:
Header row: Piece¦Jump¦NW¦SW¦SE¦NE¦Opposite Row¦
Data row: Normal¦y¦y¦n¦n¦y¦king¦ Actually I'm thinking of chess where many pieces are used. I can't remember the rules of checkers, so if there are only two pieces then just global variables could be used.
In the above example of a database, you can see that each time a piece moves, the data can be checked to see if it is valid or not. So if a backwards move is made, the data tells PHP that there is an "n" for SW or SE moves, so no go. Also when it reaches the opposite row, the piece becomes a king.
This data can be read in in one go at the start of the program, and variables used to store it that can be manipulated during the game. Each piece might also have a set variable, defining what it was.
Well, that's my initial take. Probably way off, but might offer some light on this. Good luck with your game.
In a few days time you can do all that? Wow, that's some fast learning! It's taken me years to get that good! :)
Never said I could do it well! ;) I still need a lot of reference close at hand, which I expect will be the case for several months or years (or always). I'm also positive that my scripts are not nearly as efficient or as secure as they should be. Added to that, the book I have is meant to teach one how to do each of those things, meaning my learning so far has been pretty targetted and I now get the fun of branching off on my own!
Anyway, back to the topic at hand, thanks for the idea about storing moves in a database. I may give that a try when I'm more comfortable with working with databases and PHP. It sounds kind of ambitious to try creating any kind of game with PHP and a database, but like I said, I'm looking at this as an exercise to sharpen my skills and if it's a challenge, so much the better!
Thanks again,
Matthew
Reading from a text database is dead easy. All you need is a simple loop. Of course there are many different ways to do it, you must find the one that suits you. If you want a sample script just shout.
Arrays are an extremely useful feature of PHP, along with functions. What is also great is that many things work like other languages, eg: Perl and ASP, though with obviously some differences.
After I master these two areas, I hope to move on to whatever kind of programming will allow me to build Windows applications.
That'll probably be C++?
try these
PHP Tutorial [ca3.php.net]
Intro to PHP book [webmasterworld.com]
Books and Recomendations [webmasterworld.com]