Winning move

Find the winning move for a tic-tac-toe game (aka noughts and crosses) assuming the next player is 'X'.

An underscore represents no counter. Where there is no winning move for X, print the input board.

eg:
X_O
XOO
___

winning move is:
X_O
XOO
X__

IN

X_O
XOO
___

OUT

X_O
XOO
X__

IN

XXO
_OX
_OO

OUT

XXO
_OX
_OO

IN

OO_
XX_
___

OUT

OO_
XXX
___

IN

X_O
___
O_X

OUT

X_O
_X_
O_X

IN

_O_
X_O
XOX

OUT

XO_
X_O
XOX
Login to submit solution


19 symbols pyth
Sun Nov 20 16:55:29 2016 Sait2000
XJjb.z@j160850TCJ\X
84 symbols bash
Sat Oct 15 16:20:53 2016 Sait2000
tr -d \\n|sed '/^O/cOO_XXX___
/^_/cXO_X_OXOX
/_$/cX_OXOOX__
/X$/cX_O_X_O_X'|fold -w3
87 symbols bash
Sat Oct 15 16:19:15 2016 Sait2000
tr -d \\n|sed '/^O/cOO_XXX___
/^_/cXO_X_OXOX
/^X_O_/cX_O_X_O_X
/_$/cX_OXOOX__'|fold -w3
90 symbols bash
Sat Oct 15 16:18:37 2016 Sait2000
tr -d \\n|sed '/^O/cOO_XXX___
/^_/cXO_X_OXOX
/^X_O_/cX_O_X_O_X
/^..OX/cX_OXOOX__'|fold -w3
91 symbols bash
Sat Oct 15 16:16:45 2016 Sait2000
tr -d \\n|sed '/^..OX/cX_OXOOX__
/^O/cOO_XXX___
/^X_O_/cX_O_X_O_X
/^_O/cXO_X_OXOX'|fold -w3
96 symbols bash
Sat Oct 15 16:13:19 2016 Sait2000
tr -d \\n|sed '/^..OX/cX_OXOOX__
/^...XX/cOO_XXX___
/^...__/cX_O_X_O_X
/^_O/cXO_X_OXOX'|fold -w3
99 symbols bash
Sat Oct 15 16:12:33 2016 Sait2000
tr -d \\n|sed '/^..OX/cX_OXOOX__
/^...XX/cOO_XXX___
/^...__/cX_O_X_O_X
/^...X_/cXO_X_OXOX'|fold -w3
100 symbols bash
Tue Aug 16 21:55:21 2016 hidefromkgb
tr -d \\n|sed '/^...XO/cX_OXOOX__
/^...XX/cOO_XXX___
/^...__/cX_O_X_O_X
/^...X_/cXO_X_OXOX'|fold -w3
104 symbols bash
Wed Jul 27 01:12:30 2016 hidefromkgb
tr -d \\n|sed $'/^...XO/cX_OXOOX__\n/^...XX/cOO_XXX___\n/^...__/cX_O_X_O_X\n/^...X_/cXO_X_OXOX'|fold -w3
125 symbols perl
Sun Sep 28 02:05:51 2014 vakorol
#!/usr/bin/perl -p0
y/XO_\n/ 21/d;for$i(0..3){$i!=1&&!$f&&s/(.)(.{$i})(.)(.{$i})(.)/"$1$3$5"<2&&++$f?"X$2X$4X$6":$&/ge}y/ 12/X_O/;s/.../$&\n/g
134 symbols perl
Mon Aug 25 06:25:31 2014 gabrielmad
#!/usr/bin/perl -p0
while(s/(_)/X/){substr$_,$-[1],1,'X';$g='(.|\n)';last if/X{3}|X$g{3}X$g{3}X|X$g{4}X$g{4}X/;substr$_,$-[1],1,1;last if$i++>9}s/1/_/g
140 symbols perl
Sun Aug 24 05:44:48 2014 gabrielmad
#!/usr/bin/perl -0n
while(s/(_)/X/){substr$_,$-[1],1,'X';$g='(.|\n)';last if/X{3}|X$g{3}X$g{3}X|X$g{4}X$g{4}X/;substr$_,$-[1],1,1;last if$i++>9}s/1/_/g;print
150 symbols perl
Sun Aug 24 03:50:56 2014 gabrielmad
#!/usr/bin/perl
$_=join'',<>;while(s/(_)/X/){substr$_,$-[1],1,'X';$g='(.|\n)';last if/X{3}|X$g{3}X$g{3}X|X$g{4}X$g{4}X/;substr$_,$-[1],1,1;last if$i++>9}s/1/_/g;print
325 symbols perl
Sat Aug 9 23:57:07 2014 danieljabailey
#!/usr/bin/perl
@w=('XXX......','...XXX...','......XXX','X..X..X..','.X..X..X.','..X..X..X','X...X...X','..X.X.X..');$_=join('',<>);s/\R//g;@r=split//;for(@r){$p++;if('_'eq$_){$t="";$m=0;$t.=$p==++$m?'X':$_ for(@r);for(@w){if($t=~/$_/){for(@r){$o.=$p==++$s?'X':$_;$o.=$/if($s%3==0)}$d=1}}}}if(!$d){for(@r){$o.=$_;$o.=$/if(++$s%3==0)}}print$o
358 symbols perl
Sat Aug 2 22:44:56 2014 danieljabailey
#!/usr/bin/perl
@w=('XXX......','...XXX...','......XXX','X..X..X..','.X..X..X.','..X..X..X','X...X...X','..X.X.X..');while(<>){$b.=$_}$_=$b;s/\R//g;@r=split //;for(@r){$p++;if($_ eq'_'){$t="";$m=0;for$c(@r){$m++;$t.=$p==$m?'X':$c}for$k(@w){if($t=~/$k/){for$c(@r){$s++;$o.=$p==$s?'X':$c;$o.="\n"if($s%3==0)}$d=1}}}}if($d!=1){for$f(@r){$s++;$o.=$f;$o.="\n"if($s%3==0)}}print$o
362 symbols perl
Sat Jun 14 03:58:16 2014 danieljabailey
#!/usr/bin/perl
@w=('XXX......','...XXX...','......XXX','X..X..X..','.X..X..X.','..X..X..X','X...X...X','..X.X.X..');while(<>){$b.=$_}$_=$b;s/\R//g;@r=split//;for(@r){$p++;if($_ eq'_'){$t="";$m=0;for$c(@r){$m++;$t.=$p==$m?'X':$c}for$k(@w){if($t=~/$k/){for$c(@r){$s++;$o.=$p==$s?'X':$c;if($s%3==0){$o.="\n"}}$d=1}}}}if($d!=1){for$f(@r){$s++;$o.=$f;if($s%3==0){$o.="\n"}}}print$o;
363 symbols perl
Sat Jun 14 03:57:01 2014 danieljabailey
#!/usr/bin/perl
@w=('XXX......','...XXX...','......XXX','X..X..X..','.X..X..X.','..X..X..X','X...X...X','..X.X.X..');while(<>){$b.=$_}$_=$b;s/\R//g;@r=split //;for(@r){$p++;if($_ eq'_'){$t="";$m=0;for$c(@r){$m++;$t.=$p==$m?'X':$c}for$k(@w){if($t=~/$k/){for$c(@r){$s++;$o.=$p==$s?'X':$c;if($s%3==0){$o.="\n"}}$d=1}}}}if($d!=1){for$f(@r){$s++;$o.=$f;if($s%3==0){$o.="\n"}}}print$o;
368 symbols perl
Fri Jun 13 03:06:36 2014 danieljabailey
#!/usr/bin/perl
@w=('XXX......','...XXX...','......XXX','X..X..X..','.X..X..X.','..X..X..X','X...X...X','..X.X.X..');while(<>){$b.=$_;}$_=$b;s/\R//g;@r=split //;for(@r){$p++;if($_ eq'_'){$t="";$m=0;for$c(@r){$m++;$t.=$p==$m?'X':$c;}for$k(@w){if($t=~/$k/){for$c(@r){$s++;$o.=$p==$s?'X':$c;if($s%3==0){$o.="\n";}}$d=1;}}}}if($d!=1){for$f(@r){$s++;$o.=$f;if($s%3==0){$o.="\n";}}}print$o;
413 symbols haskell
Sat Mar 14 13:00:54 2015 YoshikuniJujo
import Data.Maybe
import Data.List
main=interact$unlines.w.lines
p=partition;u='X';t="XX";n=Just;f=Nothing
w b=head$mapMaybe($b)[h,v,d,e,n]
h[]=f
h(l:m)=maybe(fmap(l:)$h m)(n.(:m))$i l
i l|(t,"_")==p(==u)l=n"XXX"|0<1=f
v[a:x,b:y,c:z]|(t,"_")==p(==u)[a,b,c]=n$map(u:)[x,y,z]|0<1=zipWith(:)[a,b,c]`fmap`v[x,y,z]
v _ = f
d[a:x,j:b:y,k:l:[c]]|(t,"_")==p(==u)[a,b,c]=n[u:x,j:u:y,k:l:"X"]|0<1=f
e=fmap reverse.d.reverse

View all solutions