commit b568d0f9c9bd41e9f18873b911bb75a448307cfd Author: Artlef Date: Sun May 15 19:28:11 2022 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c3c7987 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +createpairings diff --git a/README.md b/README.md new file mode 100644 index 0000000..85ba3a9 --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +1. Write players separated by newline in players.txt +2. Output can be redirected in a csv file: `./createpairings > results.csv` diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..286a068 --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module git.artlef.pw/createpairings + +go 1.18 diff --git a/main.go b/main.go new file mode 100644 index 0000000..a7eea6f --- /dev/null +++ b/main.go @@ -0,0 +1,91 @@ +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 +} diff --git a/players.txt b/players.txt new file mode 100644 index 0000000..606113a --- /dev/null +++ b/players.txt @@ -0,0 +1,3 @@ +Alice +Bob +Charlie