用C++可以做什么小游戏 一定不要太复杂的 我现在还没学完C++的 求大神指导 求源代码。谢谢。 5

我有更好的答案
邀请更新
2013-03-25 最佳答案
#include <iostream>

#include <cstdlib>
#include <ctime>
using namespace std;
#define LENGTH 3

int randf(int num) //生成随机数,小于num
{
srand((unsigned)time(NULL));
return rand() % num;
}
class NumberM
{
public:
NumberM();
~NumberM();
void start();
bool up();
bool down();
bool left();
bool right();
void show();
bool win();
private:
int number[LENGTH][LENGTH];
int zeroX, zeroY;
};
NumberM::NumberM()
{
int n = 1;
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
number[i][j] = n++;
}
}
number[LENGTH - 1][LENGTH - 1] = 0;
}
NumberM::~NumberM()
{
cout << "**************胜利**************" << endl;
char any;
cin >> any ;
}
bool NumberM::up()
{
if(zeroY == LENGTH - 1) return false;
number[zeroY][zeroX] = number[zeroY + 1][zeroX];
number[zeroY + 1][zeroX] = 0;
zeroY += 1;
//cout<<"x=" <<zeroX+1 <<"y=" <<zeroY+1 <<endl;
return true;
}
bool NumberM::down()
{
if(zeroY == 0) return false;
number[zeroY][zeroX] = number[zeroY - 1][zeroX];
number[zeroY - 1][zeroX] = 0;
zeroY -= 1;
//cout<<"x=" <<zeroX+1 <<"y=" <<zeroY+1 <<endl;
return true;
}
bool NumberM::left()
{
if(zeroX == LENGTH - 1) return false;
number[zeroY][zeroX] = number[zeroY][zeroX + 1];
number[zeroY][zeroX + 1] = 0;
zeroX += 1;
//cout<<"x=" <<zeroX+1 <<"y=" <<zeroY+1 <<endl;
return true;
}
bool NumberM::right()
{
if(zeroX == 0) return false;
number[zeroY][zeroX] = number[zeroY][zeroX - 1];
number[zeroY][zeroX - 1] = 0;
zeroX -= 1;
//cout<<"x=" <<zeroX+1 <<"y=" <<zeroY+1 <<endl;
return true;
}
void NumberM::start()
{
zeroX = LENGTH - 1;
zeroY = LENGTH - 1;
int move = 0;
do
{
for (int i = 1; i <= 50; i++)
{
int b = randf(LENGTH * i - i);
int q = 0, w = 1, e = 2, r = 3;
//cout<<"b=" <<b <<endl;
if(b % 5 == q)
{
up();
move++;
}
else if(b % 5 == w)
{
down();
move++;
}
else if(b % 5 == e)
{
left();
move++;
}
else if(b % 5 == r)
{
right();
move++;
}
else
{
q++;
w++;
e++;
r++;
}
}
}
while(win() || move <= 100);
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
if(number[i][j] == 0)
{
zeroX = j;
zeroY = i;
}
}
}
}
void NumberM::show()
{
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
if(number[i][j] == 0) cout << " " ;
else
{
if(number[i][j] < 10) cout << " ";
cout << " " << number[i][j] ;
}
}
cout << endl;
}
}
bool NumberM::win()
{
using std:cou

int n = 1;
for (int i = 0; i < LENGTH; i++)
{
for (int j = 0; j < LENGTH; j++)
{
if(i == LENGTH - 1 && j == LENGTH - 1) continue;
if(number[i][j] != n++) return false;
}
}
return true;
}

int main(int argc, char* argv[])
{
NumberM num;
num.start();
char moveGet;
bool isGet;
while(!num.win())
{
num.show();
do
{
cout << "左=h , 右=l , 上=k , 下=j " << endl;
cin >> moveGet ;
if(moveGet == 'k') num.up() ? isGet = true : isGet = false;
else if(moveGet == 'j') num.down() ? isGet = true : isGet = false;
else if(moveGet == 'h') num.left() ? isGet = true : isGet = false;
else if(moveGet == 'l') num.right() ? isGet = true : isGet = false;
}
while(!isGet);
}
num.show();
return 0;
}

很久以前写的,代码比较烂。。

其他回答

可以做文字游戏
代码满天飞
我只有自己写的,没注释的
自己写更好吧
南柯一梦之小 | 发布于2013-03-25
评论
用JAVE比较好吧。
丙丁朱雀火 | 发布于2013-03-25
评论

为您推荐:

广告

您可能关注的内容

×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消