Make a small game by yourself (1 ).
I am a newcomer to programming and have no programming experience. I am studying programming by myself. Recently I saw cs50 In the Netease Open Course and saw a small game similar to HuaRong road displayed by the lecturer. So I decided to write it myself, it is also a consolidation of the content of the previous videos.
The game forms an n-level matrix based on the input number. There is a vacant space, you can move the number next to the vacant space, and finally turn this matrix into an orderly arrangement, that is, the game is successful.
1. First, I decided to write out the part that generates the matrix based on the input order,
This matrix should be arranged randomly. I first assign all the numbers in the Matrix to the square [] array, and then use the random function to randomly exchange the numbers in the array, finally, the form of the matrix is printed.
Here, the generation of random numbers has plagued me for a long time. Through Baidu, I know that rand () is only a pseudo-random number, so it should be written as follows:
# Include <stdlib. h> # include <time. h> srand (time (0); // each time a seed rand () is generated based on the time; // different random numbers are given through different seed.
Then there is the numeric exchange function in the array. Through the explanation of the CS50 video, I understand the usage of the pointer and understand that the operation of the main function in vitro does not affect the memory in the function body, therefore, the pointer should be used for operations.
Swap function:
voidswap(int *a, int *b){ int tem; tem = *a; *a = *b; *b = tem; }
The entire matrix is like this.
# Include <stdio. h> # include <stdlib. h> # include <time. h> void swap (int * a, int * B); intmain (int argc, char * argv []) {int f, I, j, n; int k = 0; int x = 1; printf ("Enter the order: \ n"); scanf ("% d", & f); int square [f * f]; for (I = 0; I <f * f; I ++) {// assign all the numbers in the Matrix to the square array. square [I] = x ++ ;} srand (time (0); // Random Seed for (j = 0; j <f * f; j ++) is generated each time) {// use a random function to randomly exchange numbers in the array. n = rand () % (f * f); swap (& square [j], & square [n]) ;}for (I = 0; I <f * f; I + = f) // print the output in the matrix form {for (j = 0; j <f; j ++) {printf ("% 2d", square [k]); k ++;} printf ("\ n ");}} voidswap (int * a, int * B) {int tem; tem = * a; * a = * B; * B = tem ;}
The running result is as follows:
However, I need to display the largest number in the matrix as "_", that is, the space. I haven't found a solution for a long time. It's too late today. It seems that I have to leave it to tomorrow.
I hope I can get more programming skills through hands-on. Good night.
_________________________________________________
Update: to display the maximum number as "_", you can make a decision in printf. if (square is the largest) prinft ("% c ",_);
Therefore, you can write a function findmax () to determine whether the current number is the maximum value in the array. The Code is as follows:
Intfindmax (int a [], int B, int c) {for (int m = 0; m <B; m ++) {if (a [c] <a [m]) return 0; // if a [c] is smaller than any number in the array, return 0} return 1; // otherwise it is the largest} // The Code related to the main function body if (findmax (square, f * f, k) prinft ("% 2c ",_);
By trying, the pointer is written
Intfindmax (int * a, int B, int c) {for (int m = 0; m <B; m ++) {if (* (a + c) <* (a + m) return 0;} return 1;} // Code related to the main function body if (findmax (& square [0], f * f, k )) // The first element address of the square array, prinft ("% 2c ",_);
The first part ends.