我在学校里要学习更多,但我想挑战自己一点。我们最近刚接触到结构,只是对其玩得很开心。
我试图制造一种riichi Majong发电机,以产生一排用于墙面的瓷砖。现在只有这一代,但目前我无法用所需的全部136个图块填充阵列。
我为tile本身和tileSet创建了标题。瓦片标题处理单个瓦片,该瓦片由类型和等级分开。 Tile-set构建了他们所谓的整个“甲板”或“墙壁”。整个过程将一直进行到荣誉瓦片开始进入为止,一旦达到索引115,它就会因“错误的分配”而崩溃
我知道使用命名空间std;是一件坏事,我现在更喜欢这样,因为我现在是自己做。我有点厌倦了必须编写std ::几乎所有内容。现在,我不使用任何其他库。
我也基于“纸牌”结构的结构。
主文件
#include <iostream>
#include <string>
#include "tileSet.h"
using namespace std;
int main()
{
tileSet tileWall;
tile currentTile;
tileWall.printSet();
tileWall.shuffle();
cout << endl << endl;
tileWall.printSet();
tileWall.shuffle();
int count = 0;
for (int i = 0; i < (136 - 28); i++)
{
currentTile = tileWall.dealTile();
cout << currentTile.print() << endl;
count++;
}
cout << endl;
cout << count << endl;
system("pause");
return 0;
}
tile.h
#ifndef H_tile
#define H_tile
#include <string>
#include <iostream>
using namespace std;
class tile
{
public:
tile(string tileType, string tileRank);
string print() const;
tile();
private:
string type;
string rank;
};
tile::tile()
{
}
tile::tile(string tileType, string tileRank)
{
type = tileType;
rank = tileRank;
}
string tile::print() const
{
return (rank + " of " + type);
}
#endif
tileSet.h
#ifndef H_tileSet
#define H_tileSet
#include "tile.h"
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <stdlib.h>
using namespace std;
int const numTiles = 136;
class tileSet
{
public:
tileSet();
void shuffle();
tile dealTile();
void printSet() const;
private:
tile *tileWall;
int currentTile;
int index;
};
void tileSet::printSet() const
{
cout << left;
for (int i = 0; i < numTiles; i++)
{
cout << setw(19) << tileWall[i].print();
}
}
tileSet::tileSet()
{
string type[] = { "Pin", "Sou", "Wan", "Honor" };
string rank[] = { "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "East", "South", "West", "North", "Haku", "Hatsu", "Chun" };
tileWall = new tile[numTiles];
currentTile = 0;
index = 0;
//Populate with Pin tiles
for (int i = 0; i < 36; i++)
{
tileWall[index++] = tile(type[0], rank[i % 9]);
}
//Populate with Sou tiles
for (int i = 0; i < 36; i++)
{
tileWall[index++] = tile(type[1], rank[i % 9]);
}
//Populate with Wan tiles
for (int i = 0; i < 36; i++)
{
tileWall[index++] = tile(type[2], rank[i % 9]);
}
//Populate with Honor tiles
for (int i = 0; i < 28; i++)
{
tileWall[index++] = tile(type[3], rank[i % 16 + 9]);
}
}
void tileSet::shuffle()
{
currentTile = 0;
for (int first = 0; first < numTiles; first++)
{
int second = (rand() + time(0)) % numTiles;
tile temp = tileWall[first];
tileWall[first] = tileWall[second];
tileWall[second] = temp;
}
}
tile tileSet::dealTile()
{
if (currentTile > numTiles)
shuffle();
if (currentTile < numTiles)
return (tileWall[currentTile++]);
return (tileWall[0]);
}
#endif
答案 0 :(得分:3)
rank[i % 16 + 9]
是未定义的行为,例如为i=7
,因为7%16+9 = 16
,但是rank
的大小仅为16
。