ok, it's quite crude because i haven't added anything in the way of rules governing legality of moves, turn system, and game-deciding factors such as checkmate, check, or castling.
i'm a real newb to this stuff (i learned this stuff like... two days ago... i only know the syntax for for loops, if statements, cout and cin (the getch thing included in the code had been a remedy to constantly having to press enter all the time), arrays and arrays within arrays (i'm guessing you can make multidimensional arrays with something like... array[n(1)][n(2)][n(3)]...[n(n-1)][n(n)]???), declaring variable of different types (char for letters and int for numbers... and i also know that the declaration double or float makes non-integer numbers) and.. making functions outside the main thing...
to be frank, i'm actually a little proud of the approach i took in making this lil proggy. i thought about it for a large portion of yesterday, mulling over if it was actually even possible to begin with (which eventually led me to taking this approach).
so basically what happens:
i have this char-based 8x8 matrix called board[8][8] that's filled in using a populator function.
i also have a char-based variable cursor_storage that "stores" the value currently holding the cursor.
by "holding" i mean that the cursor can "pick up" items on the board, and the item that had been on the board is replaced by a blank space (represented by a period). you'll see this in the function void movement().
look at the functions display() and movement() to see exactly how i made the cursor possible...
anyway on to the main code:
Code:
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
//let us write a single matrix chess program.
int i;
int j;
char command;
char board[7][7];
int xpos;
int ypos;
int pickupstate;
char cursor_storage;
void populator()
{
for (i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
board[i][j]='.';
}
} //seeds the board with periods as placeholders
//the following is the specific placement of pieces; capital letters
//represent white pieces, while the lowercase letters represent white
//pieces.
board[0][0]='r';
board[0][1]='n';
board[0][2]='b';
board[0][3]='q';
board[0][4]='k';
board[0][5]='b';
board[0][6]='n';
board[0][7]='r';
board[7][0]='R';
board[7][1]='N';
board[7][2]='B';
board[7][3]='Q';
board[7][4]='K';
board[7][5]='B';
board[7][6]='N';
board[7][7]='R';
for(i=0;i<8;i++)
{
board[1][i]='p';
board[6][i]='P';
}
xpos=0;
ypos=0;
cursor_storage='*';
pickupstate=0;
//these are simply initial values.
}
void display()
{
for(i=0;i<8;i++)
{
for(j=0;j<8;j++)
{
if(i!=ypos || j!=xpos)
cout<<board[i][j];
if(i==ypos && j==xpos)
cout<<cursor_storage;
}
cout<<endl;
//displays the board if the current coordinates
//being represented (i,j) are not in accordance with the
//'real coordinates' of where the cursor currently 'is'
//(ypos,xpos). if i and j are both equal to ypos/xpos
//respectively, the character currently stored as the
//cursor marker is displayed instead of board[i][j].
}
}
void movement()
{
command=getch();
if(command=='w')//northward cursor movement
{
ypos=ypos-1;
}
if(command=='s')//southward cursor movement
{
ypos++;
}
if(command=='a')//westward cusor movement
{
xpos=xpos-1;
}
if(command=='d')//eastward cursor movement
{
xpos++;
}
if(command=='k')//'k' is the button to pickup/release pieces currently on
//the board.
{
if(pickupstate==0)//pickupstate of 0 indicates no piece
//currently 'held' by the cursor
{
cursor_storage=board[ypos][xpos];
board[ypos][xpos]='.';
pickupstate=1;
}
if(pickupstate==1)//pickupstate of 1 indicates a piece
//is currently being held by cursor
{
board[ypos][xpos]=cursor_storage;
cursor_storage='*';
pickupstate=0;
}
}
system("cls"); //clears the after images to make it look like asingleboard
}
int main()//now i just call in the three functions in an infinte loop.
//press ctrl+c to terminate the program.
{
populator();
for(;;)
{
display();
movement();
}
return 0;
}
if you compile and run this prog, you'll find a gross bug/error that i have no idea on how to circumvent. first of all, it doesn't properly display the board matrix... it seems that a portion of the board is warped on the right side, making for a "protruding" pawn line and rear ranks. secondly, half the rear rank for the white side, the right side of this rank, does not display correctly and instead shows some eerie lines that i know must not be "real" computer letters like abc's, 123's, and !@#$%^&*() etc. also the computer makes a beeping noise whenever the the cursor is sent to the right-most column (therefore, whenever xpos=7, i guess... but then again maybe not because of that "protrusion" phenomenon that i mentioned earlier). thirdly, the cursor just won't pick items on the board up. refer to the lines in movement() to see... i mean, i'm just so sure it should work, but it doesn't. but then again that's what bugs are, heh.
oh, and use wasd keys to move the thing around. "k" is supposed to pick stuff up, but like i said... it doesn;t work. oh and another thing is not to move past the boundaries. i haven;t yet implemented any counter measures for that :P and, sorry for the ;'s instead of apostrophes, i broke my right hand (the pinky... like not the actual finger but the bone actually "in the palm", according to the doctor) yesterday when i punched my wall... ring finger's been real busy since yesterday hehe.
please help!~ i'm really desperate after sitting here for three hours to no avail.