first commit

This commit is contained in:
Artlef 2022-05-15 19:28:11 +02:00
commit b568d0f9c9
5 changed files with 100 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
createpairings

2
README.md Normal file
View File

@ -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`

3
go.mod Normal file
View File

@ -0,0 +1,3 @@
module git.artlef.pw/createpairings
go 1.18

91
main.go Normal file
View File

@ -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
}

3
players.txt Normal file
View File

@ -0,0 +1,3 @@
Alice
Bob
Charlie