Go Fish/Go: Difference between revisions

Content added Content deleted
(Less idiosyncratic and gofmt'ed.)
(Improved quality.)
Line 54: Line 54:
card := gm.deck[0]
card := gm.deck[0]
gm.deck = gm.deck[1:]
gm.deck = gm.deck[1:]
if gm.turn == 0 {
if gm.isPlayerTurn() {
fmt.Printf("You drew a %s.\n", card)
fmt.Printf("You drew a %s.\n", card)
}
}
Line 60: Line 60:
//Check for books
//Check for books
gm.checkForBooks()
gm.checkForBooks()
}
}

// endPly ends the current person's turn.
// It then either calls the next person's
// turn or prints a game over message.
func (gm *GoFishGame) endPly() {
gameOver := gm.isGameOver()
if gameOver {
gm.printGameOverMessage()
} else if gm.turn == 1 {
gm.playerTurn(getPickComputer)
} else {
gm.playerTurn(getPickUser)
}
}
}
}
Line 114: Line 100:
func (gm *GoFishGame) isGameOver() bool {
func (gm *GoFishGame) isGameOver() bool {
return gm.scores[0]+gm.scores[1] == 13
return gm.scores[0]+gm.scores[1] == 13
}

// isPlayerTurn returns if its the player's turn to move.
func (gm *GoFishGame) isPlayerTurn() bool {
return gm.turn == 0
}
}


Line 167: Line 158:
}
}
}
}
gm.endPly()
}
}


Line 192: Line 182:
func (gm *GoFishGame) stealCards(purge string, side int) int {
func (gm *GoFishGame) stealCards(purge string, side int) int {
count := 0
count := 0
tList := gm.hands[side]
var filtered []string
var filtered []string
for _, card := range tList {
for _, card := range gm.hands[side] {
if purge == card {
if purge == card {
count++
count++
Line 218: Line 207:
scores[1] = 0
scores[1] = 0
game := GoFishGame{hands, deck, 0, scores}
game := GoFishGame{hands, deck, 0, scores}
for {
game.playerTurn(getPickUser)
if game.isPlayerTurn() {
game.playerTurn(getPickUser)
} else {
game.playerTurn(getPickComputer)
}
if game.isGameOver() {
break
}
}
game.printGameOverMessage()

}
}

</lang>
</lang>