The Artima Developer Community
Sponsored Link

Java Answers Forum
Java problem in a card game

5 replies on 1 page. Most recent reply: Sep 15, 2005 11:05 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 5 replies on 1 page
ciro bonano

Posts: 3
Nickname: kresjer
Registered: Sep, 2005

Java problem in a card game Posted: Sep 12, 2005 2:53 AM
Reply to this message Reply
Advertisement
I have the following problem. I'm making a simple card game for online play. Here's a outline of my code:

class duuzenden
{
GameStatus game; // contains all the hands, the score, etc
CardPanel myhandpanel,hishandpanel;
main{
myhandpanel = new CardPanel (spel.myhand);
hishandpanel = new CardPanel (spel.hishand);
}

void submit ()
// send Main.game

void receive ()
// receive a game and put it into Main.game
}

class GameStatus
{
Hand myhand, hishand, etc..
}

class CardPanel extends JPanel
{
Hand h;
CardPanel(Hand h)
this.h = h;

paintComponent(Graphics g)
g.draw(h);
}


The problem now is that the information about the cards I have is stored in two places (Main.game.myhand and myhandpanel.h). When the cards change, I have to change them in two places: in Main.game.myhand to send Main.game to the other player, and in myhandpanel.h for correct display. How can I remove this redundency?

I'd rather give the CardPanel some kind of reference to the Main.myhand and Main.hishand, so that it'd look like

class CardPanel extends JPanel
{
Hand *h;
CardPanel(*Hand h)
this.h = h;

paintComponent(Graphics g)
g.draw(&h);
}

so that each instance of the CardPanel class still manipulates the objects in Main.game.

Thanks for your help!

Ciro.


Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java problem in a card game Posted: Sep 12, 2005 3:19 AM
Reply to this message Reply
Repost your code in a legible format using the
java tags. When posting there is a little text
on your right that states: "Formatting Your Post",
we just might look at your code if its more
presentable.

ciro bonano

Posts: 3
Nickname: kresjer
Registered: Sep, 2005

Re: Java problem in a card game Posted: Sep 12, 2005 6:21 AM
Reply to this message Reply
I have the following problem. I'm making a simple card game for online play. Here's a outline of my code:
class duuzenden
{
GameStatus game; // contains all the hands, the score, etc
CardPanel myhandpanel,hishandpanel;
main{
myhandpanel = new CardPanel (spel.myhand);
hishandpanel = new CardPanel (spel.hishand);
}
 
void submit ()
// send Main.game
 
void receive ()
// receive a game and put it into Main.game
}
 
class GameStatus
{
Hand myhand, hishand, etc..
}
 
class CardPanel extends JPanel
{
Hand h;
CardPanel(Hand h)
this.h = h;
 
paintComponent(Graphics g)
g.draw(h);
}


The problem now is that the information about the cards I have is stored in two places (Main.game.myhand and myhandpanel.h). When the cards change, I have to change them in two places: in Main.game.myhand to send Main.game to the other player, and in myhandpanel.h for correct display. How can I remove this redundency?

I'd rather give the CardPanel some kind of reference to the Main.myhand and Main.hishand, so that it'd look like

class CardPanel extends JPanel
{
Hand *h;
CardPanel(*Hand h)
this.h = h;
 
paintComponent(Graphics g)
g.draw(&h);
}


so that each instance of the CardPanel class still manipulates the objects in Main.game.


Put into another way, I'd like this to output "foobar"
public class Main {
    static String hier;
    public static void main(String[] args) {
        text = "foo";
        myString i = new myString(text);
        System.out.println(i.get()); // output foo
        hier = hier + "bar";
        System.out.println(i.get()); // still outputs foo
    }
}
 
class myString
{
    public String s;
    public myString(String st)
    {
        s = st;
    }
    public String get()
    {
        return s;
    }
}



Thanks for your help!

Ciro.

Kondwani Mkandawire

Posts: 530
Nickname: spike
Registered: Aug, 2004

