|
| 1 | +table = {} |
| 2 | + |
| 3 | +def show(table) |
| 4 | + temp = [] |
| 5 | + a=%w{Ps Team P W D L GF GA GD P} |
| 6 | + printf "%2s %-20s %2s %2s %2s %2s %2s %2s %4s %2s\n", *a |
| 7 | + new = table.keys.sort_by {|i| 1000*table[i][:points] + table[i][:gf] - table[i][:ga]}.reverse |
| 8 | + new.each_with_index {|team,i| |
| 9 | + r = table[team] |
| 10 | + r[:pos] = i+1 |
| 11 | + printf "%2s %-20s %2d %2d %2d %2d %2d %2d %4d %2d\n", |
| 12 | + r[:pos], team, r[:played], r[:won], r[:drawn], r[:lost], r[:gf], r[:ga], r[:gf]-r[:ga], r[:points] |
| 13 | + temp[i+1] = team |
| 14 | + } |
| 15 | + if table['Derby'][:pos].to_i != 20 then |
| 16 | + puts "DERBY ARE NOT BOTTOM" |
| 17 | + end |
| 18 | + if table[temp[19]][:points] > table['Derby'][:points] + 3*(38-table['Derby'][:played]) then |
| 19 | + puts "DERBY ARE RELEGATED" |
| 20 | + end |
| 21 | +end |
| 22 | + |
| 23 | +def loss_for(x, table) |
| 24 | + table[x][:points] = table[x][:points] + 3 |
| 25 | + table[x][:won] = table[x][:won] + 1 |
| 26 | + table[x][:played] = table[x][:played] + 1 |
| 27 | + table[x][:gf] = table[x][:gf] + 1 |
| 28 | +end |
| 29 | + |
| 30 | +def win_for(x, table) |
| 31 | + table[x][:lost] = table[x][:lost] + 1 |
| 32 | + table[x][:played] = table[x][:played] + 1 |
| 33 | + table[x][:ga] = table[x][:ga] + 1 |
| 34 | +end |
| 35 | + |
| 36 | +def draw_for(x, y, table) |
| 37 | + table[x][:points] = table[x][:points] + 1 |
| 38 | + table[x][:drawn] = table[x][:drawn] + 1 |
| 39 | + table[x][:played] = table[x][:played] + 1 |
| 40 | + table[y][:points] = table[y][:points] + 1 |
| 41 | + table[y][:drawn] = table[y][:drawn] + 1 |
| 42 | + table[y][:played] = table[y][:played] + 1 |
| 43 | +end |
| 44 | + |
| 45 | +File.open("table") { |f| |
| 46 | + f.readlines.each { |l| |
| 47 | + pos, team, played, won, drawn, lost, goals_for, goals_against, *junk = l.gsub(/\t+/, ' ').split ' ' |
| 48 | + points = junk[-1] |
| 49 | + table[team] = { :pos => pos.to_i, :played => played.to_i, :won => won.to_i, :drawn => drawn.to_i, :lost => lost.to_i, :gf => goals_for.to_i, :ga => goals_against.to_i, :points => points.to_i } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +max_points = table['Derby'][:points] + 3*(38-table['Derby'][:played]) |
| 54 | + |
| 55 | +File.open("fixtures") { |f| |
| 56 | + f.readlines.each { |l| |
| 57 | + if l =~ /(.*?) v (.+)/ then |
| 58 | + t_away = $2 |
| 59 | + home = $1.gsub(/ /, '_') # this will blat $2 |
| 60 | + away = t_away.gsub(/ /, '_') |
| 61 | + puts "#{home} (#{table[home][:pos]}, #{table[home][:points]}) v #{away} (#{table[away][:pos]}, #{table[away][:points]}) D(#{table['Derby'][:pos]}, #{table['Derby'][:points]})" |
| 62 | + if home == 'Derby' then |
| 63 | + win_for home, table |
| 64 | + loss_for away, table |
| 65 | + puts "Derby win at home" |
| 66 | + elsif away == 'Derby' |
| 67 | + loss_for home, table |
| 68 | + win_for away, table |
| 69 | + puts "Derby win away" |
| 70 | + else |
| 71 | + if table[home][:points] > max_points then # Derby can never overhaul these people |
| 72 | + if table[away][:points] > max_points then # or these people so let them draw |
| 73 | + puts "#{home} v #{away} non-reachable draw" |
| 74 | + draw_for home, away, table |
| 75 | + else |
| 76 | + puts "#{home} (non-reachable) beat #{away} (reachable)" |
| 77 | + win_for home, table |
| 78 | + loss_for away, table |
| 79 | + end |
| 80 | + elsif table[away][:points] > max_points then # or these people so let them draw |
| 81 | + if table[home][:points] > max_points then # Derby can never overhaul these people |
| 82 | + puts "#{home} v #{away} non-reachable draw (2)" |
| 83 | + draw_for home, away, table |
| 84 | + else |
| 85 | + puts "#{away} (non-reachable) beat #{home} (reachable)" |
| 86 | + loss_for home, table |
| 87 | + win_for away, table |
| 88 | + end |
| 89 | + elsif table[home][:pos] > table['Derby'][:pos] then |
| 90 | + if table[home][:points] + 4 < table['Derby'][:points] then |
| 91 | + puts "#{home} beat #{away} because < Derby on pos, points" |
| 92 | + win_for home, table |
| 93 | + loss_for away, table |
| 94 | + else |
| 95 | + draw_for home, away, table |
| 96 | + end |
| 97 | + elsif table[away][:pos] > table['Derby'][:pos] then |
| 98 | + if table[away][:points] + 4 < table['Derby'][:points] then |
| 99 | + puts "#{away} beat #{home} because < Derby on pos, points" |
| 100 | + loss_for home, table |
| 101 | + win_for away, table |
| 102 | + else |
| 103 | + puts "#{home} draw with #{away}" |
| 104 | + draw_for home, away, table |
| 105 | + end |
| 106 | + elsif table[home][:pos] > table['Derby'][:pos] - 3 then |
| 107 | + if table[away][:pos] < table['Derby'][:pos] - 4 then |
| 108 | + puts "#{away} draw with #{home}, both close" |
| 109 | + draw_for home, away, table |
| 110 | + else |
| 111 | + puts "#{away} beat #{home} because home is close" |
| 112 | + win_for away, table |
| 113 | + loss_for home, table |
| 114 | + end |
| 115 | + elsif table[away][:pos] > table['Derby'][:pos] - 3 then |
| 116 | + if table[home][:pos] < table['Derby'][:pos] - 3 then |
| 117 | + puts "#{home} draw with #{away}, both close" |
| 118 | + draw_for home, away, table |
| 119 | + else |
| 120 | + puts "#{home} beat #{away} because away is close" |
| 121 | + win_for home, table |
| 122 | + loss_for away, table |
| 123 | + end |
| 124 | + elsif table[home][:pos] < 6 then |
| 125 | + puts "#{home} beat #{away} because top-5" |
| 126 | + win_for home, table |
| 127 | + loss_for away, table |
| 128 | + elsif table[away][:pos] < 6 then |
| 129 | + puts "#{away} beat #{home} because top-5" |
| 130 | + loss_for home, table |
| 131 | + win_for away, table |
| 132 | + else |
| 133 | + puts "#{home} draw with #{away}" |
| 134 | + draw_for home, away, table |
| 135 | + end |
| 136 | + end |
| 137 | + end |
| 138 | + if l =~ /2008/ then |
| 139 | + show table |
| 140 | + end |
| 141 | + } |
| 142 | +} |
| 143 | +show table |
0 commit comments