This repository was archived by the owner on Jul 1, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruleta.cc
More file actions
362 lines (316 loc) · 8.09 KB
/
ruleta.cc
File metadata and controls
362 lines (316 loc) · 8.09 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#include <string>
#include <iostream>
#include <list>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include "persona.h"
#include "crupier.h"
#include "jugador.h"
#include "ruleta.h"
using namespace std;
Ruleta::Ruleta(Crupier crupier):crupier_(crupier) //Constructor
{
srand(time(NULL));
bola_=-1;
banca_=1000000;
tiradas_=0;
}
/*
It will return false or true, depends if bola is between 0-36
*/
bool Ruleta::setBola(int bola)
{
if(bola<0 || bola>36)
return false;
else
bola_=bola;
tiradas_++;
return true;
}
/*
It will return false or true, depends if banca is above 0
*/
bool Ruleta::setBanca(int banca)
{
if(banca<0)
return false;
else
banca_=banca;
return true;
}
/*
Proc: It will verify if DNI file from such player is created.
If its not created, it will create and return true, if it exits it will return true and if dniJugador
and introduced if isnt the same, it will return false
Modify: Jugador list
*/
bool Ruleta::addJugador(Jugador jugador) //It receives a player from jugador.h as a parameter
{
for(list<Jugador>::iterator it=jugadores_.begin(); it != jugadores_.end(); it++){ //Verify that Player isnt in the list(It creates Jugadores) list with iterator it as a 'name' and it will iterate while it is different than list funcion .end, it sum it++)
if(it->getDNI()==jugador.getDNI())
return false;
}
//If it isn't exists the Player, it will added with push_back
jugadores_.push_back(jugador);
string name=(jugador.getDNI() + ".txt");
ifstream DNI;
DNI.open(name.c_str()); //It opens in write mode
//Si usamos un string, luego habrá que usar .c_str()
if(DNI)
return true; //File exists
else
{
ofstream DNIj; //It creates file
DNIj.open(name.c_str()); //It doesnt exist
DNIj.close();
}
return true;
}
/*
It will erase the list from the beginning and it will go over the list with the iterator it, when the introduced dni is the same, it will delete with .erase(it)
*/
int Ruleta::deleteJugador(string DNIj)
{
list<Jugador>::iterator it;
if(jugadores_.empty()) //Verify if list is empty
return -1;
for(it=jugadores_.begin(); it != jugadores_.end(); it++)
{
if(it->getDNI()==DNIj) //If Dni of it is equals than DNIj
{
jugadores_.erase(it); //It will erase the element linked with it and DNIj
return 1; //Player erased
}
}
return -2; //Player already doesnt exit
}
int Ruleta::deleteJugador(Jugador jugador)
{
return(deleteJugador(jugador.getDNI()));
}
/*
It will create a file named jugadores.txt and it will push from the back all the elements that a player and a croupier have.
*/
void Ruleta::escribeJugadores()
{
list<Jugador>::iterator it;
ofstream ent;
ent.open("jugadores.txt");
for(it=jugadores_.begin(); it!=jugadores_.end(); it++)
ent<<it->getDNI()<<","<< it->getCodigo()<<","<< it->getNombre()<<","<< it->getApellidos()<<","<< it->getDireccion()<<","<< it->getLocalidad()<<","<< it->getProvincia()<<","<< it->getPais()<<","<< it->getDinero()<<endl;
/*It will be displayed like this
DNI,código,nombre,apellidos,dirección,localidad,provincia,país,dinero (Spanish)
ID,code,name,surnames,adress,locality,province,country,money (English)*/
ent.close();//It will close ent file
}
/*
It will read all the elements from the file jugadores.txt and it will push from the back to the list
*/
void Ruleta::leeJugadores()
{
ifstream jug;
string DNI, nombre, codigo, apellidos, direccion, localidad, provincia, pais, dinero;
Jugador jugador("", "", "", "", "", "", "", "");
jugadores_.clear();
jug.open("jugadores.txt");
while(getline(jug, DNI, ','))
{
jugador.setDNI(DNI);
getline(jug, codigo, ',');
jugador.setCodigo(codigo);
getline(jug, nombre, ',');
jugador.setNombre(nombre);
getline(jug, codigo, ',');
jugador.setApellidos(apellidos);
getline(jug, apellidos, ',');
jugador.setDireccion(direccion);
getline(jug, direccion, ',');
jugador.setLocalidad(localidad);
getline(jug, localidad, ',');
jugador.setProvincia(provincia);
getline(jug, provincia, ',');
jugador.setPais(pais);
getline(jug, dinero, '\n');
jugador.setDinero(atoi(dinero.c_str()));
jugadores_.push_back(jugador); //It will push to the back of the list
}
jug.close();
}
/*
It will spin the roulette and throw the ball
*/
void Ruleta::giraRuleta()
{
bola_=rand()%37; //Random number from 0 to 36
tiradas_++;
}
/*
Bet for ball number
*/
void Ruleta::apuestaSencilla(int &dinero, Apuesta p)
{
if(bola_==atoi(p.valor.c_str())) //If bola value is equal to struct valor value
{
dinero = dinero + (p.cantidad)*35;
banca_ = banca_ -(p.cantidad)*35;
}else
{
dinero = dinero - (p.cantidad);
banca_ = banca_ + (p.cantidad);
}
}
/*
Bet for colour number
*/
void Ruleta::apuestaRojoyNegro(int &dinero, Apuesta p)
{
if((bola_!=0 && p.valor==color(bola_))) //If bola is different than 0 and colour bola value is equal to struct valor value
{
dinero = dinero + (p.cantidad);
banca_ = banca_ - (p.cantidad);
}else{
dinero = dinero - (p.cantidad);
banca_ = banca_ + (p.cantidad);
;
}
}
/*
Bet for number
*/
void Ruleta::apuestaImparypar(int &dinero, Apuesta p){
if((bola_!=0 && p.valor==paridad(bola_))){ //If bola is different than 0 and paridad bola value is equal to struct valor value
dinero = dinero + (p.cantidad);
banca_ = banca_ - (p.cantidad);
}else{
dinero = dinero - (p.cantidad);
banca_ = banca_ + (p.cantidad);
}
}
string Ruleta::paridad(int bola){
if(bola%2==0)
return "par";
else
return "impar";
}
void Ruleta::apuestaAltoyBajo(int &dinero, Apuesta p){
if(bola_!=0 && p.valor==nivel(bola_)){ //If bola is different than 0 and nivel bola value is equal to struct valor value
dinero = dinero + (p.cantidad);
banca_ = banca_ - (p.cantidad);
}else{
dinero = dinero - (p.cantidad);
banca_ = banca_ + (p.cantidad);
}
}
string Ruleta::nivel(int bola){
if(bola>0 && bola<19)
return "bajo";
else
return "alto";
}
string Ruleta::color(int bola){
switch(bola){
case 1:
case 3:
case 5:
case 7:
case 9:
case 12:
case 14:
case 16:
case 18:
case 19:
case 21:
case 23:
case 25:
case 27:
case 30:
case 32:
case 34:
case 36:
return "rojo";
default:
return "negro";
}
}
/*
This fuction will save bets and sum with the currently money of the player to resave the money of them
*/
void Ruleta::getPremios(){
list<Apuesta>apuestaslist;
list<Jugador>::iterator it;
list<Apuesta>::iterator ot;
for(it=jugadores_.begin(); it != jugadores_.end(); it++){
int dinero=0;
it->setApuestas();
apuestaslist=it->getApuestas();
dinero=it->getDinero();
for(ot=apuestaslist.begin(); ot != apuestaslist.end(); ot++){
switch (ot->tipo){
case 1:
apuestaSencilla(dinero, (*ot));
break;
case 2:
apuestaRojoyNegro(dinero, (*ot));
break;
case 3:
apuestaImparypar(dinero, (*ot));
break;
case 4:
apuestaAltoyBajo(dinero, (*ot));
break;
}
it->setDinero(dinero);
escribeJugadores();
}
}
}
/*This fuction it will return bets value without sum the money of the player*/
int Ruleta::getBets(){
list<Apuesta>apuestaslist;
list<Jugador>::iterator it;
list<Apuesta>::iterator ot;
int dinero;
for(it=jugadores_.begin(); it != jugadores_.end(); it++){
dinero=0;
it->setApuestas();
apuestaslist=it->getApuestas();
for(ot=apuestaslist.begin(); ot != apuestaslist.end(); ot++){
switch (ot->tipo){
case 1:
apuestaSencilla(dinero, (*ot));
break;
case 2:
apuestaRojoyNegro(dinero, (*ot));
break;
case 3:
apuestaImparypar(dinero, (*ot));
break;
case 4:
apuestaAltoyBajo(dinero, (*ot));
break;
}
}
}
return dinero;
}
/*
It will return money valye of all players
*/
int Ruleta::dineroJug(int dinero){
list<Jugador>::iterator it;
for(it=jugadores_.begin(); it != jugadores_.end(); it++){
dinero=it->getDinero();
}
return dinero;
}
void Ruleta::getEstadoRuleta(int &jugadoresTotal, int &sumaDinero, int &tiradasTotales, int &bancaTotal){
list<Jugador>::iterator it;
sumaDinero=0;
for(it=jugadores_.begin(); it != jugadores_.end(); it++)
sumaDinero=sumaDinero+it->getDinero();
sumaDinero=sumaDinero+getBanca();
jugadoresTotal=jugadores_.size();
tiradasTotales=getTiradas();
bancaTotal=getBanca();
}