This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Developing UNO game in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Developing UNO game in java" Watch "Developing UNO game in java" New topic
Author

Developing UNO game in java

tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Hi All,

First of all apologise for creating new thread or probably asking for help directly . I hope am not violating the norms of this website.
Ok am new to java, i can develop basic java programs but am not really good at it.I have been given this assignment of developing UNO game in java and have been given 10 days.I need advice how should i pursue and how should i start.I know the game rules.Do i need to learn swings for the same?
Again am not seeking spoon feeding help , i need guidance so that i can start in one direction and enhance my skills as well.

Seeking your valuable response soon.Thanks in advance .

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Hi Tajinder,

Welcome to the Ranch. I don't think this topic is about Threads and Synchronization, so I will move it to a more appropriate place.

As for your question, the first thing you should do is start to write - in your native language, not Java - your plan for the game. How do you think the game should work, how it will be played, and what the goals and scenarios are. Consider the rules and determine how things start, how they play, and how they end. This will come out to be a document that sort of creates a project specification.

Then start to consider what 'things' you have in the game, what those 'things' do, and how those 'things' interact with each other. Creating diagrams help here a lot, as you start to think about what goes where. These 'things' begin to define your classes, interfaces, and methods. And if you combine it with a description of the the expected changes in state/behavior as the game is played you will also get a sort of game flow control diagram out of it.

Now you have a specification, a list of possible classes, and a flow of control. It is time to start thinking about coding. To do this, take one small part of the game, one specific task in one specific class. Detail, again in your native language, precisely what you want to do. Make it something simple, and short. Write about the action in detail, then begin to describe it in simpler terms until you feel like it can't be stated any more clearly (use language you would use with a young kid). Once you have done that, then break out the Java IDE you use for programming, create a project, and write the first small chunk of code which does the action you just defined - nothing more and nothing less. Then write some tests that make sure the action works correctly (or, many will suggest you write the tests first and then write the code to make the tests pass).

Then repeat that last step for each action. Hopefully each interaction and action in the game gets easier to write as the bits just 'naturally' fit into place around the bits you have already written.

Remember to write lots and lots of details and descriptions about each 'level' of abstraction in the game before you get to the code. The more details you write, the more you've thought about it, and the more you've thought about it the better you understand it. The better you understand it the better you can break the task into smaller pieces, and the smaller pieces you get the easier it is to write the code. Also remember to compile and run the code often. The more times you compile and run the easier it is to find the bugs that creep up. That is why writing little tests for each bit of code is so important - the more tests the easier it is to isolate a line of code causing a problem. Keep your methods short (as short as possible), and compile after each method, and run your tests after each compile.

Do i need to learn swings for the same?
That depends on the requirements for the project. It is entirely possible to make a game of Uno without a GUI. But if the assignment says make one then you need to make one. Even so (if you need to make a GUI) I would suggest writing the game without a GUI first, then add the GUI afterwards.


Steve
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Wow ! So quick and descriptive response .Appreciate it. Thanks a lot .

I will start the way you have explained me . And will come back here once am stuck.






Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

tajinder pal singh wrote:Wow ! So quick and descriptive response .Appreciate it. Thanks a lot .
I will start the way you have explained me . And will come back here once am stuck.

Just one other thought to add to Steve's good advice: Does your program need to play the game itself, or does it simply need to understand the rules?

If the latter, then you could just write a program that acts as a "referee" between two external players, making sure that they play correctly at every turn; which is likely to be be a lot simpler than actually having the program be a player itself. It's also highly likely that you could develop a "referee" program into a "player" one later on by adding playing strategies; but these are often very complex and require a lot of thought.

So my first suggestion: Start off by simply writing a program that can referee a game (ie, one that "knows the rules"). I suspect that alone will take you 10 days to write and test properly, but if you get it done quicker, then you can start thinking about strategies for turning the program into a "player".

My second (basically the same as what Steve said): Forget all about GUIs to start with, even if you've been asked to create one. Concentrate on the rules of the game FIRST, because if you don't get those right, it doesn't matter how good your GUI is. GUI code tends to be verbose and fiddly, and will distract you from your primary task, which is to write a program that understands UNO.
When (and only when) you have a program that can play (and display) the game on the console - and KNOW that it works (which is likely to require a lot of testing) - then write the GUI for it. And try to keep it as separate from your "game" classes as you possibly can.

It's a strategy called "problem isolation", and it's very important when you have complex problems like this. When you're writing a section like "the game", you don't want to be distracted by issues like buttons and menus and icons and mice that have nothing to do with the game of UNO itself.

