Advertisement
Guest User

Untitled

a guest
May 7th, 2016
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace project_slot_machine
  8. {
  9.     public class FruitMachine
  10.     {
  11.         public List<int[]> rows;
  12.  
  13.         public int tokensPlayed;
  14.  
  15.         public int points;
  16.  
  17.         public FruitMachine()
  18.         {
  19.             this.rows = new List<int[]>();
  20.         }
  21.  
  22.         public void initialize()
  23.         {
  24.             for (int i = 0; i < 3; i++)
  25.             {
  26.                 this.rows.Add(new int[3]);
  27.             }
  28.         }
  29.  
  30.         public bool wonInRow()
  31.         {
  32.             if(this.tokensPlayed == 1) {
  33.                 int[] middleRow = this.rows[1];
  34.                 return middleRow[0] == middleRow[1] && middleRow[1] == middleRow[2];
  35.             }
  36.  
  37.             if(this.tokensPlayed == 2) {
  38.                 foreach( int[] row in this.rows) {
  39.                     if (row[0] == row[1] && row[1] == row[2]) return true;
  40.                 }
  41.             }
  42.  
  43.             return false;
  44.         }
  45.  
  46.         public bool wonInColumn()
  47.         {
  48.             List<List<int>> cols = new List<List<int>>();
  49.             for( int i = 0; i < this.rows.Count; i++) {
  50.                 cols[0][i] = this.rows[i][0];
  51.                 cols[1][i] = this.rows[i][1];
  52.                 cols[2][i] = this.rows[i][2];
  53.             }
  54.            
  55.             if(this.tokensPlayed == 1)
  56.             {
  57.                 //pick middle column
  58.                 var col = cols[1];
  59.                 // if in middle row exists two the same values return true;
  60.                 return col[0] == col[1] || col[1] == col[2];
  61.             }
  62.  
  63.             if(this.tokensPlayed == 2)
  64.             {
  65.                 // If in any of columns exits pair of the same values return true
  66.                 foreach (List<int> col in cols)
  67.                 {
  68.                  
  69.                     if (col[0] == col[1] || col[1] == col[2]) return true;
  70.                 }
  71.             }
  72.  
  73.             return false;
  74.         }
  75.  
  76.         public int getPoints()
  77.         {
  78.             if (this.wonInRow() && this.tokensPlayed == 2) return 4;
  79.             if (this.wonInRow() && this.tokensPlayed == 1) return 2;
  80.             if (this.wonInColumn()) return 2;
  81.             return 0;
  82.         }
  83.  
  84.         public int play(int tokensToPlay)
  85.         {
  86.             if(tokensToPlay > 2) {
  87.                 throw new ArgumentException("Maximum tokens to play is 2");
  88.             }
  89.             this.tokensPlayed = tokensToPlay;
  90.  
  91.             return this.getPoints();
  92.         }
  93.  
  94.         // Loop through each row and add random generated row;
  95.         public void spin()
  96.         {
  97.            
  98.  
  99.             for (int i = 0; i < this.rows.Count(); i ++)
  100.             {
  101.                 //3 = number of slots in row
  102.                 this.rows[i] = this.getRandRow(3);
  103.             }
  104.         }
  105.  
  106.         public int[] getRandRow(int slots)
  107.         {
  108.             Random rnd = new Random();
  109.             int[] row = new int[3];
  110.             row[0] = rnd.Next(1, 4); row[1] = rnd.Next(1, 4); row[2] = rnd.Next(1, 4);
  111.             return row;
  112.         }
  113.     }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement