Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace project_slot_machine
- {
- public class FruitMachine
- {
- public List<int[]> rows;
- public int tokensPlayed;
- public int points;
- public FruitMachine()
- {
- this.rows = new List<int[]>();
- }
- public void initialize()
- {
- for (int i = 0; i < 3; i++)
- {
- this.rows.Add(new int[3]);
- }
- }
- public bool wonInRow()
- {
- if(this.tokensPlayed == 1) {
- int[] middleRow = this.rows[1];
- return middleRow[0] == middleRow[1] && middleRow[1] == middleRow[2];
- }
- if(this.tokensPlayed == 2) {
- foreach( int[] row in this.rows) {
- if (row[0] == row[1] && row[1] == row[2]) return true;
- }
- }
- return false;
- }
- public bool wonInColumn()
- {
- List<List<int>> cols = new List<List<int>>();
- for( int i = 0; i < this.rows.Count; i++) {
- cols[0][i] = this.rows[i][0];
- cols[1][i] = this.rows[i][1];
- cols[2][i] = this.rows[i][2];
- }
- if(this.tokensPlayed == 1)
- {
- //pick middle column
- var col = cols[1];
- // if in middle row exists two the same values return true;
- return col[0] == col[1] || col[1] == col[2];
- }
- if(this.tokensPlayed == 2)
- {
- // If in any of columns exits pair of the same values return true
- foreach (List<int> col in cols)
- {
- if (col[0] == col[1] || col[1] == col[2]) return true;
- }
- }
- return false;
- }
- public int getPoints()
- {
- if (this.wonInRow() && this.tokensPlayed == 2) return 4;
- if (this.wonInRow() && this.tokensPlayed == 1) return 2;
- if (this.wonInColumn()) return 2;
- return 0;
- }
- public int play(int tokensToPlay)
- {
- if(tokensToPlay > 2) {
- throw new ArgumentException("Maximum tokens to play is 2");
- }
- this.tokensPlayed = tokensToPlay;
- return this.getPoints();
- }
- // Loop through each row and add random generated row;
- public void spin()
- {
- for (int i = 0; i < this.rows.Count(); i ++)
- {
- //3 = number of slots in row
- this.rows[i] = this.getRandRow(3);
- }
- }
- public int[] getRandRow(int slots)
- {
- Random rnd = new Random();
- int[] row = new int[3];
- row[0] = rnd.Next(1, 4); row[1] = rnd.Next(1, 4); row[2] = rnd.Next(1, 4);
- return row;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement