# Thread: Chess board coordinate moves input/output

1. ## Chess board coordinate moves input/output

Hi
I am trying to make a simple C function to deal with inputting moves to a console chessboard (either 1D [64] array or 2D [8][8] array). I am only an amateur/beginner programmer.
I think the simplest function might be to input moves in pgn LAN notation(ie 1.e2e4 b8c6 etc).
I saw an implementation for a C++ board on a thread here 2003/4 at c++ chess game help

I think this is a reasonable approach to input/output moves and I am trying to adapt that code for C and to work properly (ie take move input as b2b4 and display board with chess char P/R/p/r etc shown moved and empty space ' ' where the char piece moved from).

So far I haven't got it working properly and I would be very grateful if anyone would look at my code and suggest how I can fix it to input moves properly and update the board array properly- ie get it working properly! I look forward to helpful replies-thanks

Code:
```#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

// function prototypes
void setup_board();
void display_board();
void set_coords();

// global variables
char piece_moved;
char board[10][10];
char choice;
char move[5];
short x, y;
short checkmate;

int main()
{
setup_board();
printf("\n");
display_board();

while(!checkmate)
{
printf("\nEnter Move From: ");
scanf("%s", move); // ex: b2

set_coords();

piece_moved = board[x][y];

board[x][y] = ' ';

printf("Enter Move To: ");
scanf("%s", move); // ex: b4

set_coords();

board[x][y] = piece_moved;
printf("\n");
display_board();
}

//system("PAUSE");
getchar();
return 0;
}

void setup_board()
{
// clear boxes
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
board[x][y] = ' ';
}
}
// color board
for(x=0;x<8;x++)
{
for(y=0;y<9;y++)
{
board[x][y] = '-';
}
}
// upper pieces
board[0][1] = 'r';
board[0][2] = 'n';
board[0][3] = 'b';
board[0][4] = 'q';
board[0][5] = 'k';
board[0][6] = 'b';
board[0][7] = 'n';
board[0][8] = 'r';

board[1][1] = 'p';
board[1][2] = 'p';
board[1][3] = 'p';
board[1][4] = 'p';
board[1][5] = 'p';
board[1][6] = 'p';
board[1][7] = 'p';
board[1][8] = 'p';

// lower piece
board[6][1] = 'P';
board[6][2] = 'P';
board[6][3] = 'P';
board[6][4] = 'P';
board[6][5] = 'P';
board[6][6] = 'P';
board[6][7] = 'P';
board[6][8] = 'P';

board[7][1] = 'R';
board[7][2] = 'N';
board[7][3] = 'B';
board[7][4] = 'Q';
board[7][5] = 'K';
board[7][6] = 'B';
board[7][7] = 'N';
board[7][8] = 'R';

// notation help
board[7][0] = '1';
board[6][0] = '2';
board[5][0] = '3';
board[4][0] = '4';
board[3][0] = '5';
board[2][0] = '6';
board[1][0] = '7';
board[0][0] = '8';

board[9][1] = 'A';
board[9][2] = 'B';
board[9][3] = 'C';
board[9][4] = 'D';
board[9][5] = 'E';
board[9][6] = 'F';
board[9][7] = 'G';
board[9][8] = 'H';

}

void display_board()
{
for(x=0;x<10;x++)
{
for(y=0;y<10;y++)
{
printf("%c", board[x][y]);
}
printf("\n");
}
printf("\n");
}

void set_coords()
{
// get x value from character
if(strchr(move,'a'))
x = 7;
else if(strchr(move,'b'))
x = 6;
else if(strchr(move,'c'))
x = 5;
else if(strchr(move,'d'))
x = 4;
else if(strchr(move,'e'))
x = 3;
else if(strchr(move,'f'))
x = 2;
else if(strchr(move,'g'))
x = 1;
else if(strchr(move,'h'))
x = 0;
// get y value from character
if(strchr(move,'8'))
y = 0;
else if(strchr(move,'7'))
y = 1;
else if(strchr(move,'6'))
y = 2;
else if(strchr(move,'5'))
y = 3;
else if(strchr(move,'4'))
y = 4;
else if(strchr(move,'3'))
y = 5;
else if(strchr(move,'2'))
y = 6;
else  if(strchr(move,'1'))
y = 7;
}```

