From e8d5411e01d38a3e6c1c6bd0fbe618f3daacd4c8 Mon Sep 17 00:00:00 2001 From: Tamas Levai Date: Wed, 22 Oct 2025 11:11:24 +0200 Subject: [PATCH] feat: Add 19-structs/04-multidim-chess MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A deepcopy exercise. Co-authored-by: Bertalan Kovács --- 19-structs/04-multidim-chess/.README.md | 43 +++++++++ .../04-multidim-chess/.exercise_test.go | 95 +++++++++++++++++++ 19-structs/04-multidim-chess/exercise.go | 20 ++++ 19-structs/04-multidim-chess/exercise.yaml | 5 + 19-structs/04-multidim-chess/exercise_test.go | 1 + 5 files changed, 164 insertions(+) create mode 100644 19-structs/04-multidim-chess/.README.md create mode 100644 19-structs/04-multidim-chess/.exercise_test.go create mode 100644 19-structs/04-multidim-chess/exercise.go create mode 100644 19-structs/04-multidim-chess/exercise.yaml create mode 100644 19-structs/04-multidim-chess/exercise_test.go diff --git a/19-structs/04-multidim-chess/.README.md b/19-structs/04-multidim-chess/.README.md new file mode 100644 index 0000000..1d73ae8 --- /dev/null +++ b/19-structs/04-multidim-chess/.README.md @@ -0,0 +1,43 @@ +# Multi-dimensional Chess +{{"\n" -}} +{{- if eq (index . "name") "32-dimensional" -}} +In Hungary the people usually play 2-dimensional chess, however in Zedonia the people enjoy playing 32-dimensional chess. Implement the neccessary `copyPosition`, `copyPiece` and `copyBoard` functions that convert a Hungarian chess `Board` to a Zedonian chess `Board` by copying the first `2` coordinates and initializing the remaining coordinates to `0`. Make sure that if the Zedonians would like to play in higher dimensions, they are able to do that. +{{- end -}} +{{- if eq (index . "name") "2-dimensional" -}} +In Zedonia the people usually play N-dimensional chess, however in Hungary the people are only capable of playing 2-dimensional chess. Implement the neccessary `copyPosition`, `copyPiece` and `copyBoard` functions that convert a Zedonian chess `Board` to a Hungarian chess `Board` by copying the first `2` coordinates and omitting the remaining ones. Make sure that if the Hungarians develop a method to play in higher dimensions, they are able to do that. +{{- end -}} +{{- if eq (index . "name") "N-dimensional" -}} +In Zedonia the people usually play N-dimensional chess, but this games takes an eternety to finish. The Zedonians developed a method to play their games in parallel to save some time. Implement the neccessary `copyPosition`, `copyPiece` and `copyBoard` functions that copy a Zedonian chess `Board` to allow them to play a game in parallel. +{{- end -}} +{{- "\n"}} +You are given the following structs: + +```go +type Position struct { + Coordinates []int +} + +type Piece struct { + Type string // e.g. Queen, King, Bishop, etc. + Color string // White or Black + Position Position +} + +type Board struct { + Pieces []Piece +} +``` + +Your task is to implement the following functions that perform the expected behavior: + +``` go +func copyPosition(p Position) Position +``` +``` go +func copyPiece(p Piece) Piece +``` +``` go +func copyBoard(b Board) Board +``` + +Insert the code into the file `exercise.go` at the placeholder `// INSERT YOUR CODE HERE`. diff --git a/19-structs/04-multidim-chess/.exercise_test.go b/19-structs/04-multidim-chess/.exercise_test.go new file mode 100644 index 0000000..c59e58a --- /dev/null +++ b/19-structs/04-multidim-chess/.exercise_test.go @@ -0,0 +1,95 @@ +package multidimchess + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestDeepCopyFunctions(t *testing.T) { + {{if eq (index . "name") "32-dimensional" -}} + origPos := Position{Coordinates: []int{3, 5}} + newPos := copyPosition(origPos) + assert.Equal(t, 32, len(newPos.Coordinates)) + assert.Equal(t, []int{3, 5}, newPos.Coordinates[:2]) + for _, v := range newPos.Coordinates[2:] { + assert.Equal(t, 0, v) + } + newPos.Coordinates[0] = 99 + assert.NotEqual(t, origPos.Coordinates[0], newPos.Coordinates[0]) + + p := Piece{Type: "Queen", Color: "White", Position: origPos} + cp := copyPiece(p) + assert.Equal(t, p.Type, cp.Type) + assert.Equal(t, p.Color, cp.Color) + cp.Position.Coordinates[0] = 99 + assert.NotEqual(t, p.Position.Coordinates[0], cp.Position.Coordinates[0]) + + board := Board{ + Pieces: []Piece{ + {Type: "King", Color: "Black", Position: Position{Coordinates: []int{0, 0}}}, + {Type: "Pawn", Color: "White", Position: Position{Coordinates: []int{1, 2}}}, + }, + } + cb := copyBoard(board) + assert.Equal(t, len(board.Pieces), len(cb.Pieces)) + cb.Pieces[0].Position.Coordinates[0] = 99 + assert.NotEqual(t, board.Pieces[0].Position.Coordinates[0], cb.Pieces[0].Position.Coordinates[0]) + {{- end -}} + + {{if eq (index . "name") "2-dimensional" -}} + origPos := Position{Coordinates: []int{1, 2, 3, 4, 5}} + newPos := copyPosition(origPos) + assert.Equal(t, 2, len(newPos.Coordinates)) + assert.Equal(t, []int{1, 2}, newPos.Coordinates) + newPos.Coordinates[0] = 99 + assert.NotEqual(t, origPos.Coordinates[0], newPos.Coordinates[0]) + + p := Piece{Type: "Bishop", Color: "Black", Position: origPos} + cp := copyPiece(p) + assert.Equal(t, p.Type, cp.Type) + assert.Equal(t, p.Color, cp.Color) + assert.Equal(t, 2, len(cp.Position.Coordinates)) + cp.Position.Coordinates[0] = 99 + assert.NotEqual(t, p.Position.Coordinates[0], cp.Position.Coordinates[0]) + + board := Board{ + Pieces: []Piece{ + {Type: "Rook", Color: "White", Position: Position{Coordinates: []int{1, 2, 3}}}, + {Type: "Knight", Color: "Black", Position: Position{Coordinates: []int{4, 5, 6}}}, + }, + } + cb := copyBoard(board) + assert.Equal(t, len(board.Pieces), len(cb.Pieces)) + assert.Equal(t, 2, len(cb.Pieces[0].Position.Coordinates)) + cb.Pieces[0].Position.Coordinates[0] = 99 + assert.NotEqual(t, board.Pieces[0].Position.Coordinates[0], cb.Pieces[0].Position.Coordinates[0]) + {{- end -}} + + {{if eq (index . "name") "N-dimensional" -}} + origPos := Position{Coordinates: []int{1, 2, 3, 4, 5}} + newPos := copyPosition(origPos) + assert.Equal(t, origPos.Coordinates, newPos.Coordinates) + newPos.Coordinates[0] = 99 + assert.NotEqual(t, origPos.Coordinates[0], newPos.Coordinates[0]) + + p := Piece{Type: "Queen", Color: "White", Position: origPos} + cp := copyPiece(p) + assert.Equal(t, p.Type, cp.Type) + assert.Equal(t, p.Color, cp.Color) + cp.Position.Coordinates[0] = 99 + assert.NotEqual(t, p.Position.Coordinates[0], cp.Position.Coordinates[0]) + + board := Board{ + Pieces: []Piece{ + {Type: "King", Color: "White", Position: Position{Coordinates: []int{1, 1}}}, + {Type: "Queen", Color: "Black", Position: Position{Coordinates: []int{2, 2}}}, + }, + } + cb := copyBoard(board) + assert.Equal(t, len(board.Pieces), len(cb.Pieces)) + cb.Pieces[0].Position.Coordinates[0] = 99 + assert.NotEqual(t, board.Pieces[0].Position.Coordinates[0], cb.Pieces[0].Position.Coordinates[0]) + {{- end -}} + {{- "\n"}} +} diff --git a/19-structs/04-multidim-chess/exercise.go b/19-structs/04-multidim-chess/exercise.go new file mode 100644 index 0000000..4ef82a8 --- /dev/null +++ b/19-structs/04-multidim-chess/exercise.go @@ -0,0 +1,20 @@ +package multidimchess + +// DO NOT REMOVE THIS COMMENT +//go:generate go run ../../exercises-cli.go -student-id=$STUDENT_ID generate + +type Position struct { + Coordinates []int +} + +type Piece struct { + Type string // e.g. Queen, King, Bishop, etc. + Color string // White or Black + Position Position +} + +type Board struct { + Pieces []Piece +} + +// INSERT YOUR CODE HERE diff --git a/19-structs/04-multidim-chess/exercise.yaml b/19-structs/04-multidim-chess/exercise.yaml new file mode 100644 index 0000000..8590636 --- /dev/null +++ b/19-structs/04-multidim-chess/exercise.yaml @@ -0,0 +1,5 @@ +name: multi-dimensional-chess +input: +- name: 32-dimensional +- name: 2-dimensional +- name: N-dimensional \ No newline at end of file diff --git a/19-structs/04-multidim-chess/exercise_test.go b/19-structs/04-multidim-chess/exercise_test.go new file mode 100644 index 0000000..4cf09ae --- /dev/null +++ b/19-structs/04-multidim-chess/exercise_test.go @@ -0,0 +1 @@ +// PLEASE RUN make generate