Code:
// some loser chess game
#include "iostream"
#include "math.h"
using namespace std;
int fun(int x, int y)// najes 1d array 2d array
{
return (8*y+x);
}
void naming(int p)// bind a number to each piece(positive white, negitive black)
{
if (p == 1)
cout << " P";
if (p == 2)
cout << " R";
if (p == 3)
cout << " K";
if (p == 4)
cout << " B";
if (p == 5)
cout << " V";
if (p == 6)
cout << " Q";
if (p == 0)
cout << " -";
if (p == -1)
cout << " p";
if (p == -2)
cout << " r";
if (p == -3)
cout << " k";
if (p == -4)
cout << " b";
if (p == -5)
cout << " v";
if (p == -6)
cout << " q";
}
void drawchessboard(int chess[]) // draw the chess board
{
cout << endl << endl << endl << " 1 2 3 4 5 6 7 8" << endl;
int b;
for(int x = 0; x < 8 ;x++)
{
cout << " " << endl << endl << x+1;
for(int y = 0; y < 8; y++)
{
b = fun(y,x);
naming(chess[b]);
}
}
}
bool game(int chess[]) // set up the win/lose function
{
int b;
int w;
int p;
for( p = 0; p < 64; p++)
{
if (chess[p] == 5)
w = 1;
if (chess[p] == -5)
b = 1;
if (b == 1 && w == 1)
return true;
}
return false;
if (b == 1 && w ==0)
cout << "black wins";
else
cout << "White Wins";
}
void execute(int sx, int dx, int sy, int dy, int chess[]) // give moove function
{
chess[fun(dx,dy)] = chess [fun(sx,sy)];
chess[fun(sx, sy)] = 0;
}
bool legalmove(int sx, int dx, int sy, int dy, int st, int dt) // check to se if it is a legal moove
{
if (abs(dx-sx) == 0 && abs(dy-sy) == 0 ) // cant moove to current position
return false;
else if (abs(st) == 2 && (abs(sy-dy)==0 || abs(sx-dx)==0)) // if rook and moving forward,back,left,or right then it can moove
return true;
else if (abs(st) == 4 && abs(dx-sx) == abs(sy-dy))// checks to maek sure bishop is mooving diagonally
return true;
else if (abs(st) == 5 && abs(dx-sx) <= 1 && abs(dy-sy) <= 1)
return true;
else if (abs(st) == 6 && ((abs(sy-dy)==0) || abs(sx-dx)==0) || (abs(dx-sx) == abs(sy-dy)))
return true;
else if (abs(st) == 3 && (abs(sy-dy) == 1 && abs(sx-dx) == 2) || (abs(sy-dy) ==2 && abs(sx-dx) == 1))
return true;
else if ((st) == 1 && (dy-sy) == 1 && dt == 0 && (dx-sx) == 0)
return true;
else if ((st) == 1 && (dy-sy) == 2 && dt == 0 && (dx-sx) == 0 && sy == 1)
return true;
else if ((st) == 1 && abs(dx-sx) == 1 && (dy-sy) == 1 && dt < 0)
return true;
else if ((st) == -1 && (dy-sy) == -1 && dt == 0 && (dx-sx) == 0)
return true;
else if ((st) == -1 && (dy-sy) == -2 && dt == 0 && (dx-sx) == 0 && sy == 6)
return true;
else if ((st) == -1 && abs(dx-sx) == 1 && (dy-sy) == -1 && dt > 0)
return true;
// kyles mom
return false;
}
bool inbounds(int sx, int dx, int sy, int dy)
{
if (sx > -1 && sx < 8 && dx > -1 && dx < 8 && sy > -1 && sy < 8 && dy > -1 && dy < 8)
return true;
else
return false;
}
bool turn(int color, int st)
{
if(color == 0 && st > 0)
return true;
if(color == 1 && st < 0)
return true;
else
return false;
}
// MAIN _________________
int main()
{
int chess[64] = {2, 3, 4, 5, 6, 4, 3, 2, 1,1,1,1,1,1,1,1, 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,
-1, -1, -1, -1, -1, -1, -1, -1, -2, -3, -4, -6, -5, -4, -3, -2};
int sx, sy, dx, dy, col, st, dt;
int color = 0;
while (game(chess))
{
drawchessboard(chess);
if (color)
cout << endl << endl << "it is black's turn";
else cout << endl << endl << "it is white's turn";
cout << endl << endl << "please type in the x and y coordinates of the selected piece and the destination of the piece in that order." << endl;
cin >> sx >> sy >> dx >> dy;
sx--;sy--;dx--;dy--;
if (inbounds(sx, dx, sy, dy))
{
st = chess[fun(sx, sy)];
dt = chess[fun(dx, dy)];
if ((st * dt <= 0) && legalmove(sx, dx, sy, dy, st, dt) && turn(color, st))
{
execute(sx, dx, sy, dy, chess);
color =!color;
}
else
cout << "illegal move";
}
}
cin.get();
return 0;
}