Optimal Othello/Reversi Move

6

This is a challenge: Given a specific board in the game of Othello (Reversi), find a move for Black that will capture the maximum number of White pieces.

(Obviously, this may not be the true "optimal" play for Black, but to keep things simple we are only interested in finding the move that will capture the most pieces.)

If you are unfamiliar with the rules of Othello (Reversi), you may refer to: http://en.wikipedia.org/wiki/Reversi

  • Input will be provided as a file, or through stdin (your choice).
  • You may assume that the board will be a standard 8x8 size.
  • Black pieces will be indicated by b and White pieces will be indicated by w. Empty spaces will be indicated by .
  • The input may not necessarily be a valid board reachable by the rules of the game. Regardless, you must still find the optimal move following the rules of the game.
  • Your program must output the move by Black that will capture the most White pieces. If there is a tie, you may choose to output any (or all) of the moves. If there is no valid move, your program must indicate so by outputting x.
  • Your output can be provided as a coordinate, such as (3,4), or in algebraic notation, such as b5. When submitting your answer, please clearly indicate your output format and specify where your coordinate system starts from.

Sample input:

........
..b.....
...b....
...wwww.
...wb...
........
........
........

Sample output in (row,col) format, with (0,0) in the top-left corner:

(5,3)

Sample input:

........
......w.
.wwwwww.
.wwwwww.
.wwwwww.
.wwwwww.
.b....b.
........

Sample output in algebraic notation, with a1 in the bottom-left corner:

b7

Sample input:

........
........
..bbbb..
..bwwb..
..bwwb..
..bbbb..
........
........

Sample output:

x

This is code golf, so the shortest solution will be accepted.

migimaru

Posted 2011-08-30T14:12:49.203

Reputation: 1 040

Answers

2

Python, 184 chars

import os,re
M=os.read(0,99)
s=0
t='x'
for p in range(72):
 c=sum(len(re.match(r'\.w+b|',M[p::d]).group()[2:])for d in(-10,-9,-8,-1,1,8,9,10))
 if c>s:s=c;t='(%d,%d)'%(p/9,p%9)
print t

Coordinates are given as (y,x) with (0,0) in the upper left.

$ ./othello.py < othello1
(5,3)
$ ./othello.py < othello2
(1,1)
$ ./othello.py < othello3
x

Keith Randall

Posted 2011-08-30T14:12:49.203

Reputation: 19 865