An Algorithmic and Social Introduction to Computer Science (CSC-105 2000S)


Class 32: PacMan, Revisited

Back to The Costs of Reliability. On to Introduction to Artificial Intelligence.

Held Friday, March 17, 2000

Overview

Today, we continue our work on PacMan. We'll discuss things to add and strategies for adding them.

Question 32 for today: What feature do you most want to see in our PacMan game? Sketch a strategy we might use to implement that feature.

Question 33 for after break: What, in your own words, would be sufficient criteria to judge a program as "intelligent"?

Notes

Contents

Summary


New Images

Jeff has contributed some new images for our game.

Current Status

Here's what we have so far. Available at

http://www.math.grin.edu/~rebelsky/Courses/CS105/2000S/Game/pacman2.html

<html>
<head>
<title>PacBill</title>
<script language="javascript" src="pacman2.js"></script>
</head>
<body>
<script language="javascript">
drawBoard(5,5);
play();
</script>
<form name="controls">
<table>
<tr align="center">
  <td></td>
  <td>
    <input type="button" name="up" value="up"
       onClick="pacDir = 1;">
  </td>
  </td>
</tr>
<tr align="center">
  <td>
    <input type="button" name="left" value="left"
       onClick="pacDir = 4;">
  </td>
  <td></td>
  <td>
    <input type="button" name="right" value="right"
       onClick="pacDir = 2;">
  </td>
</tr>
<tr align="center">
  <td></td>
  <td>
   <input type="button" name="down" value="down"
   onClick="pacDir = 3;">
  </td>
  <td></td>
</tr>
</table>

</form>
</body>
</html>
// pacman
//   A simple implementation of PacMan in JavaScript.
//   Created by Sam Rebelsky and the students of CSC105 2000S.
//   Feel free to adapt and reuse this code.

// +-----------+-----------------------------------------------
// | Variables |
// +-----------+

// The names of the images we use
var emptyImage = "Images/blank.gif";
var pacImage = "Images/billgates.evil.gif";
var monsterImage = "Images/janetreno.gif";
var wallImage = "Images/wall.gold.gif";
var foodImage = "Images/dollar.sign.gif";

// Keep track of Column and Row of PacMan
var pacCol;
var pacRow;
// Keep track of the direction of PacMan.
// 1 - up, 2 - right, 3 -down, 4 - left
var pacDir;
// Keep track of the board boundaries
var maxCol;
var maxRow;

// +-----------+-----------------------------------------------
// | Functions |
// +-----------+

// Generate the name of the image for the position at a particular
// row and column on the board.
function cellName(row,col) {
  return "cell" + row + "x" + col;
} // cellName()

// Set the image at row,col to newImage
function setImage(row,col,newImage)
{
  eval("document." + cellName(row,col) + ".src =newImage;");
} // setImage()

// Draw the board
function drawBoard(cols, rows)
{
  var col; // A counter for columns
  var row; // A counter for rows
  // For each row
  for (row = 1; row <= rows; row++) {
    // For each column
    for (col = 1; col <= cols; col++) {
      document.writeln('<img src="' + foodImage + '" name="'
                       + cellName(row,col) + '">');
    }
    // Move to next row
    document.writeln("<br>");
  } // for each row
  
  // Note boundaries
  maxCol = cols;
  maxRow = rows;

  // Place the pacman
  pacCol = 1;
  pacRow = 1;
  pacDir = 2;
  setImage(pacRow, pacCol, pacImage);
} // drawBoard(cols, rows)

// Do one "round" of the game.
function play()
{
  movePacMan();
  setTimeout("play()", 200);
} // play()

// Move the pac man
function movePacMan()
{
  // Remember the old poistion of the PacMan
  var oldRow = pacRow;
  var oldCol = pacCol;

  // Determine the new position of the PacMan
  if (pacDir == 1) {
    pacRow = pacRow - 1;
  }
  else if (pacDir == 2) {
    pacCol = pacCol + 1;
  }
  else if (pacDir == 3) {
    pacRow = pacRow + 1;
  }
  else if (pacDir == 4) {
    pacCol = pacCol - 1;
  }
  else {
    pacDir = 0;
  }

  // Update in case it runs off the board
  if (pacRow > maxRow) {
    pacRow = maxRow;
    pacDir = 0;
  }
  if (pacRow < 1) {
    pacRow = 1;
    pacDir = 0;
  }
  if (pacCol > maxCol) {
    pacCol = maxCol;
    pacDir = 0;
  }
  if (pacCol < 1) {
    pacCol = 1;
    pacDir = 0;
  }

  // If the pac man is moving then
  if (pacDir != 0) {
    // Replace the pac man with an empty space
    setImage(oldRow, oldCol, emptyImage);
    // Draw the pac man at its new position
    setImage(pacRow, pacCol, pacImage);
  }
  window.status = cellName(pacRow,pacCol);
} // movePacMan()

Dealing with Key Presses

Here's some code I quickly hacked together to deal with key presses. We may want to see if there are other ways to do so.

<html>
<head>
<title>KeyPress</title>
<script language="javascript">
var stuff = "";
function checkKeys() {
  document.whatever.junk.focus();
  var key = document.whatever.key.value;
  if (key.length > 0) {
    alert(key);
    document.whatever.key.value = "";
  }
  stuff = stuff+key;
  window.status = stuff;
  document.whatever.key.focus();
  setTimeout("checkKeys()", 100);
}
</script>
</head>
<body onload="checkKeys()">

<form name="whatever">
<input type="text" value="" name="key" size=0>
<input type="text" value="" name="junk" size=0>
</form>
</body>
</html>

History

Saturday, 22 January 2000

Friday, 17 March 2000

Back to The Costs of Reliability. On to Introduction to Artificial Intelligence.


Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.

This page may be found at http://www.math.grin.edu/~rebelsky/Courses/CS105/2000S/Outlines/outline.32.html

Source text last modified Fri Mar 17 09:45:04 2000.

This page generated on Fri Mar 17 09:50:18 2000 by Siteweaver. Validate this page's HTML.

Contact our webmaster at rebelsky@grinnell.edu