I learned several C++ knowledge points when I wrote the experiment report in the afternoon, and realized that C++ course knowledge is very important, and my help to ACM competition may have been underestimated. I didn’t pay enough attention to homework courses before (although I was much more active than advanced mathematics and physics). Starting from tomorrow, I will write an experiment report and learn relevant knowledge every day.
New + reviewed knowledge:
A,: :
The functionality of the scope operator
The first is used to represent global variables.
int a = 2;
int main(a)
{
int a = 3;
cout << a * a << ' ' << a * ::a << endl;
return 0;
}
Copy the code
The second kind of effect is not mentioned, for the use of two variables of the same name in an inherited class.
Allocation and release of references and dynamic space
Every time I review it, I forget it soon. I hope I remember this time. New returns a pointer.
Unsigned modifier
Emmm has nothing to say, too dishes, today I know how to use it. .
C language 2048 small game optimization
When I was in school, I wrote once. I was too young at that time. I wrote many bugs.
Code:
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
int mp[4] [4];
char temp[100];
void color(short x) // The custom function changes color according to the parameter
{
if (x >= 0 && x <= 15)
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), x);
else
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 7);
}
int main(a)
{
int score = 0; // Calculate the score
int i, j, t, x, y, if_end = 10, level;
srand(time(NULL));
printf("-- -- -- -- -- \ n"); // Print interface
printf(Please choose the game difficulty: "| | \ n");
printf("| 1. Simple pattern (2048) | \ n");
printf("(4096) | | 2. Difficult mode \ n");
printf("Number, press enter to confirm | | input \ n");
printf("-- -- -- -- -- \ n");
scanf("%d", &level);
if (level == 1) // Select difficulty
level = 2048;
else if (level == 2)
level = 4096;
printf("Please press any arrow key ↑↓ ← → Start the game \n");
for (t = 0; t < 10000; t++)
{
int if_Victory = 0;
int ch1 = 0;
int ch2 = 0;
if (ch1 = getch())
ch2 = getch(a);int sum = 0;
if (t == 0)
sum = 1;
if (ch2 == 72) / /
{
system("cls");
for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i - 1][j] == 0&& mp[i][j] ! =0)
{
mp[i - 1][j] = mp[i][j];
mp[i][j] = 0; }}}for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == mp[i - 1][j])
{
mp[i][j] = 0;
mp[i - 1][j] *= 2; }}}for (i = 1; i <= 3; i++)
{
for (j = 0; j <= 3; j++)
{
if (mp[i - 1][j] == 0&& mp[i][j] ! =0)
{
mp[i - 1][j] = mp[i][j];
mp[i][j] = 0; }}}int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) // If there is only one empty cell left, just generate a random number
geshu++;
while(geshu ! =2&& geshu ! = if_end -1&& sum ! =0)
{
x = rand() % 4;
y = rand() % 4; // Coordinates of the generated random number
if (mp[x][y] == 0)
{
int tempp = rand() % 2; // Randomly generate 2 or 4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("-- -- -- -- -- - \ n"); // Print the map
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\ n -- -- -- -- --, \ n");
}
if(! if_end)for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1> =0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1> =0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1< =3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1< =3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; // Calculate the score
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(- 6 "score = % d -- -- -- \ n \ n", score);
color(20); // Cancel the color
if (if_Victory == 1)
{
printf("Congratulations! Successful game! \n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf("Game over! \n");
gets(temp);
gets(temp); }}if (ch2 == 80) / /
{
system("cls");
for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i + 1][j] == 0&& mp[i][j] ! =0)
{
mp[i + 1][j] = mp[i][j];
mp[i][j] = 0; }}}for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == mp[i + 1][j])
{
mp[i][j] = 0;
mp[i + 1][j] *= 2; }}}for (i = 2; i >= 0; i--)
{
for (j = 0; j <= 3; j++)
{
if (mp[i + 1][j] == 0&& mp[i][j] ! =0)
{
mp[i + 1][j] = mp[i][j];
mp[i][j] = 0; }}}int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) // If there is only one empty cell left, just generate a random number
geshu++;
while(geshu ! =2&& geshu ! = if_end -1&& sum ! =0)
{
x = rand() % 4;
y = rand() % 4; // Coordinates of the generated random number
if (mp[x][y] == 0)
{
int tempp = rand() % 2; // Randomly generate 2 or 4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("-- -- -- -- -- - \ n"); // Print the map
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\ n -- -- -- -- --, \ n");
}
if(! if_end)for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1> =0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1> =0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1< =3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1< =3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; // Calculate the score
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(- 6 "score = % d -- -- -- \ n \ n", score);
color(20); // Cancel the color
if (if_Victory == 1)
{
printf("Congratulations! Successful game! \n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf("Game over! \n");
gets(temp);
gets(temp); }}if (ch2 == 75) / / left
{
system("cls");
for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j - 1] = =0&& mp[i][j] ! =0)
{
mp[i][j - 1] = mp[i][j];
mp[i][j] = 0; }}}for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j] == mp[i][j - 1])
{
mp[i][j] = 0;
mp[i][j - 1] * =2; }}}for (j = 1; j <= 3; j++)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j - 1] = =0&& mp[i][j] ! =0)
{
mp[i][j - 1] = mp[i][j];
mp[i][j] = 0; }}}int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) // If there is only one empty cell left, just generate a random number
geshu++;
while(geshu ! =2&& geshu ! = if_end -1&& sum ! =0)
{
x = rand() % 4;
y = rand() % 4; // Coordinates of the generated random number
if (mp[x][y] == 0)
{
int tempp = rand() % 2; // Randomly generate 2 or 4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("-- -- -- -- -- - \ n"); // Print the map
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\ n -- -- -- -- --, \ n");
}
if(! if_end)for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1> =0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1> =0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1< =3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1< =3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; // Calculate the score
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(- 6 "score = % d -- -- -- \ n \ n", score);
color(20); // Cancel the color
if (if_Victory == 1)
{
printf("Congratulations! Successful game! \n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf("Game over! \n");
gets(temp);
gets(temp); }}if (ch2 == 77) / / right
{
system("cls");
for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j + 1] = =0&& mp[i][j] ! =0)
{
mp[i][j + 1] = mp[i][j];
mp[i][j] = 0; }}}for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j] == mp[i][j + 1])
{
mp[i][j] = 0;
mp[i][j + 1] * =2; }}}for (j = 2; j >= 0; j--)
{
for (i = 0; i <= 3; i++)
{
if (mp[i][j + 1] = =0&& mp[i][j] ! =0)
{
mp[i][j + 1] = mp[i][j];
mp[i][j] = 0; }}}int geshu = 0;
sum = 0;
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
if (mp[i][j] == 0)
sum++;
if (sum == 1) // If there is only one empty cell left, just generate a random number
geshu++;
while(geshu ! =2&& geshu ! = if_end -1&& sum ! =0)
{
x = rand() % 4;
y = rand() % 4; // Coordinates of the generated random number
if (mp[x][y] == 0)
{
int tempp = rand() % 2; // Randomly generate 2 or 4
if (tempp % 2)
mp[x][y] = 2;
else
mp[x][y] = 4;
geshu++;
}
else
continue;
}
int if_end = 0;
printf("-- -- -- -- -- - \ n"); // Print the map
for (i = 0; i <= 3; i++)
{
printf("|");
for (j = 0; j <= 3; j++)
{
if (mp[i][j] == 0)
{
if_end++;
}
if (level == mp[i][j])
if_Victory = 1;
printf("%4d|", mp[i][j]);
}
printf("\ n -- -- -- -- --, \ n");
}
if(! if_end)for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
{
if (i - 1> =0 && mp[i][j] == mp[i - 1][j])
if_end = 1;
if (j - 1> =0 && mp[i][j] == mp[i][j - 1])
if_end = 1;
if (i + 1< =3 && mp[i][j] == mp[i + 1][j])
if_end = 1;
if (j + 1< =3 && mp[i][j] == mp[i][j + 1])
if_end = 1;
}
printf("\n\n\n");
score = 0; // Calculate the score
for (i = 0; i <= 3; i++)
for (j = 0; j <= 3; j++)
score += mp[i][j];
color(11);
printf(- 6 "score = % d -- -- -- \ n \ n", score);
color(20); // Cancel the color
if (if_Victory == 1)
{
printf("Congratulations! Successful game! \n");
gets(temp);
gets(temp);
break;
}
else if (if_end == 0)
{
printf("Game over! \n");
gets(temp);
gets(temp); }}}return 0;
}
Copy the code