您的位置:首页 > 编程语言 > C语言/C++

【C++游戏】2048的实现和简单AI

2016-10-28 16:34 961 查看
一个命令行下简单的2048游戏

我想到的实现方式是判断每一个格子里的数字是否可移动,不过需要判断的比较多,加了很多for循环导致时间复杂度很高,因此游戏运行到一定程度会出现卡顿,所以我加入了多线程,可是实际效果并不尽如人意,有的移动依旧是会卡顿下。vs2010不支持AMP并行运算,等我将编译器更新到2015后再尝试使用gpu,看可不可以去除卡顿。

AI部分很简单,能下就向下移动,否则左或右或上。

ChessBoard.h

#pragma once
#include <iostream>
#include<conio.h>
#include<windows.h>
#include <process.h>
#include <time.h>
#include <cstdlib>
using namespace std;

const int BoardLength = 4;
static int Board[BoardLength][BoardLength];
static bool IsMove=true;
static int goal=0;

int Sum(int,int);
void RandomBoard(void);
void Control(void);
void PrintBoard(void);
void GetGoal(int);
//移动部分
void MoveUp(void);
void ThreadUp(LPVOID lpParamter);
void MoveDown(void);
void ThreadDown(LPVOID lpParamter);
void MoveLeft(void);
void ThreadLeft(LPVOID lpParamter);
void MoveRight(void);
void ThreadRight(LPVOID lpParamter);
//AI部分
int AI1(void);
void AIcontrol(void);
bool TestUp();
bool TestDown();
bool TestLeft();
bool TestRight();
// 调用此函数运行游戏。
void Start(void);


ChessBoard.cpp

#include "ChessBoard.h"

int Sum(int a,int b)
{
GetGoal(a+b);
return a+b;
}

void RandomBoard()
{
srand((unsigned)time(NULL));
int a[4];
for(int i=0;i<4;i++)
{
a[i] = rand()%4;
}
while(Board[a[0]][a[1]]!=0)
{
srand((unsigned)time(NULL));
for(int i=0;i<2;i++)
{
a[i] = rand()%4;
}
}
Board[a[0]][a[1]] = 2;
}

void MoveUp()
{
IsMove = false;
for(int i=0;i<BoardLength;i++)
{
_beginthread(ThreadUp,0,(void*)i);
}
}

void ThreadUp(LPVOID lpParamter)
{
int i = (int)lpParamter;
for(int j=0;j<BoardLength;j++)
{
if(i==0)
{
break;
}
if(Board[i][j]==0)
{
continue;
}
for(int m=i-1;m>=0;m--)
{
if(Board[m][j]==0)
{
IsMove = true;
Board[m][j] = Board[m+1][j];
Board[m+1][j] = 0;
}
else if(Board[m][j] != Board[m+1][j])
{
break;
}
else if(Board[m][j] == Board[m+1][j])
{
IsMove= true;
Board[m][j] = Sum(Board[m][j],Board[m+1][j]);
Board[m+1][j] = 0;
for(int n=m-1;n>=0;n--)
{
if(Board
[j] == 0)
{
Board
[j] = Board[n+1][j];
Board[n+1][j] =0;
}
else{
break;
}
}
break;
}
}
}
}

void MoveDown()
{
IsMove = false;
for(int i=BoardLength-1;i>=0;i--)
{
_beginthread(ThreadDown,0,(void*)i);
}
}

void ThreadDown(LPVOID lpParamter)
{
int i = (int)lpParamter;
for(int j=BoardLength-1;j>=0;j--)
{
if(i==BoardLength-1)
{
continue;
}
if(Board[i][j]==0)
{
continue;
}
for(int m=i+1;m<BoardLength;m++)
{
if(Board[m][j]==0)
{
IsMove= true;
Board[m][j] = Board[m-1][j];
Board[m-1][j] = 0;
}
else if(Board[m][j] == Board[m-1][j])
{
IsMove= true;
Board[m][j] = Sum(Board[m][j],Board[m-1][j]);
Board[m-1][j] = 0;
for(int n=m+1;n<BoardLength;n++)
{
if(Board
[j] == 0)
{
Board
[j] = Board[n-1][j];
Board[n-1][j] =0;
}
else{
break;
}
}
break;
}
else if(Board[m][j] != Board[m-1][j])
{
break;
}
}
}
}

void MoveLeft()
{
IsMove = false;
for(int j=0;j<BoardLength;j++)
{
_beginthread(ThreadLeft,0,(void*)j);
}
}

void ThreadLeft(LPVOID lpParamter)
{
int j = (int)lpParamter;
for(int i=0;i<BoardLength;i++)
{
if(j==0)
{
break;
}
if(Board[i][j]==0)
{
continue;
}
for(int m=j-1;m>=0;m--)
{
if(Board[i][m]==0)
{
IsMove= true;
Board[i][m] = Board[i][m+1];
Board[i][m+1] = 0;
}
else if(Board[i][m] == Board[i][m+1])
{
IsMove= true;
Board[i][m] = Sum(Board[i][m],Board[i][m+1]);
Board[i][m+1] = 0;
for(int n=m-1;n>=0;n--)
{
if(Board[i]
== 0)
{
Board[i]
= Board[i][n+1];
Board[i][n+1] =0;
}
else{
break;
}
}
break;
}
else if(Board[i][m] != Board[i][m+1])
{
break;
}
}
}
}

