Pygame编写扫雷游戏,我使用了C++来生成地图,并储存在了Message.txt文件里,可左右两侧并没有被生成,除了表示雷的x外,全是0,求各位帮助一下
Python:
import sys
import pygame
from pygame.locals import *
import Game#我自己编写的一个文件,不用管
mapsize = ((9,9),(16,16),(30,16))
#Screen size is map_size times 40.
bombsNum = (10,40,99)
name = Game.Login()
difficulty = Game.ChooseDifficulty(sys)#"Easy" is 0, "Normal" is 1, "Hard" is 2
fout = open("Message.txt","w")
fout.write(str(difficulty) + "\n")
fout.write(str(mapsize[difficulty][1]) + " " + str(mapsize[difficulty][0]) + "\n")
fout.write(str(bombsNum[difficulty]))
fout.close()
C++:
#include <cstdio>
#include <fstream>
#include <cstdlib>
#include <cstring>
#include <ctime>
using namespace std;
int difficulty;
char map[25][31];
int height, width;
int bombsPos[200], bombsNum;
int main()
{
memset(map, '0', sizeof(map));
ifstream fin("Message.txt");
fin >> difficulty >> height >> width >> bombsNum;
fin.close();
srand((unsigned int)time(NULL));
for (int i = 0; i < bombsNum; i++)
map[(rand() % height) + 1][(1 + rand() % width) + 1] = 'x';
for (int i = 2; i <= height - 1; i++)
{
for (int j = 2; j <= width - 1; j++)
{
if (map[i][j] != 'x')
{
if (map[i + 1][j] == 'x')
map[i][j]++;
if (map[i + 1][j + 1] == 'x')
map[i][j]++;
if (map[i][j + 1] == 'x')
map[i][j]++;
if (map[i - 1][j + 1] == 'x')
map[i][j]++;
if (map[i - 1][j] == 'x')
map[i][j]++;
if (map[i - 1][j - 1] == 'x')
map[i][j]++;
if (map[i][j - 1] == 'x')
map[i][j]++;
if (map[i + 1][j - 1] == 'x')
map[i][j]++;
}
}
}
for (int i = 2; i <= width - 1; i++)
{
if (map[0][i] != 'x')
{
if (map[0][i + 1] == 'x')
map[0][i]++;
if (map[1][i + 1] == 'x')
map[0][i]++;
if (map[1][i] == 'x')
map[0][i]++;
if (map[1][i - 1] == 'x')
map[0][i]++;
if (map[0][i - 1] == 'x')
map[0][i]++;
}
}
for (int i = 2; i <= height - 1; i++)
{
if (map[i][0] != 'x')
{
if (map[i - 1][0] == 'x')
map[i][0]++;
if (map[i - 1][1] == 'x')
map[i][0]++;
if (map[i][1] == 'x')
map[i][0]++;
if (map[i + 1][1] == 'x')
map[i][0]++;
if (map[i + 1][0] == 'x')
map[i][0]++;
}
if (map[i][width] == 'x')
{
if (map[i - 1][width] == 'x')
map[i][width]++;
if (map[i - 1][width - 1] == 'x')
map[i][width]++;
if (map[i][width - 1] == 'x')
map[i][width]++;
if (map[i + 1][width - 1] == 'x')
map[i][width]++;
if (map[i + 1][width] == 'x')
map[i][width]++;
}
}
for (int i = 1; i < width; i++)
{
if (map[height][i] != 'x')
{
if (map[height][i - 1] == 'x')
map[height ][i]++;
if (map[height - 1][i - 1] == 'x')
map[height][i]++;
if (map[height - 1][i] == 'x')
map[height][i]++;
if (map[height - 1][i + 1] == 'x')
map[height][i]++;
if (map[height][i + 1] == 'x')
map[height][i]++;
}
}
ofstream fout("Message.txt");
for (int i = 1; i <= height; i++)
{
for (int j = 1; j <= width; j++)
fout << map[i][j] << " ";
fout << endl;
}
fout << flush;
fout.close();
return 0;
}