Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialSteve Kinsey
4,463 PointshasTile method?
"Can you also please help me write out the hasTile method? It should return true if the hand has the tile, and false if it doesn't. Thanks!"
I don't really understand what the hasTile method is supposed to achieve exactly.
Is it that only one of each letter is allowed in the hand (not the case in Scrabble though I don't think)? Is it checking the number of tiles in the hand against a maximum amount?
Having problems writing the code because I don't understand what the hasTile method is actually for :-(
Any pointers would be much appreciated :-)
Cheers SK
public class ScrabblePlayer {
private String mHand;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public void addTile(char tile) {
mHand += tile;// Adds the tile to the hand of the player
}
public boolean hasTile(char tile) {
if (mHand = tile) {
return true;
} else {
return false;
}
}
7 Answers
Allie O.
11,601 PointsHi Steve,
hasTile method will check your current hand and see if the tile you're looking for is a tile that you have. If you do, it will return true. If not, it will return false. So, you can do something like this:
public boolean hasTile(char tile) {
boolean currentHand = mHand.indexOf(tile) >= 0;
if(currentHand){
return true;
} else {
return false;
}
}
Hope it helps!
Steve Kinsey
4,463 PointsBrilliant stuff, cheers Allie :-)
It was more that I couldn't make sense of what I was being asked to get the hasTile method to do...but your explanation is great, I get it now... and your suggested code makes sense too so I'll try it out later...
Thanks again Allie, I really appreciate you taking the time to help out... as they say here in Dublin, fair play to ye :-)
Peace SK
Allie O.
11,601 PointsYou're very welcome! It took me a while to figure out exactly what was being asked too, so no worries. :) We're all here to help each other out! :)
Steve Kinsey
4,463 PointsPerfect...'Congrats' and green for go :-)
Thanks again Allie...
(but sometimes in scrabble it's ok and actually kinda useful to have 2 of the same letter...I think that's what threw me a bit in understanding the purpose of this bit of code...s'all good though, onwards...)
MUZ140564 Kudakwashe Jena
4,895 Pointspublic class ScrabblePlayer { private String mHand;
public ScrabblePlayer() {
mHand = "";
}
public String getHand() {
return mHand;
}
public void addTile(char tile) {
// Adds the tile to the hand of the player
}
public boolean hasTile(char tile) {boolean currentHand = mHand.indexOf(tile) >= 0; if(currentHand){ return true; } else; {return false; } } am getting this error./ScrabblePlayer.java:18: error: reached end of file while parsing } ^ 1 error where am i going wrong
ISAIAH S
1,409 PointsTry adding an extra }.
Chaun Fogarty
9,803 PointsJust as a thought, since you've already made currentHand a boolean that is true when you have the tile and false when you don't, you could just return currentHand instead of worrying about the if/else.
adil rafaa
3,194 Pointssame here , I think my code is fine but still I cant pass it
public boolean hasTile(char tile) {
// TODO: Determine if user has the tile passed in
boolean isTile = tiles.indexOf(tile) >= 0 ;
if (isTile) {
return true;
} else {
return false;
}
}
Ray Porras
4,425 PointsRay Porras
4,425 PointsGreat work! I made a little revision to it: