1/***************************************************************************
2* KBlocks, a falling blocks game for KDE *
3* Copyright (C) 2010 Zhongjie Cai <squall.leonhart.cai@gmail.com> *
4* *
5* This program is free software; you can redistribute it and/or modify *
6* it under the terms of the GNU General Public License as published by *
7* the Free Software Foundation; either version 2 of the License, or *
8* (at your option) any later version. *
9***************************************************************************/
10#include <stdlib.h>
11#include <stdio.h>
12
13#include "KBlocksPieceGenerator.h"
14#include "KBlocksPiece.h"
15
16KBlocksPieceGenerator::KBlocksPieceGenerator(int size)
17{
18 maxCapacity = size;
19 pieceIndex = 0;
20 maPieceList = new int[maxCapacity];
21}
22
23KBlocksPieceGenerator::~KBlocksPieceGenerator()
24{
25 delete [] maPieceList;
26}
27
28void KBlocksPieceGenerator::genList(int seed)
29{
30 srand(seed);
31
32 for(int i = 0; i < maxCapacity; i++)
33 {
34 maPieceList[i] = rand() % PieceType_Detail_Max_Count;
35 }
36
37 pieceIndex = 0;
38}
39
40int KBlocksPieceGenerator::getPiece()
41{
42 pieceIndex++;
43
44 if (pieceIndex > maxCapacity)
45 {
46 pieceIndex = 0;
47 genList(maPieceList[0]);
48 }
49
50 return maPieceList[pieceIndex];
51}
52
53int KBlocksPieceGenerator::getIndex()
54{
55 return pieceIndex;
56}
57
58