Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Major API changes #176

Merged
merged 12 commits into from
Mar 31, 2022
5 changes: 3 additions & 2 deletions cmd/giu-game/tic-tac-go.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ import (
"image/png"
"log"

"github.com/gucio321/tic-tac-go/pkg/game"

"github.com/AllenDang/giu"

"github.com/gucio321/tic-tac-go/pkg/core/players/player"
"github.com/gucio321/tic-tac-go/pkg/giuwidget"
)

Expand All @@ -29,6 +30,6 @@ func main() {
wnd := giu.NewMasterWindow("Tic-Tac-Go", screenX, screenY, 0)
wnd.SetIcon([]image.Image{logo})
wnd.Run(func() {
giu.SingleWindow().Layout(giuwidget.Game(player.PlayerPerson, player.PlayerPC))
giu.SingleWindow().Layout(giuwidget.Game(game.PlayerTypeHuman, game.PlayerTypePC))
})
}
2 changes: 0 additions & 2 deletions internal/terminalgame/game/doc.go

This file was deleted.

2 changes: 2 additions & 0 deletions internal/terminalgame/gameimpl/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package gameimpl contains terminal game implementation
package gameimpl
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package game
package gameimpl

import (
"bufio"
"fmt"
"log"
"os"

"github.com/gucio321/tic-tac-go/pkg/core/board/letter"

"github.com/gucio321/go-clear"
"github.com/gucio321/terminalmenu/pkg/menuutils"

"github.com/gucio321/tic-tac-go/pkg/core/players/player"
"github.com/gucio321/tic-tac-go/pkg/game"
)

Expand All @@ -20,10 +21,10 @@ type TTG struct {
}

// NewTTG creates a new TTG.
func NewTTG(w, h, chainLen byte, player1Type, player2Type player.Type) *TTG {
func NewTTG(w, h, chainLen byte, playerXType, playerOType game.PlayerType) *TTG {
result := &TTG{
reader: bufio.NewReader(os.Stdin),
Game: game.Create(player1Type, player2Type),
Game: game.Create(playerXType, playerOType),
}

result.SetBoardSize(int(w), int(h), int(chainLen)).OnContinue(func() {
Expand All @@ -41,13 +42,13 @@ func NewTTG(w, h, chainLen byte, player1Type, player2Type player.Type) *TTG {
func (t *TTG) Run() {
endGame := make(chan bool, 1)

t.Game.Result(func(p *player.Player) {
t.Game.Result(func(l letter.Letter, name string) {
// handle game end
switch p {
case nil:
switch l {
case letter.LetterNone:
fmt.Println("DRAW")
default:
fmt.Println(p.Name() + " won")
fmt.Println(name + " won")
}

if err := menuutils.PromptEnter("Press ENTER to continue "); err != nil {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package game
package gameimpl

import (
"errors"
Expand Down
17 changes: 9 additions & 8 deletions internal/terminalgame/menu/menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,20 @@ import (
"strings"
"time"

"github.com/gucio321/go-clear"

"github.com/jaytaylor/html2text"
"github.com/pkg/browser"
"github.com/russross/blackfriday"

"github.com/gravestench/osinfo"

"github.com/gucio321/go-clear"
terminalmenu "github.com/gucio321/terminalmenu/pkg"
"github.com/gucio321/terminalmenu/pkg/menuutils"

"github.com/gucio321/tic-tac-go/internal/terminalgame/game"
"github.com/gucio321/tic-tac-go/internal/terminalgame/gameimpl"
"github.com/gucio321/tic-tac-go/pkg/core/board"
"github.com/gucio321/tic-tac-go/pkg/core/players/player"
"github.com/gucio321/tic-tac-go/pkg/game"
)

const githubURL = "https://github.com/gucio321/tic-tac-go"
Expand Down Expand Up @@ -83,28 +84,28 @@ func (m *Menu) Run() {
}

func (m *Menu) runPVP() {
pvp := game.NewTTG(m.width, m.height, m.chainLen, player.PlayerPerson, player.PlayerPerson)
pvp := gameimpl.NewTTG(m.width, m.height, m.chainLen, game.PlayerTypeHuman, game.PlayerTypeHuman)
pvp.Run()
}

func (m *Menu) runPVC() {
var g *game.TTG
var g *gameimpl.TTG

// nolint:gomnd // two players in game
r := rand.Intn(2) // nolint:gosec // it is ok

switch r {
case 0:
g = game.NewTTG(m.width, m.height, m.chainLen, player.PlayerPerson, player.PlayerPC)
g = gameimpl.NewTTG(m.width, m.height, m.chainLen, game.PlayerTypeHuman, game.PlayerTypePC)
case 1:
g = game.NewTTG(m.width, m.height, m.chainLen, player.PlayerPC, player.PlayerPerson)
g = gameimpl.NewTTG(m.width, m.height, m.chainLen, game.PlayerTypePC, game.PlayerTypeHuman)
}

g.Run()
}

func (m *Menu) runDemo() {
demo := game.NewTTG(m.width, m.height, m.chainLen, player.PlayerPC, player.PlayerPC)
demo := gameimpl.NewTTG(m.width, m.height, m.chainLen, game.PlayerTypePC, game.PlayerTypePC)
demo.Run()
}

Expand Down
Loading