[ create a new paste ] login | about

Link: http://codepad.org/A4cDq5tQ    [ raw code | fork ]

Loyen_ - C++, pasted on Sep 21:
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/*

    Title: Slot machine

    Filename: inlupp_2.cpp

    Description: A "slot machine" where you bet money and that betmoney will be

                 multiplied if you got any rows, otherwise lost. 

    Input: Money, money bet, replay answer

    Output: Game board, rows player got, money betted, total money, money won/lost.

    Author: Patrik Freij

*/

#include <iostream>

#include <ctime>



using namespace std;



/*

	Creating player specific variables like money, betted money and

    money won/lost and "wins-per-round"

*/

struct player

{

	int

		money,

		moneybet,

		moneywl,

		

		wins;

};



/*

	Creating function "boardcheck" which checks for wins on the board. Returns wins.

*/

int boardcheck(int number[][3]) 

{

	int wins = 0;

	

	/*

        We check each "3 in a row" path to see if the second and third value in

        a row is thesame as the first one, if it is, add 1 to "wins".

    */

    // Horizontal check

	if((number[0][0] == number[0][1]) && (number[0][0] == number[0][2])) {

		wins++;

	}

    if((number[1][0] == number[1][1]) && (number[1][0] == number[1][2])) {

		wins++;

	}

    if((number[2][0] == number[2][1]) && (number[2][0] == number[2][2])) {

		wins++;

	}

	// Vertical check

	if((number[0][0] == number[1][0]) && (number[0][0] == number[2][0])) {

		wins++;

	}

    if((number[0][1] == number[1][1]) && (number[0][1] == number[2][1])) {

		wins++;

	}

    if((number[0][2] == number[1][2]) && (number[0][2] == number[2][2])) {

		wins++;

	}

	// "cross" check

	if((number[0][0] == number[1][1]) && (number[0][0] == number[2][2])) {

		wins++;

	}

    if((number[2][0] == number[1][1]) && (number[2][0] == number[0][2])) {

		wins++;

	}

	

	return wins;

}



/*

    Tells the game how many times to multiplicate "moneybet" depending on how

    many rows you got in the game. If no rows, return -1 since you will loose money. 

*/

int rowmultiply(int &wins)

{

    switch(wins)

    {

        case 1:

            return 2;

            break;

        case 2:

            return 3;

            break;

        case 3:

            return 4;

            break;

        case 4:

            return 5;

            break;

        case 5:

            return 7;

            break;

        case 6:

            return 8;

            break;

        case 7:

            return 9;

            break;

        case 8:

            return 10;

            break;

        default:

            return -1;

            break;

    }

}



// Starting the program

int main()

{

	// Creating the player and setting default values

	player patrik;

	patrik.money = 0;

	patrik.moneybet = 0;

	patrik.moneywl = 0;

	

	// Creating variables

	const int cells = 3;

	int

		replay = 1,

		betvalid = 0,

		number[cells][cells];

	char

		letter = 'a';

	

	srand(time(0));

	// If the player wants to play again after he have played once time, make this true and play again.

	while(replay)

	{

		/*

			If the player has less than 1 krona, make betvalid false.

            if betvalid is false let the player add more money.

			The value has to be between 100, 300 or 500 kronor to be valid.

		*/

		if(patrik.money < 1) { betvalid = 0; }

		while(betvalid != 1)

		{

			cout<<"You don't have enough money to play, please cash in some money: (100, 300, 500) ";

			cin>>patrik.money;

			

			if(patrik.money == 100 || patrik.money == 300 || patrik.money == 500)

			{

				betvalid = 1;

			} else {

				cout<<"Invalid value"<<endl;

			}

		}

		cout<<"You currently got "<<patrik.money<<" kronor."<<endl;

		betvalid = 0;

		/*

			If betvalid is not true, let the player bet money.

			If the player has bet an amount that isn't 100, 300 or 500 or doesn't

            have that amount of money it will run again.

		*/

		while(betvalid != 1)

		{

			cout<<"Place your bets: ";

			cin>>patrik.moneybet;

			

			if(patrik.money >= patrik.moneybet && patrik.moneybet > 0)

			{

				betvalid = 1;

			} else {

				cout<<"Invalid bet"<<endl;

			}

		}

		

		/*

			Print out the board with 2 for loops (one for y axis and one for x axis).

			Add the current "cordinate" to the current cell and then

			add a random number between 1 and 3 in each field. Then replace the number with a letter.

		*/

		for(int y=0;y<cells;y++)

		{

			cout<<"-------------"<<endl;

			for(int x=0;x<cells;x++)

			{

				if(x==0) { cout<<"| "; }

				number[y][x] = rand() % 3 + 1;

				switch(number[y][x])

				{

					case 1:

						letter = 'O';

						break;

					case 2:

						letter = 'X';

						break;

					default:

						letter = 'A';

						break;

				}

				cout<<letter<<" | ";

			}

			if(y==2) { cout<<endl<<"-------------"; }

			cout<<endl;

		}

		

		/*

            Check for wins with "boardcheck", then multiply your bet depending

            on how many rows you got with "rowmultiply".

        */

		patrik.wins = boardcheck(number);

		cout<<"You got "<<patrik.wins<<" in a row!"<<endl;

		cout<<"You betted "<<patrik.moneybet;

		patrik.moneybet = patrik.moneybet * rowmultiply(patrik.wins);

		if(patrik.moneybet > 0)

		{

            cout<<" kronor and won "<<patrik.moneybet<<" kronor!"<<endl;

        } else {

            cout<<" kronor and lost "<<patrik.moneybet*-1<<" kronor!"<<endl;

        }

        // Add won/lost money to your "moneywl" and current money

		patrik.moneywl = patrik.moneywl + patrik.moneybet;

		patrik.money = patrik.money + patrik.moneybet;

		cout<<"You currently got "<<patrik.money<<" kronor."<<endl;

		

        //	Ask the player to replay the game.

		cout<<"Replay? (1/0) ";

		cin>>replay;

	}

	// Tell the player how much he won/lost and that it will be given to/taken from his account.

	if(patrik.moneywl > 0)

	{

        cout<<"You won "<<patrik.moneywl<<" kronor and will now be cashed in to your account"<<endl;

    } else {

        cout<<"You lost "<<patrik.moneywl*-1<<" kronor! Better luck next time."<<endl;

    }

    // Shut it down! After you have pressed any button of course.

    system("PAUSE");

	return 0;

}


Create a new paste based on this one


Comments: