X-Git-Url: https://git.ucc.asn.au/?p=progcomp2013.git;a=blobdiff_plain;f=agents%2Fsilverfish%2Fqchess.h;h=db2f9a9c8791ffa8921626cd0edad4f5d7e04954;hp=c020ad2673d4ffb5011e013374f1320508a74c4e;hb=ef3a2d186de4f86b736dc357948a3ca319ef5423;hpb=7518ec8d771e731d6ffbbe00b275b7e3c4b23301 diff --git a/agents/silverfish/qchess.h b/agents/silverfish/qchess.h deleted file mode 120000 index c020ad2..0000000 --- a/agents/silverfish/qchess.h +++ /dev/null @@ -1 +0,0 @@ -../c++/qchess.h \ No newline at end of file diff --git a/agents/silverfish/qchess.h b/agents/silverfish/qchess.h new file mode 100644 index 0000000..db2f9a9 --- /dev/null +++ b/agents/silverfish/qchess.h @@ -0,0 +1,191 @@ +/** + * agent++ : A Sample agent for UCC::Progcomp2013 + * @file qchess.h + * @purpose Declarations for game related classes; Piece, Square, Board + */ + +#ifndef _QCHESS_H +#define _QCHESS_H + + +// board height and width (don't change!) +#define BOARD_HEIGHT 8 +#define BOARD_WIDTH 8 + +#include +#include +#include +#include +#include + +#include +#include // for vfprintf... for the Exception + +class Board; //forward declaration + +/** + * @class Piece + * @purpose Represent a quantum chess piece + */ +class Piece +{ + public: + + typedef enum {PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, UNKNOWN} Type; + typedef enum {WHITE=0, BLACK=1} Colour; + + static Type AllTypes[]; + static Colour AllColours[]; + + + virtual ~Piece() {} // destructor + + int x; int y; // position of the piece + Colour colour; // colour of the piece + int type_index; // indicates state the piece is in; 0, 1, or -1 (unknown) + Type types[2]; // states of the piece + Type current_type; // current state of the piece + + static Type str2type(const std::string & str); + static Colour str2colour(const std::string & str); + static const char * type2str(const Type & t); + + static Piece * AddPiece(std::vector & v, int x, int y, const Colour & colour, const Type & type1, const Type & type2, int type_index=-1); + static Piece * AddPiece(std::vector & v, const Piece & cpy); + + static Colour Opposite(const Colour & c) + { + return (c == WHITE) ? BLACK : WHITE; + } + + double ProbIsType(Board & b, const Piece::Type & t); + + private: + friend class Board; + Piece(int x, int y, const Colour & colour, const Type & type1, const Type & type2 + , int type_index); // constructor + Piece(const Piece & cpy); // copy constructor + + + +}; + +/** + * @class Square + * @purpose Represent a Square on the board; not necessarily occupied + */ +class Square +{ + public: + Square() : x(-1), y(-1), piece(NULL) {} // constructor + Square(int new_x, int new_y, Piece * new_piece = NULL) : x(new_x), y(new_y), piece(new_piece) {} //UNUSED + Square(const Square & cpy) : x(cpy.x), y(cpy.y), piece(cpy.piece) {} // copy constructor (UNUSED) + virtual ~Square() {} //destructor + int x; int y; // position of the square + Piece * piece; // Piece that is in the Square (NULL if unoccupied) + + const std::map & Coverage(const Piece::Colour & c) + { + return (c == Piece::WHITE) ? coverage[Piece::WHITE] : coverage[Piece::BLACK]; + } + + void Update_coverage(Board & b); + + private: + std::map > coverage; +}; + +/** + * @class Board + * @purpose Represent a quantum chess board + */ +class Board +{ + public: + Board(bool choose_types = false); // constructor + Board(Board & parent); // clones a board, copy on write + virtual ~Board(); // destructor + + + // helper; return vector of pieces given player colour + std::vector & pieces(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white : black);} + // helper; return map of unidentified 2nd types for given colour + std::map & unknown_types(const Piece::Colour & colour) + { + return ((colour == Piece::WHITE) ? white_unknown : black_unknown); + } + + int & nUnknown(const Piece::Colour & colour) + { + return ((colour == Piece::WHITE) ? white_nUnknown : black_nUnknown); + } + + // helper; return king given player colour + Piece * king(const Piece::Colour & colour) {return ((colour == Piece::WHITE) ? white_king : black_king);} + + void Update_move(int x, int y, int x2, int y2); // move a piece + void Update_select(int x, int y, int index, const std::string & type); // update a selected piece + void Update_select(int x, int y, int index, const Piece::Type & t); + + Square & square(int x, int y) {return grid[x][y];} // get square on board + + void Get_moves(Square & s, const Piece::Type & t, std::vector & v); + void Get_moves(Piece * p, std::vector & v); // get allowed moves for piece of known type + + // determine if position is on the board + bool Valid_position(int x, int y) const {return (x >= 0 && x <= BOARD_WIDTH-1 && y >= 0 && y <= BOARD_HEIGHT-1);} + + + // scans from a square until a piece is reached + Piece * ScanPiece(Square & s, int vx, int vy); + + // Get a piece + Square & SquareAt(int x, int y); + + private: + Square grid[BOARD_WIDTH][BOARD_HEIGHT]; + + // All pieces for each player + std::vector white; + std::vector black; + // The number of pieces with each 2nd type that are still unidentified + std::map white_unknown; + std::map black_unknown; + int white_nUnknown; + int black_nUnknown; + Piece * white_king; + Piece * black_king; + + + + // Add a move to the vector if it is valid + void CheckMove(Piece * p, int x, int y, std::vector & v); + + // Add all valid moves in a direction, stopping at the first invalid move + void ScanMoves(Piece * p, int vx, int vy, std::vector & v); + + +}; + +/** + * @class Exception + * @purpose The only exception. + */ +class Exception +{ + public: + Exception(const char * funct, const char * fmt, ...) + { + fprintf(stderr, "Exception in %s - ", funct); + va_list va; + va_start(va, fmt); + vfprintf(stderr, fmt, va); + va_end(va); + fprintf(stderr, "\n"); + } + +}; + +#endif //_QCHESS_H + +//EOF