Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions 19-structs/04-multidim-chess/.README.md
Original file line number Diff line number Diff line change
@@ -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`.
95 changes: 95 additions & 0 deletions 19-structs/04-multidim-chess/.exercise_test.go
Original file line number Diff line number Diff line change
@@ -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"}}
}
20 changes: 20 additions & 0 deletions 19-structs/04-multidim-chess/exercise.go
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions 19-structs/04-multidim-chess/exercise.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: multi-dimensional-chess
input:
- name: 32-dimensional
- name: 2-dimensional
- name: N-dimensional
1 change: 1 addition & 0 deletions 19-structs/04-multidim-chess/exercise_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
// PLEASE RUN make generate
Loading