Skip to content

Commit

Permalink
chess: add Character method to PieceType
Browse files Browse the repository at this point in the history
  • Loading branch information
e0ff committed Nov 26, 2023
1 parent c0f1b21 commit a9810ad
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions internal/chess/piece.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@ type Piece uint8

type PieceType uint8

// Character returns the character associated with the piece type.
func (t PieceType) Character() rune {
switch t {
case Pawn:
return 'p'
case Knight:
return 'n'
case Bishop:
return 'b'
case Rook:
return 'r'
case Queen:
return 'q'
case King:
return 'k'
}

panic(fmt.Sprintf("Character: unknown piece type %d", t))
}

const (
None PieceType = 0x00
Pawn PieceType = 0x10
Expand Down Expand Up @@ -40,29 +60,7 @@ func (p Piece) Type() PieceType {

// Character returns the character associated with the piece.
func (p Piece) Character() rune {
var character rune

switch p.Type() {
case Pawn:
character = 'p'
break
case Knight:
character = 'n'
break
case Bishop:
character = 'b'
break
case Rook:
character = 'r'
break
case Queen:
character = 'q'
break
case King:
character = 'k'
break
}

character := p.Type().Character()
if p.Color() == White {
return unicode.ToUpper(character)
}
Expand Down

0 comments on commit a9810ad

Please sign in to comment.