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?