[SOLVED] CS204 Homework 1

30.00 $

Programming resource
Digital learning resource
Category:
✓ Practical programming resource
✓ Suitable for guided study and reference
✓ Tutor guidance available when needed

Description

5/5 - (3 votes)

, you will implement Sabancı version of the world-wide internet sensation “Wordle”. The

regular game challenges the users to guess a word, and if they can guess the correct word in six attempts or

less, they win. Every time the user guesses a word, the game will give hints about the correctness of the

guess. When a user guesses a word, the letters of the guessed word will be colored with one of three colors:

green, yellow, and black, and each of these colors has different meanings. For each letter in the guessed

word, if the letter falls in the same position in the correct word, it will be colored green. If the letter is in

the correct word, but is in the wrong position, it will be colored yellow. Finally, if the letter does not occur

at all in the correct word, it will be colored black. Here’s an example. Let’s assume the word the user needs

to guess is “trade”. First, user tries the word “ready” as shown in Figure 1.

Figure 1: User entered “ready” when the correct word is “trade”.© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

As you can see in Figure 1, the letter “y” is colored black because it does not occur in the word “trade”.

The letters “r” and “e” are colored yellow because they occur in “trade”, but in wrong positions. Finally,

the letters “a” and “d” are in the correct positions, so they are colored green.

Next, the user tries “grade” (Figure 2). Only one letter is wrong!

Figure 2: user entered “grade” when the correct word is “trade”.

Finally, the user tries “trade” and finds the correct word, it is a win (Figure 3)!

Figure 3: user entered “trade” which is the correct word.

The game that you will build will be different than the regular Wordle, so we rename it as SWordle (Sabancı

version of Wordle). Mainly, there are four differences:

  1. You will be able to specify the number of attempts that the user can try; it could be more or less

than 6 attempts.

  1. The words that the user will guess don’t need to be 5 letters long. It can be shorter or longer.
  2. There are some extra input checks for the guesses that regular Wordle does not have (see below).
  3. Regular Wordle enforces to use a meaningful word as the guess, but in SWordle, we do not have

such a rule.

Inputs, Outputs, Matrix Uage and Program Flow

First, the user will be prompted for the name of a txt file. If the file with the given name cannot be opened,

user will be prompted again until a file can successfully be opened. Your program should read this txt file.

First line contains one integer that specifies the number of attempts the user is allowed to try. Second line

of the file contains the word to be guessed. After reading the file, you should display the number of letters

in the secret word and the total number of attempts.© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

You will use a 2D matrix to represent the current state of the user’s attempts. The matrix will be made up

of cell structs, of which the definition is given below:

The letter data member will contain the letter in the cell, and the color data member will contain the

characters ‘-’, ‘G’, ‘Y’, or ‘B’ depending on the color of the cell, no color (representing unused guess),

green, yellow, and black, respectively.

This matrix must be a 2D vector (i.e. a vector of vector of cell structs). It will have one row for each

attempt the user is allowed, and it will have as many columns as the number of letters in the correct word.

Initially, all the cells must have the letter and color members as ‘-‘ as shown in Figure 4 (for an example

case where the correct word has 4 letters and the number of attempts is 3).

Figure 4: The initial state of an example matrix for a 4-letter word and 3 attempts.

Once the matrix is created, your program should start asking the user for guesses. The user will input the

words using the keyboard. Every time the user inputs a word, your code will do some input checks (which

will be discussed in the next section). If the user enters a guess that fails any of the input checks, you must

ask the user for another input for the same attempt. You will keep asking the user for input until it passes

the input checks. Once the input passes the checks, you will add the word to the matrix, determine the

success of the guess (i.e. the colors), print out the status of the matrix, then check if the game is over.

To demonstrate how you will fill the matrix, let’s look at an example. Let’s assume that in this example,

the user is allowed three attempts, and that the correct word is “play”. Figure 5 shows what the matrix will

look like after the user tries the word “pale”.

struct cell

{

char letter; // letter inside the cell

char color; // color of the cell

};© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Figure 5: The letters and colors of the matrix after the user enters “pale” when the correct word is