2. When i built software for reversi , i think that the input was something like yours(pretty much).
So i used fgets to read the whole line into a buffer.Then every element of the buffer contains what you want and can be easily be accessed.

3. I agree with std10093, pick apart the move:fromCol fromRow toCol toRow, variables assigned from move[0] through move[4], but then you have the weird moves like a pawn reaching the last row, etc.

These are highly recommended chess programming resources:

The first two are the best chess programming forums - talk chess especially (but they do squabble a lot and with good reason). They have a few programmers there that either are or have been, among the top programs in the world.

Note: The "clone" wars are definitely in progress. Don't mention clones unless you want your thread to be inundated with that topic, in Talk Chess. Open Chess has a slower and more relaxed atmosphere, and a very nice chess game re-player.

OpenChess &bull; Index page
TalkChess.com :: Index

This has a ton of chess programming info:
http://chessprogramming.wikispaces.com/

There are hundreds of open source chess programs that you can study also. Google "First Chess", for a VERY simple start. (It was intended as a tutorial, not a competitive player).

4. Thanks std10093 & Adak I will try your advice but may (almost certainly) need more help to get the C function to work properly.

I first started looking at chess programming several years ago when I wanted to clean & manipulate large pgn database files as some programs needed strict pgn values. I started with Perl & regexes & that more or less fixed my problems (Perl is good at text/data wrangling-but I only learnt the basics of using Perl and Perl on Windows). Then I wanted to try to actually make my own chess program (engine + gui) but was bewildered by the number of languages that chess programs can be made in. I opted to look at Java for as well as being able to do programming logic it has better graphic capabilities than some other native languages (esp C). I found it hard going though to program as I am self taught & I made a Java chess game with help from ThinkQuest and managed to do some Java Swing etc.
Recently I thought it would be good to actually make my own chess engine so I understood every bit of the code & not just copy & paste some functions in and only vaguely know what they were doing. I elected to try C and have been trying to learn C over the last couple of months. Every time I look to find info on what I want done for a chess engine in C I find the info for some other language eg python,ruby,C++,C#,VisualC++,Basic etc etc etc. The basic C engines MSCP/TSCP/FirstChess/SecondChess may be simple and basic to seasoned C programmers but to a novice like me much of the code remains a mystery! I do acknowledge they are helpful to look for guidance to. I did subscribe a few years ago to Winboard forum where I found conflicting help. Some members were helpful & supportive but others were so unhelpful and arrogant. The arrogant ones saying in effect it is impossible for a novice programmer to make a chess engine and the only worthy engines were theirs with ELO ratings of 3000+ using parallelised processing and super human algorithms throughout! Ha... cobblers to their arrogant attitude!:-) I enjoy chess & chess programming-end of story!

I still aim to make my simple/basic chess engine and fully realise that understanding & implementing basic search algorithms (minimax) and move generators is a challenge for me in C. I am enjoying learning C and feel I may be able later to use some of the concepts to better understand my other coding in my web programming and developing (with HTML5,JavaScript & PHP & ASP etc) which is my main area of expertise.

Goodluck to all here and especially all C chess programmers!

5. On the Chess programming forums, you MUST absolutely ignore the attitudes! There's one or two that frankly, should be checking their meds, if not straightaway be locked up! They rarely appear, thank the heavens. But several of them are elitist snobs and severely condescending.

Water off a ducks back is the only attitude that gets you by, over there.

I actually learned C because I wanted to program a chess game - this was way back when Kaissa and Stanford's chess program played for the World Championship, but the game had to be adjudicated by a chess Master. Neither program had the ability to play the end game!

By the time I got a computer and had learned enough C to start however, the programs had already surpassed my chess playing skills, and my ideas for them playing in the end game, as well. Thanks largely to the end game table bases, which allow perfect play.

It's hard to play against perfection - wears you down!

I should dig out that old code and see if it will compile on today's compiler.

I hope you will come back and ask away. After all these years, I think Chess programming algorithms, and the programs playing, is fascinating.

