0% found this document useful (0 votes)
60 views20 pages

Chess Game

The document describes a chess game program written in C++ using object-oriented programming principles. It includes functions to initialize the board, randomly determine the starting player, and define legal moves for pieces like bishops, rooks, and kings. The main game loop displays the board, tracks turns, and allows player input to select a start and end position for each move.

Uploaded by

Lois Razon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
60 views20 pages

Chess Game

The document describes a chess game program written in C++ using object-oriented programming principles. It includes functions to initialize the board, randomly determine the starting player, and define legal moves for pieces like bishops, rooks, and kings. The main game loop displays the board, tracks turns, and allows player input to select a start and end position for each move.

Uploaded by

Lois Razon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

// Chess Game using C++ OOP

#include <iostream>

#include <string>

#include <stdlib.h>

#include <conio.h>

#include <cstdlib>

#include <ctime>

Using namespace std;

Int rollTurn(){

Srand((unsigned) time(0));

Int rTurn = (rand() % 2) + 1;

Return rTurn;

Char cBoard [8][8] = {

‘R’,’N’,’B’,’Q’,’K’,’B’,’N’,’R’,

‘P’,’P’,’P’,’P’,’P’,’P’,’P’,’P’,

‘-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,

‘-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,

‘-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,

‘-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,’-‘,

‘p’,’p’,’p’,’p’,’p’,’p’,’p’,’p’,

‘r’,’n’,’b’,’q’,’k’,’b’,’n’,’r’

};
Int pathBoard [8][8] = {

‘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’,

‘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’

};

Bool moveBishop(int sRow, int sCol, int eRow, int eCol){

Int I;

Int x = sRow – 1, y = sCol;

If (sRow == eRow && sCol == eCol) return false;

If (sRow < eRow) {

If (sCol < eCol){ // Up

For (I = sCol; I <= eCol; ++i){ // Upper Right

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;

X++;

If (cBoard[x][I + 1] != ‘-‘) return false;

Else if (eRow == x && eCol == (I + 1)) return true;

Else if (sCol > eCol){

For (I = sCol; I >= eCol; --i){ // Upper Left

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;

X++;
If (cBoard[x][I – 1] != ‘-‘) return false;

Else if (eRow == x && eCol == (I – 1)) return true;

}/*

Else if (sRow > eRow) {

If (sCol < eCol){ // Down

For (I = sCol; I <= eCol; ++i){ // Lower Right

x--;

if (cBoard[x][I + 1] != ‘-‘) return false;

else if (eRow == x && eCol == (I + 1)) return true;

Else if (sCol > eCol){

For (I = sCol; I >= eCol; --i){ // Lower Left

x--;

if (cBoard[x][I – 1] != ‘-‘) return false;

else if (eRow == x && eCol == (I – 1)) return true;

}*/

Bool eatBishop(int sRow, int sCol, int eRow, int eCol){

Int I;

Int x = sRow, y = sCol;

If (sRow == eRow && sCol == eCol) return false;

If (sRow < eRow) {


If (sCol < eCol){ // Up

For (I = sCol; I < eCol; ++i){ // Upper Right

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;

X++;

If (cBoard[x – 1][I – 1] != ‘-‘) return false;

Else if (eRow == x && eCol == (I + 1)) return true;

Else if (sCol > eCol){

For (I = sCol; I > eCol; --i){ // Upper Left

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;

X++;

If (cBoard[x – 1][I + 1] != ‘-‘) return false;

Else if (eRow == (x + 1) && eCol == (I – 1)) return true;

Else if (sRow > eRow) {

If (sCol < eCol){ // Down

For (I = sCol; I < eCol; ++i){ // Lower Right

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;

x--;

if (cBoard[x + 1][I – 1] != ‘-‘) return false;

else if (eRow == (x – 1) && eCol == (I + 1)) return true;

Else if (sCol > eCol){

For (I = sCol; I > eCol; --i){ // Lower Left

Cout << x << “ “ << I << “ | “ << eRow << “ “ << eCol << endl;
x--;

if (cBoard[x + 1][I + 1] != ‘-‘) return false;

else if (eRow == (x – 1) && eCol == (I – 1)) return true;

//return true;

Bool moveRook(int sRow, int sCol, int eRow, int eCol){

Int I;

If (sRow == eRow && sCol == eCol) return false;

If (sRow == eRow) {

If (sCol < eCol) { // Horiz

For (I = sCol + 1; I <= eCol; ++i) // Right

If (cBoard[sRow][i] != ‘-‘) return false;

Else {

For (I = sCol – 1; I >= eCol; --i) // Left

If (cBoard[sRow][i] != ‘-‘) return false;

Else if (sCol == eCol) {

If (sRow < eRow) { // Vert

For (I = sRow + 1; I <= eRow; ++i) // Down

If (cBoard[i][sCol] != ‘-‘) return false;

}
Else {

For (I = sRow – 1; I >= eRow; --i) // Up

If (cBoard[i][sCol] != ‘-‘) return false;

Else {return false;}

Return true;

Bool eatRook(int sRow, int sCol, int eRow, int eCol){

Int I;

If (sRow == eRow && sCol == eCol) return false;

If (sRow == eRow) {

If (sCol < eCol) { // Horiz

For (I = sCol + 1; I < eCol; ++i) // Right

If (cBoard[sRow][i] != ‘-‘) return false;

Else {

For (I = sCol – 1; I > eCol; --i) // Left

If (cBoard[sRow][i] != ‘-‘) return false;

Else if (sCol == eCol) {

If (sRow < eRow) { // Vert

For (I = sRow + 1; I < eRow; ++i) // Down

If (cBoard[i][sCol] != ‘-‘) return false;

}
Else {

For (I = sRow – 1; I > eRow; --i) // Up

If (cBoard[i][sCol] != ‘-‘) return false;

Else {return false;}

Return true;

Bool moveKing(int sRow, int sCol, int eRow, int eCol){

Bool eatKing(int sRow, int sCol, int eRow, int eCol){

Void showBoard(){

Int yCol1 = 8;

For (int xRow = 8; xRow > 0; xRow--){

Cout << yCol1 << “ |”;

yCol1--;

for (int yCol2 = 0; yCol2 < 8; yCol2++){

cout << “ “ << cBoard [xRow – 1] [yCol2] << “ “;

Cout << endl;

}
Cout << “ |------------------------” << endl;

Cout << “ 1 2 3 4 5 6 7 8 “ << endl;

Void gInstructions(){

Cout << “Toss a coin, or pick who should go first!\n” << endl;

Cout << “Upper Case = ‘WHITE’ Pieces; Lower Case = ‘black’ Pieces” << endl;

Cout << “r = Rook, n = Knight/ Horse, q = Queen, k = King, p = Pawn\n” << endl;

Cout << “Choose a number between 11 and 88.\nNumbers outside of them will be invalidated!” <<
endl;

Void gameStart(){

gInstructions();

string pWB;

int totalTurns = 1;

int gameTurn = 0;

int sMove = 0;

int eMove = 0;

int sRow, sCol, eRow, eCol;

bool gPass;

bool gameEnd = false;


while(gameEnd != true){

cout << “\n”;

showBoard();

gPass = false;

if(gameTurn == 0){pWB = “WHITE”;}

else if(gameTurn == 1){pWB = “black”;}

else{}

cout << “It’s “ << pWB << “’s turn!” << endl;

cout << “Turns: “ << totalTurns << endl;

while (!gPass){

cout << “\nPick your piece: “;

cin >> sMove;

sRow = (sMove % 10) – 1;

sCol = (sMove / 10) – 1;

if (cBoard[sRow][sCol] != ‘-‘){

cout << “To: “;

cin >> eMove;

eRow = (eMove % 10) – 1;

eCol = (eMove / 10) – 1;

cout << eRow << “ “ << eCol << endl;

// Checks if the tile is empty


If (cBoard[eRow][eCol] == ‘-‘){

If (gameTurn == 0){

Switch(cBoard[sRow][sCol]){

Case ‘P’:

If (eRow == (sRow + 1)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘P’;

Break;

Case ‘N’:

If ((eRow == (sRow + 2) && eCol == (sCol + 1)) ||

(eRow == (sRow + 2) && eCol == (sCol – 1)) ||

(eRow == (sRow – 2) && eCol == (sCol + 1)) ||

(eRow == (sRow – 2) && eCol == (sCol – 1)) ||

(eRow == (sRow + 1) && eCol == (sCol + 2)) ||

(eRow == (sRow + 1) && eCol == (sCol – 2)) ||

(eRow == (sRow – 1) && eCol == (sCol + 2)) ||

(eRow == (sRow – 1) && eCol == (sCol – 2))){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘N’;

Break;

Case ‘R’:

moveRook(sRow, sCol, eRow, eCol);

if(moveRook(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;
cBoard[eRow][eCol] = ‘R’;

Break;

Case ‘B’:

Cout << eRow << “ “ << eCol << endl;

moveBishop(sRow, sCol, eRow, eCol);

if(moveBishop(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘B’;

Break;

Case ‘Q’:

moveRook(sRow, sCol, eRow, eCol);

if(moveRook(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘Q’;

Break;

Default:

Cout << “Invalid Move!” << endl;

Break;

Else if (gameTurn == 1){

Switch(cBoard[sRow][sCol]){

Case ‘p’:

If (eRow == (sRow – 1)){


gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘p’;

Break;

Case ‘n’:

If ((eRow == (sRow + 2) && eCol == (sCol + 1)) ||

(eRow == (sRow + 2) && eCol == (sCol – 1)) ||

(eRow == (sRow – 2) && eCol == (sCol + 1)) ||

(eRow == (sRow – 2) && eCol == (sCol – 1)) ||

(eRow == (sRow + 1) && eCol == (sCol + 2)) ||

(eRow == (sRow + 1) && eCol == (sCol – 2)) ||

(eRow == (sRow – 1) && eCol == (sCol + 2)) ||

(eRow == (sRow – 1) && eCol == (sCol – 2))){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘n’;

Break;

Case ‘r’:

moveRook(sRow, sCol, eRow, eCol);

if(moveRook(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘r’;

Break;

Case ‘b’:

Cout << eRow << “ “ << eCol << endl;


moveBishop(sRow, sCol, eRow, eCol);

if(moveBishop(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘b’;

Break;

Case ‘q’:

moveRook(sRow, sCol, eRow, eCol);

if(moveRook(sRow, sCol, eRow, eCol) == true){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘q’;

Break;

Default:

Cout << “Invalid Move!” << endl;

Break;

// Piece eats a piece

Else{

If (gameTurn == 0){

Switch(cBoard[sRow][sCol]){

Case ‘P’:

If ((((int)cBoard[sRow + 1][sCol – 1] >= 98 && (int)cBoard[sRow + 1][sCol – 1] <= 114


&& sCol > 0) ||
((int)cBoard[sRow + 1][sCol + 1] >= 98 && (int)cBoard[sRow + 1][sCol + 1] <= 114 &&
sCol < 7))){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘P’;

Break;

Case ‘N’:

If ((eRow == (sRow + 2) && eCol == (sCol + 1)) ||

(eRow == (sRow + 2) && eCol == (sCol – 1)) ||

(eRow == (sRow – 2) && eCol == (sCol + 1)) ||

(eRow == (sRow – 2) && eCol == (sCol – 1)) ||

(eRow == (sRow + 1) && eCol == (sCol + 2)) ||

(eRow == (sRow + 1) && eCol == (sCol – 2)) ||

(eRow == (sRow – 1) && eCol == (sCol + 2)) ||

(eRow == (sRow – 1) && eCol == (sCol – 2))){

If ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||

((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘N’;

Break;

Case ‘R’:

eatRook(sRow, sCol, eRow, eCol);

if(eatRook(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||


((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘R’;

Break;

Case ‘B’:

eatBishop(sRow, sCol, eRow, eCol);

if(eatBishop(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||

((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘B’;

Break;

Case ‘Q’:

eatRook(sRow, sCol, eRow, eCol);

if(eatRook(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||

((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘Q’;

Break;
Default:

Cout << “Invalid Move!” << endl;

Break;

Else if (gameTurn == 1){

Switch(cBoard[sRow][sCol]){

Case ‘p’:

If ((((int)cBoard[sRow – 1][sCol – 1] >= 66 && (int)cBoard[sRow – 1][sCol – 1] <= 82 &&


sCol > 0) ||

((int)cBoard[sRow – 1][sCol + 1] >= 66 && (int)cBoard[sRow – 1][sCol + 1] <= 82 &&


sCol < 7))){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘p’;

Break;

Case ‘n’:

If ((eRow == (sRow + 2) && eCol == (sCol + 1)) ||

(eRow == (sRow + 2) && eCol == (sCol – 1)) ||

(eRow == (sRow – 2) && eCol == (sCol + 1)) ||

(eRow == (sRow – 2) && eCol == (sCol – 1)) ||

(eRow == (sRow + 1) && eCol == (sCol + 2)) ||

(eRow == (sRow + 1) && eCol == (sCol – 2)) ||

(eRow == (sRow – 1) && eCol == (sCol + 2)) ||

(eRow == (sRow – 1) && eCol == (sCol – 2))){

If ((((int)cBoard[eRow][eCol] >= 66 && (int)cBoard[eRow][eCol] <= 82)) ||

((int)cBoard[eRow][eCol] >= 66 && (int)cBoard[eRow][eCol] <= 82)){


gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘n’;

Break;

Case ‘r’:

eatRook(sRow, sCol, eRow, eCol);

if(eatRook(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 66 && (int)cBoard[eRow][eCol] <= 82)) ||

((int)cBoard[eRow][eCol] >= 66 && (int)cBoard[eRow][eCol] <= 82)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘r’;

Break;

Case ‘b’:

eatBishop(sRow, sCol, eRow, eCol);

if(eatBishop(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||

((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘b’;

Break;

Case ‘q’:
eatRook(sRow, sCol, eRow, eCol);

if(eatRook(sRow, sCol, eRow, eCol) == true){

if ((((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)) ||

((int)cBoard[eRow][eCol] >= 98 && (int)cBoard[eRow][eCol] <= 114)){

gPass = true;

cBoard[sRow][sCol] = ‘-‘;

cBoard[eRow][eCol] = ‘q’;

Break;

Default:

Cout << “Invalid Move!” << endl;

Break;

If (!gPass){cout << “Invalid Piece!” << endl;}

If (gameTurn != 1) {gameTurn++;}

Else {gameTurn--;}

totalTurns++;

Int main(){

//string name;

//cout << “Simple Chess Game” << endl;


//cout << “1 – Player vs Player” << endl;

//cout << “2 – Player vs AI” << endl;

//cout << “\nChoose number: “;

//getline (cin, name);

gameStart();

#include <iostream>

Using namespace std;

Int Main()

Int I, j, rows;

String b, w, t;

B = “black”;

W = “white”;

Cout << “nn Display checkerboard pattern with the words ‘black’ and ‘white’:n”;

Cout << “---------------------------------------------------------------------n”;

Cout << “ Input number of rows: “;

Cin >> rows;

For (I = 1; I <= rows; i++)

For (j = 1; j <= rows; j++)

If (j % 2 != 0)

Cout << b;

If (j < rows)

{
Cout << “-“;

Else if (j % 2 == 0)

Cout << w;

If (j < rows)

Cout << “-“;

T = b;

B = w;

W = t;

Cout << endl;

You might also like