伊莉討論區
標題:
chat gpt 做21點基本策略判定程式
[打印本頁]
作者:
st970049
時間:
2023-5-24 11:13 PM
標題:
chat gpt 做21點基本策略判定程式
本帖最後由 st970049 於 2023-5-24 11:14 PM 編輯
看影片講到21點基本策略表就想來做一個可以自動判斷獨立運行的程式
原本想說看影片學可是或許是因為沒有概念所以完全聽不懂一頭霧水
所以我就半放棄跑去求助chat gpt
這是我摸了一個晚上的結果想說先曬一下
[attach]138679062[/attach]
[attach]138679063[/attach]
目前建議只會給要牌跟停牌
分牌跟加倍在塞進去的過程總是一直出錯
然後目前程式碼長這樣
import
random
# 定義手牌的點數值
card_values
=
{
'A'
: [
1
,
11
],
'2'
:
2
,
'3'
:
3
,
'4'
:
4
,
'5'
:
5
,
'6'
:
6
,
'7'
:
7
,
'8'
:
8
,
'9'
:
9
,
'10'
:
10
,
'J'
:
10
,
'Q'
:
10
,
'K'
:
10
}
def
calculate_hand_value
(
hand
):
"""計算手牌的點數值"""
value
=
0
aces
=
0
for
card
in
hand
:
if
card
==
'A'
:
aces
+=
1
else
:
value
+=
card_values
[
card
]
for
_
in
range
(
aces
):
if
value
+
card_values
[
'A'
][
1
]
<=
21
:
value
+=
card_values
[
'A'
][
1
]
else
:
value
+=
card_values
[
'A'
][
0
]
return
value
def
get_action_description
(
player_hand
,
dealer_card
):
"""根據手牌和莊家的明牌返回動作描述"""
player_value
=
calculate_hand_value
(
player_hand
)
if
len
(
player_hand
)
==
2
and
'A'
in
player_hand
:
if
'2'
<=
dealer_card
<=
'6'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
if
player_value
>=
17
:
return
"STAND"
# 停牌
if
player_value
<=
8
:
return
"HIT"
# 加牌
if
player_value
==
9
:
if
'2'
<=
dealer_card
<=
'6'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
if
player_value
==
10
:
if
'2'
<=
dealer_card
<=
'9'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
if
player_value
==
11
:
if
'2'
<=
dealer_card
<=
'10'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
if
player_value
==
12
:
if
'4'
<=
dealer_card
<=
'6'
:
return
"STAND"
# 停牌
else
:
return
"HIT"
# 加牌
if
13
<=
player_value
<=
16
:
if
'2'
<=
dealer_card
<=
'6'
:
return
"STAND"
# 停牌
else
:
return
"HIT"
# 加牌
if
'A'
in
player_hand
:
if
player_value
==
13
or
player_value
==
14
:
if
'5'
<=
dealer_card
<=
'6'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
elif
player_value
==
15
or
player_value
==
16
:
if
'4'
<=
dealer_card
<=
'6'
:
return
"DOUBLE"
# 賭倍
else
:
return
"HIT"
# 加牌
elif
player_value
==
17
:
if
'2'
<=
dealer_card
<=
'6'
:
return
"STAND"
# 停牌
elif
dealer_card
in
[
'7'
,
'8'
]:
return
"STAND"
# 停牌
else
:
return
"HIT"
# 加牌
elif
player_value
>=
18
:
return
"STAND"
# 停牌
if
player_hand
[
0
]
==
player_hand
[
1
]:
if
player_hand
[
0
]
==
'2'
:
if
'2'
<=
dealer_card
<=
'7'
:
return
"SPLIT"
# 分牌
else
:
return
"HIT"
# 加牌
elif
player_hand
[
0
]
==
'3'
:
if
'2'
<=
dealer_card
<=
'7'
:
return
"SPLIT"
# 分牌
else
:
return
"HIT"
# 加牌
elif
player_hand
[
0
]
==
'4'
:
if
dealer_card
==
'5'
or
dealer_card
==
'6'
:
return
"SPLIT"
# 分牌
else
:
return
"HIT"
# 加牌
elif
player_hand
[
0
]
==
'6'
:
if
'2'
<=
dealer_card
<=
'6'
:
return
"SPLIT"
# 分牌
else
:
return
"HIT"
# 加牌
elif
player_hand
[
0
]
==
'7'
:
if
'2'
<=
dealer_card
<=
'7'
:
return
"SPLIT"
# 分牌
else
:
return
"HIT"
# 加牌
elif
player_hand
[
0
]
==
'8'
:
return
"SPLIT"
# 分牌
elif
player_hand
[
0
]
==
'9'
:
if
dealer_card
==
'7'
:
return
"STAND"
# 停牌
else
:
return
"SPLIT"
# 分牌
elif
player_hand
[
0
]
==
'A'
:
return
"SPLIT"
# 分牌
return
"STAND"
# 停牌
def
main
():
print
(
"歡迎使用21點基本策略表程式!"
)
while
True
:
player_hand
=
input
(
"請輸入您的手牌(以空格分隔):"
).
split
()
dealer_card
=
input
(
"請輸入莊家的明牌:"
)
action_description
=
get_action_description
(
player_hand
,
dealer_card
)
print
(
"您應該"
,
action_description
)
continue_playing
=
input
(
"是否繼續遊戲?(輸入 'y' 繼續,其他鍵退出):"
)
if
continue_playing
.
lower
()
!=
'y'
:
break
if
__name__
==
'__main__'
:
main
()
作者:
dvds500
時間:
2023-6-5 04:34 PM
請問你是用哪一種語言寫的?你是想寫APP還是純粹好奇呢?
歡迎光臨 伊莉討論區 (http://a04.eyny.com/)
Powered by Discuz!