Thursday 27 November 2014

Application of Random Class: CRAPS

CRAP is a dice game played in casino and back alleys throughout the world.

A Game of Chance CRAP
Rules of the game:
You roll two dice. Each die has a six faces, which contain one, two, three, four, five, six spots, respectively. After the dice have come to rest, the some of spots n the two upward face is calculated.
if some is 7 or 11 on the first throw, you win.
if some is  2,3 or 12 on the first throw (called CRAPS ) you lose (i.e the house wins).
if some is 4,5,6,8,9 or 10 on the first throw, that sum become "your point".
         To win you must continues rolling your dice until you "make your point" (i.e roll that same value).
You lose by rolling a 7 before making your point.
Source code:
// class crap.cs 
  1. using System;
  2. public class CRAP
  3. {
  4. //Creating a random number generator for use in methd RollDice
  5. private Random randomNumber = new Random();
  6. private enum Status //ENUM WITH CONSTATNT THAT REPRESENT GAME STATUS
  7. {
  8. CONTINUE,
  9. WON,
  10. LOST
  11. }
  12. private enum DiceNames
  13. {
  14. SNAKE_EYES = 2,
  15. TREY = 3,
  16. SEVEN = 7,
  17. YO_LEVEN = 11,
  18. BOX_CARS = 12
  19. }
  20. // PLAY ONE GAME OF CRAP
  21. public void Play()
  22. {
  23. Status gameStatus = Status.CONTINUE;
  24. int myPoint = 0;
  25. int sumOfDice = RollDice();
  26. switch ((DiceNames)sumOfDice)
  27. {
  28. case DiceNames.SEVEN:
  29. case DiceNames.YO_LEVEN:
  30. gameStatus = Status.WON;
  31. break;
  32. case DiceNames.BOX_CARS:
  33. case DiceNames.SNAKE_EYES:
  34. case DiceNames.TREY:
  35. gameStatus = Status.LOST;
  36. break;
  37. default:
  38. gameStatus= Status.CONTINUE;
  39. myPoint = sumOfDice;
  40. Console.WriteLine("Point is {0}", myPoint);
  41. break;
  42. }
  43. while (gameStatus == Status.CONTINUE)
  44. {
  45. sumOfDice = RollDice(); // roll dice again
  46. if (sumOfDice == myPoint)
  47. gameStatus = Status.WON;
  48. if (sumOfDice == (int)DiceNames.SEVEN)
  49. gameStatus = Status.LOST;
  50. }// end of while method
  51. // Display won or loss
  52. if (gameStatus == Status.WON)
  53. Console.WriteLine("Palyer Wins");
  54. else
  55. Console.WriteLine("Player Losses");
  56. }//end of method Play
  57. public int RollDice()
  58. {
  59. int die1 = randomNumber.Next(1, 7);
  60. int die2 = randomNumber.Next(1, 7);
  61. int sum = die1 + die2;
  62. //Display result of this roll
  63. Console.WriteLine("Player rolled {0} + {1} = {2}", die1, die2, sum);
  64. return sum;
  65. }// End method roll dice
  66. }
//class CrapTest.cs 

  1. using System;
  2. class CrapTets
  3. {
  4. public static void Main()
  5. {
  6. CRAP game = new CRAP();
  7. game.Play();
  8. Console.ReadLine();
  9. }
  10. }

Screen Shots of Output: 


Dice game in C-Sharp.Application of Random Class
Fig 1: Snapshot of output



Dice game in C-Sharp.Application of Random Class
Fig 2: Snapshot of output



Dice game in C-Sharp.Application of Random Class
Fig 3: Snapshot of output


Dice game in C-Sharp.Application of Random Class
Fig 4: Snapshot of output


Monday 24 November 2014

Introducing random class in c-sharp

Case Study: Rolling a six dice problem.


In this problem the application simulate 20 rolls of a six sided dice and display the value of each roll. So, in this problem we use
random class : the object of class random can produce random byte,int,double values
Scaling factor:
 int randomValue = random.Next(6);

In the above statement we use randomvalue of type integer random is the object of class "Random;", Next is the method of class random
The above statement returns value 0,1,2,3,4,5. The Argument is called Scaling factor- Represent the no. of that Next should produce (in this case 0,1,2,3,4 and 5)
This manipulation is called scaling the range of value produce by Random method Next.
Shifting factor
Suppose we want to simulate six die and that has the numbers 1-6 on its faces, not 0-5. Scaling the range of value is not enough. So we shift the range of numbers we produced. We could do this by adding shifting value – in this case 1- to the result of method next, as in
         int randomValue =1+ random.Next(6);
OR
int randomValue = random.Next(1,6);

Here we have two example of class random.

