75 lines
1.6 KiB
Go
75 lines
1.6 KiB
Go
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
|
|
}
|