13
\$\begingroup\$

Football is the sport where players kick the ball, not carry it. Some confused individuals might call this soccer.


A football team has one goalkeeper, and 10 players out on the pitch. There are many formations used in football, that dictates where each player should be (the player of course moves around, but it's the base position).

The most common formation is 4-4-2, which means that there are 4 defenders, 4 midfielders and two attackers. Other formations are ("defenders, midfielders, attackers" or "defenders, midfielders, midfielders, attackers"):

  • 4-4-2
  • 4-3-3
  • 5-3-2
  • 3-4-3
  • 3-5-2
  • 4-5-1
  • 5-4-1
  • 4-4-1-1
  • 4-3-1-2
  • 4-1-2-3
  • 4-1-3-2
  • 4-3-2-1
  • 3-4-1-2
  • 3-3-3-1

The challenge is to take two inputs, one for each of the two teams and output a overview of the players on the field.

In general: Most information about the layout of the ASCII-art can be found in the figures (a picture says more than 1000 words). Only the way to place the 10 players on the field is explained in detail:

  • The keeper and the penalty area takes up 3 rows of ASCII-characters
    • Layout and number of spaces can be found in the figure below
  • There is no empty row between the penalty area and the defenders
  • If there are 3 numbers in the formation (e.g. 4-4-2, 4-3-3 etc. Not 4-3-2-1):
    • There is no empty row between the defenders and the midfielders
    • There is one empty row between the midfielders and the attackers
  • If there are 4 numbers in the formation (e.g. 4-3-2-1, 3-3-3-1 etc. Not 4-4-2):
    • There is no empty row between the defender and the first row of midfielders
    • There is no empty row between the first row of midfielders and the second
    • There is no empty row between the second row of midfielders and the attackers
  • There is no empty rows between the attackers and the center line
  • The team on the upper half are marked as x, and the team on the second half are marked as o.
  • Each row of players shall be distributed on the pitch as shown in the figures below. The number of spaces can be seen in the figure.

The following figure does not represent a valid formation, but is used to illustrate the layout and number of required spaces between each player. The input for this would be 2 3 4 5 and 5 4 2:

+-----------------+
|     |  x  |     |
|     +-----+     |
|     x     x     |
|    x   x   x    |
|  x   x   x   x  |
|  x  x  x  x  x  |
+-----------------+
|     o     o     |
|                 |
|  o   o   o   o  |
|  o  o  o  o  o  |
|     +-----+     |
|     |  o  |     |
+-----------------+ 

Valid examples:

Input:
4 4 2, 5 3 1 1


+-----------------+
|     |  x  |     |
|     +-----+     |
|  x   x   x   x  |
|  x   x   x   x  |
|                 |
|     x     x     |
+-----------------+
|        o        |
|        o        |
|    o   o   o    |
|  o  o  o  o  o  |
|     +-----+     |
|     |  o  |     |
+-----------------+

Input:
3 5 2, 4 4 1 1


+-----------------+
|     |  x  |     |
|     +-----+     |
|    x   x   x    |
|  x  x  x  x  x  |
|                 |
|     x     x     |
+-----------------+
|        o        |
|        o        |
|  o   o   o   o  |
|  o   o   o   o  |
|     +-----+     |
|     |  o  |     |
+-----------------+

Rules:

  • Input can be on any convenient format, separated however you want. Format can be a single string (5311), comma separated digits (5,3,1,1), etc.
    • The input should not contain any other information than the two formations
  • The output should look exactly as the sample figures, but trailing spaces and newlines are OK.
  • You can assume only valid input is given (only formations in the list will be used).
  • Full program or function

This is code golf, so the shortest code in bytes win.

\$\endgroup\$
  • 1
    \$\begingroup\$ Football's the one with the sticks, right? \$\endgroup\$ – Mego Feb 9 '16 at 17:05
  • \$\begingroup\$ No, that's shuffleboard. You want the one with ponies. \$\endgroup\$ – Geobits Feb 9 '16 at 17:06
  • 1
    \$\begingroup\$ No, that's dressage. You want the one with goals. \$\endgroup\$ – Morgan Thrapp Feb 9 '16 at 17:16
  • 4
    \$\begingroup\$ What! No middle-field circle? \$\endgroup\$ – Luis Mendo Feb 9 '16 at 17:32
  • 1
    \$\begingroup\$ @LuisMendo, just assume it's a rainy day in Stoke! The middle-field circle is often hard to find =P \$\endgroup\$ – Stewie Griffin Feb 9 '16 at 17:36
1
\$\begingroup\$

JavaScript (ES6), 258 262

Anonymous function, taking 2 parameters as numeric arrays

(a,b,H=f=>(f[3]||f.push(0,f.pop()),[z='+-----------------+',...[6,7,...f].map(x=>`|${'98,8o8,5o5o5,4o3o3o4,2o3o3o3o2,2o2o2o2o2o2,5|2o2|5,5+-----+5'.replace(/\d/g,x=>' '.repeat(x)).split`,`[x]}|`),'']))=>H(a).join`
`.replace(/o/g,'x')+z+H(b).reverse().join`
`

Test

F=(a,b,
   H=f=>(
    f[3]||f.push(0,f.pop()),
    [z='+-----------------+',...[6,7,...f].map(x=>`|${'98,8o8,5o5o5,4o3o3o4,2o3o3o3o2,2o2o2o2o2o2,5|2o2|5,5+-----+5'.replace(/\d/g,x=>' '.repeat(x)).split`,`[x]}|`),'']
   )
)=>
  H(a).join`\n`.replace(/o/g,'x')+z+H(b).reverse().join`\n`

  
function test() {
  var f1=F1.value.match(/\d+/g),f2=F2.value.match(/\d+/g)
  O.textContent=F(f1,f2)
}

test()
x <input id=F1 value='4,4,2' oninput='test()'><br>
o <input id=F2 value='4,3,1,2' oninput='test()'><br>
<pre id=O>

\$\endgroup\$
2
\$\begingroup\$

Python 2, 401 377 bytes

def g(x,o):
 r=lambda r:["|"+"  x"*5+"  |","|        x        |","|     x     x     |","|    x   x   x    |","|  x   x   x   x  |"][r%5];d="+"+"-"*17+"+";h=[d,"|     |  x  |     |","|     +-----+     |"]+map(r,x);b=map(lambda r:r.replace("x","o"),[s for s in h[:3]]+map(r,o))[::-1];e="|"+" "*17+"|"
 if len(x)-4:h.insert(5,e)
 if len(o)-4:b.insert(1,e)
 print"\n".join(h+[d]+b)

Ungolfed version with test environment here!

Function that takes two lists of the format [defenders, midfielders, midfielders, attackers] while the one midfielder number is optional. Team X (top) comes first, team O (bottom) second.

\$\endgroup\$
  • \$\begingroup\$ there is a useless space in one of your lambda lambda a:r(a), x) ^^ \$\endgroup\$ – Erwan Feb 11 '16 at 8:22
  • \$\begingroup\$ @Erwan Thanks, good catch! \$\endgroup\$ – Denker Feb 11 '16 at 8:29
  • \$\begingroup\$ i think it's worst to define t=lambda a:r(a) as you use it 2 times \$\endgroup\$ – Erwan Feb 11 '16 at 8:32
  • \$\begingroup\$ better solution remove all occurence of lambda a:r(a) replace it by just r \$\endgroup\$ – Erwan Feb 11 '16 at 8:38
  • \$\begingroup\$ @Erwan Thanks, missed that too! \$\endgroup\$ – Denker Feb 11 '16 at 8:52
