The Friday Fragment

Published on 13 November 2010
This post thumbnail

It's Friday, and time again for the Friday Fragment: our weekly programming-related puzzle.

This Week's Fragment

Let's do another "game bot" - this time, Othello (a.k.a., Reversi):

Code a simple REST web service to start playing Reversi. It should accept a 64-character string representing the current 8x8 "game board" with dot (empty space), X (black), or O (white) in each position (starting from the top left, going left-to-right and down). It should return a 64-character string with your next move added. Don't worry about turning pieces yet, just find the next best move. We'll work on flipping pieces in next week's fragment.

To “play along,” provide either the URL or the code for the solution. You can post it as a comment or send it via email.

Last Week's Fragment - Solution

Last week's puzzle continued with our a tic-tac-toe "bot":

Write code to score the results of calling two tic-tac-toe web services playing each other. Check for wins, ties, and illegal moves.

Here's a solution with my bot competing against itself (not surprisingly, it ends in a tie). Of course, it still places a lot of trust in the bots to behave, but we're keeping things simple here.

<?php
  $urls[0] = 'http://writestreams.com/bots/tictactoe';
  $urls[1] = $urls[0];  // Your bot here
  $board = '         ';
  $turn = 'X';

  for ($round=0; $round<5; $round++) {
    for ($i=0; $i<2; $i++) {
      $board = file_get_contents($urls[$i].'?board=|'.rawurlencode($board).'|');
      $board = substr($board, 1, 9);
      show_board($board);
      if (!is_null($result = score_board($board, $round, $turn)))
        exit(show_result($result));
      elseif ($round==4)
        exit(show_result('Tie'));
      $turn = $turn == 'X' ? 'O' : 'X';
    }
  }

  function score_board($board, $round, $turn) {
    STATIC $wins = array( array(0,1,2), array(3,4,5), array(6,7,8),
                          array(0,3,6), array(1,4,7), array(2,5,8),
                          array(0,4,8), array(2,4,6) );

     if (substr_count($board, $turn) != $round + 1)
       return 'Wrong number of '.$turn.'s';
     for ($i=0; $i<8; $i++) {
       for ($j=0, $piececount=0; $j<3; $j++) {
         if ($board[$wins[$i][$j]] == $turn)
           $piececount++;
       }
       if ($piececount == 3)
         return $turn.' wins!';
     }
   }

   function show_board($board) {
     echo '<table border="1">';
     for ($i=0; $i<3; $i++) {
       echo '<tr border="1">';
       for ($j=0; $j<3; $j++)
         echo '<td border="1">'.$board[($i*3)+$j].'&nbsp;</td>';
       echo '</tr>';
     }
     echo '</table><p>&nbsp;</p>';
   }

   function show_result($result) {
     return printf('<p><b>%s</b></p>', $result);
   }
?>

You can run this solution and the bots themselves from the links at http://writestreams.com/bots.

Stephen told me of a tic-tac-toe bot he wrote, but, alas, I forgot the full URL. I'll try to post it along with next week's puzzle.