I am in school to learn more of it but I wanted to challenge myself a little. We recently got into structs, just barely and having fun with it.
I tried to make a riichi Majong generator, to generate an array of tiles to use in a wall. Only the generation right now, but currently I can't populate the array with all 136 tiles needed.
I created headers for the tile itself and tileSet. The tile header deals with the individual tile, which is separated by the type and rank it is. Tile-set builds the entire "deck" or "wall" as they call it. The whole thing will work up until the honor tiles start getting in, once it reaches index 115 it crashes with "bad alloc"
I'm aware using namespace std; is a bad thing, I prefer it this way as I'm doing this on my own for now. I kinda got tired of having to write std:: for nearly everything. For now I'm not using any other libraries.
I also was basing the structure of of a "deck of cards" structure too.
main file
#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
rank[i % 16 + 9]
is undefined behavior e.g. for i=7
, because 7%16+9 = 16
, but the size of rank
is only 16
.