Last active
March 12, 2017 22:41
-
-
Save Folling/8154f6a28d95060dc203595c6b1a74e3 to your computer and use it in GitHub Desktop.
Chess Main.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#ifndef MAIN_H | |
#define MAIN_H | |
#include <conio.h> | |
#include <iostream> | |
#include <ctype.h> | |
//dimensions of the board | |
#define B_Width 8 | |
#define B_Height 8 | |
#define White 1 | |
#define Black 2 | |
//defines the values of each piece, per pawn point where 100 = 1 pawn. | |
#define pawn 100 | |
#define bishop 305 | |
#define knight 300 | |
#define rook 500 | |
#define queen 900 | |
#define king 2000 | |
bool gameIsDone; | |
int board[B_Height][B_Width]; | |
int currentPlayer = 1; | |
//the start set up of the board | |
const int startup[8][8] = {{ rook, knight, bishop, queen, king, bishop, knight, rook }, | |
{ pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn }, | |
{ 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 }, | |
{ -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn, -pawn }, | |
{-rook,-knight,-bishop,-queen,-king,-bishop,-knight,-rook }}; | |
//method to have the board start with the starting position | |
void setup() { | |
for (int i = 0; i < B_Height; i++) | |
{ | |
for (int j = 0; j < B_Width; j++) | |
{ | |
board[i][j] = startup[i][j]; | |
} | |
} | |
currentPlayer = 1; | |
} | |
//method to print the board at any given time | |
void printBoard() { | |
using namespace std; | |
string piece; | |
for (int i = B_Height - 1; i > -1; i--) | |
{ | |
for (int j = 0; j < B_Width; j++) | |
{ | |
switch (board[i][j]) | |
{ | |
case 0 : piece = "0"; | |
break; | |
case pawn : piece = "P"; | |
break; | |
case -pawn : piece = "p"; | |
break; | |
case bishop: piece = "B"; | |
break; | |
case -bishop: piece = "b"; | |
break; | |
case knight: piece = "N"; | |
break; | |
case -knight: piece = "n"; | |
break; | |
case rook : piece = "R"; | |
break; | |
case -rook : piece = "r"; | |
break; | |
case king : piece = "K"; | |
break; | |
case -king : piece = "k"; | |
break; | |
case queen : piece = "Q"; | |
break; | |
case -queen : piece = "q"; | |
break; | |
} | |
cout << piece; | |
} | |
cout << endl; | |
} | |
cout << endl << endl << "The current player is: "; | |
currentPlayer == 1 ? cout << "White.\n" : cout << "Black.\n"; | |
} | |
//introduction message | |
void introduction() { | |
using namespace std; | |
cout << endl << endl; | |
cout << "Enter your moves in 4 letter algebraic." << endl << | |
"I.e. e2e4. Keep in mind to use lower case only." << endl; | |
cout << "The available commands are: \"exit\" to quit the game and \"new\" for a new game" << endl; | |
} | |
//checks whether you're trying to capture your own piece | |
int checkForSamePiece(int row, int line) | |
{ | |
switch (currentPlayer) | |
{ | |
case 1: if (board[row][line] > 0) | |
{ | |
std::cout << "You can't capture your own piece!\n"; | |
currentPlayer++; | |
return 1; | |
} | |
else return 0; | |
case 2: if (board[row][line] < 0) | |
{ | |
std::cout << "You can't capture your own piece!\n"; | |
currentPlayer++; | |
return 1; | |
} | |
else return 0; | |
} | |
} | |
//function to check whether a chess valid move for each piece was made, returns 1 if yes 0 if not | |
int checkForCorrectMove(int row, int line, int toRow, int toLine) | |
{ | |
int piece = board[line][row]; | |
if (piece == pawn) | |
{ | |
//check if capture is possible or not | |
if ( toLine==line + 1 && (toRow == row + 1 || toRow == row - 1)) | |
{ | |
if (board[toLine][toRow] < 0) return 1; | |
else return 0; | |
} | |
if (line == 1) | |
{ | |
//if on second rank you can move up to two squares | |
if (toLine > line && toLine-line <=2 && toLine-line > 0 && toRow == row) return 1; | |
else return 0; | |
} | |
else | |
{ | |
if (toLine > line && toLine-1 == line && toRow == row) return 1; | |
else return 0; | |
} | |
} | |
if (piece == -pawn) | |
if ((toLine == line - 1 && (toRow == row + 1 || toRow == row - 1 ))) | |
{ | |
//checks if capture is possible or not | |
if (board[toLine][toRow] > 0) return 1; | |
else return 0; | |
} | |
if (line == 6) | |
{ | |
//iff on seventh rank you can move up to two squares | |
if (toLine < line && toLine - line >= -2 && toLine - line < 0 && toRow == row) return 1; | |
else return 0; | |
} | |
else | |
{ | |
if (toLine > line && toLine + 1 >= line && toRow == row) return 1; | |
else return 0; | |
} | |
} | |
//main app, runs in a loop | |
void runGame() | |
{ | |
using namespace std; | |
string input; | |
while (true) | |
{ | |
cin >> input; | |
/*********************** | |
****List of Commands**** | |
************************/ | |
if (input == "exit") break; | |
if (input == "new") setup(); | |
//checks if user actually inputs something between a1 and h8 | |
if (input.substr(0, 1) >= "a" &&input.substr(0, 1) <= "h" && input.substr(2, 1) >= "a" && input.substr(2, 1) <= "h" | |
&& input.substr(1, 1) >= "1" && input.substr(1, 1) <= "8" && input.substr(3, 1) >= "1" && input.substr(3, 1) <= "8") | |
{ | |
//the four specifics of lines and rows, seperated by each. e2e4 will be e ; 2; e ; 4 or piece from e2 moves to e4 | |
int fromRow = input[0] - 'a'; | |
int fromLine = input[1] - '1'; | |
int toRow = input[2] - 'a'; | |
int toLine = input[3] - '1'; | |
//checking if trying to select a nonexisting piece | |
if (board[fromLine][fromRow] == 0) | |
{ | |
cout << "Sorry, your move was invalid\n"; | |
currentPlayer++; | |
} | |
//checks if you're trying to move a black piece as white or vice versa | |
else if(currentPlayer == 1 && board[fromLine][fromRow] < 0) | |
{ | |
cout << "You can't use the other person's pieces!\n"; | |
currentPlayer++; | |
} | |
else if (currentPlayer == 2 && board[fromLine][fromRow] > 0) | |
{ | |
cout << "You can't use the other person's pieces!\n"; | |
currentPlayer++; | |
} | |
//checks for whether the piece can move that way | |
else if (checkForCorrectMove(fromRow, fromLine, toRow, toLine) == 0) | |
{ | |
cout << "This piece can't move there!\n"; | |
currentPlayer++; | |
} | |
//check for trying to capture your own piece if not it will move the piece | |
else if (checkForSamePiece(toLine, toRow) == 0) | |
{ | |
board[toLine][toRow] = board[fromLine][fromRow]; | |
board[fromLine][fromRow] = 0; | |
cin.ignore(256, '\n'); | |
} | |
} | |
//if player didn't enter anything between a1 and h8 | |
else | |
{ | |
cout << "Sorry, your move is invalid\n", currentPlayer++; | |
} | |
//player switches from 1 to 2 and from 2 to 1, then it refreshes the game | |
currentPlayer % 2 == 0 ? currentPlayer = 1 : currentPlayer = 2; | |
printBoard(); | |
} | |
//game ends | |
cout << "See you next time\n"; | |
_getch(); | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment