Scrabble |
val letterValues = mapOf( 'A' to 1, 'B' to 3, 'C' to 3, 'D' to 2, 'E' to 1, 'F' to 4, 'G' to 2, 'H' to 4, 'I' to 1, 'J' to 8, 'K' to 5, 'L' to 1, 'M' to 3, 'N' to 1, 'O' to 1, 'P' to 3, 'Q' to 10, 'R' to 1, 'S' to 1, 'T' to 1, 'U' to 1, 'V' to 4, 'W' to 4, 'X' to 8, 'Y' to 4, 'Z' to 10)Players have to form words. Words are valid according to certain rules. We will use the official word list for North American scrabble, which you can download as a text file scrabble.txt.
You can read this file into a set like this:
val words = java.io.File("scrabble.txt").useLines { it.toSet() }
The score of a word is simply the sum of the values of the letters of the word.
Your task is to implement a game inspired by the rules of Scrabble, but without using the Scrabble board. In each round of the game, you receive a hand of HAND letter tiles. (You can think about the case HAND = 7 as in Scrabble.) Your task is to form words from these tiles.
Here is an example run of the game:
Welcome to KAIST Scrabble! Here is your hand: A E F G H L L Enter your word: HEAL HEAL is a good word and scores 7 points You have these letters left: F G L Enter your word: GIRL You cannot make GIRL with your hand. You have these letters left: F G L Enter your word: Your total score was 7 points. Would you like to play again? y Here is your hand: A A C I O P T Enter your word: TAPIOCA TAPIOCA is a good word and scores 11 points. Your total score was 61 points. Would you like to play again? y Here is your hand: C E E F I P Z Enter your word: CEEF CEEF is not a word. Try again! You have these letters left: C E E F I P Z Enter your word: PIECE PIECE is a good word and scores 9 points You have these letters left: F Z Enter your word: Your total score was 9 points. Would you like to play again? n Good bye!
In every round, the computer deals you a hand of HAND letter tiles (HAND must be a parameter defined at the top of your program). It does this as follows: The first HAND / 3 (integer division) tiles are vowels, chosen randomly (with replacement) from the set A E I O U. The remaining tiles are chosen from the set of all 26 letters, again randomly with replacement.
(Bonus: If you want, you can instead deal a hand by sampling randomly without replacement from a bag of Scrabble tiles as they appear in a real Scrabble game—see the picture below.)
Enter your word:
If the word entered is not in the offical Scrabble list, the computer refuses it:
Enter your word: CEEF CEEF is not a word. Try again!
If you don't have the tiles to make the word, the computer refuses it:
Enter your word: GIRL You cannot make GIRL with your hand.
If the word is accepted, the computer computes the score of the word, displays it, and shows you the letters you have left:
Enter your word: PIECE PIECE is a good word and scores 9 points
If there are any tiles left, you can then try to enter another word:
You have these letters left: C E E F I P Z Enter your word:
If you cannot form another word, you can just press Enter. This means that your round has ended. (The round ends automatically when you have no tiles left.)
The computer displays the total score of all your words:
Your total score was 9 points. Would you like to play again?
If you enter a question mark ? instead of a word, then the computer will display all words that can be made using your letters, and display them together with their scores, sorted by decreasing score:
Here is your hand: A D G I J R X Enter your word: ? 13 RADIX 11 JIG 11 JAG 10 RAX 10 RAJ 10 JAR 9 XI 9 AX 6 GRID 6 GRAD 6 GIRD 6 GADI 6 DRAG 5 RAID 5 RAGI 5 GID 5 GAD 5 DIG 5 DAG 5 ARID 4 RIG 4 RID 4 RAG 4 RAD 4 GAR 4 AID 3 RIA 3 RAI 3 ID 3 AIR 3 AG 3 AD 2 AR 2 AI You have these letters left: A D G I J R X Enter your word:
If you manage to use all letters in your hand in the first go, then you receive 50 extra points (see TAPIOCA in the example run).
Scrabble |