Mahjong tiles abstract and encode Mahjong tiles

Mahjong game business in one sentence:

  • Three-player mode <Fannie and Freddie> onlyarticleThe child andcylinderThe two suits are the same72CARDS.
  • Four-player mode <To the last> usecylinder,article,wanThe three suits are total108CARDS.

series

  1. Golang develops production grade Mahjong game server – Part 1
  2. Golang develops production grade Mahjong game server – Part 2
  3. Golang development production grade mahjong game server – chapter 3
  4. Golang development production grade Mahjong game server – chapter 4
  5. Golang development production grade Mahjong game server – chapter 5
  6. Golang development production grade Mahjong game server – chapter 6
  7. Golang development production grade Mahjong game server – chapter 7

introduce

This will be a complete, fully implemented Golang game server development tutorial series for DevOps/GitOps and cloud processes on Kubernetes.

This series of tutorials is a complete teardown of the open source Nanoserver project, designed to help you get started on the Golang server backend. Understand the essence of Golang development through practice — Share memory by communication.

The project may also involve work on Linux performance tuning (BPF related tools) and system assurance (SRE).

Step By Step development Mahjong Server

  • Monomer architectureunderstandMahjong ServerBusiness – >Nano Distributed Game Server + Micro serviceTransformation.
  • Demo: go – mahjong – server

Tiles are abstract

Define Tile struct

type Tile struct {
	Id    int
	Suit  int
	Rank  int
	Index int
}
Copy the code
  • Id:108CARDS, use0 ~ 107Identify each card.
  • Suit: Three colors, with0 ~ 2Identify each suit (0:article, 1:cylinder, 2:wan).
  • Rank:9Seed points, with1 ~ 9Signs (e.g. : 1, 9, 1 cylinder, 9 cylinder, 10,000, 90,000, etc.) .
  • Index: index (bar:1 ~ 9, drum:11 ~ 19M:21 to 29).

Look at a table

Three suits and the Index of each type of card:

Strips (1 ~ 9) Cylinder (11 ~ 19) Wan (21 ~ 29)
Article 1 1. 11 1 barrel 21 10000
Article 2 2 12 2 cylinder 22 20000
Article 3 3 13 3 cylinder 23, 30000
Article 4 4 14 4 cylinder 24 40000
Article 5 5 15 5 barrel 25, 50000
Article 6 6. 16 6 cylinder 26, 60000
Article 7 7 17 7 barrel 27, 70000
Article 8 8. 18 8 cylinder 28 80000
Article 9 9 19 9 cylinder 29, 90000

ID numbers of all cards (here 108) :

Bar (0 ~ 35) Cylinder (36 ~ 71) Ten thousand (72 ~ 107)
0 1 36 1 barrel 72, 10000,
Article 1 1. 37 1 barrel 73, 10000,
Article 2 1 38 1 barrel 74, 10000,
Article 3 1 39 1 barrel 75, 10000,
Article 4 2 40 2 cylinder 76, 20000,
Article 5 2 41 2 cylinder 77, 20000,
Article 6 2 42 2 cylinder 78, 20000,
Article 7 2 43 2 cylinder 79, 20000,
Article 8 3 44 3 cylinder 80, 30000,
Article 9 3 45 3 cylinder 81, 30000,
Article 10 3 46 3 cylinder 82, 30000,
Article 11 3. 47 3 cylinder 83, 30000,
Article 12 4 48 4 cylinder 84, 40000,
Article 13 4 49 4 cylinder 85, 40000,
Article 14 4 Fifty four cylinder 86, 40000,
Article 15 April 51 4 cylinder 87, 40000,
Article 16 5 52 5 barrel 88, 50000,
Article 17. 5 53 5 barrel 89, 50000,
Article 18 5 54 5 barrel 90, 50000,
Article 19 5 55 5 barrel 91, 50000,
Article 20 6 56 6 cylinder 92, 60000,
Article 21 June 57 6 cylinder 93, 60000,
Article 22 June 58 6 cylinder 94, 60000,
Article 23 June 59 6 cylinder 95, 60000,
Article 24 7 60 7 barrel 96, 70000,
Article 25 July 61 7 barrel 97, 70000,
Article 26 July 62 7 barrel 98, 70000,
Article 27 July 63 7 barrel 99, 70000,
Article 28 eight 64 8 cylinder 100, 80000,
Article 29 August 65 8 cylinder 101, 80000,
Article 30 8 66 8 cylinder 102, 80000,
Article 31 August 67 8 cylinder 103, 80000,
Article 32 9 68 9 cylinder 104, 90000,
Article 33 9 69 9 cylinder 105, 90000,
Article 34 9 70 9 cylinder 106, 90000,
Article 35 9 71 9 cylinder 107, 90000,

