1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
|
#include "stdafx.h"
#include <iostream>
using namespace std;
class Piece
{
protected:
char Color;
char PieceName;
int Value;
public:
friend class Board;
friend class ChessGame;
virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]);
};
class Pawn : public Piece
{
public:
friend class Board;
Pawn(char PieceColor)
{
Color = PieceColor;
PieceName = 'P';
Value = 1;
}
~Pawn(){ }
virtual bool CheckValidMove(int CurrRow, int CurrCol, int DestRow, int DestCol, Piece* Square[][8]) //Checking valid move for white pawns
{
if(Color == 'W') // Checking valid move for white pawns
{
if(CurrRow == 6 && CurrRow - DestRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid moves for first move of a white pawn
{
return true;
}
else
if(CurrRow - DestRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for normalwhite pawn move
{
return true;
}
else
if(CurrRow - DestRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'W') //Checking valid move for killing move of white pawn
{
return true;
}
else
{
return false;
}
}
else //Checking valid move for blackpawns
{
if(CurrRow == 1 && DestRow - CurrRow == 2 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for first move of a black pawns
{
return true;
}
else
if(DestRow - CurrRow == 1 && CurrCol == DestCol && Square[DestRow][DestCol] == 0) //Checking valid move for normal white pawn move
{
return true;
}
else
if(DestRow - CurrRow == 1 && (CurrCol - DestCol == 1 || DestCol - CurrCol == 1) && Square[DestRow][DestCol] != 0 && Square[DestRow][DestCol]->Color != 'B') //Checking valid move for killing move of a white pawn
{
return true;
}
else
{
return false;
}
}
}
};
//Similar classes for the other pieces
class Board
{
Piece* Square[8][8];
public:
friend class ChessGame;
friend class Piece;
friend bool CheckValidMove(int, int, int, int, Piece*);
Board()
{ }//set all the pieces on the board
}
~Board()
{
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
Square[i][j] = 0;
}
}
}
void DisplayBoard()
{
cout << "\n\t\t\t\t Chess" << endl << endl;
cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl;
cout << "" << (char) 179 << " " << (char)179 << endl;
cout << "" << (char) 179 << " " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << " " << (char)179 << endl;
for(int row = 0; row < 8; row++)
{
cout << "" << (char) 179 << " " << (char) 179 << "";
if(row%2 == 0)
{
cout << " :::: :::: :::: ::::" << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row)) << "" << (char) 179 << "";
for(int col = 0; col < 8; col++)
{
if(col % 2 == 0)
{
cout << " ";
if(Square[row][col] == 0)
{ cout << " ";}
else
{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
cout << " ";
}
else
{
cout << ":";
if(Square[row][col] == 0)
{ cout << "::";}
else
{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
cout << ":";
}
if(col == 7)
{
cout << (char)179 << " " << (char)179;
}
}
cout << "\n" << (char) 179 << " " << (char) 179 << " :::: :::: :::: ::::" << (char)179 << " " << (char)179 << "" << endl;
}
else
if(row % 2 == 1)
{
cout << ":::: :::: :::: :::: " << (char)179 << " " << (char)179 << "\n" << (char) 179 << "" << static_cast<char>(64+(8-row)) << "" << (char) 179 << "";
for(int col = 0; col < 8; col++)
{
if(col % 2 == 0)
{
cout << ":";
if(Square[row][col] == 0)
{ cout << "::";}
else
{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
cout << ":";
}
else
{
cout << " ";
if(Square[row][col] == 0)
{ cout << " ";}
else
{ cout << Square[row][col]->Color << Square[row][col]->PieceName;}
cout << " ";
}
if(col == 7)
{
cout << (char)179 << " " << (char)179;
}
}
cout << "\n" << (char) 179 << " " << (char) 179 << ":::: :::: :::: :::: " << (char)179 << " " << (char)179 << "" << endl;
}
}
cout << "" << (char) 179 << " " << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << " " << (char)179 << endl;
cout << "" << (char) 179 << " 1 2 3 4 5 6 7 8 " << (char)179 << endl;
cout << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << (char)196 << endl << endl;
}
};
class ChessGame
{ } //Game content
int main()
{}
|