decision matrix in C#
-
While playing around with a Black Jack program, I got wondering about the best way to do a decision matrix in C#. What I am thinking of is basically a 2 dimensional matrix with each row/col combination containing a value representing 1 of a number of different options (e.g. Hit, Stand, Double, Split, etc). Is a 2 dimensional array the best way to implement it, or is there a better method? Any opinions?
David Wilkes
-
While playing around with a Black Jack program, I got wondering about the best way to do a decision matrix in C#. What I am thinking of is basically a 2 dimensional matrix with each row/col combination containing a value representing 1 of a number of different options (e.g. Hit, Stand, Double, Split, etc). Is a 2 dimensional array the best way to implement it, or is there a better method? Any opinions?
David Wilkes
amatbrewer wrote:
While playing around with a Black Jack program, I got wondering about the best way to do a decision matrix in C#. What I am thinking of is basically a 2 dimensional matrix with each row/col combination containing a value representing 1 of a number of different options (e.g. Hit, Stand, Double, Split, etc). Is a 2 dimensional array the best way to implement it, or is there a better method?
That seems pretty elegant to me, but what you're proposing is only good for evaluating the 1st move of the hand. If one axis represents what the dealer is showing, and the other axis represents what you have showing, it's a very fast lookup of what to do. After the first move, you either need to code a more traditional
if {} else {}
type code construct, or have another decision matrix, since you don't have options like splitting and doubling-down available to you after the first decision... The pure decision matrix approach scores higher for me than theif {} else {}
construct for your purpose because: 1) Once the array is initialized, evaluating decisions is very, very fast 2) If you structure your code properly, your intentions are perfectly clear. You cannot mess up the logic. Pseudo-code:enum Action {
Hit = 0,
Stand = 1,
DD = 2,
Split = 3
}
private static Action[][] _decision = new Action[][]
// Dealer showing -> 2 3 4 5 6 ...
// You showing V
{ new Action[] */ 2 */ { Hit, Hit, Stand, DD, Hit, ... },
new Action[] /* 3 */ { Hit, Hit, Stand, Hit, DD, ... },
.
.
.
new Action[] /*21 */ { Stand, Stand, Stand, Stand, ...}
}
private static Action Decision(Int32 dealerShow, Int32 meShow) {
return _decision[meShow - 2][dealerShow - 2];
}Share and enjoy. Sean
-
amatbrewer wrote:
While playing around with a Black Jack program, I got wondering about the best way to do a decision matrix in C#. What I am thinking of is basically a 2 dimensional matrix with each row/col combination containing a value representing 1 of a number of different options (e.g. Hit, Stand, Double, Split, etc). Is a 2 dimensional array the best way to implement it, or is there a better method?
That seems pretty elegant to me, but what you're proposing is only good for evaluating the 1st move of the hand. If one axis represents what the dealer is showing, and the other axis represents what you have showing, it's a very fast lookup of what to do. After the first move, you either need to code a more traditional
if {} else {}
type code construct, or have another decision matrix, since you don't have options like splitting and doubling-down available to you after the first decision... The pure decision matrix approach scores higher for me than theif {} else {}
construct for your purpose because: 1) Once the array is initialized, evaluating decisions is very, very fast 2) If you structure your code properly, your intentions are perfectly clear. You cannot mess up the logic. Pseudo-code:enum Action {
Hit = 0,
Stand = 1,
DD = 2,
Split = 3
}
private static Action[][] _decision = new Action[][]
// Dealer showing -> 2 3 4 5 6 ...
// You showing V
{ new Action[] */ 2 */ { Hit, Hit, Stand, DD, Hit, ... },
new Action[] /* 3 */ { Hit, Hit, Stand, Hit, DD, ... },
.
.
.
new Action[] /*21 */ { Stand, Stand, Stand, Stand, ...}
}
private static Action Decision(Int32 dealerShow, Int32 meShow) {
return _decision[meShow - 2][dealerShow - 2];
}Share and enjoy. Sean
Thanks for the reply! Blackjack got me thinking about it, but I can think of a number of projects (current and proposed) that this could be useful for. I appreciate your example; it is much more elegant then what I was originally thinking of. Not sure I understand the syntax of “Action[][] _decision” and “private static Action Decision(Int32 dealerShow, Int32 meShow)” but it gives me something to research. Thanks again!
David Wilkes
-
Thanks for the reply! Blackjack got me thinking about it, but I can think of a number of projects (current and proposed) that this could be useful for. I appreciate your example; it is much more elegant then what I was originally thinking of. Not sure I understand the syntax of “Action[][] _decision” and “private static Action Decision(Int32 dealerShow, Int32 meShow)” but it gives me something to research. Thanks again!
David Wilkes
amatbrewer wrote:
Thanks for the reply!
No prob. Evaluating card hands with multi-dimensional arrays[^] is sort of a hobby of mine. :) Share and enjoy. Sean