-
Notifications
You must be signed in to change notification settings - Fork 1
/
Game_2.cpp
147 lines (146 loc) · 2.96 KB
/
Game_2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#include"Packet.h"
class Analysis
{
private:
int MaxPeole;
double* Sum;
int* Luck;
double* PreMoney;
public:
Analysis(int M,int I);
~Analysis();
void Print();
int Statistics(double*,int);
};
Analysis::Analysis(int M,int I)
{
MaxPeole=M;
Sum=new double[MaxPeole+1];
Luck=new int[MaxPeole+1];
PreMoney=new double[MaxPeole+1];
for(int i=1;i<=MaxPeole;i++)
PreMoney[i]=I;
memset(Sum,0,sizeof(double)*(MaxPeole+1));
memset(Luck,0,sizeof(int)*(MaxPeole+1));
}
Analysis::~Analysis()
{
if(Sum!=NULL)
delete Sum;
if(Luck!=NULL)
delete Luck;
if(PreMoney!=NULL)
delete PreMoney;
}
void Analysis::Print()
{
for(int i=1;i<=MaxPeole;i++)
cout<<"第"<<i<<"个人运气王次数:"<<Luck[i]<<"次"<<endl;
for(int i=1;i<=MaxPeole;i++)
cout<<"第"<<i<<"个人抢得金额为:"<<Sum[i]<<"元"<<endl;
}
int Analysis::Statistics(double* Money,int PacketMoney)
{
double Max=0;
int ID=0;
for(int i=1;i<=MaxPeole;i++)
{
double Get=Money[i]-PreMoney[i];
Sum[i]+=Get;
if(Get>Max)
{
Max=Get;
ID=i;
}
PreMoney[i]=Money[i];
}
Luck[ID]++;
PreMoney[ID]-=PacketMoney;
return ID;
}
class Game
{
private:
int InitMoney;
int MaxPeole;
int PacketMoney;
int PacketQuota;
double* Money;
int *Order;
public:
Analysis Debug;
Game(int,int,int,int);
~Game();
void Start(int);
void Print();
void GetOrder();
};
Game::Game(int IM,int MP,int PM,int PQ):Debug(MP,IM)
{
InitMoney=IM;
MaxPeole=MP;
PacketMoney=PM;
PacketQuota=PQ;
Money=new double[MaxPeole+1];
Order=new int[MaxPeole+1];
for(int i=1;i<=MaxPeole;i++)
Money[i]=InitMoney;
}
Game::~Game()
{
if(Money!=NULL)
delete Money;
if(Order!=NULL)
delete Order;
}
void Game::GetOrder()
{
for(int i=1;i<=MaxPeole;i++)
Order[i]=i;
srand(GetTime());
for(int i=1;i<=(MaxPeole*4);i++)
{
int Left=rand()%MaxPeole+1;
int Right=rand()%MaxPeole+1;
swap(Order[Left],Order[Right]);
}
/*
for(int i=1;i<=MaxPeole;i++)
{
cout<<Order[i];
if(i==MaxPeole)
cout<<endl;
else
cout<<" ";
}*/
}
void Game::Start(int Round)
{
for(int i=1;i<=Round;i++)
{
double Max=0;
int Luck=0;
Packet RedPacket(PacketMoney,PacketQuota);
GetOrder();
for(int j=1;j<=PacketQuota;j++)
{
double Get=RedPacket.GetMoney();
int Index=Order[j];
Money[Index]+=Get;
}
Luck=Debug.Statistics(Money,PacketMoney);
Money[Luck]-=PacketMoney;
}
}
void Game::Print()
{
for(int i=1;i<=MaxPeole;i++)
cout<<"第"<<i<<"个人还有"<<Money[i]<<"元"<<endl;
}
int main()
{
Game Solitaire(100,10,10,10);
Solitaire.Start(20);
Solitaire.Debug.Print();
Solitaire.Print();
}