用C++可以做什么小游戏 一定不要太复杂的 我现在还没学完C++的 求大神指导 求源代码。谢谢。
3个回答
展开全部
#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;
}
很久以前写的,代码比较烂。。
#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;
}
很久以前写的,代码比较烂。。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询