Wednesday, December 17, 2008

Code for EvenMarker Game



import java.io.*;

import java.util.Random;

// Program: Keyin
// Class that provides keyboard input

public class Keyin
{

// Method to display the user's prompt string
public static void printPrompt(String prompt)
{
  System.out.print(prompt + " ");
  System.out.flush();
}

// Method to make sure no data is available in the
// input stream
public static void inputFlush()
{
  int dummy;
  int bAvail;

  try
  {
  while((System.in.available()) != 0)
  dummy = System.in.read();
  }
  catch(java.io.IOException e)
  {
  System.out.println("Input error");
  }
}
   
 
//********************************
// data input methods for
// string, int, char, and double
//********************************
public static String inString(String prompt)
  {
  inputFlush();
  printPrompt(prompt);
  return inString();
  }

public static String inString()
  {
  int aChar;
  String s = "";
  boolean finished = false;

  while(!finished)
  {
  try
  {
  aChar = System.in.read();
  if (aChar < achar ="="" finished =" true;" s =" s" finished =" true;" achar =" 0;" achar =" System.in.read();">


//program: Even


public class Even
{


   
  public static void main(String[] args)
 {
   
  boolean gameOver;
  int a, b;
  int e = 0, l = 0;
  int e1, l1;
  int m, p;
  int r[][] = new int[2][6];
  int numberToRemove;
  Random jRandom = new Random();
  
  System.out.println("Even Wins\n");
  System.out.println("Take turns with the computer removing markers (1 to 4).");
  System.out.println("You win if you finish with an even number of markers.\n");
  // Variables to help teach computer
  for (int i = 0; i < gameover =" false;" p =" (jRandom.nextInt(9)" a =" 0;" b =" 0;" e1 =" e;" l1 =" l;" e =" (a" l =" (int)">= p)
  {
  // took last one, game over
  gameOver = true;
  r[e][l] = p;
  m = p;
  b += p;
  }
  else
  {
  gameOver = false;
  m = r[e][l];
  b += m;
  }
  System.out.println("I take " + m + " markers.");
  p -= m;
  if (!gameOver)
  {
  System.out.println("\nYour turn:");
  // your turn
  drawMarkers(p, a, b);
  if (p > 4)
  {
  numberToRemove = 4;
  }
  else
  {
  numberToRemove = p;
  }
  do
  {
  m = Keyin.inInt("How many markers do you want to remove (1 to " + numberToRemove + ")?");
  }
  while (m <= 0 || m > numberToRemove);
  a += m;
  p -= m;
  if (p == 0)
  {
  gameOver = true;
  }
  }
  }
  while (!gameOver);
  System.out.println("Game is over. I have " + b + " markers. You have " + a + " markers.");
  // find winner
  if ((float) (b)/ 2 == (int) (b / 2))
  {
  System.out.println("I won!!!");
  }
  else
  {
  System.out.println("You won!!!");
  // computer knowledge gained by you winning
  if (r[e][l] == 1)
  {
  if (r[e1][l1] != 1)
  {
  r[e1][l1]--;
  }
  }
  else
  {
  r[e][l]--;
  }
  }
  } 
  while (Keyin.inString("\nPlay again? (enter a y for yes)").equals("y"));
  }
  
  private static void drawMarkers(int N, int A, int B)
  {
  System.out.println("There are " + N + " markers remaining:");
  for (int i = 0; i <>

No comments:

Post a Comment