# Dice 3 Dice Simulation Program Dice dice new

• Slides: 105

Dice. Simulation. Program片段 Dice dice = new Dice(); dice. Toss(); Console. Write. Line( "擲出"+dice. Face. Value ); 5

Dice. Simulation. Dice 片段 public class Dice { int face. Value = 1; Random rand = new Random(); public int Face. Value { get { return face. Value; } set { face. Value = value; } } public void Toss() { face. Value = rand. Next() % 6 + 1; } } 6

TDD 參考資料 • P. Provost, “Test-driven development in. NET, ” http: //www. codeproject. com/KB/dotnet/tdd_in _dotnet. aspx 22

Calculator. Test. Program片段 Calculator calculator = new Calculator(); Console. Write("輸入第一個數字: "); operand 1 = int. Parse(Console. Read. Line()); Console. Write("輸入第二個數字: "); operand 2 = int. Parse(Console. Read. Line()); result = calculator. Add(operand 1, operand 2); Console. Write. Line("{0} + {1} = {2} ", operand 1, operand 2, result); 30

Calculator. Test. Calculator 片段 public class Calculator { public int Add(int a, int b) { int result = a + b; return result; } public int Subtract(int a, int b) { int result = a - b; return result; }. . . } 31

Swapping. Integers. Program 片段 int x = 3; int y = 5; Swap(ref x, ref y); Debug. Assert(x == 5 && y == 3); . . . static void Swap(ref int x, ref int y) { int temp = x; x = y; y = temp; } 37

Pass. By. Reference. And. Out. Progra 片段(1/2) double length =m 100. 0; Square s = new Square(); s. Side = length; double area 1 = 0. 0; double perimeter 1 = 0. 0; s. Get. Area. And. Perimeter(ref area 1, ref perimeter 1); Console. Write. Line( "正方形邊長: {0}, 面積: {1}, 周長: {2}", length, area 1, perimeter 1); 38