void MoveRight()
{
IsMove = false;
for(int j=BoardLength-1;j>=0;j--)
{
_beginthread(ThreadRight,0,(void*)j);
}
}

void ThreadRight(LPVOID lpParamter)
{
int j = (int)lpParamter;
for(int i=BoardLength-1;i>=0;i--)
{
if(j==BoardLength-1)
{
break;
}
if(Board[i][j]==0)
{
continue;
}
for(int m=j+1;m<BoardLength;m++)
{
if(Board[i][m]==0)
{
IsMove= true;
Board[i][m] = Board[i][m-1];
Board[i][m-1] = 0;
}
else if(Board[i][m] == Board[i][m-1])
{
IsMove= true;
Board[i][m] = Sum(Board[i][m],Board[i][m-1]);
Board[i][m-1] = 0;
for(int n=m+1;n<BoardLength;n++)
{
if(Board[i]
== 0)
{
Board[i]
= Board[i][n-1];
Board[i][n-1] =0;
}
else{
break;
}
}
break;
}
else if(Board[i][m] != Board[i][m-1])
{
break;
}
}
}
}

void GetGoal(int getGoal)
{
goal += getGoal;
}

void Control()
{
char ch;
while(true)
{
ch = _getch();
if (ch == -32)
{
ch = _getch();
switch (ch)
{
case 72:
MoveUp();
break;
case 80:
MoveDown();
break;
case 75:
MoveLeft();
break;
case 77:
MoveRight();
break;
default:
break;
}
}
Sleep(250);
if(IsMove)
{
RandomBoard();
}
PrintBoard();
}
}

void AIcontrol(void)
{
char ch;
while(true)
{
ch = AI1();
switch (ch)
{
case 72:
MoveUp();
break;
case 80:
MoveDown();
break;
case 75:
MoveLeft();
break;
case 77:
MoveRight();
break;
default:
break;
}
Sleep(250);
if(IsMove)
{
RandomBoard();
}
PrintBoard();
}
}

void PrintBoard(void)
{
system("cls");
for(int i=0;i<BoardLength;i++)
{
for(int j=0;j<BoardLength;j++)
{
cout<<" "<<Board[i][j]<<" | ";
}
cout << endl;
}
cout <<"得分为: "<<goal<<endl;
}

int AI1(void){
if(TestDown())
{
return 80;
}
else if(TestLeft())
{
return 75;
}
else if(TestRight())
{
return 77;
}
else if(TestUp())
{
return 72;
}
else
{
cout<<"gameover"<<endl;
return 0;
}
}

bool TestUp()
{
for(int i=1;i<BoardLength;i++)
{
for(int j=0;j<BoardLength;j++)
{
if(Board[i][j]==0)
{
continue;
}
int temp=i-1;
if(Board[temp][j]==0)
{
return true;
}
if(Board[temp][j]==Board[i][j])
{
return true;
}
}
}
return false;
}

bool TestDown()
{
for(int i=BoardLength-2;i>=0;i--)
{
for(int j=BoardLength-1;j>=0;j--)
{
if(Board[i][j]==0)
{
continue;
}
int temp=i+1;
if(Board[temp][j]==0)
{
return true;
}
if(Board[temp][j]==Board[i][j])
{
return true;
}
}
}
return false;
}

bool TestLeft()
{
for(int j=1;j<BoardLength;j++)
{
for(int i=0;i<BoardLength;i++)
{
if(Board[i][j]==0)
{
continue;
}
int temp=j-1;
if(Board[i][temp]==0)
{
return true;
}
if(Board[i][temp]==Board[i][j])
{
return true;
}
}
}
return false;
}

bool TestRight()
{
for(int j=BoardLength-2;j>=0;j--)
{
for(int i=BoardLength-1;i>=0;i--)
{
if(Board[i][j]==0)
{
continue;
}
int temp=j+1;
if(Board[i][temp]==0)
{
return true;
}
if(Board[i][temp]==Board[i][j])
{
return true;
}
}
}
return false;
}

void Start()
{
char ch ;
while(1)
{
cout<<"2048游戏启动界面"<<endl;
cout<<"1.手动运行"<<endl;
cout<<"2.AI运行"<<endl;
ch = _getch();
if(ch=='1')
{
system("cls");
break;
}
else if(ch=='2')
{
system("cls");
break;
}
}

RandomBoard();
PrintBoard();
if(ch=='1')
{
Control();
}
else if(ch=='2')
{
AIcontrol();
}
}


游戏截图



在main函数中调用Start();函数就可以运行游戏了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c语言 游戏