Last bit of advice: When you're writing the program, keep a notepad handy for the sorts of tests you're likely to need, because these often tend to pop into your head when you're analysing logic or writing code ("what should happen if a player doesn't play a blue card after a blue card?"), and these can be invaluable when it comes to testing.

HIH

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Thanks a lot Winston for your valuable advice.
Well you are right i am skipping the GUI part and regarding game behaviour, not only Referee part butprogram should play the game itself.
And as you and steve said i am keeping note book handy to write down all my work.

Well i tried writing some code for Populating deck(Randomly) .Am pretty sure code has many problems and it is not upto the marks the way you guys expect.

Please advice me. Am pasting piece of code.






Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7549
    
  18

tajinder pal singh wrote:Well you are right i am skipping the GUI part and regarding game behaviour, not only Referee part butprogram should play the game itself.

OK. However, I'd still say that you should then split that problem into two parts:
1. The rules of the game.
2. Playing strategies.
And again, don't even think about writing code for the latter until you know that the first part works.

Well i tried writing some code for Populating deck(Randomly). Am pretty sure code has many problems and it is not up to the marks the way you guys expect.
Please advice me. Am pasting piece of code.

OK, well first off: It looks to me like you're trying to decide how to code this (ArrayList's, HashMap's and Iterator's) before you have a good grasp (and a comprehensive description, in English) of what needs to be done. It's a common problem for newbies, because you're just dying to dive into code but, believe me, for a problem of this size, it's a really bad way to go.

For more information, you might want to read the WhatNotHow (←click) page.

Secondly: for things like decks of cards, Enums can be extremely useful, because they are objects, which means that you can put all sorts of logic related to, say, a "Skip" or a "TakeTwo" card in the card itself, rather than constantly dealing with numbers that you then have to "translate" into cards or logic.
The enum chapter of the JLS actually has a very good example in its "members" section (Example 8.9.3-3) of how to create a standard deck, but I strongly suggest you read the whole chapter to get a really good idea of how they work.

Thirdly: What is a "deck" of cards? Answer: it's basically a collection (or List - java.util.List) of individual cards; and the Collections class (java.util.Collections) has a wonderful method called shuffle(), which allows you to randomise your "deck" without worrying about numbers or random number generators at all. After, it's shuffled, you just deal cards off the top, just like an Iterator does.

However, a lot of those last two items have to do with the "how", when what you need to concentrate on right now is the "what". Do as Steve suggested, and write down, in detail, and in English (or your native language), what the rules of the game of UNO are. And I'd suggest that you turn your computer OFF while you're doing it, so you're not tempted to start coding again.

HIH

Winston
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
I went back to that code and added code tags, which you should always use; don't they make it look better
Unfortunately, as well as the formatting problems (you seem to have mixed spaces and tabs) I do not like the code. Your ifs look very complicated; they look like what people write when they are guessing. Also, why is so much marked static?
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 172
You're "new to java" and have "10 days" to come up with an AI that plays the game? That seems way harder than a GUI for any AI that isn't completely clueless.

I'd suggest coding a game that will work for human players, and then when you're done with that ask your teacher if he or she is actually serious about the AI part.
Ed Macall
Greenhorn

Joined: May 23, 2014
Posts: 7
hi tajinder,

It looks like you hit Ctrl-V twice when you posted your code on May 12, 2014.

People who try to cut & paste this thing into their IDE will probably want to start the selection at " package pavan.uno " on line 68 and finish the selection around line 177. So there's only about 109 lines to copy, instead of 218.

Wow man. This takes me back. I remember from 35 years ago, sitting on the floor of the gym in school, playing Uno with Sherry Coleman. As far as having women notice me, I should have tried to stay with her, but I didn't know what was going on back then. I didn't know that my choices were going downhill from there. Oh well...
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Hi All,

Am not sure if you guys will still check this thread and will help me. Because of some personal reasons i didn't able to pursue my project .
please check the following classes and see if code is fine


tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
My code runs for only 2 players. I would request you all to suggest me what ll changes i should made.

