Skip to content

Commit

Permalink
draft 1
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinzhu00 committed Oct 5, 2019
1 parent 9cb72d1 commit 218df86
Showing 1 changed file with 48 additions and 29 deletions.
77 changes: 48 additions & 29 deletions newscheduler.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import sys
from random import randint
import os.path
from itertools import permutations

class Match:
def __init__(self, b1, b2, g1, g2):
Expand All @@ -13,7 +14,8 @@ class Team:
def __init__(self, id, name):
self.id = id
self.name = name
self.lastMatch = -6
self.canPlay = 0
self.playedWith = []

schools = []

Expand All @@ -23,41 +25,58 @@ def process(line):

input = sys.stdin.readline().rstrip().split(" ")
csvFile = input[0]
totalRounds = int(input[1])
roundsPerTeam = int(input[1])

with open(csvFile) as f:
for line in f:
process(line)

gap = 2 #Time before school can play again
matches = []
teamsLeft = schools.copy()
totalRounds = len(schools)//4*3
for i in range(totalRounds):
teamsLeft = schools.copy()
matchedTeams = []
alreadyPlayed = []
while len(teamsLeft) >= 4:
for i in range(4):
team = randint(0, len(teamsLeft) - 1)
while teamsLeft[team].lastMatch == len(matches) - 1:
team = randint(0, len(teamsLeft) - 1)
matchedTeams.append(teamsLeft[team])
alreadyPlayed.append(teamsLeft[team])
del teamsLeft[team]
matches.append(Match(matchedTeams[0], matchedTeams[1], matchedTeams[2], matchedTeams[3]))
for team in matchedTeams:
team.lastMatch = len(matches) - 1
matchedTeams = []
if len(teamsLeft) != 0:
matchedTeams.extend(teamsLeft)
for i in range(4 - len(teamsLeft)):
team = randint(0, len(alreadyPlayed) - 1)
while alreadyPlayed[team].lastMatch == len(matches) - 1:
team = randint(0, len(alreadyPlayed) - 1)
matchedTeams.append(alreadyPlayed[team])
del alreadyPlayed[team]
matches.append(Match(matchedTeams[0], matchedTeams[1], matchedTeams[2], matchedTeams[3]))
for team in matchedTeams:
team.lastMatch = len(matches) - 1
matchedTeams = []
if len(teamsLeft) >= 4:
j = 0
while j < 4:
team = teamsLeft[randint(0, len(teamsLeft) - 1)]
if team.canPlay >= 0:
matchedTeams.append(team)
teamsLeft.remove(team)
j += 1
else:
matchedTeams = teamsLeft[:]
restOfTeams = 4 - len(matchedTeams)
teamsLeft = schools.copy()
k = 0
while k < restOfTeams:
team = teamsLeft[randint(0, len(teamsLeft)-1)]
if team.canPlay >= 0 and team not in matchedTeams:
matchedTeams.append(team)
teamsLeft.remove(team)
k += 1
perm = list(permutations(range(4)))
for p in perm:
if matchedTeams[p[0]] not in matchedTeams[p[1]].playedWith and matchedTeams[p[2]] not in matchedTeams[p[3]].playedWith:
matches.append(Match(matchedTeams[p[0]],matchedTeams[p[1]],matchedTeams[p[2]],matchedTeams[p[3]]))
matchedTeams[p[0]].playedWith.append(matchedTeams[p[1]])
matchedTeams[p[1]].playedWith.append(matchedTeams[p[0]])
matchedTeams[p[2]].playedWith.append(matchedTeams[p[3]])
matchedTeams[p[3]].playedWith.append(matchedTeams[p[2]])
for t in [matchedTeams[p[r]] for r in range(4)]:
t.canPlay = gap*-1 -1
break
for s in schools:
s.canPlay += 1
if teamsLeft:
ghosts = 4 - len(teamsLeft)
for g in range(ghosts):
teamsLeft.append(Team(-g-1,"Spooky Team"))
perm = list(permutations(range(4)))
for p in perm:
if matchedTeams[p[0]] not in matchedTeams[p[1]].playedWith and matchedTeams[p[2]] not in matchedTeams[p[3]].playedWith:
matches.append(Match(matchedTeams[p[0]],matchedTeams[p[1]],matchedTeams[p[2]],matchedTeams[p[3]]))
break

writeHeader = False
if not os.path.isfile("matches.csv"):
Expand Down

0 comments on commit 218df86

Please sign in to comment.