目标:1.实现地图打印、自动调整大小
?????????? 2.实现随机生成
?????????? 3.实现数字移动、合成
?????????? 4.实现重来功能
解决方案:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
#include<cstdlib>
using namespace std;
头文件,没什么好说的
int numbers[10][10]={0},key;
bool fail=false,stop=false;
numbers:数字数组
key:存储按下按键
fail,stop:判断游戏结束
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Debug(){
for(int i = 1;i <= 4;i ++){
for(int j = 1;j <= 4;j ++)
cout<<numbers[i][j]<<" ";
cout<<endl;
}
}
HideCursor:隐藏光标,增强游戏体验
Debug:调试:输出数组所有数据
void init(){
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 4;j ++)
numbers[i][j] = 0;
}
int random(){
int tmp=rand()%5;
while(tmp == 0)
tmp = rand()%5;
return tmp;
}
void Generate(){
int x=random(),y=random();
while(numbers[x][y] != 0){
x=random();
y=random();
}
numbers[x][y] = 2;
}
init:数组初始化
random:随机返回一个1~5间的整数
Generate:随机在数组一零处生成一“2”
void CelingPrint(){
for(int i = 0;i < 29;i ++)
printf("%c",celing[i]);
}
void WallPrint(){
for(int i = 0;i < 29;i ++)
printf("%c",wall[i]);
}
int GetNumberLength(int n){
int cnt=0;
while(n>0){
n/=10;
cnt++;
}
return cnt;
}
void NumberPrint(int x,int y){
printf(" %d",numbers[x][y]);
for(int i = 0;i < 5-GetNumberLength(numbers[x][y]);i ++)
printf(" ");
}
void NumberFloorPrint(int x){
printf("|");
for(int i = 1;i <= 4;i ++){
if(numbers[x][i] == 0)
printf(" |");
else{
NumberPrint(x,i);
printf("|");
}
}
}
void OneFloor(int floor){
WallPrint();
cout<<endl;
NumberFloorPrint(floor);
cout<<endl;
WallPrint();
cout<<endl;
CelingPrint();
}
void MapPrint(){
printf(" 2048控制台版 作者:clzmgl\n");
CelingPrint();
cout<<endl;
for(int i = 1;i <= 4;i ++){
OneFloor(i);
cout<<endl;
}
}
Print类
CeilingPrint:打印“横”
WallPrint:打印“竖”
GetNumberLength:获取一数长度,以便调整打印地图时的空格数
NumberPrint:根据一数长度打印该数在的格子
NumberFloorPrint:打印含有数字的一个楼层
OneFloor:三层(空,数字,空)为一组,打印该组
MapPrint:打印整张地图
void Update(string s){
if(s == "up"){
for(int i = 1;i <= 3;i ++)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j]==0 && numbers[i+1][j]!=0)
swap(numbers[i][j],numbers[i+1][j]);
}
if(s == "down"){
for(int i = 4;i >= 2;i --)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j]==0 && numbers[i-1][j]!=0)
swap(numbers[i][j],numbers[i-1][j]);
}
if(s == "left"){
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 3;j ++)
if(numbers[i][j]==0 && numbers[i][j+1]!=0)
swap(numbers[i][j],numbers[i][j+1]);
}
if(s == "right"){
for(int i = 1;i <= 4;i ++)
for(int j = 4;j >= 2;j --)
if(numbers[i][j]==0 && numbers[i][j-1]!=0)
swap(numbers[i][j],numbers[i][j-1]);
}
}
void PlayerControl(){
switch(getch()){
case 224:
switch(getch()){
case 72:
key=72;
system("cls");
break;
case 80:
key=80;
system("cls");
break;
case 75:
key=75;
system("cls");
break;
case 77:
key=77;
system("cls");
break;
}
}
}
void Composite(){
if(key == 72){
Update("up");
for(int i = 1;i <= 3;i ++)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j] == numbers[i+1][j]){
numbers[i][j] *= 2;
numbers[i+1][j] = 0;
}
}
if(key == 80){
Update("down");
for(int i = 4;i >= 2;i --)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j] == numbers[i-1][j]){
numbers[i][j] *= 2;
numbers[i-1][j] = 0;
}
}
if(key == 75){
Update("left");
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 3;j ++)
if(numbers[i][j] == numbers[i][j+1]){
numbers[i][j] *= 2;
numbers[i][j+1] = 0;
}
}
if(key == 77){
Update("right");
for(int i = 1;i <= 4;i ++)
for(int j = 4;j >= 2;j --)
if(numbers[i][j] == numbers[i][j-1]){
numbers[i][j] *= 2;
numbers[i][j-1] = 0;
}
}
}
Player类
Update:将数组向一侧(上,下,左,右)更新
PlayerControl:根据玩家按下按键设置key状态
Composite:根据key状态将数组进行更新、合成
void FailJudge(){
fail = true;
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 4;j ++){
if(numbers[i][j] == 0)
fail = false;
if(numbers[i][j] == 2048)
success = true;
}
}
void Lose(){
char s;
if(fail){
printf("\n你输了,再来一次吗?(y:是 n:否)\n");
scanf("%c",&s);
if(s == 'n')
stop=true;
init();
system("cls");
Generate();
MapPrint();
}
}
void Win(){
char s;
if(success){
printf("\n你赢了,再来一次吗?(y:是 n:否)\n");
scanf("%c",&s);
if(s == 'n')
stop=true;
init();
system("cls");
Generate();
MapPrint();
}
}
Judge类
FailJudge:判断输 / 赢
Lose:游戏失败
Win:游戏成功
int main(){
srand(time(NULL));
HideCursor();
Generate();
MapPrint();
while(1){
FailJudge();
Lose();
Win();
if(stop)
break;
PlayerControl();
for(int i = 0;i < 4;i ++)
Composite();
Generate();
MapPrint();
}
return 0;
}
激动人心的主函数
srand:随机数种子
其它没什么好说的
完整代码:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
#include<time.h>
#include<string>
#include<cstdlib>
using namespace std;
char celing[50]="-----------------------------";
char wall[50]="| | | | |";
int numbers[10][10]={0},key;
bool fail=false,stop=false,success=false;
void HideCursor(){
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Debug(){
for(int i = 1;i <= 4;i ++){
for(int j = 1;j <= 4;j ++)
cout<<numbers[i][j]<<" ";
cout<<endl;
}
}
void init(){
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 4;j ++)
numbers[i][j] = 0;
}
int random(){
int tmp=rand()%5;
while(tmp == 0)
tmp = rand()%5;
return tmp;
}
void Generate(){
int x=random(),y=random();
while(numbers[x][y] != 0){
x=random();
y=random();
}
numbers[x][y] = 2;
}
void CelingPrint(){
for(int i = 0;i < 29;i ++)
printf("%c",celing[i]);
}
void WallPrint(){
for(int i = 0;i < 29;i ++)
printf("%c",wall[i]);
}
int GetNumberLength(int n){
int cnt=0;
while(n>0){
n/=10;
cnt++;
}
return cnt;
}
void NumberPrint(int x,int y){
printf(" %d",numbers[x][y]);
for(int i = 0;i < 5-GetNumberLength(numbers[x][y]);i ++)
printf(" ");
}
void NumberFloorPrint(int x){
printf("|");
for(int i = 1;i <= 4;i ++){
if(numbers[x][i] == 0)
printf(" |");
else{
NumberPrint(x,i);
printf("|");
}
}
}
void OneFloor(int floor){
WallPrint();
cout<<endl;
NumberFloorPrint(floor);
cout<<endl;
WallPrint();
cout<<endl;
CelingPrint();
}
void MapPrint(){
printf(" 2048控制台版 作者:clzmgl\n");
CelingPrint();
cout<<endl;
for(int i = 1;i <= 4;i ++){
OneFloor(i);
cout<<endl;
}
}
void Update(string s){
if(s == "up"){
for(int i = 1;i <= 3;i ++)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j]==0 && numbers[i+1][j]!=0)
swap(numbers[i][j],numbers[i+1][j]);
}
if(s == "down"){
for(int i = 4;i >= 2;i --)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j]==0 && numbers[i-1][j]!=0)
swap(numbers[i][j],numbers[i-1][j]);
}
if(s == "left"){
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 3;j ++)
if(numbers[i][j]==0 && numbers[i][j+1]!=0)
swap(numbers[i][j],numbers[i][j+1]);
}
if(s == "right"){
for(int i = 1;i <= 4;i ++)
for(int j = 4;j >= 2;j --)
if(numbers[i][j]==0 && numbers[i][j-1]!=0)
swap(numbers[i][j],numbers[i][j-1]);
}
}
void PlayerControl(){
switch(getch()){
case 224:
switch(getch()){
case 72:
key=72;
system("cls");
break;
case 80:
key=80;
system("cls");
break;
case 75:
key=75;
system("cls");
break;
case 77:
key=77;
system("cls");
break;
}
}
}
void Composite(){
if(key == 72){
Update("up");
for(int i = 1;i <= 3;i ++)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j] == numbers[i+1][j]){
numbers[i][j] *= 2;
numbers[i+1][j] = 0;
}
}
if(key == 80){
Update("down");
for(int i = 4;i >= 2;i --)
for(int j = 1;j <= 4;j ++)
if(numbers[i][j] == numbers[i-1][j]){
numbers[i][j] *= 2;
numbers[i-1][j] = 0;
}
}
if(key == 75){
Update("left");
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 3;j ++)
if(numbers[i][j] == numbers[i][j+1]){
numbers[i][j] *= 2;
numbers[i][j+1] = 0;
}
}
if(key == 77){
Update("right");
for(int i = 1;i <= 4;i ++)
for(int j = 4;j >= 2;j --)
if(numbers[i][j] == numbers[i][j-1]){
numbers[i][j] *= 2;
numbers[i][j-1] = 0;
}
}
}
void FailJudge(){
fail = true;
for(int i = 1;i <= 4;i ++)
for(int j = 1;j <= 4;j ++){
if(numbers[i][j] == 0)
fail = false;
if(numbers[i][j] == 2048)
success = true;
}
}
void Lose(){
char s;
if(fail){
printf("\n你输了,再来一次吗?(y:是 n:否)\n");
scanf("%c",&s);
if(s == 'n')
stop=true;
init();
system("cls");
Generate();
MapPrint();
}
}
void Win(){
char s;
if(success){
printf("\n你赢了,再来一次吗?(y:是 n:否)\n");
scanf("%c",&s);
if(s == 'n')
stop=true;
init();
system("cls");
Generate();
MapPrint();
}
}
int main(){
srand(time(NULL));
HideCursor();
Generate();
MapPrint();
while(1){
FailJudge();
Lose();
Win();
if(stop)
break;
PlayerControl();
for(int i = 0;i < 4;i ++)
Composite();
Generate();
MapPrint();
}
return 0;
}
|