Coding of actual combat

Get Tile by ID

An arithmetic problem. There’s nothing to say.

  • Three suits, each of them9Class different cards, and each class has4The same card.

internal/game/tile.go

func TileFromID(id int) *Tile {
	if id < 0 {
		panic("illegal tile id")}var (
		tmp = id / 4
		h   = tmp / 9
		v   = tmp%9 + 1
		i   = h*10 + v
	)
	return &Tile{Suit: h, Rank: v, Index: i, Id: id}
}
Copy the code

Write unit test functions

Right-click on the TileFromID Function and select Generate Unit Tests For Function, For which we write a Unit test Function.

internal/game/tile_test.go

func TestTileFromID(t *testing.T) {
	type args struct {
		id int
	}
	tests := []struct {
		name string
		args args
		want *Tile
	}{
    // Define a bunch of use cases
		{"1", args{id: 0}, &Tile{Suit: 0, Rank: 1, Index: 1, Id: 0}},
		{"1", args{id: 1}, &Tile{Suit: 0, Rank: 1, Index: 1, Id: 1}},
		{"1", args{id: 2}, &Tile{Suit: 0, Rank: 1, Index: 1, Id: 2}},
		{"1", args{id: 3}, &Tile{Suit: 0, Rank: 1, Index: 1, Id: 3}},
		{"Nine", args{id: 35}, &Tile{Suit: 0, Rank: 9, Index: 9, Id: 35}},
		{"1 barrel", args{id: 36}, &Tile{Suit: 1, Rank: 1, Index: 11, Id: 36}},
		{"1 barrel", args{id: 37}, &Tile{Suit: 1, Rank: 1, Index: 11, Id: 37}},
		{"1 barrel", args{id: 38}, &Tile{Suit: 1, Rank: 1, Index: 11, Id: 38}},
		{"1 barrel", args{id: 39}, &Tile{Suit: 1, Rank: 1, Index: 11, Id: 39}},
		{"9 cone", args{id: 71}, &Tile{Suit: 1, Rank: 9, Index: 19, Id: 71}},
		{"10000", args{id: 72}, &Tile{Suit: 2, Rank: 1, Index: 21, Id: 72}},
		{"10000", args{id: 73}, &Tile{Suit: 2, Rank: 1, Index: 21, Id: 73}},
		{"10000", args{id: 74}, &Tile{Suit: 2, Rank: 1, Index: 21, Id: 74}},
		{"10000", args{id: 75}, &Tile{Suit: 2, Rank: 1, Index: 21, Id: 75}},
		{"90000", args{id: 107}, &Tile{Suit: 2, Rank: 9, Index: 29, Id: 107}}},for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			ifgot := TileFromID(tt.args.id); ! reflect.DeepEqual(got, tt.want) { t.Errorf("TileFromID() = %v, want %v", got, tt.want)
			}
		})
	}
}
Copy the code

Perform:

cd internal/game/mahjong 
go test -v tile_test.go tile.go mahjong.go
Copy the code

I am weishao wechat: uuhells123 public number: hackers afternoon tea add my wechat (mutual learning exchange), pay attention to the public number (for more learning materials ~)Copy the code