5 Replies - 15210 Views - Last Post: 16 April 2009 - 08:20 AM Rate Topic: -----

#1 jojopaulm  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 25-November 08

chess programming in c language

Post icon  Posted 25 November 2008 - 03:58 AM

plz help me in doing chess program in c language
Is This A Good Question/Topic? 0
  • +

Replies To: chess programming in c language

#2 AmitTheInfinity  Icon User is offline

  • C Surfing ∞
  • member icon

Reputation: 117
  • View blog
  • Posts: 1,559
  • Joined: 25-January 07

Re: chess programming in c language

Posted 25 November 2008 - 04:23 AM

People here would be able to help you better, if you post whatever you have tried till now. And your problems in it, if you are facing any.
Was This Post Helpful? 0
  • +
  • -

#3 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: chess programming in c language

Posted 26 November 2008 - 12:55 AM

Help yourself by asking specific questions.

If you'd simply like to know where to start, then you should say that. How are we to know what you've already done and what you haven't? We need you to help us help you.
Was This Post Helpful? 0
  • +
  • -

#4 Deepglue555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 116
  • Joined: 13-April 09

Re: chess programming in c language

Posted 13 April 2009 - 09:02 AM

View Postjojopaulm, on 25 Nov, 2008 - 02:58 AM, said:

plz help me in doing chess program in c language



well for starters you need a board.

int board[64];

if you do some research there are multiple ways to express the 8x8 array.
Including 0x88 and mailbox.

If you prefer simplicity I reccomend integer[64] rather than integer[8][8];
But if you prefer speed then the mailbox or 0x88 method is better.

If i is a index for a square number 0 - 63 then to get files (assuming 0 = 'a1' and 63 = 'h8')
Then i mod 8 == file (where 0 - 7 represents 'a' to 'h') in C++: file = i % 8;
and to get the rank (0-7) then use int(i / 8) (or in c++ (int) i/8.

I reccomend [8][8] that way you'll never waste cycles converting [64] to [8][8] in the move generation.

Also see article on AlphaBeta.

Move generation, In my program I had the output of the move generation in a single string.

"e2e4f2f4g2g4" etc
But i made the mistake of putting a move every four characters instead if i had done it in 5 char i could have done underpromotions as separate moves.

ie "e2e4 e7e8Qe7e8Re7e8Be7f8Q" etc

another problem i encountered was hash tables and opening Book, Database.
Seeing as FEN strings come in varied lengths, i invented my own notation.
Where each character represents 'a1' - 'h8'
ie "RNBQKBNRPPPPPPPP---------------------------------ppppppppprnbqkbnr"
then separated with underscores whose turn it is to play b/w (lowercase) then enpassant e6, f6, c6 or '--' etc
followed by castling (whether castling was ruined or not) represented by white KQ (kinside and Queenside and black kq (lowercase for black)
example 'RNLQKLNRPPPP-PPP------------P-----p-------------pp-ppppprnlqklnr_w_--_KQkq_'

if you know chess you will see i left out the fifty move rule.(which FEN includes)
which will rarely affect the positions transposition value unless it is near 50 (100 plys).
you could make a statement to see if plys since a capture or pawn move ie 50 move rule is greater than 40 moves or 80 plys but otherwise state it as xx or -- if its under that.
that way you can compare positions by C++ string comparisons ie if (string1 == string2) -position matches if true.

C++ supports strings but C doesn't support the same thing.

If you need more simple than this just ask :D
Was This Post Helpful? 1
  • +
  • -

#5 Deepglue555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 116
  • Joined: 13-April 09

Re: chess programming in c language

Posted 14 April 2009 - 08:14 AM

View Postjojopaulm, on 25 Nov, 2008 - 02:58 AM, said:

plz help me in doing chess program in c language



Well for starters I hope you mean C++ rather than 'C'.

for starters you need a board:

 int board[8][8]; 


2nd you need values that indicate the pieces:

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;


you will need a container to store whose move it is:
 
string onmove;
// if onmove = 'w' (lowercase always for simplicity) then its whites turn to move otherwise onmove should be set to 'b'



then you need the start position stored for 'new game'
const startup[64] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};

void setup (void) {
int i;
	for (i = 0; i < 64; i++){
	board[i] = startup[i]; //setup starting position
	}
  onmove = 'w';

}


from there you are on your own, you still need to parse the move input and decide if the given move is legal.
there are more rules, like, enpassant and castling, 3rd repetition, and fifty move rule draw and insufficient material draw. also checkmate and stalemate.

anyhow good luck.
Was This Post Helpful? 0
  • +
  • -

#6 Deepglue555  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 116
  • Joined: 13-April 09

Re: chess programming in c language

Posted 16 April 2009 - 08:20 AM

View PostDeepglue555, on 14 Apr, 2009 - 07:14 AM, said:

View Postjojopaulm, on 25 Nov, 2008 - 02:58 AM, said:

plz help me in doing chess program in c language



Well for starters I hope you mean C++ rather than 'C'.

for starters you need a board:

 int board[8][8]; 


2nd you need values that indicate the pieces:

const int pawn = 100;
const int bishop = 305;
const int knight = 300;
const int rook = 500;
const int queen = 900;
const int king = 2000;


you will need a container to store whose move it is:
 
string onmove;
// if onmove = 'w' (lowercase always for simplicity) then its whites turn to move otherwise onmove should be set to 'b'



then you need the start position stored for 'new game'
const startup[64] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};

void setup (void) {
int i;
	for (i = 0; i < 64; i++){
	board[i] = startup[i]; //setup starting position
	}
  onmove = 'w';

}


from there you are on your own, you still need to parse the move input and decide if the given move is legal.
there are more rules, like, enpassant and castling, 3rd repetition, and fifty move rule draw and insufficient material draw. also checkmate and stalemate.

anyhow good luck.


Correction:

const startup[8][8] = { rook, knight, bishop, queen, king, bishop, knight, rook, pawn, pawn,pawn,pawn,pawn,pawn,pawn, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -rook, -knight, -bishop, -queen, -king, -bishop, -knight, -rook};

void setup (void) {
int i;
	for (i = 0; i < 8; i++){
	  for (j = 0; j < 8; j++){
	board[i][j] = startup[i][j]; //setup starting position
	}
}
  onmove = 'w';

}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1