“play”.

The letter ‘p’ is green since it’s in the correct position. The letters ‘a’ and ‘l’ are yellow since they occur in

the correct word but in different positions. The letter ‘e’ is black since it does not occur in the correct word

at all.

After every attempt by the user, your program must print out the current state of the matrix. For a sample

format of printing, please check the sample runs.

Your code will keep asking the user for words until either the user guesses the correct word (winning case)

or he/she runs out of attempts (losing case). Once one of these two outcomes happens, the program will end

execution.

Input Checks

Before the input checks, let us give the assumptions first. You may assume that there will not be any empty

line in the text file and there are exactly two lines in it. Moreover, the number of allowed attempts is

assumed to be given as a positive integer. Also, the word in the text file is assumed to contain only lowercase

letters (no numbers, special characters, uppercase letters, etc.). Moreover, we also assume that the word in

the text file does not contain any repeated letters. All of these assumptions are guaranteed and you do not

need to check them.

However, there are some input checks to be done by your program. These are for the user guesses that you

take as input. Specifically, when the user enters a word as a guess, you must check the following conditions

in this exact order:

  1. The number of letters in the user’s entered word matches that of the correct word.
  2. The characters of the word are all lowercase letters.
  3. There are no repeated letters in the word.
  4. The word has not been tried previously.
  5. If, a particular letter was green in one of the user’s previous attempts, then the same letter must be

in the newly entered word and in the same position. In other words, once you correctly guess a

letter, you have to use that letter all the time correctly for the upcoming trials. For example, in the

example shown in Figure 5, if the user tries to enter the word “liar” as the second guess, then this

check fails. This is because the first letter of “liar” is not “p”.

If any of these input checks fail, you will ask the user for another input for the same attempt. You will keep

asking the user for inputs until he/she enters a word that passes all checks. Please note that these checks

will be done sequentially and if any of them fails, an appropriate error message will be printed (see sample

runs) and others will not be checked for that input. In other words, if there are two or more input check © Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

problems of an entry, only one of them (the one encountered first in the above list) will be printed as error.

For example, if an input has more letters than the correct word and has repeated letters, you must only print

an error message stating that the word has too many letters. As another example, if an input has correct

amount of characters, but has repeated letters and non-lowercase characters, the error message regarding

the non-lowercase letters will be displayed only.

Sample Runs

Some sample runs are given below, but these are not comprehensive; therefore, you must consider all

possible cases to get full mark.

You do not need to give the output exactly as in the sample runs, provided that your output is understandable

and clear.

Sample Run 1:

Contents of the file “word1.txt”:

Console output (input is in bold italics):

5

trade

Welcome to SWordle!

Please enter the file name: word1.txt

The word that you will guess has 5 letters and you have 5 attempts.

What’s your guess? ready

|| r , Y || e , Y || a , G || d , G || y , B ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

What’s your guess? grade

|| r , Y || e , Y || a , G || d , G || y , B ||

|| g , B || r , G || a , G || d , G || e , G ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

What’s your guess? trade

|| r , Y || e , Y || a , G || d , G || y , B ||

|| g , B || r , G || a , G || d , G || e , G ||

|| t , G || r , G || a , G || d , G || e , G ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

You win!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 2:

Contents of the file “word2.txt”:

Console output (input is in bold italics):

3

saw

Welcome to SWordle!

Please enter the file name: word2.txt

The word that you will guess has 3 letters and you have 3 attempts.

What’s your guess? long

The word is too long!

What’s your guess? toolong

The word is too long!

What’s your guess? sh

The word is too short!

What’s your guess? new

|| n , B || e , B || w , G ||

|| – , – || – , – || – , – ||

|| – , – || – , – || – , – ||

What’s your guess? que

You did not use the green letter in your word!

What’s your guess? wow

Your input has a duplicate letter!

What’s your guess? woo

Your input has a duplicate letter!

What’s your guess? won

You did not use the green letter in your word!

What’s your guess? new

You’ve already tried this word!

What’s your guess? now

|| n , B || e , B || w , G ||

|| n , B || o , B || w , G ||