Re: Java problem in a card game Posted: Sep 12, 2005 7:04 AM
Reply to this message Reply
>
> public class Main {
>     static String hier;
>     public static void main(String[] args) {
>         text = "foo";
>         myString i = new myString(text);
>         System.out.println(i.get()); // output foo
>         hier = hier + "bar";
>         System.out.println(i.get()); // still outputs foo
>     }
> }
 
Dude, I have no clue what your going on about in the other
parts but the above Q is straight forward:
 
[java]
text = "foo";
myString i = new myString(text);
System.out.println(i.get()); // output foo
text = text + "bar";
i = new myString(text);
System.out.println(i.get());


Obviously what you had written would produce foo both times;
you never change the String arguement for myString()
which is the only way the class below provides to
manipulate the attribute s - via the constructor.

Why not introduce a setter?

> class myString
> {
> public String s;
> public myString(String st)
> {
> s = st;
> }
> public String get()
> {
> return s;
> }
> }
> [/java]
>
>
> Thanks for your help!
>
> Ciro.

ciro bonano

Posts: 3
Nickname: kresjer
Registered: Sep, 2005

Re: Java problem in a card game Posted: Sep 12, 2005 7:17 AM
Reply to this message Reply
> >
> > public class Main {
> >     static String hier;
> >     public static void main(String[] args) {
> >         text = "foo";
> >         myString i = new myString(text);
> >         System.out.println(i.get()); // output foo
> >         hier = hier + "bar";
> >         System.out.println(i.get()); // still outputs
> foo
> >     }
> > }
> 

> Dude, I have no clue what your going on about in the
> other
> parts
I can understand, it's hard to explain when typing; I succesfully explained it in real life (without a good answer).


> But the above Q is straight forward:
>
>
> text = "foo";
> myString i = new myString(text);
> System.out.println(i.get()); // output foo
> text = text + "bar";
> i = new myString(text);
> System.out.println(i.get());
> 

>
> Obviously what you had written would produce foo both
> times;
> you never change the String arguement for myString()
> which is the only way the class below provides to
> manipulate the attribute s - via the constructor.
Yes, I understand completely; I'd like to have a construction such that it would work. I don't want to store the text "foo" or "bar" BOTH in main() and in the object, because I have to change them both if my game needs to change them.

I would like to have, when I type
text = "foo";
myString i = new myString(text);

that the new myString object still knows where his argument text came from, and that myString is able to manipulate Main.text.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Java problem in a card game Posted: Sep 15, 2005 11:05 PM
Reply to this message Reply
That IS possible, but ony if you never ever use Main.text = ... after initialising MyString since that woudld generate a new String Object instead of changing the content.


My way to do this would be using a interface like
interface TextContainer {
    public String getText();
    public void setText(String s);
}
and let Main implement this interface

MyString would then be changed like this
class MyString implements TextContainer {
    /** Create a new MyString instance
     * @param owner this owner will be altered when the setText Method is called.
     */
    public MyString(TextContainer owner) {
        this.owner = owner;
        s = (owner == null) ? null : new String(owner.getText()); // you can also assign the String directly. Doing that, character operations would affect this object and the owner.
    }
    /** Create a new MyString reference without a owner.
     * @param text a copy of this text will be stored in this object.
     */
    public MyString(String text) {
        this.owner = null;
        s = new String(text);
    }
    /** returns a copy of the text stored in this object
     * @return the text of this object
     */
    public String getText() {
        return new String(s);
    }
    /** Sets the text of this Object and alters the text of the owner
     * @param text this text will be copied into this object and into the owner
     */
    public void setText(String text) {
        s = new String(text);
        if (owner != null)
            owner.setText(text);
    }
//    public String get() {
//        return getText();
//    }
    private String s;
    private TextContainer owner;
}


I let MyString implement TextContainer, so it can be used as for other MySttring objects.

Flat View: This topic has 5 replies on 1 page
Topic: path of a class Previous Topic   Next Topic Topic: need help on exercise

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use