6. I agree chess programming is fascinating and it appears almost a rite of passage for programmers in all languages with classic puzzles as Knights tour & 8 Queens checks etc and of course the many ways to represent chess in code.
Anyway I am having a torrid & horrid time with this coordinate code. I have spent an hour changing the code to try to use moveto & movefrom variables with no success so far. It may end up being a complete rewrite (will be alright if I learn something from this!).I am also trying to use array as function parameter eg displayboard(board [x][y]) with no success. I haven't tried using arrays as function arguments/parameters before! I must search for rules and examples to learn about that! I have looked at some TicTacToe examples in C but cannot remember if they were helpful in this respect of move entry - I will have to look at them again! For tttoe a player enters a character (X or O usually) on a board using the board coordinates but this seems to me different to the chess model where the pieces are already on the board and a player enters coordinates to move a piece(character) around.

I will continue to work at this and try to get it working but meanwhile I would be most grateful if anyone could give more help & advice and post some working code to show me how to get this coordinate input/output working. Thanks

7. Well I have got it working finally (it took me quite sometime and it has nearly driven me completely nutty!).
I am though pleased it is working and now I can consider improving it! (It would be nice to add AI/computer player but that probably will send me nutty-I might try though :-) Firstly though I might pretty up the board & put the coords back & it would be good to get the move input in one line eg e2e4 & then update the board).

To get it working I kept playing with the array indices but with no success so I ditched the board notation and went back to a basic array board[8][8]
and again redited the set coords() function and so it is a near complete rewrite and (hallelujah) the input move and board update works. Here is a sample console output to show it working:
rnbqkbnr
pppppppp
--------
--------
--------
--------
PPPPPPPP
RNBQKBNR

Enter Move From: e2
Enter Move To: e4

rnbqkbnr
pppppppp
--------
--------
----P---
--------
PPPP PPP
RNBQKBNR

Enter Move From: e7
Enter Move To: e5

rnbqkbnr
pppp ppp
--------
----p---
----P---
--------
PPPP PPP
RNBQKBNR

Enter Move From: g1
Enter Move To: f3

rnbqkbnr
pppp ppp
--------
----p---
----P---
-----N--
PPPP PPP
RNBQKB R

Enter Move From: b8
Enter Move To: c6

r bqkbnr
pppp ppp
--n-----
----p---
----P---
-----N--
PPPP PPP
RNBQKB R

Enter Move From: f3
Enter Move To: e5

r bqkbnr
pppp ppp
--n-----
----N---
----P---
----- --
PPPP PPP
RNBQKB R

Enter Move From:

Neat (but it will never replace ChessMaster or Fritz chess programs!).The joys of C programming! Onwards and best wishes to all :-)

8. To: richardpd

Hello richardpd,
I have been trying to make a simple C chess program like the one you've shown for a few days now. I have been reading forums and reading from a C book, but can't quite put all the pieces together myself. How'd did you finally figure it out? Would you mind showing the edited code you made? I played with the code you posted above originally and found that the coordinates were not entered properly into the else if statements. But even then as I was moving the pieces the '-' marks would disappear wherever the pieces had been. How would you make it so the '-' would come back?

9. If a piece is moved, mark the from square as empty, print that empty square with the empty square char '-', and assign the to square with the new value, and print it in that square.

10. @richardpd - good effort mate, that is a great problem to work on in char session, nice result - bet you learnt loads, well done

11. I had a thought how you might advance this without getting to far into the AI side - something you could build on top of your board handling.

You know in notation that if your piece is a pawn, say in the opening, you can just note 'e4' - and the convention is that your pawn has moved 2.
equally if there is a piece available for capture by the pawn (lets say pawn on e4) you will just see the game annotated 'd5'
lets refer to the above as a 'shorthand situation'

So you might allow the player to only enter one value for their move - then if it is not a shorthand situation your program prompts for the 'move to' location

maybe this will illustrate what i mean:

(input prompt changes to 'enter move)

P1 enter move:
e4

P2 enter move:
d5

P1 enter move:
d5
white pawn captures black pawn on d5

P2 enter move:
B7
enter landing square (knight):
C6
black knight moves to edge of board

P1 enter move:
d6