|| – , – || – , – || – , – ||

What’s your guess? saw

|| n , B || e , B || w , G ||

|| n , B || o , B || w , G ||

|| s , G || a , G || w , G ||

You win!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 3:

Contents of the file “word3.txt”:

Console output (input is in bold italics):

1

knit

Welcome to SWordle!

Please enter the file name: word3.txt

The word that you will guess has 4 letters and you have 1 attempts.

What’s your guess? ball

Your input has a duplicate letter!

What’s your guess? grit

|| g , B || r , B || i , G || t , G ||

You lose!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 4:

Contents of the file “word4.txt”:

Console output (input is in bold italics):

4

ice

Welcome to SWordle!

Please enter the file name: word4.txt

The word that you will guess has 3 letters and you have 4 attempts.

What’s your guess? lie

|| l , B || i , Y || e , G ||

|| – , – || – , – || – , – ||

|| – , – || – , – || – , – ||

|| – , – || – , – || – , – ||

What’s your guess? bye

|| l , B || i , Y || e , G ||

|| b , B || y , B || e , G ||

|| – , – || – , – || – , – ||

|| – , – || – , – || – , – ||

What’s your guess? ion

You did not use the green letter in your word!

What’s your guess? lie

You’ve already tried this word!

What’s your guess? die

|| l , B || i , Y || e , G ||

|| b , B || y , B || e , G ||

|| d , B || i , Y || e , G ||

|| – , – || – , – || – , – ||

What’s your guess? tie

|| l , B || i , Y || e , G ||

|| b , B || y , B || e , G ||

|| d , B || i , Y || e , G ||

|| t , B || i , Y || e , G ||

You lose!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 5:

Contents of the file “word5.txt”:

Console output (input is in bold italics):

5

knife

Welcome to SWordle!

Please enter the file name: word5.txt

The word that you will guess has 5 letters and you have 5 attempts.

What’s your guess? 51ler

Your input has illegal letters!

What’s your guess? ready

|| r , B || e , Y || a , B || d , B || y , B ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

What’s your guess? KNIFE

Your input has illegal letters!

What’s your guess? KniFE

Your input has illegal letters!

What’s your guess? knife

|| r , B || e , Y || a , B || d , B || y , B ||

|| k , G || n , G || i , G || f , G || e , G ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – || – , – ||

You win!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 6:

Contents of the file “word6.txt”:

Console output (input is in bold italics):

3

h

Welcome to SWordle!

Please enter the file name: wrong_file.txt

Couldn’t find the file!

Please enter the file name: not_word6.txt

Couldn’t find the file!

Please enter the file name: word6.txt

The word that you will guess has 1 letters and you have 3 attempts.

What’s your guess? a

|| a , B ||

|| – , – ||

|| – , – ||

What’s your guess? b

|| a , B ||

|| b , B ||

|| – , – ||

What’s your guess? c

|| a , B ||

|| b , B ||

|| c , B ||

You lose!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Sample Run 7:

Contents of the file “word7.txt”:

Console output (input is in bold italics):

4

when

Welcome to SWordle!

Please enter the file name: word9.txt

Couldn’t find the file!

Please enter the file name: word7.txt

The word that you will guess has 4 letters and you have 4 attempts.

What’s your guess? wedr

|| w , G || e , Y || d , B || r , B ||

|| – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – ||

What’s your guess? wtec

|| w , G || e , Y || d , B || r , B ||

|| w , G || t , B || e , G || c , B ||

|| – , – || – , – || – , – || – , – ||

|| – , – || – , – || – , – || – , – ||

What’s your guess? wheq

|| w , G || e , Y || d , B || r , B ||

|| w , G || t , B || e , G || c , B ||

|| w , G || h , G || e , G || q , B ||

|| – , – || – , – || – , – || – , – ||

What’s your guess? when

|| w , G || e , Y || d , B || r , B ||

|| w , G || t , B || e , G || c , B ||

|| w , G || h , G || e , G || q , B ||

|| w , G || h , G || e , G || n , G ||

You win!© Sabancı University, unauthorized reproduction, usage, posting are strictly prohibited

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.