How to convert two elements into one
Is it possible to convert one element (string) from one linkedlist with another element (string) from another linkedlist to have an output of one element that joined the two strings?
I am trying to show that a card is facing up so I need to add the string UP in the card name without producing two elements (card, up) -- should be cardUP.
Re: How to convert two elements into one
It sounds like the upness is a property of a card. Consider modelling this by writing a Card class whose toString() or some other method produces the desired string.
One consequence of this approach is that the "parallel" lists of String become a single list of Card which can be easier to keep track of.
Re: How to convert two elements into one
This is a solitaire game (without graphics) that I am trying to write. I can't figure out how to write a class with that property because the cards are not always facing down. By default they are facing down. So the status changes.
Re: How to convert two elements into one
Quote:
So the status changes
That's what setter methods are for. Your Card class would have an instance variable like isUp or whatever, and a setter method:
Code :
public class Card {
private boolean isUp = false;
/** Sets the the card's upness to a given value. */
public void setUp(boolean b) {
isUp = b;
}
public String toString() {
String str = /* whatever */
if(isUp) {
str += "(UP)";
}
return str;
}
}
As well as isUp you might have other instance variables like suit and rank and a constructor to set up a card with a particular suit and rank.
Re: How to convert two elements into one
So what will be the output of that code if I do sys.out.println(str) if the string is KD?
I have the code below and the corresponding output.
Code java:
void openTopCards(){
LinkedList<String> fliper = new LinkedList <String>();
face.add("Up");
face.add("Dn");
for (int i=0; i < m; i++){
if (maneuver[i].size() != 0){
fliper.add(maneuver[i].pollLast());
fliper.add(face.peekFirst());
maneuver[i].addAll(fliper);
}
fliper.clear();
System.out.println("maneuver[" + i + "]" + maneuver[i]);
}
}
The output is:
maneuver[0][AD, Up]
maneuver[1][KD, 7D, Up]
maneuver[2][QD, 6D, AH, Up]
maneuver[3][JD, 5D, KH, 9H, Up]
maneuver[4][10D, 4D, QH, 8H, 5H, Up]
maneuver[5][9D, 3D, JH, 7H, 4H, 2H, Up]
maneuver[6][8D, 2D, 10H, 6H, 3H, AS, KS, Up]
I want the output to be like this:
maneuver[6][8D, 2D, 10H, 6H, 3H, AS, KSUp or Up KS] because there will other cards going on top of KS in the future.
I am thinking of putting all the open cards in another linkedlist then adding them all to the maneuver linkedlist.