/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 *
 * This file is part of SanKit -- A multiplatform set of C code
 * for reading and writing PGN formatted chess games. In addition
 * it can be used as an engine to determine in real time if proposed
 * moves are legal or not. It appears to generate near perfect PGN
 * from incorrectly formatted files.
 *
 * Copyright 2012 David Hoerl All Rights Reserved.
 *
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 *    1. Redistributions of source code must retain the above copyright notice, this list of
 *       conditions and the following disclaimer.
 *
 *    2. Redistributions in binary form must reproduce the above copyright notice, this list
 *       of conditions and the following disclaimer in the documentation and/or other materials
 *       provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY David Hoerl ''AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL David Hoerl OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

#ifndef SanKitCLI_CPP_Common_h
#define SanKitCLI_CPP_Common_h

#include <cstdio>
#include <string>
#include <vector>

namespace SANKit
{
	typedef enum { empty=0, wP, wR, wN, wB, wQ, wK, bP, bR, bN, bB, bQ, bK, null } piece;

	typedef std::vector<std::string *> Plays_t;
	typedef Plays_t::iterator Plays_Itor_t;

	typedef std::vector<std::string *> Boards_t;
	typedef Boards_t::iterator Boards_Itor_t;

	class Game
	{
		public:
			Game(FILE *pgnFP, FILE *boardFP);
			~Game();
			
			bool GetSuccess() const { return success; }
			bool GetIsForfeit() const { return forfeit; }	// no boards - recorded as "0-1" or "1-0"
			size_t GetFullMoveCount() const { return (half_moves + 1)/2; }
			size_t GetHalfMoveCount() const { return half_moves; }
			
			std::string GetHeader() const { return std::string(header); }
			std::string GetFullMove(size_t move) const { return std::string(*plays[move]); }

			std::string GetNewBoard() const;
			std::string GetBoard(size_t full_move, bool white) const;
			
			void AppendNewBoard(std::string& str) const;	// convenience: get a starting board
			void AppendBoard(std::string& str, size_t full_move, bool white) const;

			piece GetPiece(size_t full_move, bool white, const char *locator) const;
			const char *StringForPiece(piece p) const;

		private:
			Game(const Game& that);
			Game& operator=(const Game& right);
			
		private:
			void DumpGame(std::string& str, const char *ptr) const;
			//const std::string * const GetMove(FILE *fp) const;
			bool ParseGame(FILE *pgnFP, FILE *boardFP);

		private:
			std::string header;
			Plays_t plays;
			Plays_t boards;
			size_t half_moves;
			bool success;
			bool forfeit;
	};
}

#endif
