C++EasyX 그래 픽 라 이브 러 리 기반 2048 미니 게임 구현
MainGame2048.cpp
/** Name: Game2048CoreClass*/
#include<iostream>
#include<graphics.h>
#include<stdio.h>
#include<windows.h>
#include<conio.h>
#include<stdio.h>
#include"Game2048.h"
#define BLOCK_SIZE 60
#define SIZE_COL 10
#define SIZE_ROW 10
using namespace std;
void DisplayMap(Game2048& mygame);
int GetMove();
int main(int argc, char * argv[])
{
HWND hwnd=initgraph(SIZE_COL*BLOCK_SIZE, SIZE_ROW*BLOCK_SIZE);
setbkmode(TRANSPARENT);
setbkcolor(RGB(180,180,180));
settextcolor(RGB(0,180,80));
cleardevice();
while (1)
{
Game2048 mygame(SIZE_ROW, SIZE_COL);
while (1)
{
DisplayMap(mygame);
int mov = GetMove();
cout << mov << endl;
if (!mygame.Run(mov))
break;
}
if (MessageBox(hwnd, " , ?", "Tips", MB_YESNO) == IDNO)
break;
}
return 0;
}
int GetMove()
{
char move = '\0';
move = getch();
if (move == 'w' || move == '8' || move == 'W')
return MOV_UP;
if (move == 's' || move == '5' || move == 'S')
return MOV_DOWN;
if (move == 'a' || move == '4' || move == 'A')
return MOV_LEFT;
if (move == 'd' || move == '6' || move == 'D')
return MOV_RIGHT;
if (move == '*')
return MOV_NULL;
return 0;
}
void DisplayMap(Game2048& mygame)
{
BeginBatchDraw();
cleardevice();
char temp[20] = { 0 };
system("cls");
cout << mygame.GetScore() << " " << mygame.GetStep() << " " << mygame.GetUsedTime()<<" " << mygame.GetMaxNum() << endl;
for (int i = 0; i<mygame.GetLines(); i++)
{
for (int j = 0; j<mygame.GetCols(); j++)
{
cout.width(4);
cout << mygame.MapAt(i,j);
if (mygame.MapAt(i, j) == 0)
{
setfillcolor(RGB(220,220,220));
solidcircle(j*BLOCK_SIZE + BLOCK_SIZE / 2, i*BLOCK_SIZE + BLOCK_SIZE / 2, BLOCK_SIZE / 2);
}
else
{
int size=mygame.MapAt(i, j);
sprintf(temp, "%d\0",size );
size /= 2;
setfillcolor(RGB(200, 255-size*20, 0+size*20));
solidcircle(j*BLOCK_SIZE + BLOCK_SIZE / 2, i*BLOCK_SIZE + BLOCK_SIZE / 2, BLOCK_SIZE / 2);
outtextxy(j*BLOCK_SIZE, i*BLOCK_SIZE,temp);
}
}
printf("
");
}
cout << "-------" << endl;
sprintf(temp,"Score : %d\0",mygame.GetScore());
outtextxy(0, 1*BLOCK_SIZE/3, temp);
sprintf(temp, "Step : %d\0", mygame.GetStep());
outtextxy(0, 2 * BLOCK_SIZE / 3, temp);
sprintf(temp, "Time : %d\0", mygame.GetUsedTime());
outtextxy(0, 3 * BLOCK_SIZE / 3, temp);
sprintf(temp, "MAX : %d\0", mygame.GetMaxNum());
outtextxy(0, 4 * BLOCK_SIZE / 3, temp);
EndBatchDraw();
}
Game2048.h
#ifndef _GAME2048_H_
#define _GAME2048_H_
/*For example:
Game2048 mygame(10,10);
int main(int argc, char * argv[])
{
while (1)
{
DisplayMap();
int mov = GetMove();
cout << mov << endl;
if (!mygame.Run(mov))
break;
}
system("pause");
return 0;
}
*/
#include<stdlib.h>
#include<time.h>
typedef int MoveDirect;
#define MOV_NULL 0
#define MOV_UP 8
#define MOV_DOWN 5
#define MOV_LEFT 4
#define MOV_RIGHT 6
class Game2048
{
public:
Game2048(int line=5,int col=5);
~Game2048();
bool Run(MoveDirect mov);
int GetCols();
int GetLines();
int MapAt(int rindex, int cindex);//return >=0 is true,-1 is bad index,0 mean space,other mean number.
int GetStep();
int GetScore();
int GetUsedTime();
int GetMaxNum();
void clear();
private:
void CreateNew();
void MoveAndResult(MoveDirect mov);
bool IsDead();
//
int * Map;
int lines;
int cols;
int step;
int core;
long runtime;
int usetime;
int maxnum;
};
#endif // _GAME2048_H_
Game2048.cpp
#include"Game2048.h"
Game2048::Game2048(int line,int col)
{
this->lines = line;
this->cols = col;
this->step=0;
this->core=0;
this->runtime=0;
this->usetime=0;
this->maxnum=2;
this->Map=(int *)malloc(sizeof(int)*(this->lines)*(this->cols));
runtime = time(NULL); // ,
srand((unsigned)time(NULL));
for (int i = 0; i<this->lines; i++)
{
for (int j = 0; j<this->cols; j++)
{
Map[i*this->cols+j] = 0;
}
}
CreateNew();
}
Game2048::~Game2048()
{
free(this->Map);
}
void Game2048::clear()
{
this->step = 0;
this->core = 0;
this->runtime = 0;
this->usetime = 0;
this->maxnum = 2;
runtime = time(NULL); // ,
srand((unsigned)time(NULL));
for (int i = 0; i<this->lines; i++)
{
for (int j = 0; j<this->cols; j++)
{
Map[i*this->cols + j] = 0;
}
}
CreateNew();
}
bool Game2048::Run(MoveDirect mov)
{
CreateNew();
MoveAndResult(mov);
if (step > (lines*cols * 2))
if (IsDead() == 1)
return false;
usetime = time(NULL) - runtime;
return true;
}
int Game2048::GetCols()
{
return this->cols;
}
int Game2048::GetLines()
{
return this->lines;
}
int Game2048::MapAt(int rindex, int cindex)
{
if (rindex<0||cindex<0||rindex >= this->lines || cindex >= this->cols)
return -1;
return Map[rindex*this->cols+cindex];
}
int Game2048::GetStep()
{
return this->step;
}
int Game2048::GetScore()
{
return this->core;
}
int Game2048::GetUsedTime()
{
return this->usetime;
}
int Game2048::GetMaxNum()
{
return this->maxnum;
}
void Game2048::CreateNew()
{
int hasfull = 1;
for (int i = 0; i<lines; i++)
{
for (int j = 0; j<cols; j++)
{
if (Map[i*this->cols+j] == 0)
hasfull = 0; // ,
}
}
if (hasfull == 1)
return;
int si, sj;
si = rand() % lines;
sj = rand() % cols;
while (Map[si*this->cols+sj] != 0)
{
si = rand() % lines;
sj = rand() % cols;
}
Map[si*this->cols+sj] = 2;
}
bool Game2048::IsDead()
{
for (int i = 0; i<lines; i++)
{
for (int j = 0; j<cols; j++)
{
if (Map[i*this->lines + j] == 0)
return false; //
int up, down, right, left;
up = i - 1;
down = i + 1;
right = j + 1;
left = j - 1; //
while (up >= 0 && Map[up*this->lines + j] == 0)
up--;
if (Map[up*this->lines + j] == Map[i*this->lines + j] && up != -1) //
return false;
while (down<lines&&Map[down*this->lines + j] == 0)
down--;
if (Map[down*this->lines + j] == Map[i*this->lines + j] && down != lines)
return false;
while (right<cols&&Map[i*this->lines + right] == 0)
right++;
if (Map[i*this->lines + right] == Map[i*this->lines + j] && right != cols)
return false;
while (left >= 0 && Map[i*this->lines + left] == 0)
left--;
if (Map[i*this->lines + left] == Map[i*this->lines + j] && left != -1)
return false;
}
}
return true; // ,
}
void Game2048::MoveAndResult(MoveDirect mov)
{
if (mov == MOV_NULL)
return;
step++; //
int ffind, nfind;
if (mov == MOV_UP)
{
for (int i = 0; i<cols; i++)
{
ffind = -1;
nfind = -1;
for (int j = 0; j<lines; j++)
{
int k = j;
while (k<lines&&Map[k*this->cols+i] == 0)
k++;
if (k != lines)
ffind = k;
k++;
while (k<lines&&Map[k*this->lines + i] == 0)
k++;
if (k != lines)
nfind = k; //
if (ffind != -1 && nfind != -1)
{
if (ffind != nfind)
{
if (Map[ffind*this->lines + i] == Map[nfind*this->lines + i]) //
{
Map[ffind*this->lines + i] *= 2;
if (Map[ffind*this->lines + i]>maxnum)
maxnum = Map[ffind*this->lines + i];
Map[nfind*this->lines + i] = 0;
core++; //
}
}
}
}
int count = 0;
for (int j = 0; j<lines; j++) //
{
if (Map[j*this->lines + i] != 0)
{
int temp = Map[j*this->lines + i];
Map[j*this->lines + i] = 0;
Map[count*this->lines + i] = temp;
count++;
}
}
}
}
else if (mov == MOV_DOWN)
{
for (int i = 0; i<cols; i++)
{
ffind = -1;
nfind = -1;
for (int j = lines; j >= 0; j--)
{
int k = j;
while (k >= 0 && Map[k*this->cols+i] == 0)
k--;
if (k != -1)
ffind = k;
k--;
while (k >= 0 && Map[k*this->cols+i] == 0)
k--;
if (k != -1)
nfind = k;
if (ffind != -1 && nfind != -1)
{
if (ffind != nfind)
{
if (Map[ffind*this->cols+i] == Map[nfind*this->cols+i])
{
Map[ffind*this->cols+i] *= 2;
if (Map[ffind*this->cols+i]>maxnum)
maxnum = Map[ffind*this->cols+i];
Map[nfind*this->cols+i] = 0;
core++;
}
}
}
}
int count = lines - 1;
for (int j = lines - 1; j >= 0; j--)
{
if (Map[j*this->cols+i] != 0)
{
int temp = Map[j*this->cols+i];
Map[j*this->cols+i] = 0;
Map[count*this->cols+i] = temp;
count--;
}
}
}
}
else if (mov == MOV_LEFT)
{
for (int i = 0; i<lines; i++)
{
ffind = -1;
nfind = -1;
for (int j = 0; j<cols; j++)
{
int k = j;
while (k<cols&&Map[i*this->cols+k] == 0)
k++;
if (k != cols)
ffind = k;
k++;
while (k<cols&&Map[i*this->cols+k] == 0)
k++;
if (k != cols)
nfind = k;
if (ffind != -1 && nfind != -1)
{
if (ffind != nfind)
{
if (Map[i*this->cols+ffind] == Map[i*this->cols+nfind])
{
Map[i*this->cols+ffind] *= 2;
if (Map[i*this->cols+ffind]>maxnum)
maxnum = Map[i*this->cols+ffind];
Map[i*this->cols+nfind] = 0;
core++;
}
}
}
}
int count = 0;
for (int j = 0; j<cols; j++)
{
if (Map[i*this->cols+j] != 0)
{
int temp = Map[i*this->cols+j];
Map[i*this->cols+j] = 0;
Map[i*this->cols+count] = temp;
count++;
}
}
}
}
else if (mov == MOV_RIGHT)
{
for (int i = 0; i<lines; i++)
{
ffind = -1;
nfind = -1;
for (int j = cols; j >= 0; j--)
{
int k = j;
while (k >= 0 && Map[i*this->cols+k] == 0)
k--;
if (k != -1)
ffind = k;
k--;
while (k >= 0 && Map[i*this->cols+k] == 0)
k--;
if (k != -1)
nfind = k;
if (ffind != -1 && nfind != -1)
{
if (ffind != nfind)
{
if (Map[i*this->cols+ffind] == Map[i*this->cols+nfind])
{
Map[i*this->cols+ffind] *= 2;
if (Map[i*this->cols+ffind]>maxnum)
maxnum = Map[i*this->cols+ffind];
Map[i*this->cols+nfind] = 0;
core++;
}
}
}
}
int count = cols - 1;
for (int j = cols - 1; j >= 0; j--)
{
if (Map[i*this->cols+j] != 0)
{
int temp = Map[i*this->cols+j];
Map[i*this->cols+j] = 0;
Map[i*this->cols+count] = temp;
count--;
}
}
}
}
}
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Visual Studio에서 파일 폴더 구분 (포함 경로 설정)Visual Studio에서 c, cpp, h, hpp 파일을 폴더로 나누고 싶었습니까? 어쩌면 대부분의 사람들이 있다고 생각합니다. 처음에 파일이 만들어지는 장소는 프로젝트 파일 등과 같은 장소에 있기 때문에 파일...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.