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.