feat: 添加21点游戏
This commit is contained in:
74
handler/blackjack/core/game.go
Normal file
74
handler/blackjack/core/game.go
Normal file
@@ -0,0 +1,74 @@
|
||||
package core
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"git.lxtend.com/qqbot/handler/blackjack/controller"
|
||||
"git.lxtend.com/qqbot/handler/blackjack/view"
|
||||
)
|
||||
|
||||
type BackJackGame struct {
|
||||
simulator *controller.BackJackSimulator
|
||||
viewer *view.LiteralViewer
|
||||
}
|
||||
|
||||
func NewBlackJackGame(simulator *controller.BackJackSimulator, viewer *view.LiteralViewer) *BackJackGame {
|
||||
return &BackJackGame{
|
||||
simulator: simulator,
|
||||
viewer: viewer,
|
||||
}
|
||||
}
|
||||
|
||||
func (game *BackJackGame) AddCommand(command string) (string, error) {
|
||||
command = strings.Split(command, " ")[0]
|
||||
response := ""
|
||||
err := error(nil)
|
||||
|
||||
switch command {
|
||||
case "start":
|
||||
{
|
||||
game.simulator.Init()
|
||||
response += game.viewer.GetResponse()
|
||||
response += "[🐧] You have started a new Blackjack game.\n"
|
||||
response += "[🐧] Type hit to hit.\n"
|
||||
response += "[🐧] Type stand to stand.\n"
|
||||
response += "[🐧] Type start to restart a new game.\n"
|
||||
response += "[🐧] Type exit to leave the game.\n"
|
||||
}
|
||||
|
||||
case "hit":
|
||||
{
|
||||
result := game.simulator.Hit()
|
||||
response += game.viewer.GetResponse()
|
||||
response += result.Description
|
||||
if result.Code != 200 {
|
||||
err = errors.New("game over")
|
||||
}
|
||||
}
|
||||
|
||||
case "stand":
|
||||
{
|
||||
result := game.simulator.Stand()
|
||||
response += game.viewer.GetResponse()
|
||||
response += "[🐧] You decide to stand !\n"
|
||||
response += result.Description
|
||||
if result.Code != 200 {
|
||||
err = errors.New("game over")
|
||||
}
|
||||
}
|
||||
|
||||
default:
|
||||
{
|
||||
response += "[🐧] Invalid command !\n"
|
||||
err = errors.New("invalid command")
|
||||
}
|
||||
}
|
||||
|
||||
return response, err
|
||||
}
|
||||
|
||||
func (game *BackJackGame) Close() {
|
||||
game.viewer = nil
|
||||
game.simulator = nil
|
||||
}
|
||||
Reference in New Issue
Block a user