Example one: Rolling a six sided die twenty times and note the result every time
Source Code

using System;
public class RandomIntegers
{
public static void Main()
{
Random random=new Random(); // create object of Random Method
int face; //store each random integer

for(int counter=1;counter<=20;counter++)
{
face=random.Next(1,7); //shifting and scaling
Console.Write("{0} ",face); //display generated value
if(counter%5==0)
Console.WriteLine();
}

}
}

Screenshots of Output:


Application of random class. Dice game
Fig 1: Snapshot of Dice game

Example 2:  Rolling a dice 6000 time and observe the frequency of outcomes.

Source Code: 

using System;
public class RollDie
{
public static void Main()
{
Random random = new Random();

int frequency1=0; //Count of 1's rolled
int frequency2=0; //Count of 2's rolled
int frequency3=0; //Count of 3's rolled
int frequency4=0; //Count of 4's rolled
int frequency5=0; //Count of 5's rolled
int frequency6=0; //Count of 6's rolled

int face;  //store most recently rolled value

for(int roll=1;roll<=6000;roll++)
{
face=random.Next(1,7); //number from 1 to 6

switch(face)
{
case 1:
frequency1++;
break;

case 2:
frequency2++;
break;

case 3:
frequency3++;
break;

case 4:
frequency4++;
break;

case 5:
frequency5++;
break;

case 6:
frequency6++;
break;
}
}

Console.WriteLine("Face\tFrequency" ); // output headers

Console.WriteLine("1\t{0}\n2\t{1}\n3\t{2}\n4\t{3}\n5\t{4}\n6\t{5}\n",
frequency1, frequency2, frequency3, frequency4, frequency5, frequency6);
}
}

Screenshot of Final Output: 


Frequency of Dice faces.
Fig 2: Frequency of dice faces

Saturday 22 November 2014

Number Pattern in C# - 14 (FLOYED TRIANGLE)

Qus: WAP  To print Floyed triangle in c-sharp.


screenshot of floyed triangle in c-sharp
Fig: Screenshot of Output

using System;
class FloyedTriangle
{
public static void Main()
{
int i,row,j,k=1;
Console.Write("Enter the no. of row of Floyd's triangle: ");
row=Convert.ToInt32(Console.ReadLine());
for(i=1;i<=row;i++)
{
for(j=1;j<=i;j++,k++)
Console.Write("{0} ",k);
Console.WriteLine();
}
}
}

Number Pattern in C# - 13

Output of number pattern in c-sharp
Fig: Screenshot of output


using System;
class PatternTest
{
public static void Main()
{
int j,k,row,r=1;

Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>=1;row--,r++)
{
for(j=r;j>=1;j--)
Console.Write(" ");

for(j=1;j<=row;j++)
Console.Write(j);

for(k=row-1;k>=1;k--)
Console.Write(k);
Console.WriteLine();
}
}
}

Number Pattern in C# - 12

output of Number pyramid in c-sharp
Fig: Screenshot of Output


using System;
class PatternTest
{
public static void Main()
{
int i,j,k,row;

Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<row;i++)
{
for(j=row-i;j>=1;j--)
Console.Write(" ");

for(j=1;j<=i;j++)
Console.Write(j);

for(k=i-1;k>=1;k--)
Console.Write(k);
Console.WriteLine();
}
}
}

Number Pattern in C# - 11

Output of number pattern in c-sharp 11
Fig: Screenshot of Output


using System;
class PatternTest
{
public static void Main()
{
int j,row,r=1;

Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());
//int temp=row;

for(;row>=1;row--,r++)
{
for(j=r;j>1;j--)
Console.Write(" ");

for(j=1;j<=row;j++)
Console.Write(j);
Console.WriteLine();


}
}
}

Number Pattern in C# - 10

Output of number pattern in c-sharp 10
Fig: Screenshot of Output


using System;
class patternProgramming
{
public static void Main()
{
int i,j,row;

Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=row;i>=1;i--)
{
for(j=1;j<=i;j++)
Console.Write(j);
Console.WriteLine();
}
}
}

Number Pattern in C# - 9

Output of number pattern in c-sharp 9
Fig: Screenshot of Output

using System;