Pass. By. Reference. And. Out. Progra m片段(2/2) double area 2; double perimeter 2; s. Get. Area. And. Perimeter. Using. Out(out perimeter 2); Console. Write. Line( "正方形邊長: {0}, 面積: {1}, 周長: length, area 2, perimeter 2); area 2, {2}", 39

Pass. By. Reference. And. Out. Square 片段 public void Get. Area. And. Perimeter( ref double area, ref double perimeter) { area = Math. Pow(a, 2); perimeter = 4. 0 * a; } public void Get. Area. And. Perimeter. Using. Out( out double area, out double perimeter) { area = Math. Pow(a, 2); perimeter = 4. 0 * a; } 40

Overloading. Demo. Adder 片段 public int Add(int a, int b) { return (a + b); } public double Add(double a, double b) { return (a + b); } 46

Dice. Simulation 2. Program 片段 int seed = 123; Dice dice = new Dice(seed); dice. Toss(); Console. Write. Line( "擲出" + dice. Face. Value ); 47

Dice. Simulation 2. Dice片段 public Dice() { rand = new Random(); Toss(); } public Dice(int seed) { rand = new Random(seed); Toss(); } 48

Using. This. Program 片段 Time t = new Time(11, 30, 52); int hour; int min; int sec; t. Get. Time(out hour, out min, out sec); Console. Write. Line( "現在時間{0} : {1} : {2}", hour, min, sec); 52

Using. This. Time片段 public Time(int hour, int min, int sec) { bool params. Are. Valid = hour >= 0 && hour < 24 && min >= 0 && min < 60 && sec >= 0 && sec < 60; if( params. Are. Valid ) { this. hour = hour; this. min = min; this. sec = sec; } else { Console. Write. Line("Time建構式參數值不合理 "); } } 53

Using. Static. Program片段 int hours. To. Mins = Time. Conversion. Hours. To. Mins(hours); int days. To. Hours = Time. Conversion. Days. To. Hours(days); Console. Write. Line(hours + " hours = " + hours. To. Mins + " minutes"); Console. Write. Line(days + " days = " + days. To. Hours + " hours"); Test t 1 = new Test(); Test t 2 = new Test(); Console. Write. Line(Test. Get. NConstructed + " Test objects were constructed"); 56

Using. Static. Time. Conversion片段 public static class Time. Conversion { private const int HOURS_PER_DAY = 24; private const int MINS_PER_HOUR = 60; public static int Hours. To. Mins( int hours ) { return hours*MINS_PER_HOUR; } public static int Days. To. Hours( int days ) { return days*HOURS_PER_DAY; } } 57

Using. Static. Test片段 public class Test { private static int n. Constructed = 0; public Test() { ++n. Constructed; } public static int Get. NConstructed { get { return n. Constructed; } } } 58

Using. Struct. Program 片段 (1/2) struct Point 2 D { public int x; public int y; } 63

Using. Struct. Program 片段 (2/2) static void Main(string[] args) { Point 2 D pt = new Point 2 D(); Console. Write. Line( "Initial location = ({0}, {1})", pt. x, pt. y); pt. x = 3; pt. y = 4; Console. Write. Line( "Final location = ({0}, {1})", pt. x, pt. y); } 64

Struct. VSClass. SPoint 2 D 片段 struct SPoint 2 D { public int x; public int y; public SPoint 2 D(int x, int y) { this. x = x; this. y = y; } } 65

Struct. VSClass. CPoint 2 D 片段 class CPoint 2 D { public int x; public int y; public CPoint 2 D() { x = 0; y = 0; } public CPoint 2 D(int x, int y) { this. x = x; this. y = y; } } 66

Struct. VSClass. Program片段 (1/4) SPoint 2 D s. Pt 1 = new SPoint 2 D(3, 4); SPoint 2 D s. Pt 2 = new SPoint 2 D(); SPoint 2 D s. Pt 3 = new SPoint 2 D(); s. Pt 2 = s. Pt 1; s. Pt 3 = s. Pt 1; Console. Write. Line("s. Pt 1 = ({0}, s. Pt 1. x, s. Pt 1. y); Console. Write. Line("s. Pt 2 = ({0}, s. Pt 2. x, s. Pt 2. y); Console. Write. Line("s. Pt 3 = ({0}, s. Pt 3. x, s. Pt 3. y); {1})", 67

Struct. VSClass. Program 片段 (2/4) CPoint 2 D c. Pt 1 = new CPoint 2 D(3, CPoint 2 D c. Pt 2 = new CPoint 2 D(); CPoint 2 D c. Pt 3 = new CPoint 2 D(); c. Pt 2 = c. Pt 1; c. Pt 3 = c. Pt 1; Console. Write. Line("c. Pt 1 = ({0}, c. Pt 1. x, c. Pt 1. y); Console. Write. Line("c. Pt 2 = ({0}, c. Pt 2. x, c. Pt 2. y); Console. Write. Line("c. Pt 3 = ({0}, c. Pt 3. x, c. Pt 3. y); 4); {1})", 68

Struct. VSClass. Program片段 (3/4) s. Pt 1. x = 10; s. Pt 1. y = 20; s. Pt 2. x = 30; s. Pt 2. y = 40; Console. Write. Line("s. Pt 1. x, s. Pt 1. y); Console. Write. Line("s. Pt 2. x, s. Pt 2. y); Console. Write. Line("s. Pt 3. x, s. Pt 3. y); = ({0}, {1})", 69

Struct. VSClass. Program片段 (4/4) c. Pt 1. x = 10; c. Pt 1. y = 20; c. Pt 2. x = 30; c. Pt 2. y = 40; Console. Write. Line("c. Pt 1. x, c. Pt 1. y); Console. Write. Line("c. Pt 2. x, c. Pt 2. y); Console. Write. Line("c. Pt 3. x, c. Pt 3. y); = ({0}, {1})", 70

Using. Copy. Constructor. CPoint 2 D片段 public CPoint 2 D() { x = 0; y = 0; } public CPoint 2 D(int x, int y) { this. x = x; this. y = y; } public CPoint 2 D(CPoint 2 D p) { x = p. x; y = p. y; } 75

Using. Copy. Constructor. Program片段 (1/2) CPoint 2 D c. Pt 1 = new CPoint 2 D(3, 4); CPoint 2 D c. Pt 2 = new CPoint 2 D( c. Pt 1 ); CPoint 2 D c. Pt 3 = new CPoint 2 D( c. Pt 1 ); Console. Write. Line("c. Pt 1 = ({0}, {1})", c. Pt 1. x, c. Pt 1. y); Console. Write. Line("c. Pt 2 = ({0}, {1})", c. Pt 2. x, c. Pt 2. y); Console. Write. Line("c. Pt 3 = ({0}, {1})", c. Pt 3. x, c. Pt 3. y); 76

Using. Copy. Constructor. Program片段 (2/2) c. Pt 1. x = 10; c. Pt 1. y = 20; c. Pt 2. x = 30; c. Pt 2. y = 40; Console. Write. Line("c. Pt 1. x, c. Pt 1. y); Console. Write. Line("c. Pt 2. x, c. Pt 2. y); Console. Write. Line("c. Pt 3. x, c. Pt 3. y); = ({0}, {1})", 77

Black. Jack_0_0_0. Program using System; using System. Diagnostics; namespace Black. Jack_0_0_0 { class Program { static void Main(string[] args) { Debug. Assert( Black. Jack. Test. Scenario_1_OK()); } } } 81

Black. Jack_0_0_1. Card 片段(1/2) public enum Suit { CLUB = 0, DIAMOND = 1, HEART = 2, SPADE = 3 } 85

Black. Jack_0_0_1. Card 片段(2/2) public struct Card { public Suit suit; public int rank; public Card(Suit suit, int rank) { this. suit = suit; this. rank = rank; } } 86

Testing. Card 2. Program using System; using System. Diagnostics; namespace Testing. Card 2 { class Program { static void Main(string[] args) { Debug. Assert(Card. Test. Spade_A_OK()); } } } 88

Testing. Card 2. Card. Test using System; using Black. Jack_0_0_1; namespace Testing. Card 2 { class Card. Test { public static bool Spade_A_OK() { Card card = new Card(Suit. SPADE, 1); return (card. suit == Suit. SPADE && card. rank == 1); } } } 89

UML 類別圖( Class Diagram ) 92

Scenario 1: Sequence Diagram 93

Is. Black. Jack()結構圖(Structure Chart) Is. Black. Jack() 計算各牌點數 無A時判斷 有A時判斷 Points() 95

Black. Jack_0_0_1. Program using System; using System. Diagnostics; namespace Black. Jack_0_0_1 { class Program { static void Main(string[] args) { Debug. Assert( Black. Jack. Test. Scenario_1_OK()); } } } 97

Black. Jack_0_0_1. Black. Jack. Test片 段 public static bool Scenario_1_OK() { Deck deck = new Deck(); Human. Player player = new Human. Player(); Computer. Player computer = new Computer. Player(); player. Save. ACard(deck. Deal. ACard()); computer. Save. ACard(deck. Deal. ACard()); player. Save. ACard(deck. Deal. ACard()); return (player. Is. Black. Jack()); } 98

Black. Jack_0_0_1. Deck 片段(1/2) private Card card 1; private Card card 2; private Card card 3; private int n. Cards = 0; public Deck() { card 1 = new Card(Suit. SPADE, 1); card 2 = new Card(Suit. HEART, 11); card 3 = new Card(Suit. DIAMOND, 10); n. Cards = 3; } 99

Black. Jack_0_0_1. Deck 片段(2/2) public Card Deal. ACard() { if (n. Cards == 3) { n. Cards--; return card 1; } if (n. Cards == 2) { n. Cards--; return card 2; } if (n. Cards == 1) { n. Cards--; return card 3; } 100

Black. Jack_0_0_1. Human. Player 片段 (1/3) private Card card 1; private Card card 2; private int n. Cards = 0; public void Save. ACard(Card card) { if (n. Cards == 0){ card 1 = card; } if (n. Cards == 1){ card 2 = card; } ++n. Cards; } 101

Black. Jack_0_0_1. Human. Player片段 (2/3) public bool Is. Black. Jack() { int point 1 = Points( card 1. rank ); int point 2 = Points( card 2. rank ); bool is. Black. Jack = (point 1 + point 2 == 21); if (!is. Black. Jack && point 1 == 1) { point 1 = 11; is. Black. Jack = (point 1 + point 2 == 21); } 102

Black. Jack_0_0_1. Human. Player片段 (3/3) if (!is. Black. Jack && point 2 == 1) { point 2 = 11; is. Black. Jack = (point 1 + point 2 == 21); } return is. Black. Jack; } private int Points(int rank) { int points = rank; if (rank > 10) points = 10; return points; } 103

Black. Jack_0_0_1. Computer. Play er Card card 1; public Computer. Player() { } public void Save. ACard(Card card) { card 1 = card; } 104