ok people, though this is not strictly a SCJP question, i found it pretty challenging, since im pretty new to java. the question is basically to implement a snakes and ladders game. there were loads of approaches i was thinking about, like a double dimensional array of size 10x10 for instance. please suggest the best approach, thanks.
'best approach' in software is sort of meaningless. almost all software decisions require you to make various trade-offs in speed, memory, disk space, etc. So, you have to decide which of those would be most important, and go from there.
Assuming you don't care about any of that, pick something you think will work, and that is easy to code and understand. If you're careful, you should be able to write it in such a way that if you get 40% of the way done and realize a 2-d array won't work, you can just drop in some other data structure that WILL work.
How would a 2-d array work? Why do you think it's a good approach? I don't have an opinion on if it is or not - I haven't thought about it enough. What have you thought about?
well, often the best place to start is by describing the problem in words. describe the game. talk about things in the game. For example, let's try tic-tac-toe. I might start by saying:
"Tic-tac-toe is a two player game, where the players alternate turns. There is a board that is a 3 x 3 grid of squares. One play traditionally uses 'X's, and the other uses 'O's, but any set of markers can be use, so long as you can distinguish one players from another's. The first player makes a mark in any unoccupied square. Then the second player does the same. After each player makes a move, a check is made to see if a player has three of their marks in any row, column, or diagonally, and if so, is declared the winner. The game can end in a tie if neither player successfully gets three in a row".
So, from this, I know I'll need to represent a 3x3 board, I'll need two sets of markers, a way for a player to select where to put his marker on his turn, and a way to check for the 3-in-a-row.
So, can you do something similar for your snakes-and-ladders game? Imaging you are trying to explain it to someone who has NO idea how it's played...
Seriously consider Fred's advice and take this project in really really small chunks. Start with English chunks... Then code that small chunk. The most important thing is to start somewhere.
There are x number of Players. So maybe a Player class makes sense. A player has to make a move. Maybe a move method makes sense for the Player.
There is a board. Maybe a board class makes sense. The board contains squares.
There are x number of squares on the board. Maybe a square object makes sense. Each square on the board can have either a snake entrance/exit and/or a ladder entrance/exit or nothing. Maybe a property makes sense for the square denoting if it has any of these.
Joined: Feb 05, 2009
hmm ok, heres what im thinking. a 10x10 array having values from 1-100, random class for die throws and two arrays containing indices of the starting point of snakes and of ladders respectively. makes sense i hope. now the question is, how do i make the game two player? coz it seems pretty straightforward to track one persons path through the array-board, but two...
Arrays >> Are no of items going to be constant ?
Mostly an indian snakes and ladders game has a standard 100 Cell board. But if you intend to, for example increase the no of cells, you can consider
Same applies for no of snakes and ladders. granted there are jus 4 points per snake or ladder, but do you want just "n" snakes and "m" ladders or do you want to do it on the fly ?
You have to indicate on screen whose turn is it Player 1 or 2 or n
for those who dont knw the game:
There is a numbered board 1-100 number starts from 1 at bottom left and ends at 100 TopLeft iterating in a zigzag manner
Each player has a single piece on the Board All starting from 1.The Dice thrown is the amount of steps you need to move forward
the board has "snakes" lying around, so if a piece lands on a snake :
variation for this game 1 : you return to cell 1
variation for this game 2 : you return to snake tail location
if a piece lands on a ladder :
you move up.
first player to reach 100 wins !!! (and shouts "jeet gaya !! tum log sab Haar gaye!")
Now for the approach:
You dont need a class for every square,a single class will do
You probably would need a class that depicts a player (name,position on board, piece color)
Of course the Dice class (randomly ensure at least one snake bite per player .. it would make it intresting, but its cheating too :-) )
The snakes and ladders logic (seperate or same class, up to you)
yo salvin, nice to see your response. well, i found it pretty simple to make the game for one player, but that is pointless. the thing is, our college refuses to start with the basics. consequences of this are, our entire program is written in the main() method. shocking, i know, but true. i can implement weird patterns and games, but i cant use object design properly, simply because i have no idea how to do it. so, i am not able to understand how to make a good program for two players. you seem to be from india, so i guess you understand what i mean by the colleges' approach. my two player wala program is a bit too messy in output, simply because i dunno how to use more than one class effectively (mind you, effectively ). thanks
Joined: Feb 05, 2009
ok, i know my code is not optimal or anything, coz i have uses the precise same logic for both the players, and i have repeated the code, which is something K&B tell me i should NEVER do. i would really appreciate it if someone would show me how to optimise the above code properly. thanks
If simplicity is what they wanted, you should have given them simplicity.....
Blind snakes and ladders:
At every turn, after rolling dice,
Choose a random number between 1 and 100, if its divisible by 10 (increase for more probability)
then its a snakebite, user rolls back to cell 1
This would have completed in 1/4 the amount of program you have written.
you can easily make the game 'n' players instead of just 1 or two:
eg for 5 players:
Joined: Feb 05, 2009
im in sardar patel, first year engineering, InfoTech. lol, they wanted simplicity and realism both. in snakes and ladders, you never do return to cell 1, so, that program would have got me a straight 'D'.
Joined: Feb 05, 2009
btw, pretty cool program there salvin . but is it possible to choose only two players or something like that?
I have seen your game and my first impression is "Impressive ", I have played on your game, I have few suggestions please,
1. Instead of alert boxes which popup and annoy sometimes, better display your messages in a seperate space beside the board.
2. Check out the messages, they sometimes wrongly mistake the ladder to be snake eg: "Player 3 rolled the dice to a 6. you almost missed a snake bite "
Your design of the board is pretty neat! keep up the good work.
I hate to say this, but why, are you even cosnidering switch statements for this?
If you look at the game of snakes and ladders what you have is an action, either move up the board are down the board to a new number,
depending on weather the square has a snake or a ladder.
Thats one object that holds a reference to the square the player is translated to.
Then if you use a Set to hold the squares that these objects are on, ie 1, 99, 87, 65 what ever.
Then you have a player object that holds the current square the player is on, when the dice rolls, you update this number.
You check the set for the new square the player is on, if there is an action, you move the player to the number in the action.
Its pretty much that simple, no arrays no switch statements.
This then makes the game very configurable as nothing is hard code into the code, you can create the boobytrapped squares from a property file of somesort, you can add extra logic for "fun" things like missing a snake etc. But the basic model isint really all that difficult.
1. Remove the irritating message box (use a log like window to show whts happening)
2. Choose no of Human players
3. Choose no of Computer players
4. Choose a Board
The point 4 is going to make this game interesting, I plan to make a "Hell Board" where there are going to be ladders everywhere in the range 10 - 40 all leading to range 70-80 BUT from 80 there are going to be a "Hell" lots of snakes all leading to extreme far positions, so getting to top would be easy because of the ladders, but winning would be difficult since there are a lot of snakes
May introduce TWO HEADED SNAKES that would make things interesting.
- When you are already playing and you go to "re-configure" the game resets your name field which is annoying.
- Moves are slightly confusing due to order of events. For example you roll the dice, then the computer moves, then you move. Shouldn't the computer roll/move, then you roll, then you move?
- Message box slightly larger and scroll past moves (text) upwards. Since the small box clears out after each move you make it makes it confusing to see what is happening.
- Add dice roll graphic. Doesn't have to be across the board... could be in mini window. Just something that graphically shows what you rolled without the textbox needing to be read.
I'm nitpicking though, it's really nice. If you don't mind I'd like to see your full code... mostly for the GUI aspects and how that connects up with the rest of the game logic. I usually use JFrame/JPanels to render my games, but I never use buttons, or configurations for anything... I just full screen the panel, disable all features, and graphically represent everything by hand. It gets old and your GUI seems smooth. If you don't mind just PM the code to me ;)
actuallly i was not mantaining state, so the username was resetting - i will fix that.
i didnt understand the second point about the computer moving before you
message box is small since the game was designed to work on smaller screens
The mesage box is actually a list box where new message is added as the first element and one element is deleted from last, i could mantain history by not deleting,
but then i didnt want to make game heavy as it was web-based.
I was thinking a lot for dice roll, but i didnt get time to make it. i can show it on screen on a modal - like dialog that pauses untill some time and disappears
automatically after a few seconds.......