Skip to content

Commit 558688c

Browse files
committed
✨ (sudoku-game): create game and board structure
0 parents  commit 558688c

File tree

9 files changed

+219
-0
lines changed

9 files changed

+219
-0
lines changed

.github/workflows/go.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: Go
2+
3+
on:
4+
push:
5+
branches:
6+
- master
7+
pull_request:
8+
branches:
9+
- master
10+
jobs:
11+
build_and_test:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- name: Checkout
15+
uses: actions/checkout@v4
16+
- name: Setup Go
17+
uses: actions/setup-go@v5
18+
with:
19+
go-version: 'stable'
20+
check-latest: true
21+
- name: Setup dependency
22+
run: go mod tidy
23+
# - name: Install Ebitengine dependencies (optional, depending on your project)
24+
# run: |
25+
# sudo apt-get update
26+
# sudo apt-get install -y libgl1-mesa-dev xorg-dev # For Linux builds
27+
# - name: Test Build
28+
# run: make build-game
29+
- name: Test
30+
run: go test -v ./internal/game/...

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# If you prefer the allow list template instead of the deny list, see community template:
2+
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
3+
#
4+
# Binaries for programs and plugins
5+
*.exe
6+
*.exe~
7+
*.dll
8+
*.so
9+
*.dylib
10+
11+
# Test binary, built with `go test -c`
12+
*.test
13+
14+
# Code coverage profiles and other test artifacts
15+
*.out
16+
coverage.*
17+
*.coverprofile
18+
profile.cov
19+
20+
# Dependency directories (remove the comment below to include it)
21+
# vendor/
22+
23+
# Go workspace file
24+
go.work
25+
go.work.sum
26+
27+
# env file
28+
.env
29+
30+
# Editor/IDE
31+
# .idea/
32+
# .vscode/
33+
bin

Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.PHONY=build
2+
3+
build-game:
4+
@CGO_ENABLED=1 GOOS=linux go build -o bin/sudoku-game cmd/main.go
5+
6+
run-game: build-game
7+
@./bin/sudoku-game
8+
9+
10+
coverage:
11+
@go test -v -cover ./internal/game/...
12+
13+
test:
14+
@go test -v ./internal/game/...

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# sudoku game
2+
3+
This is a implementation sudoku game with golang
4+

go.mod

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module github.com/leetcode-golang-classroom/sudoku-game
2+
3+
go 1.24.0
4+
5+
require github.com/stretchr/testify v1.11.1
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
4+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
5+
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
6+
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
7+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
8+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
9+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/game/cell.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package game
2+
3+
// CellType 代表數獨格子的狀態
4+
type CellType int
5+
6+
const (
7+
Empty CellType = iota // 空格
8+
Preset // 題目預設的數字
9+
Input // 玩家輸入的數字
10+
)
11+
12+
// Cell 代表數獨的一個格子
13+
type Cell struct {
14+
Value int // 數字,0 表示空格
15+
Type CellType // 狀態:Empty、Preset、Input
16+
}

internal/game/sudoku.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package game
2+
3+
// Board - 盤面
4+
type Board struct {
5+
Cells [9][9]Cell
6+
}
7+
8+
// NewBoard 建立一個空的數獨盤面
9+
func NewBoard() *Board {
10+
board := &Board{}
11+
for row := 0; row < 9; row++ {
12+
for col := 0; col < 9; col++ {
13+
board.Cells[row][col] = Cell{Value: 0, Type: Empty}
14+
}
15+
}
16+
return board
17+
}
18+
19+
// Game - 遊戲結構
20+
type Game struct {
21+
board *Board
22+
}
23+
24+
// NewGame - 建構遊戲結構
25+
func NewGame() *Game {
26+
return &Game{
27+
board: NewBoard(),
28+
}
29+
}

internal/game/sudoku_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package game
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestNewGame(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
wantBoard *Board
13+
}{
14+
{
15+
name: "Empty Board",
16+
wantBoard: &Board{
17+
Cells: [9][9]Cell{
18+
{
19+
{0, Empty}, {0, Empty}, {0, Empty},
20+
{0, Empty}, {0, Empty}, {0, Empty},
21+
{0, Empty}, {0, Empty}, {0, Empty},
22+
},
23+
{
24+
{0, Empty}, {0, Empty}, {0, Empty},
25+
{0, Empty}, {0, Empty}, {0, Empty},
26+
{0, Empty}, {0, Empty}, {0, Empty},
27+
},
28+
{
29+
{0, Empty}, {0, Empty}, {0, Empty},
30+
{0, Empty}, {0, Empty}, {0, Empty},
31+
{0, Empty}, {0, Empty}, {0, Empty},
32+
},
33+
{
34+
{0, Empty}, {0, Empty}, {0, Empty},
35+
{0, Empty}, {0, Empty}, {0, Empty},
36+
{0, Empty}, {0, Empty}, {0, Empty},
37+
},
38+
{
39+
{0, Empty}, {0, Empty}, {0, Empty},
40+
{0, Empty}, {0, Empty}, {0, Empty},
41+
{0, Empty}, {0, Empty}, {0, Empty},
42+
},
43+
{
44+
{0, Empty}, {0, Empty}, {0, Empty},
45+
{0, Empty}, {0, Empty}, {0, Empty},
46+
{0, Empty}, {0, Empty}, {0, Empty},
47+
},
48+
{
49+
{0, Empty}, {0, Empty}, {0, Empty},
50+
{0, Empty}, {0, Empty}, {0, Empty},
51+
{0, Empty}, {0, Empty}, {0, Empty},
52+
},
53+
{
54+
{0, Empty}, {0, Empty}, {0, Empty},
55+
{0, Empty}, {0, Empty}, {0, Empty},
56+
{0, Empty}, {0, Empty}, {0, Empty},
57+
},
58+
{
59+
{0, Empty}, {0, Empty}, {0, Empty},
60+
{0, Empty}, {0, Empty}, {0, Empty},
61+
{0, Empty}, {0, Empty}, {0, Empty},
62+
},
63+
},
64+
},
65+
},
66+
}
67+
for _, tt := range tests {
68+
t.Run(tt.name, func(t *testing.T) {
69+
game := NewGame()
70+
assert.Equal(t, tt.wantBoard.Cells, game.board.Cells)
71+
})
72+
}
73+
}

0 commit comments

Comments
 (0)