Skip to content

Instantly share code, notes, and snippets.

@stephenmm
Created August 11, 2012 21:33
Show Gist options
  • Save stephenmm/3327470 to your computer and use it in GitHub Desktop.
Save stephenmm/3327470 to your computer and use it in GitHub Desktop.
Start of simple chess analysis tool
/*
g++ -c -Wall -g chess_test.cpp && \
g++ -g -o chess_test.exe chess_test.o &&\
./chess_test.exe
*/
#include <iostream>
#include <iomanip>
#include <list>
#include <vector>
using namespace std;
#define DEF_RESET "\33[0m"
#define DEF_BLACK "\33[30m" /* Black */
#define DEF_RED "\33[31m" /* Red */
#define DEF_GREEN "\33[32m" /* Green */
#define DEF_YELLOW "\33[33m" /* Yellow */
#define DEF_BLUE "\33[34m" /* Blue */
#define DEF_MAGENTA "\33[35m" /* Magenta */
#define DEF_CYAN "\33[36m" /* Cyan */
#define DEF_WHITE "\33[37m" /* White */
#define DEF_BOLDBLACK "\33[1m\33[30m" /* Bold Black */
#define DEF_BOLDRED "\33[1m\33[31m" /* Bold Red */
#define DEF_BOLDGREEN "\33[1m\33[32m" /* Bold Green */
#define DEF_BOLDYELLOW "\33[1m\33[33m" /* Bold Yellow */
#define DEF_BOLDBLUE "\33[1m\33[34m" /* Bold Blue */
#define DEF_BOLDMAGENTA "\33[1m\33[35m" /* Bold Magenta */
#define DEF_BOLDCYAN "\33[1m\33[36m" /* Bold Cyan */
#define DEF_BOLDWHITE "\33[1m\33[37m" /* Bold White */
enum dir_enum { N, NE, E, SE, S, SW, W, NW, K01, K02, K04, K05, K07, K08, K10, K11, CSTL1, CSTL2, DIR_ENUM_COUNT };
enum piece_type_enum { PAWN=1, ROOK=5, BISHOP=3, KNIGHT=3, QUEEN=9, KING=200, PIECE_TYPE_ENUM_COUNT };
enum color_enum { WHITE, BLACK, COLOR_ENUM_COUNT };
template <class T, int WIDTH, int HIEGHT>
class Array2d {
public:
const T& operator ()(size_t col, size_t row) const {
// Assert col < WIDTH and row < HIEGHT
return m_data [( row * WIDTH + col)];
}
T& operator ()(size_t col, size_t row) {
// Assert col < WIDTH and row < HIEGHT
return m_data [( row * WIDTH + col)];
}
private:
T m_data[WIDTH * HIEGHT];
};
typedef struct square;
struct direction {
dir_enum dir;
int max_distance;
int initial_max_distance;
bool attack;
bool moved;
direction( dir_enum _dir, int _max_distance=8, bool _attack=true, int _initial_max_distance=8 ){
dir = _dir;
max_distance = _max_distance;
attack = _attack;
initial_max_distance = _initial_max_distance;
};
};
struct piece {
std::string name;
color_enum color;
piece_type_enum piece_type;
bool is_alive;
int x;
int y;
bool moved;
list<direction*> dirs;
square *s;
piece( square *_s, color_enum _color, std::string _name ) : s(_s), color(_color), name(_name) {
is_alive = true;
moved = false;
name = _name;
};
void print_name() {
cout << " " << name << " ";
};
virtual void set_parameters() = 0; // pure virtual - require derived classes to define this
};
struct pawn : piece {
pawn( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = PAWN;
};
void set_parameters() {
dirs.push_back( new direction( NW, 1, true ) );
dirs.push_back( new direction( N , 1, false, 2 ) );
dirs.push_back( new direction( NE, 1, true ) );
};
};
struct king : piece {
king( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = KING;
};
void set_parameters() {
dirs.push_back( new direction( N , 1 ) );
dirs.push_back( new direction( NE, 1 ) );
dirs.push_back( new direction( E , 1 ) );
dirs.push_back( new direction( SE, 1 ) );
dirs.push_back( new direction( S , 1 ) );
dirs.push_back( new direction( SW, 1 ) );
dirs.push_back( new direction( W , 1 ) );
dirs.push_back( new direction( NW, 1 ) );
dirs.push_back( new direction( CSTL1, 3, false ) );
dirs.push_back( new direction( CSTL2, 3, false ) );
};
};
struct queen : piece {
queen( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = QUEEN;
};
void set_parameters() {
dirs.push_back( new direction( N ) );
dirs.push_back( new direction( NE ) );
dirs.push_back( new direction( E ) );
dirs.push_back( new direction( SE ) );
dirs.push_back( new direction( S ) );
dirs.push_back( new direction( SW ) );
dirs.push_back( new direction( W ) );
dirs.push_back( new direction( NW ) );
};
};
struct rook : piece {
rook( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = ROOK;
};
void set_parameters() {
dirs.push_back( new direction( N ) );
dirs.push_back( new direction( E ) );
dirs.push_back( new direction( S ) );
dirs.push_back( new direction( W ) );
};
};
struct bishop : piece {
bishop( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = BISHOP;
};
void set_parameters() {
dirs.push_back( new direction( NE ) );
dirs.push_back( new direction( SE ) );
dirs.push_back( new direction( SW ) );
dirs.push_back( new direction( NW ) );
};
};
struct knight : piece {
knight( square *_s, color_enum _color, std::string _name ) : piece( _s, _color, _name ) {
set_parameters();
piece_type = KNIGHT;
};
void set_parameters() {
dirs.push_back( new direction( K01, 1 ) );
dirs.push_back( new direction( K02, 1 ) );
dirs.push_back( new direction( K04, 1 ) );
dirs.push_back( new direction( K05, 1 ) );
dirs.push_back( new direction( K07, 1 ) );
dirs.push_back( new direction( K08, 1 ) );
dirs.push_back( new direction( K10, 1 ) );
dirs.push_back( new direction( K11, 1 ) );
};
};
struct square {
Array2d< square*, 8, 8 >* board;
int x;
int y;
//vector<square*> neighbors;
vector< vector<square*> > neighbors;
piece *p;
square( int _x, int _y, Array2d< square*, 8, 8 >* _board ) {
x = _x;
y = _y;
board = _board;
};
std::string print_square(){
if( p ) return p->name;
return "";
};
void connect_squares(){
for (int i = 0; i < COLOR_ENUM_COUNT; i++) {
neighbors.push_back(vector<square*>()); // Add an empty row
}
vector<square*> temp (8);
neighbors[WHITE] = temp;
neighbors[WHITE][ N ] = ( ( y+1 > 7 )) ? (*board)( x, y+1 ) : 0;
neighbors[WHITE][ NE ] = (( x+1 > 7 ) && ( y+1 > 7 )) ? (*board)( x+1, y+1 ) : 0;
neighbors[WHITE][ E ] = (( x+1 > 7 ) ) ? (*board)( x+1, y ) : 0;
neighbors[WHITE][ SE ] = (( x+1 > 7 ) && ( y-1 < 0 )) ? (*board)( x+1, y-1 ) : 0;
neighbors[WHITE][ S ] = ( ( y-1 < 0 )) ? (*board)( x, y-1 ) : 0;
neighbors[WHITE][ SW ] = (( x-1 < 0 ) && ( y-1 < 0 )) ? (*board)( x-1, y-1 ) : 0;
neighbors[WHITE][ W ] = (( x-1 < 0 ) ) ? (*board)( x-1, y ) : 0;
neighbors[WHITE][ NW ] = (( x-1 < 0 ) && ( y+1 > 7 )) ? (*board)( x-1, y+1 ) : 0;
neighbors[BLACK] = temp;
neighbors[BLACK][ N ] = neighbors[WHITE][ S ];
neighbors[BLACK][ NE ] = neighbors[WHITE][ SW ];
neighbors[BLACK][ E ] = neighbors[WHITE][ W ];
neighbors[BLACK][ SE ] = neighbors[WHITE][ NW ];
neighbors[BLACK][ S ] = neighbors[WHITE][ N ];
neighbors[BLACK][ SW ] = neighbors[WHITE][ NE ];
neighbors[BLACK][ W ] = neighbors[WHITE][ E ];
neighbors[BLACK][ NW ] = neighbors[WHITE][ SE ];
};
void set_piece( piece *_p ){ p = _p; };
piece* get_piece(){ return p; };
void clear_piece(){ p = 0; };
piece* get_first_piece( const dir_enum &dir, const color_enum &color, int &max_depth ){
if(p) return p;
if(!max_depth) return 0;
return neighbors[color][ dir ]->get_first_piece( dir, color, --max_depth );
}
};
struct application {
Array2d< square*, 8, 8 > board;
void start() {
// Create board squares
for( int i=0; i<8; ++i )
for( int j=0; j<8; ++j )
board(i,j) = new square( i, j, &board );
// Connect squares
for( int i=0; i<8; ++i )
for( int j=0; j<8; ++j )
board(i,j)->connect_squares();
// Place initial pieces
board(0,0)->set_piece( new rook( board(0,0), WHITE, "WR1" ) );
board(1,0)->set_piece( new knight( board(1,0), WHITE, "WKN1" ) );
board(2,0)->set_piece( new bishop( board(2,0), WHITE, "WB1" ) );
board(3,0)->set_piece( new queen( board(3,0), WHITE, "WQ1" ) );
board(4,0)->set_piece( new king( board(4,0), WHITE, "WK1" ) );
board(5,0)->set_piece( new bishop( board(5,0), WHITE, "WB2" ) );
board(6,0)->set_piece( new knight( board(6,0), WHITE, "WKN2" ) );
board(7,0)->set_piece( new rook( board(7,0), WHITE, "WR2" ) );
board(0,1)->set_piece( new pawn( board(0,1), WHITE, "WP1" ) );
board(1,1)->set_piece( new pawn( board(1,1), WHITE, "WP2" ) );
board(2,1)->set_piece( new pawn( board(2,1), WHITE, "WP3" ) );
board(3,1)->set_piece( new pawn( board(3,1), WHITE, "WP4" ) );
board(4,1)->set_piece( new pawn( board(4,1), WHITE, "WP5" ) );
board(5,1)->set_piece( new pawn( board(5,1), WHITE, "WP6" ) );
board(6,1)->set_piece( new pawn( board(6,1), WHITE, "WP7" ) );
board(7,1)->set_piece( new pawn( board(7,1), WHITE, "WP8" ) );
board(0,7)->set_piece( new rook( board(0,7), BLACK, "BR1" ) );
board(1,7)->set_piece( new knight( board(1,7), BLACK, "BKN1" ) );
board(2,7)->set_piece( new bishop( board(2,7), BLACK, "BB1" ) );
board(3,7)->set_piece( new queen( board(3,7), BLACK, "BQ1" ) );
board(4,7)->set_piece( new king( board(4,7), BLACK, "BK1" ) );
board(5,7)->set_piece( new bishop( board(5,7), BLACK, "BB2" ) );
board(6,7)->set_piece( new knight( board(6,7), BLACK, "BKN2" ) );
board(7,7)->set_piece( new rook( board(7,7), BLACK, "BR2" ) );
board(0,6)->set_piece( new pawn( board(0,6), BLACK, "BP1" ) );
board(1,6)->set_piece( new pawn( board(1,6), BLACK, "BP2" ) );
board(2,6)->set_piece( new pawn( board(2,6), BLACK, "BP3" ) );
board(3,6)->set_piece( new pawn( board(3,6), BLACK, "BP4" ) );
board(4,6)->set_piece( new pawn( board(4,6), BLACK, "BP5" ) );
board(5,6)->set_piece( new pawn( board(5,6), BLACK, "BP6" ) );
board(6,6)->set_piece( new pawn( board(6,6), BLACK, "BP7" ) );
board(7,6)->set_piece( new pawn( board(7,6), BLACK, "BP8" ) );
print_board();
coordinate_move( 7,1, 7,3 );
print_board();
cout << DEF_RED << "red3 " << DEF_RESET
<< DEF_BOLDRED << "red4 " << DEF_RESET <<endl;
};
void print_board() {
// Print squares
cout << " CURRENT BOARD STATE" << endl;
cout << " ===========================================" << endl;
for( int j=7; j>=0; --j ) {
cout << j << " |";
for( int i=0; i<8; ++i ) {
cout << "|" /*<< i << j*/ ;
cout << setw( 4 ) << left << board(i,j)->print_square();
}
cout << "||" <<endl;
}
cout << " ";
for( int i=0; i<8; ++i ) {
cout << " ";
cout << setw( 3 ) << i;
cout << " ";
}
cout << " " <<endl;
cout << endl;
};
void coordinate_move( int i, int j, int x, int y ) {
board( x, y )->set_piece( board( i, j )->get_piece() );
board( i, j )->set_piece( 0 );
};
};
int main() {
application app;
app.start();
return 0;
}
//
//#include <iostream>
//#include <algorithm>
//#include <vector>
//using namespace std;
//
//int orig_array[] = {1,2,3,4,5,6,7,8,9};
//
//bool IsOdd (int i) { return ((i%2)==1); }
//int num_of_elements() {
// return sizeof( orig_array ) / sizeof( orig_array[0] );
//};
//
//struct square {
// piece* getPiece( char dir, square* init_sqr, piece* init_piece ){
// if(p) return p;
// if( s[dir] ) return s[dir]->getPiece( dir, init_sqr, init_piece );
// return 0;
// }
//};
//
//struct piece {
// // first attempt
// //virtual find_obsticles( square* initiator ) = 0; // pure virtual - require derived classes to define this.
// // second attempt
// piece* find_obsticles( square* sqr ) {
// foreach( potential_move_dir_list dir ){
// //p['n'] = s['n'] ? s['n']->getPiece( 'n', sqr, this ) : 0;
// //if( s[dir] ) {
// // o[dir] = s[dir]->getPiece( dir, sqr, this ) : 0;
// // return p[dir]->getPiece( dir, sqr, this ) : 0;
// //}
// }
// }
//};
//
//struct Rook {
// // first attempt
// //find_obsticles( square* sqr ) {
// // p['n'] = s['n'] ? s['n']->getPiece( 'n', sqr, this ) : 0;
// // p['e'] = s['e'] ? s['e']->getPiece( 'e', sqr, this ) : 0;
// // p['s'] = s['s'] ? s['s']->getPiece( 's', sqr, this ) : 0;
// // p['w'] = s['w'] ? s['w']->getPiece( 'w', sqr, this ) : 0;
// //}
// // second attempt
// // just identify my potential_move_dir_list
//};
//
//int main () {
// vector<int*> orig_vect;
// //vector<int *> orig_vect( orig_array, orig_array + num_of_elements() );
// //vector<int> orig_vect( orig_array, orig_array + (sizeof( orig_array ) / sizeof( orig_array[ 0 ] )) );
// vector<int*> copy_vect;
// vector<int*>::iterator it;
//
// cout << "num_of_elements = " << num_of_elements() << endl;
// for (int i = 0; i < num_of_elements(); ++i)
// orig_vect.push_back( &(orig_array[i]) );
//
// //remove_copy_if (orig_array,orig_array+9,copy_vect.begin(),IsOdd); // Could not get this method to work.
//
// cout << "orig_vect contains:";
// for (it=orig_vect.begin(); it!=orig_vect.end(); ++it)
// cout << " " << **it;
// cout << endl;
//
// for (it=orig_vect.begin(); it!=orig_vect.end(); ++it) {
// if( **it%2 ) // FILTER -- give me just the odd numbers
// copy_vect.push_back( *it );
// }
//
// cout << "copy_vect contains:";
// for (it=copy_vect.begin(); it!=copy_vect.end(); ++it)
// cout << " " << **it;
// cout << endl;
//
// return 0;
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment