# data程序讲解、辅导C++程序设计、c/c++编程语言调试 辅导Python程序|讲解留学生Prolog

- 首页 >> Web
Computing Machinery I
Assignment 1
The lead TA for this assignment is Abdullah Sarhan asarhan@ucalgary.ca
Objective
The objective of this assignment is to practice basic C concepts, including 2D arrays, random numbers, and
searching.
Note
It is your responsibility to make sure your code runs on arm.cpsc.ucalgary.ca. Code tested on your
laptop or even linux.ucalgary.ca may not run as desired on arm.cpsc.ucalgary.ca. We will
test your code on the arm servers and we will follow the published marking rubric.
Skills needed for this assignment
• Ability to work with 2D arrays in C
• Ability to use a random number generator
• Ability to employ a searching algorithm
• Ability to use command-line arguments
• Ability to write and read text files in C
• Ability to define functions in C
• Ability to pass parameters by value and by reference
Overview
Your program will implement a simple memory game. The user attempts to match face-down cards. If the
user guesses wrong, the program provides hints.
Details
The game board is a 2D array. The size of which is 2Nx2N where N is a number provided by the user as a
command-line argument. For instance, the program can be started as to initialize a 4x4 grid:
./assign1.o playerName 2
To initialize the values in the board array, follow the steps:
1. Create an array cards of size 2N*N such that card[i] = i.
2. Shuffle the array cards using the following simple algorithm:
for (i = 0; i <2N-2; i++) {
j = random(i,n2*N*N-1); // generate a random number j such that i  j  n2*N*N-1
swap(cards[i],cards[j]);
}
3. Copy cards to board, one row at a time, starting from row 0.
4. Shuffle cards again
5. Copy cards to the second half board
Once board is initialized print it to the screen:
0 3 7 2
5 6 1 4
3 5 1 2
4 6 0 7
Wait for the user to hit return, then hide your board displaying them face down:
X X X X
X X X X
X X X X
X X X X
You need to clear the screen; you can call Linux clear command from your program as:
system("clear");
The user then selects a card to uncover:
Enter the coordinates of a card: 0 3
The board is re-printed showing card (0,3) uncovered:
X X 7 X
X X X X
X X X X
X X X X
Enter the coordinates of a card: 2 3
X X 7 X
X X X X
X X X 2
X X X X
The user in this example missed to match card 7. So, a hint is provided:
You are 1 card away!
Press any key to continue …
Note that “one card away” means that 7 could be in any of the immediate neighbors of card (2,3). That is,
any of (3,3), (1,3), or (2,2).
When the user continues the game, the mismatched card is flipped back to face down:
X X 7 X
X X X X
X X X X
X X X X
Enter the coordinates of a card: 3 3
This time the user guesses right and the matched cards remain revealed until the end of the game
X X 7 X
X X X X
X X X X
X X X 7
Enter the coordinates of a card:
The user can play the game until all cards are matched, the score becomes 0, or they decide to quit. The
user exists the game by entering q or Q.
The score is calculated as follows. A user starts with a score of 2N. A match adds N points to the score. A
miss subtracts 1 point.
When the user exists or the game ends, the player name and score are recorded in a text file.
Modularity
Your code must be divided into functions as appropriate. At a minimum, you must define the following
functions (we are not showing all necessary arguments, but what is shown must be used as shown):
- initialize(*board)
- shuffle(*cards)
- swap(*cards, i, j)
- randomNum(n,m); n and m are the inclusive lower and upper bounds for the random
number. You can use the C library function rand().
- display(*board)
- findDistance(*board, i1, j1, i2, j2); returns the distance between cards (i1,j1)
and (i2,j2). This distance is 0 if it is a hit; otherwise, the value is used to provide a hint to the user.
- logFile(*playerName, score)
Submission
• Note: The TA may provide further submission instructions
• Create a script file and call it assign1.script
• Name your log file assign1.log
• You may submit a README file providing extra instructions or information for your TA
[optional]
• Submit your work to the appropriate dropbox on D2L.
Late Submission Policy
Late submissions will be penalized as follows:
-12.5% is applied for each late day or portion of a day.
Hence, no submissions will be accepted after 8 days (including weekend days) of the announced deadline.