Please conduct the following tasks alone. For implementation details you can refer to the lecture slides or the Apple developer website. Please do not hesitate to ask me or the tutor if you have any questions. Under "Tip" you can find some hints for the task. The "Checklist" specifies what should happen when your implementation is correct.
Open a Playground and write a program that creates a shoppingList with 5 shopping items. Next, iterate over the shoppingList and output the items to the console. Then remove the item number 4 in the list and print the new number of items in the shoppingList to the console. The output should look like this:
Use an Array of Strings to store the items in the shoppingList. Iterate over the list using a for-loop.
Create a new Xcode Project and choose OS X - Application -> Command Line Tool.
If you have problems compiling your application. Check if Swift runtime tools are missing. You can download them here.
Name your project „NumberGuess“ and select „Swift“ as the Language.
Now write a game that asks the user:
„Please think about a number between 0 and 1000“
„Is the number 500 (g)reater or (s)maller than your number or is it (c)orrect?“
To read input from the command line use the following code:
textInput = readLine()!
At the beginning of the game, the number 0 is stored in a variable „min“. The number 1000 is stored in a variable „max“. The number 500 in this case is stored in a variable called „currentValue“ and is calculated as follows:
currentValue = min + (max - min) / 2
When the „readLine()“ function is called, the user can enter text in the console. The user should now enter „g“, „s“ or „c“ in the console correspondingly, until the number is correct. If the user enters another letter than „g“,“s“ or „c“, the program should output „Please only type g,s or c“
If the user enters „g“, the value of „max“ is set to „currentValue“, i.e.:
max = currentValue
In the case of „s“ the value of „min“ is set to „currentValue“, i.e.:
min = currentValue
When the user enters „c“ the program prints to the console how many tries were needed to guess the number.
The following shows an example game output:
Use a variable „tries“ to count the tries. Use a while-loop as „game-loop“ and a boolean „finished“ to check if the user pressed „c“. Use a switch statement to check what the user has entered. Use println() to output text and input() to read text from the console