File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Game Development and the fly likes snakes and ladders game Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Game Development
Bookmark "snakes and ladders game" Watch "snakes and ladders game" New topic
Author

snakes and ladders game

Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
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.


SCJP 6
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

I don't think this is SCJP at ALL, so I'll move it to a better location for you.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Treimin Clark
Ranch Hand

Joined: Nov 12, 2008
Posts: 757
Anurag Bhatt wrote:there were loads of approaches i was thinking about, like a double dimensional array of size 10x10 for instance. please suggest the best approach.


Well, as a programmer, you should decide it yourself.
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

'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?
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
frankly, i am not able to figure out what to do
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11230
    
  16

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...
Paul Yule
Ranch Hand

Joined: May 12, 2008
Posts: 229
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.
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
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...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Very easy to make it a two-player game. You have two Player objects.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Questions you should ask yourself:

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
Collections.

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 ?


Two players:
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)


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

By the way you have inspired me to create this game at my site
I plan to implement it in gwt, will upload it when done !!
thanks for this
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

so, dude, did you get any luck with the game ???


did you complete it ?
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
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
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41


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
Suresh K Babu
Greenhorn

Joined: Feb 10, 2007
Posts: 16
a bit optimized code of above program.
Thank

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

First of all yeh kyonsa College hai ???


Yes i made the game at my site:
Snakes and Ladders at Salvin Site

I still have to enhance my version, but its a start, as you may see, I am both a developer and a designer,

once again, Woh Kyonsa College Hai ???
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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:




Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
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'.
Jehaan Butt
Ranch Hand

Joined: Feb 05, 2009
Posts: 41
btw, pretty cool program there salvin . but is it possible to choose only two players or something like that?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

my game is not complete, i will probably allow 2-5 players, allow player to choose icon, computer players, etc....

I m not getting time for it.

the dialog box is irritating, i will remove it,

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Any critics on wht i have made?
Srikanth Nutigattu
Ranch Hand

Joined: Oct 30, 2007
Posts: 114
Hello Salvin,

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.

Srikanth.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

thanks for the response friend,
like i said, the dialog box will be removed,
the messages, yes, i made them in a haste. will definately look into it.

btw my current code is configurable to set a different board configs, etc. but Designing takes time

Thanks once again, i will change it soon though i cant commit when, if i get good ideas i can make more games too.
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
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.

hope this helps
G

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

i agree with gavin

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

With respect to a console implementation by OP,

Yes, so many switch cases are undesirable.
Infact the program is not scalable at all.


We must always design programs so that they are scalable, and ready for changes to be made with minimal efforts.

Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
It is even possible to to do console level application, without OO (as the OP indicated he was unabel to do OO) and still not need a switch statement.
Shrinivasan Neelamegam
Greenhorn

Joined: Aug 11, 2008
Posts: 3
Salvin, your S and L games is amazing! how much time it took to come up with this?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

2 days (I have a sat-sun off so sometimes I do programming at home)


Next Plan: Three Cards game (teen patti)
watch this post : Click here to see post
tejas naik
Greenhorn

Joined: Mar 29, 2009
Posts: 1
heyy salvin whr can i get the code for your snake and ladder game ???
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Here are my basic components:

common constants:










These are just Beans for my code, the main logic is in a class named BoardScreen.java
But i dont see the point to post the actual logic here. Its simply crippling people to use thier minds,


still in case, here is the Construct of my class:
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Enhancements planned this week:

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.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

game completed,

check http://salvin.in/SnakesLadders/

Please let me know if you find a bug in this.

Any Criticism would be great!
Brian Legg
Ranch Hand

Joined: Nov 07, 2008
Posts: 488
Salvin, great game of Snakes 'n' Ladders!

Suggestions:

- 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 ;)

Thanks!


SCJA
~Currently preparing for SCJP6
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

hey cool,

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.......
 
Don't get me started about those stupid light bulbs.
 
subject: snakes and ladders game