The Friday Fragment

Published on 17 December 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 bring our games into the modern era. You'll notice in my solutions that every play causes a full redraw of the page. That's so very 1990s, so let's fix it:

Upgrade your game (rock-paper-scissorstic-tac-toe, or Reversi, your choice) to use async (AJAX-style) calls and updates, rather than postbacks. Use any frameworks you'd like, or just plain old Javascript.

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. If you'd like, you can build atop the games, bots, and/or source code at http://writestreams.com/bots.

Last Week's Fragment - Solution

Last's week's puzzle returned to our Reversi game:

Write code to let man compete against machine (a game bot) in Reversi. Present the game board on a web page, let the human player make the next move, and then call the game bot for its move. Continue this until one side wins.

My solution is at http://writestreams.com.us/bots/reversi/playreversi.php. I built it from last week's code and called our Reversi bot unmodified. Here it is:

<?php
  require('reversimove.php');
  $url = 'http://writestreams.com/bots/reversi/';  // Your bot here

  // Set up the session. If this is a new game, show the initial board:
  session_start();
  if (!isset($_SESSION['board']) || !isset($_REQUEST['row'])) {
    $board = str_repeat('.',64);
    $board[27]='O'; $board[28]='X';
    $board[35]='X'; $board[36]='O';
    $_SESSION['board'] = $board;
    exit(show_board($board));
  }

  // Get the board, add Xs (the human's) move, and score it:
  $play = $board = $_SESSION['board'];
  $play[($_REQUEST['row'] * 8 ) + $_REQUEST['col']] = 'X';
  $board = update_board($board, $play);
  if (substr_count($board, '.') == 0) {
    unset($_SESSION['board']);
    exit(show_board($board));
  }

  // Call the web service bot to get its (O's) move and score it:
  $play = file_get_contents($url.'?board='.$board.'&piece=O');
  $board = update_board($board, $play);
  show_board($board);
  $_SESSION['board'] = $board;

  function show_board($board) {
    STATIC $images = array( 'X' => 'black.jpg', 'O' => 'white.jpg', '.' => 'green.jpg');
    $scores = array('.' => 0, 'X' => 0, 'O' => 0);
    echo '<table border="1">';
    for ($i=0; $i<8; $i++) {
      echo '<tr border="1">';
      for ($j=0; $j<8; $j++) {
        $piece = $board[($i*8)+$j];
        $scores[$piece]++;
        if ($piece == '.' && can_play($board, 'X', ($i*8)+$j))
          printf('<td border="1"><a href="playreversi.php?row=%d&col=%d"><img src="%s"></td></a>',
                   $i, $j, $images[$piece]);
        else
          printf('<td border="1"><img src="%s"></td>', $images[$piece]);
      }
      echo '</tr>';
    }
    printf('</table><p>X: %d, O: %d.&nbsp;<a href="playreversi.php">Start Over</a></p>',
               $scores['X'], $scores['O']);
  }
?>

To keep it simple, there's not a lot of error handling. If you haven't already done so, try coding your own bot, plug it in at the "Your bot here" line, and compete against it.