Skip to content
Snippets Groups Projects
Commit 6f1271fc authored by Bolla Péter's avatar Bolla Péter
Browse files

Upload New File

parent f117f65a
No related branches found
No related tags found
No related merge requests found
chess.cpp 0 → 100644
#include "chess.hpp"
#include "math.h"
Chess::Chess(int w, int h)
{
for(int s = 0; s<64; s++)
{
WID = w;
HEIG = h;
Piece p;
bool o;
int row = floor(s/8);
int col = s%8;
Color c = (row%2 == col%2) ? Black : White;
if(row == 0 || row == 1) //Bottom 2 rows (all the squares have Light pieces)
{
o = true;
p.team = Light;
if(col == 1 || col == 6)
{
p.type = Pawn;
}
else if(col == 0 || col == 7)
{
p.type = Rook;
}
else if(col == 1 || col == 6)
{
p.type = Knight;
}
else if(col == 2 || col == 5)
{
p.type = Bishop;
}
else if(col == 3)
{
p.type = Queen;
}
else if (col == 4)
{
p.type = King;
}
}
else if(row == 6 || row == 7) //Top 2 rows (all the squares have Dark pieces)
{
o = true;
p.team = Dark;
if(col == 1 || col == 6)
{
p.type = Pawn;
}
else if(col == 0 || col == 7)
{
p.type = Rook;
}
else if(col == 1 || col == 6)
{
p.type = Knight;
}
else if(col == 2 || col == 5)
{
p.type = Bishop;
}
else if(col == 3)
{
p.type = Queen;
}
else if (col == 4)
{
p.type = King;
}
}
else{
o = false;
p.type = Empty;
p.team = None;
}
Table.push_back({p,c,o,row,col});
}
}
void Chess::Move(int oi, int ti)
{
/*int _or = 8-_oy/HEIG *8;
int _oc = _ox/WID *8;
int _tr = 8 - _ty/HEIG *8;
int _tc = _tx/WID *8;
int oi;
int ti;
for(int i = 0; i<Table.size(); i++)
{
if(Table[i].row == _or && Table[i].column == _oc)
{
oi = i;
}
else if(Table[i].row == _tr && Table[i].column == _tc)
{
ti = i;
}
}*/
int dr = Table[oi].row-Table[ti].row; //Row difference; <0 if moving "up" | >0 if moving "down"
int dc = Table[oi].column-Table[ti].column; //Column difference; <0 if moving right | >0 if moving left
bool allempty = true;
switch(Table[oi].piece.type)
{
case Pawn:{
if(Table[oi].piece.team == Light)
{
if(Table[ti].occupied)
{
if(dr == -1 && (dc == 1 || dc == -1) && CanCapture(oi, ti)) //CAPTURE MOVE
{
//Move&Capture
}
else{
//Don't move
}
}
else{
if(dr == -1 && dc == 0) //ONE MOVE FORWARD
{
//Move
}
else if(dr == -2 && dc == 0) //TWO MOVES FORWARD
{
if(!Table[oi+8].occupied && Table[oi].row == 1)
{
//Move
}
else{
//Don't move
}
}
else{
//Don't move
}
}
}
else if(Table[oi].piece.team == Dark)
{
if(Table[ti].occupied)
{
if(dr == 1 && (dc == 1 || dc == -1) && CanCapture(oi,ti)) //CAPTURE MOVE
{
//Move&Capture
}
else{
//Don't move
}
}
else{
if(dr == 1 && dc == 0) //ONE MOVE FORWARD
{
//Move
}
else if(dr == 2 && dc == 0) //TWO MOVES FORWARD
{
if(!Table[oi+8].occupied && Table[oi].row == 6)
{
//Move
}
else{
//Don't move
}
}
else{
//Don't move
}
}
}
else{
//Don't move
}
break;
}
case Rook: {
if(dr == 0)
{
if(dc < 0)//Move to the right
{
for(int d = 1; d<abs(dc)-1 && allempty; d++)
{
}
}
}
break;
}
case Knight:
case Bishop:{
if(abs(dr) == abs(dc) && dr !=0)
{
if(dr>0 && dc>0) //Move "down-left"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi-9].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
//Move
}
else if(CanCapture(oi, ti))
{
//Capture
}
else{
//Don't move
}
}
else{
//Don't move
}
}
else if(dr>0 && dc <0) //Move "down-right"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi-7].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
//Move
}
else if(CanCapture(oi, ti))
{
//Capture
}
else{
//Don't move
}
}
else{
//Don't move
}
}
else if(dr<0 && dc < 0)//Move "up-right"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi+9].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
//Move
}
else if(CanCapture(oi, ti))
{
//Capture
}
else{
//Don't move
}
}
else{
//Don't move
}
}
else if(dr<0 && dc>0) //Move "up-left"
{
for(int d = 1; d<abs(dr)-1 && allempty; d++)
{
if(!Table[oi+7].occupied && allempty)
{
allempty = false;
}
}
if(allempty)
{
if(!Table[ti].occupied) {
//Move
}
else if(CanCapture(oi, ti))
{
//Capture
}
else{
//Don't move
}
}
else{
//Don't move
}
}
}
else{
//Don't move
}
break;
}
case King:
case Queen:
case Empty:
break;
}
}
void Chess::Capture(int _tx, int _ty)
{
}
bool Chess::Occupied(int x, int y)
{
for(Square t: Table)
{
if(t.row == x && t.column == y) return t.occupied;
}
}
bool Chess::Opponents(int i1, int i2)
{
if(Table[i1].occupied && Table[i2].occupied)
{
return Table[i1].piece.team!=Table[i2].piece.team;
}
else{
return false;
}
}
bool Chess::CanCapture(int i1, int i2)
{
if(Opponents(i1, i2) && Table[i2].piece.type != King) return true;
else return false;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment