0
 I am trying to create a match program with 4 fields. There will be 4 games going on at the same time, one at each fields.

A team will never play 2 matches in a row - and can not play at the same time on multiple fields. With 32 matches and 4 fields this is probably not possible.

I am trying with Python to create the best match program as possible with the least errors. Then, with the program with the least errors - create a PAUSE where the error occurs so that the team get a chance to rest.

This is my matches.txt file Team1, Team2, Team1 ID, Team 2 ID

Trom 1,Trom 2,41,42
Trom 1,Trom 3,41,43
Trom 1,VAW,41,44
Trom 2,Trom 3,42,43
Trom 2,VAW,42,44
Trom 3,VAW,43,44
Team 1,Team 2,45,46
Team 1,Team 3,45,47
Team 1,Team 4,45,48
Team 2,Team 3,46,47
Team 2,Team 4,46,48
Team 3,Team 4,47,48
Team 5,Team 6,49,50
Team 5,Team 7,49,51
Team 5,Team 8,49,52
Team 5,Team 9,49,53
Team 6,Team 7,50,51
Team 6,Team 8,50,52
Team 6,Team 9,50,53
Team 7,Team 8,51,52
Team 7,Team 9,51,53
Team 8,Team 9,52,53
Team 10,Team 11,54,55
Team 10,Team 12,54,56
Team 10,Team 13,54,57
Team 10,Team 14,54,58
Team 11,Team 12,55,56
Team 11,Team 13,55,57
Team 11,Team 14,55,58
Team 12,Team 13,56,57
Team 12,Team 14,56,58
Team 13,Team 14,57,58

Python file:

import csv, random, io, codecs
g = globals()

f = open("pymatches.txt", "w"); ## Create output file
f.close()

## Number of courts
f = open("num_court.txt", "r");
#numCourt = int(f.read())
numCourt = 4;
f.close()

counter = 0 ## Teller
matches = [] ## array for kampene


minErrors = 100000;

i = 1
while i < 250:
    pyMatches = [];
    rounds = [];
    round = 1
    errors = 0;
    with codecs.open ('matches.txt', 'r') as f: ## Open files with matches
        csv_reader = csv.reader(f, delimiter=',') ## Delimiter is comma (,)
        numGames = 0 ## Count lines
        for row in csv_reader:
            numGames += 1
            matches.append(row) ## Add match to array

    random.shuffle(matches) ## Shuffle // random

    for x in range(1, numCourt+1): ## numCourt is number of courts, usually 4.
        for y in range(1, (numGames/numCourt)+1):
            if y not in rounds:
                rounds.append(y)
                g[y] = []

            maxTry = 150
            Try = 0
            while Try < maxTry:
                Try += 1
                if y == 1:
                    if matches[0][2] in g[y]:
                        random.shuffle(matches)
                    elif matches[0][3] in g[y]:
                        random.shuffle(matches)
                else:
                    if matches[0][2] in g[y]:
                        random.shuffle(matches)
                    elif matches[0][3] in g[y]:
                        random.shuffle(matches)
                    elif matches[0][2] in g[y-1]:
                        random.shuffle(matches)
                    elif matches[0][3] in g[y-1]:
                        random.shuffle(matches)

            if y >= 2:
                if matches[0][2] in g[y]:
                    print(matches[0][0],"Error, samme runde ", x,y)
                    errors += 1
                if matches[0][3] in g[y]:
                    print(matches[0][1],"Error, samme runde ", x,y)
                    errors += 1
                if matches[0][2] in g[y-1]:
                    print(matches[0][0],"Error, forrige runde ", x,y)
                    errors += 1
                if matches[0][3] in g[y-1]:
                    print(matches[0][1],"Error, forrige runde ", x,y)
                    errors += 1

            else:
                if matches[0][2] in g[y]:
                    #print(matches[0][0],"Error, samme runde ", x,y)
                    errors += 1
                if matches[0][3] in g[y]:
                    #print(matches[0][1],"Error, samme runde ", x,y)
                    errors += 1

            if matches[0][2] not in g[y]:
                g[y].append(matches[0][2])
            if matches[0][3] not in g[y]:
                g[y].append(matches[0][3])
            matches[0].append(x)
            matches[0].append(y)
            matchinfo = str(matches[0][0]) + "," + str(matches[0][1]) + "," + str(matches[0][2]) + "," + str(matches[0][3]) + "," + str(x) + "," + str(y)
            pyMatches.append(matchinfo)
            matches.pop(0) ## Remove match from array


    if errors < minErrors:
        minErrors = errors
        f = open("pymatches.txt", "a+")
        f.truncate(0)
        f.write(str(errors) + "\n")
        for line in pyMatches:
            if line:
                f.write(line + "\n")
        f.close()
        if errors == 0:
            break
    i += 1

Something it generates 0 errors, but still there will be a team that plays 2 games in a row. I can't see how that is possible?

Erik Nilsen
  • 21
  • 1
  • 7
  • 1
    What part of this code is meant to prevent this? – Scott Hunter Nov 04 '21 at 12:34
  • You mean, if error is non zero you are expecting few teams to play >= 2 matches in a row vice versa? – Ashutosh Nov 04 '21 at 12:39
  • Yeah, I mean if error is non zero I excepts a team to play >= 2 matches in a row OR 2 matches simultaneously in 1 round (4 fields, so 4 games are playing at the same time). A team should NEVER play 2 matches in the same round. A team CAN play 2 matches IN a row if we insert a pause (an empty round), but I want that to happen as few times as possible. – Erik Nilsen Nov 04 '21 at 12:57
  • @ScottHunter in the while Try < maxTry loop.. if matches[0][2] <- teamid1 are in g[y] and so on.. – Erik Nilsen Nov 04 '21 at 13:01
  • Why do you have `x += 1` inside of `for x in range`? – Scott Hunter Nov 04 '21 at 13:28
  • @ScottHunter because x = fieldnumber, and I dont want to have a field = 0.. – Erik Nilsen Nov 04 '21 at 14:38
  • But `x += 1` will be executed *every iteration*, not just the first. And there is an argument for `range` to specify the starting value. – Scott Hunter Nov 04 '21 at 14:42
  • @ScottHunter but x += 1 are not the problem in this code? g[y].append(matches[0][2]) and [0][3] appends the teamID to the current round (variable Y). Before this I am checking if the current team ID already exists in this round, and also checking if the current team ID exists in the previous round id (y-1), but still errors return 0 sometimes despite it has errors... – Erik Nilsen Nov 05 '21 at 10:19
  • The posted code is not valid Python; there is nothing indented under `if y not in rounds:`. – Scott Hunter Nov 05 '21 at 12:12

0 Answers0