initiate commit

This commit is contained in:
2020-03-03 20:36:40 +08:00
parent 626229e6f1
commit 959fca4045
8 changed files with 138 additions and 4 deletions
+13
View File
@@ -0,0 +1,13 @@
package game
import "github.com/c-my/MahjongServer/model"
type Manager interface {
Start()
GenerateTiles() []model.Tile
GetFirstPlayer()
CanChow(tiles []model.Tile, newTile model.Tile) bool
CanPong(tiles []model.Tile, newTile model.Tile) bool
CanKong(tiles []model.Tile, newTile model.Tile) bool
CanWin(tiles []model.Tile, newTile model.Tile) bool
}
+5
View File
@@ -0,0 +1,5 @@
module github.com/c-my/MahjongServer
go 1.14
require github.com/gorilla/websocket v1.4.1
+2
View File
@@ -0,0 +1,2 @@
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
+26 -2
View File
@@ -1,7 +1,31 @@
package main
import "fmt"
import (
"github.com/gorilla/websocket"
"log"
"net/http"
)
var upgrader = websocket.Upgrader{
ReadBufferSize: 1024,
WriteBufferSize: 1024,
CheckOrigin: func(r *http.Request) bool {
return true
},
}
var room *Room
func handler(w http.ResponseWriter, r *http.Request) {
log.Println("connection received")
conn, _ := upgrader.Upgrade(w, r, nil)
room.AddConn(conn)
}
func main() {
fmt.Println("Hello,world")
room = NewRoom()
room.Start()
http.HandleFunc("/", handler)
http.ListenAndServe("127.0.0.1:1114", nil)
}
+6
View File
@@ -0,0 +1,6 @@
package model
//type GameMessage struct {
// Action string `json:"action"`
//}
type GameMessage string
+25
View File
@@ -0,0 +1,25 @@
package model
import "sort"
type PlayerTile struct {
handTiles []Tile
showTiles []Tile
}
func (t *PlayerTile) SetHandTiles(tiles []Tile) {
t.handTiles = tiles
t.SortHand()
}
func (t *PlayerTile) GetHandTiles() []Tile {
return t.handTiles
}
func (t *PlayerTile) SortHand() {
sort.Slice(t.handTiles, func(i, j int) bool {
iValue := int(t.handTiles[i].Suit)*10 + t.handTiles[i].Number
jValue := int(t.handTiles[j].Suit)*10 + t.handTiles[j].Number
return iValue < jValue
})
}
+5 -2
View File
@@ -1,4 +1,4 @@
package Model
package model
type Suit int
@@ -6,7 +6,6 @@ const (
Character Suit = iota
Bamboo
Dot
Bonus
Wind
Dragon
Flower
@@ -17,3 +16,7 @@ type Tile struct {
Suit Suit `json:"suit"`
Number int `json:"number"`
}
func NewTile(suit Suit, number int) *Tile {
return &Tile{Suit: suit, Number: number}
}
+56
View File
@@ -0,0 +1,56 @@
package model
import (
"math/rand"
"time"
)
type Wall struct {
tiles []Tile
front int
back int
generator func() []Tile
}
func NewWall(generator func() []Tile) *Wall {
t := generator()
wall := &Wall{tiles: t, front: 0, back: len(t) - 1, generator: generator}
wall.Shuffle()
return wall
}
func (w *Wall) FrontDraw() Tile {
if w.back < w.front {
panic("front-draw from empty wall")
}
tile := w.tiles[w.front]
w.front++
return tile
}
func (w *Wall) backDraw() Tile {
if w.back < w.front {
panic("back-draw from empty wall")
}
tile := w.tiles[w.back]
w.back--
return tile
}
func (w *Wall) ReShuffle() {
w.tiles = w.generator()
w.front = 0
w.back = len(w.tiles) - 1
w.Shuffle()
}
func (w *Wall) Length() int {
return w.back - w.front + 1
}
func (w *Wall) Shuffle() {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(w.tiles), func(i, j int) {
w.tiles[i], w.tiles[j] = w.tiles[j], w.tiles[i]
})
}