Dice 3 Dice Simulation Program Dice dice new

  • Slides: 105
Download presentation

類別Dice 3

類別Dice 3

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

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

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

程序( Procedure )呼叫 *J. G. Brookshear, Computer Science – An Overview, 8 th edition,

程序( Procedure )呼叫 *J. G. Brookshear, Computer Science – An Overview, 8 th edition, Addison-Wesley, 2005 7

函式呼叫流程 static void Main(string[] args) { public class Dice {. . . dice. Toss();

函式呼叫流程 static void Main(string[] args) { public class Dice {. . . dice. Toss(); public void Toss() { } face. Value = rand. Next() % 6 + 1; }. . . } public class Random {. . . public int Next() {. . . } 8

資料型別 • 實值型別(Value Type) – 結構(Structs) • • • 數值(Numeric) 布林(bool) 使用者定義Structs – 列舉(Enumerations)

資料型別 • 實值型別(Value Type) – 結構(Structs) • • • 數值(Numeric) 布林(bool) 使用者定義Structs – 列舉(Enumerations) • 參考型別(Reference Type) – 字串(string) – 物件(object) 9

堆疊(Stack)與堆積(Heap) Heap . . . Stack 10

堆疊(Stack)與堆積(Heap) Heap . . . Stack 10

物件(Object)觀念 dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2. face. Value

物件(Object)觀念 dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2. face. Value 記憶體地址2 dice 2. rand 函式Toss進入地址 函式成員Toss進入點 13

物件的設定(Assignment) dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2 記憶體地址2 dice

物件的設定(Assignment) dice 1. face. Value 記憶體地址1 dice 1. rand 函式Toss進入地址 dice 2 記憶體地址2 dice 2 = dice 1; dice 2. face. Value dice 2. rand 函式Toss進入地址 函式成員Toss進入點 14

存取修飾詞 • • public private protected internal 15

存取修飾詞 • • public private protected internal 15

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

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

類別 Card 23

類別 Card 23

程式Testing. Card. Program. cs using System; using System. Diagnostics; namespace Testing. Card { class

程式Testing. Card. Program. cs using System; using System. Diagnostics; namespace Testing. Card { class Program { static void Main(string[] args) { Card. Test test = new Card. Test(); Debug. Assert(test. Spade_A_OK()); } } } 25

程式Testing. Card. Test. cs using System; namespace Testing. Card { class Card. Test {

程式Testing. Card. Test. cs using System; namespace Testing. Card { class Card. Test { public bool Spade_A_OK() { Card card = new Card(); card. Suit = 's'; card. Rank = 1; return (card. Suit == 's' && card. Rank == 1); } } } 26

程式Testing. Card. cs using System; namespace Testing. Card { public class Card { private

程式Testing. Card. cs using System; namespace Testing. Card { public class Card { private char suit; private int rank; public char Suit { get { return suit; } set { suit = value; } } public int Rank { get { return rank; } set { rank = value; } } 27

Calculator. Test. Program片段 Calculator calculator = new Calculator(); Console. Write("輸入第一個數字: "); operand 1 =

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)

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

數學函數呼叫流程 double y = Math. Sqrt( 2. 0 ); class { Math static double

數學函數呼叫流程 double y = Math. Sqrt( 2. 0 ); class { Math static double Sqrt( double x ) {. . . return result; } } 33

函式傳回值 Program. Main() result calculator. Add() result return value 35

函式傳回值 Program. Main() result calculator. Add() result return value 35

練習 • 設定operand值並利用Assert敘述改寫 Calculator. Test程式 36

練習 • 設定operand值並利用Assert敘述改寫 Calculator. Test程式 36

Swapping. Integers. Program 片段 int x = 3; int y = 5; Swap(ref x,

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

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.

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

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

練習(2/3) • 函式Swap虛擬碼 Procedure Swap( card 1, card 2 ) 1. temp = card

練習(2/3) • 函式Swap虛擬碼 Procedure Swap( card 1, card 2 ) 1. temp = card 1; 2. card 1 = card 2; card 1 3. card 2 = temp; temp return card 2 43

Overloading. Demo. Program 片段 Adder adder = new Adder(); int a = 3; int

Overloading. Demo. Program 片段 Adder adder = new Adder(); int a = 3; int b = 5; Debug. Assert(adder. Add(a, b) == 8); double ad = 3. 2; double bd = 5. 1; Debug. Assert(adder. Add(ad, bd) == 8. 3); 45

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

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. 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. Simulation 2. Dice片段 public Dice() { rand = new Random(); Toss(); } public Dice(int seed) { rand = new Random(seed); Toss(); } 48

物件產生與消滅流程 static void main( string[] arg ) { Dice dice = new Dice( seed

物件產生與消滅流程 static void main( string[] arg ) { Dice dice = new Dice( seed ); 物件宣告 public Dice(int seed) {. . . } 物件生成 ~Dice() {. . . } } 刪除物件dice 離開主函式, 程式結束 50

Using. This. Program 片段 Time t = new Time(11, 30, 52); int hour; int

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.

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

類別Time. Conversion 55

類別Time. Conversion 55

Using. Static. Program片段 int hours. To. Mins = Time. Conversion. Hours. To. Mins(hours); int

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

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;

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

練習 (2/2) • 實作並測試一靜態類別Eq. Solver,內含兩 靜態成員函式double Linear(double a, double b)及double Quadratic(double a, double b,

練習 (2/2) • 實作並測試一靜態類別Eq. Solver,內含兩 靜態成員函式double Linear(double a, double b)及double Quadratic(double a, double b, double c)分別解一次方程式a x + b = 0及 a x 2 + b x + c = 0 62

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

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

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;

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;

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

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

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

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

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

類別物件記憶配置 c. Pt 1. x 記憶體地址1 c. Pt 1. y c. Pt 2 記憶體地址2

類別物件記憶配置 c. Pt 1. x 記憶體地址1 c. Pt 1. y c. Pt 2 記憶體地址2 c. Pt 2 = c. Pt 1; c. Pt 2. x c. Pt 2. y heap space 72

結構物件記憶配置 s. Pt 1. x s. Pt 1. y s. Pt 2. x s.

結構物件記憶配置 s. Pt 1. x s. Pt 1. y s. Pt 2. x s. Pt 2. y stack 73

Using. Copy. Constructor. CPoint 2 D片段 public CPoint 2 D() { x = 0;

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

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.

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

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_0. Black. Jack. Test using System; namespace Black. Jack_0_0_0 { class Black.

混充類別 Black. Jack_0_0_0. Black. Jack. Test using System; namespace Black. Jack_0_0_0 { class Black. Jack. Test { public static bool Scenario_1_OK() { return true; } } } 82

二十一點遊戲模擬原型方案規畫 • 方案:Black. Jack_0_0_1 • 專案:Black. Jack_0_0_1 – Program, Black. Jack. Test, Deck, Human.

二十一點遊戲模擬原型方案規畫 • 方案:Black. Jack_0_0_1 • 專案:Black. Jack_0_0_1 – Program, Black. Jack. Test, Deck, Human. Player, Computer. Player, Hand, Card • 專案:Testing. Card 2 – Program, Card. Test 84

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

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;

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 {

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

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

UML 類別圖( Class Diagram ) 92

Scenario 1: Sequence Diagram 93

Scenario 1: Sequence Diagram 93

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

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

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 =

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

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)

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;

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

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)

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

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