class PatternProgramming
{
public static void Main()
{
int i,j,row;

Console.Write("Enter the no. of row yu want to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<=row;i++)
{
for(j=i;j>=1;j--)
Console.Write(j);
Console.WriteLine();
}


}
}

Number Pattern in C# - 8

Output of Number Pattern in C-sharp 8
Fig: Screenshot of Output


using System;
class PatternProgramming
{
public static void Main()
{
int i,row;
Console.Write("Enter the no. of row you want to enter: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>=1;row--)
{
for(i=row;i>=1;i--)
Console.Write(i);
Console.WriteLine();
}
}
}

Number Pattern in C# - 7

Output of Number pattern in C-sharp
Fig: Screenshot of Output


using System;
class PatternProgramming
{
public static void Main()
{
int i,j,row;
Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<=row;i++)
{
for(j=row-i;j>=1;j--)
Console.Write(" ");

for(j=1;j<=i;j++)
Console.Write(j);
Console.WriteLine();
}
}
}

Number Pattern in C# - 6

output of number pattern program in c-sharp
Fig: Screenshot of output


using System;
class PatternProgramming
{
public static void Main()
{
int i,row;
Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(int j=1;j<=row;j++)
{
for(i=1;i<=j;i++)
Console.Write(i);
Console.WriteLine();
}
}
}

Number Pattern in C# - 5

Snapshot of Number pattern program output
Fig: Snapshots of Output


using System;
class PatternProgramming
{
public static void Main()
{
int i,row;
Console.Write("Enter the no. of row: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>=1;row--)
{
for(i=1;i<=row;i++)
Console.Write(i);
Console.WriteLine();
}
}
}

Number Pattern in C# - 4


using System;
class PatternProgramming
{
public static void Main()
{
int i,j,row;

Console.Write("Enter the no. of rows you want to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<=row;i++)
{
for(j=1;j<=row;j++)
Console.Write(" ");

for(j=1;j<=row;j++)
Console.Write(i);
Console.WriteLine();
}
}
}
Number pattern Output
Fig: Snapshot of Output




Number Pattern in C# - 3

using System;
class PatternProgramming
{
public static void Main()
{
int i,j,row;

Console.Write("Enter the no. of line you have to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(j=1;j<=row;j++)
{
for(i=1;i<=row;i++)
Console.Write(" ");

for(i=row;i>=1;i--)
Console.Write(i);
Console.WriteLine();
}
}
}
Snapshot of Output of Number pattern in c-sharp
Fig: Snapshots of Output



Number Pattern in C# - 2

using System;
class PatternProgramming
{
public static void Main()
{
int i,j,row;

Console.Write("Enter the no. of line you have to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(j=1;j<=row;j++)
{
for(i=1;i<=row;i++)
Console.Write(" ");

for(i=1;i<=row;i++)
Console.Write(i);
Console.WriteLine();
}
}
}

Number pattern output
Fig: Snapshot of Output


Number Pattern in C# - 1

using System;
class PatternProgramming
{
public static void Main()
{
int i,row;

Console.Write("Enter the no. of line you have to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>=1; row--)
{
for(i=1;i<=row;i++)
Console.Write(i);
Console.WriteLine();
}
}
}
Number pattern programming output
Fig: Snapshot of Output







Star Pattern in C# - 6



using System;
class PatternProgramming
{
public static void Main()
{
int row,i,j,k;

Console.Write("Enter the no. of row you wantt to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<=row;i++)
{

for(j=row-i;j>=1;j--)
Console.Write(" ");

for(k=1;k<=i;k++)
Console.Write("@");
Console.WriteLine();
}
}
}
Fig: Snapshot of Output





Star Pattern in C# - 5

using System;
class Pattern_star
{
public static void Main()
{
int i,row;
Console.Write("Enter the no. of row you want to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(i=1;i<=row;i++)
{
for(int j=1;j<=i;j++)
Console.Write("#");
Console.WriteLine();

}
}
}
Star pattern in c-sharp output.
Fig: Snapshots of output

Star Pattern in C# - 4

Star Pattern type 4 in C# tutorial
Fig: Snapshot of Output
using System;
class Patters_test
{
public static void Main()
{
int row,i;
Console.Write("Enter the no. of row you have to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>0;row--)
{
for(i=0;i<row;i++)

Console.Write("#");
Console.WriteLine();
}
}
}

Star Pattern in C# - 3

Star Pattern in C-sharp
Fig 1: Snapshots of output


using System;
class Pattern_Test3
{
public static void Main()
{
int i=1;
int row;
int j;

Console.Write("Enter no. of row you have to print: ");
row=Convert.ToInt32(Console.ReadLine());

for(;row>0;row--,i++)
{
for(j=i;j>1;j--)
Console.Write(" ");

for(int c=0;c<row;c++)
Console.Write("*");
Console.WriteLine();
}
}
}

Star Pattern in C# - 2

Star Pattern Programming
Fig 1: Pattern Programming Output


using System;

class Pattern_Star
{
public static void Main()
{
int i,j,row;

Console.WriteLine("Enter the no. of row you have to print");
row=Convert.ToInt32(Console.ReadLine());

for(i=0;i<row;i++)
{
for(j=0;j<row;j++)
Console.Write("@");
Console.WriteLine();
}
}
}