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 trial
Simen Anthonsen
1,097 PointsScrabble functions
I´m not making any scrabble app but i need my app to have some of the same functions as other scrabble apps have.
The function I desire is the one where the user writes in a word and tap the "Play" button and get the notification "You played (Word)". And when he writes in another word he gets the notification saying "You played (another word)".
I have managed the first part, but i haven´t managed the last part. When i write another word and push the button play I get a notification saying "You played (word) + (another word)" and not the desired result.
Anyone who knows how these scrabble functions works or have any good thought on how I can manage this?
3 Answers
ianhan3
4,263 PointsCreate an array of strings for played words, create a variable wordPlayed connected to a textfield entry, create a button that when pressed appends the textfield value to the array, assigns it to the variable word, and uses that variable to change a label saying "You played (word)". That way you can call the array to see past played words. Would that work? I'll make a quick project to illustrate and put it on GitHub. Give me a few and I'll post the link.
Simen Anthonsen
1,097 PointsThanks for your help! It works and looks great but there is one thing i forgot to explain.
In my app, I plan to have multiple textfields, organised just like a scrabble board,and just like in scrabble you will only be able to write one letter in a textfield. This I have got under control.
In my app, if you write for example "S" "O" "C" "K" in the first four textfields and push the button "Play", you will get a notification saying "You played sock" and the four textfield will be enabled user interaction. This I have managed. But when round 2 comes, and the second user writes for example "Y" "E" "S" in the next three textfields, the notification says "You played sockyes" instead of the desired result "You played yes".
Do you have any ideas on how to do this? The function I am looking for will probably cause some problems down the road, seeing as I ultimately want to "include" some "already written letters" in my playWord variable. What i mean by this is if you write HARD horizontally in the first four textfield from the upper left corner, and then on the second round you write (H)OT vertically from the upper left corner, I want the notification to say "You played HOT." I will deal with these complications down the road, but I won´t stop you if you have some good ideas or advices :D
But for now I will be happy if find a way to play two separate words on the same "game board".
I have just created a simple demo version of my app, and will put it on GitHub. I will give give you a link, if you want to check it out.
In the demo version i have got an array of all the textfield and a variable named wordPlayed that contains an empty string. I have used a for in loop in my Button Action to illiterate through all the textfields and check which of them that contains a letter. If it that´s the case, the letter will be assigned to the variable playedWord.
Simen Anthonsen
1,097 PointsSorry, but I have never used GitHub before so I don´t know how to upload a project... I´ll take a later look at it, but here is my code at least.
var textfields = [Array of all textfield(my "board" consists of 5*5 textfields)]
var wordPlayed = ""
@IBAction func playButtonPushed(sender: UIButton) {
for textfield in textfields {
if textfield.text?.isEmpty == false {
wordPlayed += textfield.text!
}
}
label.text = wordPlayed
wordPlayed = ""
}
ianhan3
4,263 Pointsianhan3
4,263 Pointshttps://github.com/ianhhan/WordEntry
ianhan3
4,263 Pointsianhan3
4,263 PointsIt's not pretty but it functions like you ask. The text entry is connected to a label that shows the word you just played. Each word is then appended to an array which is then connected to a label below showing the array. Clearly not pretty. If I was to expand on the app, I would have a few labels that just had the last few words played. You could do this potential by accessing the index value of the array you just created in various labels? I think that would probably work. I didn't do any auto layout or anything so just run it on the iPhone 5 simulator to play around.