C++留学生程序讲解、辅导program程序、讲解c++编程语言
- 首页 >> Algorithm 算法 Imperative
Programming in C
Assignment: Word Search
Introduction
A Word Search Puzzle consists of a grid of letters, with words hidden inside horizontally,
vertically and diagonally. The player’s task is to find all the words hidden in the grid.
Your task for this assignment is to create a Word Search Puzzle generator, and a program that
lets someone play the game against the clock.
An example Word Search Puzzle can be seen below.
In our version, we will ask the user to pick a category from a list, create a word search puzzle
based on that category and then let them type words they can see in the grid. If they find all
words before a timer reaches zero, they have won!
Specification
Your program should have the following features (30 marks in total):
1. Word Search Generation
a) Store a list of words on specific subjects (animals, colors, planets etc.) (2 marks)
b) Generate a 2D grid of letters, containing at least 6 words from a category found in
(1a). The size of the 2D grid should be definable (minimum 12x12).
o The words should be randomly placed within the grid
i. Horizontally (40% of the time) (2 marks)
ii. Vertically (40% of the time) (2 marks)
iii. Diagonally (20% of the time) (4 marks)
Diagonal words can be ascending or descending.
c) Ensure words do not overwrite one another. (5 marks)
2. Word Search Game
a) Welcome user to the program. (1 mark)
b) Present the user with a choice of categories from (1a). (1 mark)
c) Ask user to type in words that they can see in the grid ( 2 marks)
d) Highlight or remove correctly guessed words from the grid (4 marks)
e) Show message to user if time limit is reached, or all words found (2 marks)
For (2e). You do not need to end the game exactly when timer reaches 0, only when
user types in a word after timer has reached 0.
Coding
Programming Proficiency (3 marks)
Commenting / Formatting (2 marks)
Programming proficiency refers to how well your program runs, how it looks etc. Marks are
awarded for impressing the assessor here.
Commenting / Formatting marks are awarded for clear documentation of your code through
appropriate comments, correct indentation etc.
Implementation Guide
While you are free to code your program any way you want, it is recommended you follow
this implementation guide especially if you are unsure what order to develop your program.
Stage 1 – Grid Initialisation
The appendix contains a shell of a program that you can use as a starting point for this
assignment. The code will create a 2D character array and fill it with ‘x’ characters.
The first stage for your assignment should be to initialise a 2D array containing a random grid
of letters from the alphabet, and print the grid with appropriate formatting.
By the end of this stage your program should generate output similar to the following:
Example function prototypes for this stage are:
char **create2DArray(); //creates a 2D array of characters, returns pointer to array
void printArray(char** array); //prints out a 2D array on screen
Stage 2 – Inserting Words Horizontally
To make debugging easier you should initialise your grid to a common character for now. In
the following examples, the grid is filled with ‘.’ characters rather than random letters.
Extend your program so that it can insert horizontal words into the grid at random positions.
The recommended function prototype is:
void insertHorizontally(char* word, char** array);
Eventually you will need to test that you are not overwriting words already inserted into the
grid. However for the time being do not worry about testing for this.
Hints
Look at previous labs, lectures and code snippets for examples of how to insert
information into a 2D array
Use a for-loop to insert the word into the array one character at a time
You can use strlen() to get the length of a string.
When inserting a word into the array, you will need to make sure that there is enough
horizontal space for all the letters.
When you’ve finished this stage, your program should be able to pick from an array of words
and place them at random horizontal positions in the array, similar to the following
screenshot:
Stage 3 – Inserting Words Vertically/Diagonally
The next stage is to extend your program so that it can display words vertically and diagonally.
These are example function prototypes for this stage:
void insertVertically(char* word, char** array);
void insertDiagonally(char* word, char** array);
Remember that diagonal words can be ascending or descending. For example
Test your program by inserting a mixture of horizontal, vertical and diagonal words into the
grid. They should appear 40%, 40% and 20% of the time respectively.
You might have noticed that some words overlap in the example screenshots so far
(Chimpbear, Lhamadile). This is because we are not doing any testing to check if we are
replacing characters that were previously placed in the grid. It is up to you if you want to work
on this part first (Stage 5), or create the game first (Stage 4).
Stage 4 – Playing the game
We are now at a stage where we can program a game using the grid and associated words.
As the specification indicates, your program should work like this:
Welcome user to the program
Ask the user which category of words they would like
Generate a grid using words from this category
Start a timer (e.g. 2 minutes)
In a loop
o Present the grid to the user
o Allow the user to type in words they see in the grid
o If they find all words in time – print congratulations message
o If the timer reaches zero – print game over message
o (Optional) Clear screen after every guess
Exit
You are free to embellish your program however you like, but you must ensure that it can
perform at least the tasks above. If in doubt refer to the specification.
An example output of the game can be seen in the appendix.
Stage 5 – Overlapping words
You are on your own with this part of the assignment. You will need to ensure that when your
program generates words to place in the grid that they do not overlap words that were
previously inserted.
There are many ways to complete this stage. One potential solution is to use another 2D array
which tracks where words have previously been inserted (0=clear, 1=occupied). This could
then act like a mask when inserting new words. This would also require changing the insertion
function prototypes e.g.
insertHorizontally(animals[i], myArray, myMask);
An example mask can be seen below.
However as previously mentioned, using a simple mask is just one potential solution to this
stage.
General Hints
In Windows you can clear the screen using the code system("cls"); Similar functionality
will exist in OSX and Linux.
Lab 3 Exercise 5 contains an example of how to set text and background colour in
Windows. Similar functionality will exist in OSX and Linux.
The code sleep(2); found in will cause your program to pause for 2 seconds.
You can use this function to manage the flow of your game.
When seeding the random number generator using srand(), if you give it a specific
number e.g. srand(5), your program will always generate the same puzzle. This can be
very useful when testing your program.
You can use strlen() to get the length of a string (found in)
strlen() is useful for several tasks in the assignment, such as fitting strings inside the
grid, and iterating over each character in a string
To setup a countdown timer you will need to research. Example code can
also be found in the appendix.
The Beep(x,y) function takes in a frequency and duration, and makes the speaker on
the PC speaker beep! For example Beep(1000,1000) will beep at 1Khz for 1 second.
You can use this to signify correct/incorrect messages, or even play a theme tune.
Windows only.
Submission
Use Blackboard to submit your source code. For this assignment you should zip your project
folder and submit the zip file. Ensure that all your program code is submitted to us. You will
also need to attend a lab session to get your work assessed before the end of week 12.
Ensure that the source code:
Contains a program header
Contains an appropriate level of comments
Follows a consistent style of indentation
Follows the usual C programming conventions
The deadline for submission will be published on Blackboard. Late submissions will be
penalised in line with School policy.
When submitting work it is your responsibility to ensure that all work submitted is
Consistent with stated requirements
Entirely your own work
Submitted through Blackboard on time
Please note that there are severe penalties for submitting work which is not your own. If
you have used code which you have found on the Internet or from any other source then you
must signal that fact with appropriate program comments and to the assessor.
Appendix A
Appendix B
Create a 2D Array
char **create2DArray(); //function prototype
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array
void main()
{
myArray = create2DArray();
}
//Creates a 2D array of WIDTH * HEIGHT and returns a pointer to it
char **create2DArray(){
int i,j;
char **array = (char **) malloc(sizeof(char *) * WIDTH);
for(i=0; i array[i] = (char *) malloc(sizeof(char) * HEIGHT);
for(i=0; i for(j=0; j // array[i][j] = 65 + rand() % 25;
array[i][j] = 'x';
return array;
}
Stopwatch
#include
clock_t start = clock();
/*Do something*/
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;
Sleep
#include
printf( "I'll be back in 10 seconds...\n\n" );
sleep(10);
printf( "I'm back!" );
getchar();
Programming in C
Assignment: Word Search
Introduction
A Word Search Puzzle consists of a grid of letters, with words hidden inside horizontally,
vertically and diagonally. The player’s task is to find all the words hidden in the grid.
Your task for this assignment is to create a Word Search Puzzle generator, and a program that
lets someone play the game against the clock.
An example Word Search Puzzle can be seen below.
In our version, we will ask the user to pick a category from a list, create a word search puzzle
based on that category and then let them type words they can see in the grid. If they find all
words before a timer reaches zero, they have won!
Specification
Your program should have the following features (30 marks in total):
1. Word Search Generation
a) Store a list of words on specific subjects (animals, colors, planets etc.) (2 marks)
b) Generate a 2D grid of letters, containing at least 6 words from a category found in
(1a). The size of the 2D grid should be definable (minimum 12x12).
o The words should be randomly placed within the grid
i. Horizontally (40% of the time) (2 marks)
ii. Vertically (40% of the time) (2 marks)
iii. Diagonally (20% of the time) (4 marks)
Diagonal words can be ascending or descending.
c) Ensure words do not overwrite one another. (5 marks)
2. Word Search Game
a) Welcome user to the program. (1 mark)
b) Present the user with a choice of categories from (1a). (1 mark)
c) Ask user to type in words that they can see in the grid ( 2 marks)
d) Highlight or remove correctly guessed words from the grid (4 marks)
e) Show message to user if time limit is reached, or all words found (2 marks)
For (2e). You do not need to end the game exactly when timer reaches 0, only when
user types in a word after timer has reached 0.
Coding
Programming Proficiency (3 marks)
Commenting / Formatting (2 marks)
Programming proficiency refers to how well your program runs, how it looks etc. Marks are
awarded for impressing the assessor here.
Commenting / Formatting marks are awarded for clear documentation of your code through
appropriate comments, correct indentation etc.
Implementation Guide
While you are free to code your program any way you want, it is recommended you follow
this implementation guide especially if you are unsure what order to develop your program.
Stage 1 – Grid Initialisation
The appendix contains a shell of a program that you can use as a starting point for this
assignment. The code will create a 2D character array and fill it with ‘x’ characters.
The first stage for your assignment should be to initialise a 2D array containing a random grid
of letters from the alphabet, and print the grid with appropriate formatting.
By the end of this stage your program should generate output similar to the following:
Example function prototypes for this stage are:
char **create2DArray(); //creates a 2D array of characters, returns pointer to array
void printArray(char** array); //prints out a 2D array on screen
Stage 2 – Inserting Words Horizontally
To make debugging easier you should initialise your grid to a common character for now. In
the following examples, the grid is filled with ‘.’ characters rather than random letters.
Extend your program so that it can insert horizontal words into the grid at random positions.
The recommended function prototype is:
void insertHorizontally(char* word, char** array);
Eventually you will need to test that you are not overwriting words already inserted into the
grid. However for the time being do not worry about testing for this.
Hints
Look at previous labs, lectures and code snippets for examples of how to insert
information into a 2D array
Use a for-loop to insert the word into the array one character at a time
You can use strlen() to get the length of a string.
When inserting a word into the array, you will need to make sure that there is enough
horizontal space for all the letters.
When you’ve finished this stage, your program should be able to pick from an array of words
and place them at random horizontal positions in the array, similar to the following
screenshot:
Stage 3 – Inserting Words Vertically/Diagonally
The next stage is to extend your program so that it can display words vertically and diagonally.
These are example function prototypes for this stage:
void insertVertically(char* word, char** array);
void insertDiagonally(char* word, char** array);
Remember that diagonal words can be ascending or descending. For example
Test your program by inserting a mixture of horizontal, vertical and diagonal words into the
grid. They should appear 40%, 40% and 20% of the time respectively.
You might have noticed that some words overlap in the example screenshots so far
(Chimpbear, Lhamadile). This is because we are not doing any testing to check if we are
replacing characters that were previously placed in the grid. It is up to you if you want to work
on this part first (Stage 5), or create the game first (Stage 4).
Stage 4 – Playing the game
We are now at a stage where we can program a game using the grid and associated words.
As the specification indicates, your program should work like this:
Welcome user to the program
Ask the user which category of words they would like
Generate a grid using words from this category
Start a timer (e.g. 2 minutes)
In a loop
o Present the grid to the user
o Allow the user to type in words they see in the grid
o If they find all words in time – print congratulations message
o If the timer reaches zero – print game over message
o (Optional) Clear screen after every guess
Exit
You are free to embellish your program however you like, but you must ensure that it can
perform at least the tasks above. If in doubt refer to the specification.
An example output of the game can be seen in the appendix.
Stage 5 – Overlapping words
You are on your own with this part of the assignment. You will need to ensure that when your
program generates words to place in the grid that they do not overlap words that were
previously inserted.
There are many ways to complete this stage. One potential solution is to use another 2D array
which tracks where words have previously been inserted (0=clear, 1=occupied). This could
then act like a mask when inserting new words. This would also require changing the insertion
function prototypes e.g.
insertHorizontally(animals[i], myArray, myMask);
An example mask can be seen below.
However as previously mentioned, using a simple mask is just one potential solution to this
stage.
General Hints
In Windows you can clear the screen using the code system("cls"); Similar functionality
will exist in OSX and Linux.
Lab 3 Exercise 5 contains an example of how to set text and background colour in
Windows. Similar functionality will exist in OSX and Linux.
The code sleep(2); found in
You can use this function to manage the flow of your game.
When seeding the random number generator using srand(), if you give it a specific
number e.g. srand(5), your program will always generate the same puzzle. This can be
very useful when testing your program.
You can use strlen() to get the length of a string (found in
strlen() is useful for several tasks in the assignment, such as fitting strings inside the
grid, and iterating over each character in a string
To setup a countdown timer you will need to research
also be found in the appendix.
The Beep(x,y) function takes in a frequency and duration, and makes the speaker on
the PC speaker beep! For example Beep(1000,1000) will beep at 1Khz for 1 second.
You can use this to signify correct/incorrect messages, or even play a theme tune.
Windows only.
Submission
Use Blackboard to submit your source code. For this assignment you should zip your project
folder and submit the zip file. Ensure that all your program code is submitted to us. You will
also need to attend a lab session to get your work assessed before the end of week 12.
Ensure that the source code:
Contains a program header
Contains an appropriate level of comments
Follows a consistent style of indentation
Follows the usual C programming conventions
The deadline for submission will be published on Blackboard. Late submissions will be
penalised in line with School policy.
When submitting work it is your responsibility to ensure that all work submitted is
Consistent with stated requirements
Entirely your own work
Submitted through Blackboard on time
Please note that there are severe penalties for submitting work which is not your own. If
you have used code which you have found on the Internet or from any other source then you
must signal that fact with appropriate program comments and to the assessor.
Appendix A
Appendix B
Create a 2D Array
char **create2DArray(); //function prototype
#define WIDTH 16
#define HEIGHT 16
char** myArray; //global array
void main()
{
myArray = create2DArray();
}
//Creates a 2D array of WIDTH * HEIGHT and returns a pointer to it
char **create2DArray(){
int i,j;
char **array = (char **) malloc(sizeof(char *) * WIDTH);
for(i=0; i
for(i=0; i
array[i][j] = 'x';
return array;
}
Stopwatch
#include
clock_t start = clock();
/*Do something*/
clock_t end = clock();
float seconds = (float)(end - start) / CLOCKS_PER_SEC;
Sleep
#include
printf( "I'll be back in 10 seconds...\n\n" );
sleep(10);
printf( "I'm back!" );
getchar();