[ Home | Program.txt | Data.txt ]

The C code for the toy benchmark


//"Toy Benchmark" - an inefficient implementation of the famous triangular peg jumping game
// By Michael Shostak


#include<stdio.h>
#include<time.h>




typedef struct{

	int one;     //first peg
	int two;     //second peg that gets jumped
	int lands;   //where first peg lands
} move_table;


int fill(move_table []);
void set_empty(int [], int);
void play(int [], int, int);
void play_move(int [], const int);
void print(const int []);
void print_array();
int pegs_left(int []);

FILE *fptr;
move_table moves[36];

long int count = 0;
long int one_peg_games=0;


int sequence[50]; //game sequences array, where each slot is a move
                  //from the moves table

int ptr=0;  //list pointer, points to end of list
int h;      //array counter variable

main()

{

   time_t start, finish;
	

   int game_board[15];  //this is the actual game board. A 1 symbolizes
			  		    //a peg, while a 0 is an empty hole


   if (fill(moves))        //fill moves reads from the file "values"
    { 		       
      time(&start);    

      for(h=0; h<15; h++)
		 
       {
 	     set_empty(game_board, h);  //start with hole "h" empty
         puts("\nInitial game board:"); 
    	 print(game_board);         //print the initial game board
	     play(game_board, 0, 0);    //play _all_ possible games
	     ptr=0;                     //reset list pointer
       }

       time(&finish);  

       printf("\n\n%s%d%s", "Run time: ", (finish-start), " seconds\n");

       printf("\n%s%d\n", "The total number of games played was: ", count);
 
       printf("\n%s%d\n", "Total number of games leaving one peg: ", one_peg_games);

   
   }


else  
  puts("Error. Data file not found."); //we couldn't find "values"


return 0;

}



//this function simply returns the number of pegs left on the board. 

int pegs_left(int b[])
 {
   int a;
   int flag=0;

   for(a=0; a<15; a++) //just run down the array, and keep a tally of 1's
    {
     if (b[a]==1)
       flag++;
    }

   return flag;

 }



//this just fills in "moves" with all the moves from the data file
 

int fill(move_table moves[])

{

  FILE *fptr;
  
  int i;
  int x,y,z;
  int flag=0;

 if ((fptr = fopen("values", "r")) != NULL)
   {
     flag=1;

     for(i=0; i<36; i++)
      {
        fscanf(fptr, "%d%d%d", &x, &y, &z);
        moves[i].one = --x;
        moves[i].two = --y;
        moves[i].lands = --z;
      }
 
   }
  
  return flag;

 }



//just puts the pegs back in the board, leaving hole "i" empty.

void set_empty(int board[], int i)

{
	int x;

	for(x=0; x<15; x++)
		board[x]=1;

	board[i]=0;

}


//Here's the main function. It takes the board, and basically
//plays every single possible game from that empty peg. 


void play(int board[], int flag, int move)

  {
	int i, j;
	int board2[15];


	for(j=0; j<15; j++)   //This really jacks up the run time
	 board2[j]=board[j];  

	if (flag==1)
	  {
	   play_move(board2, move); //play the move
	   sequence[ptr]=move+1;    //add the move to the list
	   ptr++;                   //increment the list pointer
	  }

	for(i=0; i<36; i++) //test every move in the database
	 {

	 	if(((board2[moves[i].one]) && (board2[moves[i].two])) && (!(board2[moves[i].lands])))
		    play(board2, 1, i);
	 }

    

     if ((pegs_left(board2))==1) //test if it's a winning game
	     one_peg_games++;    //increment the counter.
	 

	count++;                 //increment the total games played.
	ptr--;                   //decrement the list pointer.

  }


//this just plays the move, all we do is flip the 0's and 1's.

void play_move(int board[], const int i)
{

	board[moves[i].one]=0;
	board[moves[i].two]=0;
	board[moves[i].lands]=1;

}




// This high power graphics code prints the game board out in its normal triangular shape.

void print(const int board[])

  {

    int index;

    printf("\n%s%d\n", "    ", board[14]);
    printf("%s%d%c%d\n", "   ", board[12], ' ', board[13]);
    printf("%s%d%c%d%c%d\n", "  ", board[9], ' ', board[10], ' ', board[11]);

    for(index = 5; index<9; index++)
       printf("%c%d", ' ', board[index]);

    printf("\n");

    for(index = 0; index<5; index++)
       printf("%d%c", board[index], ' ');

    printf("\n\n");

  }




//This prints out the game board in array format.
//Remember, the first number is the peg we left empty. I did that for
//indexing purposes. The actual moves played begins with the 2nd number.

void print_array()
 {
  int x;

  printf("%d%c", h, ' ');

  for(x=0; x<ptr; x++)
   printf("%d%c", sequence[x], ' ');

  printf("\n");

 }

The Data File Used:

1 2 3
1 6 10
2 3 4
2 7 11
3 2 1
3 4 5
3 8 12
3 7 10
4 3 2
4 8 11
5 4 3
5 9 12
6 10 13
6 7 8
7 8 9
7 11 14
8 7 6
8 11 13
9 8 7
9 12 14
10 6 1
10 7 3
10 11 12
10 13 15
11 7 2
11 8 4
12 8 3
12 9 5
12 11 10
12 14 15
13 10 6
13 11 8
14 11 7
14 12 9
15 13 10
15 14 12