92 lines
1.8 KiB
Go
92 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bufio"
|
|
"fmt"
|
|
"log"
|
|
"os"
|
|
)
|
|
|
|
type Match struct {
|
|
WhitePlayer string
|
|
BlackPlayer string
|
|
}
|
|
|
|
func getPlayerlist() []string {
|
|
var playerList []string
|
|
file, err := os.Open("players.txt")
|
|
if err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
defer file.Close()
|
|
|
|
scanner := bufio.NewScanner(file)
|
|
for scanner.Scan() {
|
|
playerList = append(playerList, scanner.Text())
|
|
}
|
|
|
|
if err := scanner.Err(); err != nil {
|
|
log.Fatal(err)
|
|
}
|
|
return playerList
|
|
}
|
|
|
|
func main() {
|
|
playerList := getPlayerlist()
|
|
matchList := getMatchs(playerList)
|
|
printMatchList(matchList)
|
|
checkMatchList(matchList, playerList)
|
|
}
|
|
|
|
func getMatchs(playerList []string) []Match {
|
|
if len(playerList) == 0 {
|
|
return nil
|
|
}
|
|
var matchList []Match
|
|
subList := playerList[1:]
|
|
for i, player := range subList {
|
|
whitePlayer := playerList[0]
|
|
blackPlayer := player
|
|
if i%2 == 0 {
|
|
whitePlayer = player
|
|
blackPlayer = playerList[0]
|
|
}
|
|
matchList = append(matchList, Match{whitePlayer, blackPlayer})
|
|
}
|
|
return append(matchList, getMatchs(subList)...)
|
|
}
|
|
|
|
func printMatchList(matchList []Match) {
|
|
for _, match := range matchList {
|
|
fmt.Printf("%s,%s\n", match.WhitePlayer, match.BlackPlayer)
|
|
}
|
|
}
|
|
|
|
func checkMatchList(matchList []Match, playerList []string) {
|
|
for _, player := range playerList {
|
|
countWhitePlayer := countWhitePlayer(matchList, player)
|
|
countBlackPlayer := countBlackPlayer(matchList, player)
|
|
fmt.Fprintf(os.Stderr, "%s has %d games with white pieces and %d with black pieces.\n", player, countWhitePlayer, countBlackPlayer)
|
|
}
|
|
}
|
|
|
|
func countWhitePlayer(matchList []Match, player string) int {
|
|
c := 0
|
|
for _, match := range matchList {
|
|
if match.WhitePlayer == player {
|
|
c++
|
|
}
|
|
}
|
|
return c
|
|
}
|
|
|
|
func countBlackPlayer(matchList []Match, player string) int {
|
|
c := 0
|
|
for _, match := range matchList {
|
|
if match.BlackPlayer == player {
|
|
c++
|
|
}
|
|
}
|
|
return c
|
|
}
|