1
\$\begingroup\$

Perl, 360 332 324 bytes

sub f{$q="";($_,$p)=@_;@x=/\S+/g;splice@x,2,0,0if@x<4;for(@x) {$s=(17-$_)/($_+1);$s=$=+1if($s!=($==$s));$x=$"x$=;@a=();push@a,$p for 1..$_;$q.=$_==0?"|$u$u$u  |\n":"|$x".join($"x$s,@a)."$x|\n"}$q}($k,$j)=<>;$u=$"x5;$^="-"x17;$i="|$u+-----+$u|";say"x$^x\n|$u|  x  |$u|\n$i\n".f($k,x)."+$^+".(reverse f$j,o)."\n$i\n|$u|  o  |$u|\nx$^x"

Requires -E|-M5.010:

$ echo $'4 4 2\n4 4 1 1' | perl -M5.010 football.pl
x-----------------x
|     |  x  |     |
|     +-----+     |
|  x   x   x   x  |
|  x   x   x   x  |
|                 |
|     x     x     |
+-----------------+
|        o        |
|        o        |
|  o   o   o   o  |
|  o   o   o   o  |
|     +-----+     |
|     |  o  |     |
x-----------------x

Somewhat ungolfed:

sub f{
    $q="";
    ($_,$p)=@_;
    @x=/\S+/g;
    splice@x,2,0,0if@x<4;
    for(@x) {
        $s=(17-$_)/($_+1);
        $s=$=+1if($s!=($==$s));
        $x=" "x$=;
        @a=();
        push@a,$p for 1..$_;
        $q.=$_==0?"|$u$u$u  |\n":"|$x".join(" "x$s,@a)."$x|\n"
    }
    $q
}

($k,$j)=<>;
$u=" "x5;
$^="-"x17;
$i="|$u+-----+$u|";
say"x$^x\n|$u|  x  |$u|\n$i\n".f($k,x)."+$^+".(reverse f$j,o)."\n$i\n|$u|  o  |$u|\nx$^x"
\$\endgroup\$
  • \$\begingroup\$ @edc65 It's just my example output that is wrong :S \$\endgroup\$ – andlrc Feb 9 '16 at 20:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.