Exemple #1
0
// HasTile checks if the passed tile is in player's hand
func (p *Player) HasTile(tile interfaces.Tile) bool {
	for _, currentTile := range p.tiles {
		if currentTile.Number() == tile.Number() && currentTile.Letter() == tile.Letter() {
			return true
		}
	}
	return false
}
Exemple #2
0
// DiscardTile removes passed tile from the tileset
func (t *Tileset) DiscardTile(tl interfaces.Tile) {
	for i, currentTile := range t.tiles {
		if currentTile.Number() == tl.Number() && currentTile.Letter() == tl.Letter() {
			t.tiles = append(t.tiles[:i], t.tiles[i+1:]...)
			break
		}
	}
}
Exemple #3
0
// TileMergeCorporations checks if the passed tile merges two or more corporations, returns a map of
// corporations categorized between "acquirer" and "defunct"
func (b *Board) TileMergeCorporations(t interfaces.Tile) (bool, map[string][]interfaces.Corporation) {
	corporations := b.AdjacentCorporations(t.Number(), t.Letter())

	if len(corporations) > 1 {
		return true, categorizeMerge(corporations)
	}
	return false, map[string][]interfaces.Corporation{}
}
Exemple #4
0
// DiscardTile discards the passed tile from player's hand
func (p *Player) DiscardTile(tile interfaces.Tile) interfaces.Player {
	for i, currentTile := range p.tiles {
		if currentTile.Number() == tile.Number() && currentTile.Letter() == tile.Letter() {
			p.tiles = append(p.tiles[:i], p.tiles[i+1:]...)
			return p
		}
	}
	return p
}
Exemple #5
0
// Returns true if a tile is permanently unplayable, that is,
// that putting it on the board would merge two or more safe corporations
func (g *Game) isTilePermanentlyUnplayable(tl interfaces.Tile) bool {
	adjacents := g.board.AdjacentCorporations(tl.Number(), tl.Letter())
	safeNeighbours := 0
	for _, adjacent := range adjacents {
		if adjacent.IsSafe() {
			safeNeighbours++
		}
		if safeNeighbours == 2 {
			return true
		}
	}
	return false
}
Exemple #6
0
// TileFoundCorporation checks if the passed tile founds a new corporation, returns a slice of tiles
// composing this corporation
func (b *Board) TileFoundCorporation(t interfaces.Tile) (bool, []interfaces.Tile) {
	var newCorporationTiles []interfaces.Tile
	adjacent := b.adjacentTiles(t.Number(), t.Letter())
	for _, adjacentTile := range adjacent {
		if adjacentTile.Type() == interfaces.CorporationOwner {
			return false, []interfaces.Tile{}
		}
		newCorporationTiles = append(newCorporationTiles, adjacentTile.(interfaces.Tile))
	}
	if len(newCorporationTiles) > 0 {
		newCorporationTiles = append(newCorporationTiles, t)
		return true, newCorporationTiles
	}
	return false, newCorporationTiles
}
Exemple #7
0
// Returns true if a tile is temporarily unplayable, that is,
// that putting it on the board would create an 8th corporation
func (g *Game) isTileTemporarilyUnplayable(tl interfaces.Tile) bool {
	if len(g.activeCorporations()) < totalCorporations {
		return false
	}
	adjacents := g.board.AdjacentCells(tl.Number(), tl.Letter())
	emptyAdjacentCells := 0
	for _, adjacent := range adjacents {
		if adjacent.Type() == interfaces.CorporationOwner {
			return false
		}
		if adjacent.Type() == interfaces.EmptyOwner {
			emptyAdjacentCells++
		}
	}
	if emptyAdjacentCells == len(adjacents) {
		return false
	}
	return true
}
Exemple #8
0
// TileGrowCorporation checks if the passed tile grows a corporation.
// Returns true if that's the case, the tiles to append to the corporation and
// the ID of the corporation which grows
func (b *Board) TileGrowCorporation(tl interfaces.Tile) (bool, []interfaces.Tile, interfaces.Corporation) {
	tilesToAppend := []interfaces.Tile{tl}
	var corporationToGrow, nullCorporation interfaces.Corporation

	corporations := b.AdjacentCorporations(tl.Number(), tl.Letter())
	if len(corporations) != 1 {
		return false, []interfaces.Tile{}, nullCorporation
	}
	corporationToGrow = corporations[0]

	adjacent := b.adjacentTiles(tl.Number(), tl.Letter())
	for _, adjacentCell := range adjacent {
		if adjacentCell.Type() == interfaces.UnincorporatedOwner {
			tilesToAppend = append(tilesToAppend, adjacentCell.(interfaces.Tile))
		}
	}
	return true, tilesToAppend, corporationToGrow
}
Exemple #9
0
// PutTile puts the passed tile on the board
func (b *Board) PutTile(t interfaces.Tile) interfaces.Board {
	b.grid[t.Number()][t.Letter()] = t
	return b
}