As per my professor requirement.
1. I should use MVC architecture.
2. Structure 101 to check the dependency between different layers ( in my code there is dependency between playturn method and specialCardProcessingMethod.
3. I should use Observer design pattern to avoid any dependency.
4. And in the end i should use Swings for graphical presentation.


Once again i sincerely thank everyone for there valuable advice .
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Hi Campbell ,

Please see now ,i have added Code Tags(As per me this is fine).
Not sure if this is all you meant.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

tajinder pal singh wrote:(via PM) Hi Steve,

Sorry for approaching you like this but i need desperate help. Please look into UNO Game thread and give me your valuable advice.

Hi Tajinder,

Please read: UseTheForumNotEmail. Give time to read your posts and answer them, trying to be too pushy to get an answer may well backfire.

The problem, as I see it, is that you don't ask a specific question. You posted a lot of code and gave your requirements. What you didn't say is what problems you are having. That makes it hard for us to help you.
Ed Macall
Greenhorn

Joined: May 23, 2014
Posts: 7
Hi Guys,
The first problem that I see is when I try to cut and paste the code into Eclipse, I see like a million errors that need to fix just to get the thing to work.
On lines 7 and 8, I had to make "cardValue" and color "public". Line 39 and 168 needed to begin with a pair of "/"'s. Line 40 and 169 needed to "import game.Card;".
I bet a bunch of people hadn't answered because of how difficult it was to get the project put inside Eclipse or NetBeans or whatever IDE they are using.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

Ed Macall wrote: I bet a bunch of people hadn't answered because of how difficult it was to get the project put inside Eclipse or NetBeans or whatever IDE they are using.

Hi Ed, not sure I have seen you around before, so Welcome to the Ranch!

That is likely to cause problems. Tajinder, if you are having problems compiling you should tells us what the specific error messages you get when you try and which line of code the error is pointing to. The more details you provide the easier it is for us to help.
Ed Macall
Greenhorn

Joined: May 23, 2014
Posts: 7
Hi Guys,
I am seeing some more glitches.
I see on line 387 that player 1's name has been matched with player 2's hand.
I see on line 331 that player 1's hand has been matched up with player 2's name on line 327.
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
Hi All,

I have edited the code and its working now and sorry for earlier , i should have checked code before putting my query .
Steve apologise for sending direct mail earlier.
I know my approach is not good or may be my queries are not the way they should be.
Let me try again.

The piece of code i have posted yesterday is working(Now after editing) as per me where 2 players can play game(UNO). Now my queries

1> Is this code ok? what changes i should make?
2> MVC architecuture: Per me my
UNOGame
class is controller and card ,MainDeck falls under model. Am i right ?
3> I have to take care of dependencies as well , and my prof has suggested to use Structure101 for the same.

if you think i have created quite a mess , so shall i start new thread posting my fresh code ?


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38025
    
  22
tajinder pal singh wrote:Hi Campbell ,

Please see now ,i have added Code Tags(As per me this is fine).
Not sure if this is all you meant.
Yes, it was, but it doesn't make it much easier because you have such long lines.
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
if someone can suggest me how i can apply Observer design pattern on above code.i read about observer design pattern but still not clear which class would implement subject interface and which one will implement observer interface.
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

tajinder pal singh wrote:if someone can suggest me how i can apply Observer design pattern on above code.i read about observer design pattern but still not clear which class would implement subject interface and which one will implement observer interface.

Which class holds data which (an)other class(es) want to know about?
Which class wants to know about the other class' data?

The first is your subject and the second is your observer.
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
OK thanks steve.

I have another query . where my java code is failing





In this code
if(tmpCard.equals(iter.next())){
never comes out to be true for me . (though i can see in IDE while debugging that same Object of type UNOCard exists in collection which iterator is iterating)

I am just trying to check if the UNOCard is special or not.
tmpCard is of type UNOCard and iterator is returning UNOCard as well.



or if you can suggest me any other way to check if my object exists in collection?

Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4167
    
  21

You never posted the code for the UNOCard class, so I can't be sure, but my guess is that you did not override the equals() method for that class. Which means you would be relying on Object's equals() method, which is just == (tmpCard would have to be the same instance of UNOCard for it to be true). You should override the equals (and hashCode) method so that instances of UNOCard that represent the same value should be equal.
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25


here is the code for UNOCard .
toString method is already overridden .
tajinder pal singh
Greenhorn

Joined: Oct 04, 2011
Posts: 25
will it make sense.




IS there any tool or plugin , which i can integrate with my IDE(Eclipse) so that i can test my Piece of code or Method while writing it.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 172
One idea you might want to try is to separate the card types with numbers from those that perform an action. For example you may want a separate class that extends the UNOCard class of type "ValueCard" which has an int field for its cardValue instead of an enum. This saves you the trouble of having to convert the enum values to integer values elsewhere in your code.
Tyson Lindner
Ranch Hand

Joined: May 16, 2012
Posts: 172
tajinder pal singh wrote:


Also, just a minor organizational thing but I don't think the above methods should actually be in the card class. Whether you can play a card or not is more a function of the game state than a feature of the actual card.
 
Consider Paul's rocket mass heater.
 
subject: Developing UNO game in java
 
Similar Threads
Uno Openoffice for document generation
Uno: Returning control to the Uno object from a player object
How to "get my foot in the door"
urgent help
Sun Java EXAMS !!!!