Friday, December 14, 2012

How to program a chess engine in C++ language compatible with Xboard/Winboard: Event, Site, Date, Game, Player info (pgn)


This function is called by the main () function to get the user input of his/her name and country of origin. An output pgn file is generated according to the standard chess pgn game file format. The date is obtained from the system date and the chess game result is yet unknown and so therefore '-' is placed.

Source code:

/*
PROGRAM 8: Chess Engine Programming - Event, Site, Date, Game, and Player information
AUTHOR: eternaltreasures
DATE: 2010 September 26
TITLE: How to program a chess engine in C++ language compatible with Xboard/Winboard:
       Event, Site, Date, Game, and Player information for output pgn file
*/

#include <iostream>
#include <time.h>
#include <fstream>
using namespace std;

#define newline '\n'

void eventgameinfo ()
{
string eventname = "Chess Game Match";
string eventsite = "International";
time_t systemdate;
string whiteplayer = "White Player";
string blackplayer = "Black Player";
string gameresult = " - ";
cout << newline;

//Capture system date and time
time(&systemdate);

//Open the output game.pgn file     
ofstream fout;    
fout.open ("game.pgn");

cout << "Please enter your name: "; cin >> whiteplayer;
cout << "Your country of origin: "; cin >> eventsite;
cout << newline;

//Information of the Event, Site, Date, Game, Player and Result (for game.pgn file format)
cout << "[Event " << "\"" << eventname << "\"]";
cout << newline;
cout << "[Site  " << "\"" << eventsite << "\"]";
cout << newline;
cout << "[Date  " << "\"" << ctime(&systemdate) << "\"]";
cout << newline;
cout << "[White  " << "\"" << whiteplayer << "\"]";
cout << newline;
cout << "[Black  " << "\"" << blackplayer << "\"]";
cout << newline;
cout << "[Result " << "\"" << gameresult << "\"]";
cout << newline;

//Write to the game.pgn file
fout << "[Event " << "\"" << eventname << "\"]";
fout << newline;
fout << "[Site  " << "\"" << eventsite << "\"]";
fout << newline;
fout << "[Date  " << "\"" << ctime(&systemdate) << "\"]";
fout << newline;
fout << "[White  " << "\"" << whiteplayer << "\"]";
fout << newline;
fout << "[Black  " << "\"" << blackplayer << "\"]";
fout << newline;
fout << "[Result " << "\"" << gameresult << "\"]";
fout << newline;

}

int main ()
{
eventgameinfo ();

return 0;
}

No comments:

Post a Comment