Here are some notes on the use of scilab for games. Please feel free to edit these pages and add information (as usual, you need to be logged in, and to be part of the GamesClassGroup, in order to edit this page).

Useful URLs

Plotting Functions

Index of plotting functions

Code snippets

Here is how you can visualize a matrix as a colormap (blu = low, red = high), and superimpose a vector field to it. You can most likely use a variation of this in order to depict the strategy, and the mines. Only problem: it would be nice to be able to draw the arrows in a better color (white?). Feel free to improve on it and post the results.

x = 0:9;
z = cos(x)'*sin(x);
xbasc()
xset("colormap",jetcolormap(64))
Sgrayplot(x,x,z)
A = rand(10,10) - 0.5;
B = rand(10,10) - 0.5;
champ(1:10,1:10,A,B);

(!) Hint: champ seems to renormalize the length of all arrows to 1, which is not so pretty. To circumvent this, just have the length of the arrow from the 10,10 element be 2, and make it point out of the frame (+1.6, -1.6) is enough. This way, the other arrows will be more visible.

(!) Hint: If you use multiple windows (with xbasc and scf), you should xset the colormap in each one. Otherwise, it will sometimes work the way you hope, and othertimes not.

Writing GIF images

You can use the following bit of code to write images to a gif file or pause between frames:

function [] = graphWriteFile(fig, filename, frame)
  if WRITE_EPS then
    outfile = msprintf("%s%s%02d.gif", PATH, filename, frame);
    xs2gif(fig, outfile, 1);
  else
    input("Hit return to continue")
  end
 endfunction

Once you have a set of images, you can use Image Magick to create an animated gif, as explained on the xs2gif help. (Note that WRITE_EPS is a constant defined earlier.) If you are only using a single graphics window, then the fig parameter should be zero.

Generating a random minescape

I thought of a couple of ways, both unsatisfactory, of generating n mines in a nXn matrix.

1. minelocs = grand(n,1,'uin',1,n*n)

2. minefield = sprand(n,n,1/n)

Anybody has a better (foolproof) way of generating n distinct mine locations? Otherwise, does it make sense to have a "double-strength" mine at the same location?

How about trial and error?

function [loc] = gen_mine (board_size, mines)
  loc = floor( rand(1) * (board_size * board_size) ) + 1;
  if intersect(loc, mines) then, loc = gen_mine(board_size, mines), end;
endfunction;

It is unpleasantly non-matrix-automagical, so it is not in the spirit of Scilab I suppose. And intersect only works on integers so I represent them in base 10.

Sparse Matrices in Scilab

If you are using matricies in Scilab which are on the order of 1250x1250 like me then you'll want to know two commands:

if n is the desired stacksize then

stacksize(n);

makes it so and if m is the 1250x1250 matrix then

m = sparse(m);

makes it sparse. Sparse matrices can't be viewed directly but their product with the identity matrix can be. They can be treated like normal matrices for most common matrix commands. (multiplication, inverse, etc.)

An interesting visualization for hw3

Here is the outline for an interesting visualization of a strategy. This plays the game for the player and bomb given a their starting state and strategy. To make the games more interesting we never check to see if player1 blows up but do slow down the bomb. Thanks to Bo for the vis. idea.

Where pl1x, pl1y, bx, by are the starting locations for pl1 & pl2. Where Strategy is the strategy at each state. Where vsigma is a value matrix. Where roh is the chance that the bomb will succeed in moving. Where n is the size of the board.

Note that s_coord discovers the state of the game given the locations of pl1, pl2, and whose turn it is.

function[] = graphPolicy(pl1x, pl1y, bx, by, Strategy, vsigma, roh, n)
  x1=pl1x; y1=pl1y; x2=bx; y2=by;
  pl1=%t;
  iterations = 0;
  while ~(x1 == n & y1 == n)
    if iterations > 25 then break end;
    for i=1:n, for j=1:n, 
      if pl1 then 
        s = s_coord(i,j,x2,y2,0,n);
      else
        s = s_coord(i,j,x2,y2,1,n);
      end;
      background(i,j) = vsigma(s,1);
    end;end;

    scf(1);
    xbasc(1)
    colorbar(0,1)
    Sgrayplot(x,x,background);
    xrect(x1-.25,y1+.25,0.5,0.5)
    xrect(x2-.25,y2+.25,0.5,0.5)
    graphWriteFile(0,"policy_iteration",iter);
    
    flip = rand(1,1);
    //update locations
    if pl1 then
      s = s_coord(x1,y1,x2,y2,0,n);
      a = Strategy(s,1);
      x1 = x1 + xactions(a);
      y1 = y1 + yactions(a);
    else
      if flip <= roh then 
        s = s_coord(x1,y1,x2,y2,1,n);
        a = Strategy(s,1);
        x2 = x2 + xactions(a);
        y2 = y2 + yactions(a);
      end;
    end;
    pl1=~pl1;
    iterations = iterations+1;
  end;
endfunction

Here you can see a sample game, played out by Player 1 (Our Hero) and Player 2 (the Horriferous Evil Mine). Both players use optimal strategies derived via the Hoffman-Karp algorithm, but through good fortune, persistence in the face of low probabilities, and sheer explosive power, the Mine triumphs in the end. ;)

Notes on Scilab (last edited 2007-03-03 12:03:31 by LucaDeAlfaro)