Created
March 14, 2017 21:41
-
-
Save Folling/ec8bb7305c35bdef3804d0acc78b8909 to your computer and use it in GitHub Desktop.
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
#include <iostream> | |
#include <string> | |
#include <cmath> | |
#include <conio.h> | |
#include "main.h" | |
int main() { | |
setup(); | |
printBoard(); | |
introduction(); | |
runGame(); | |
_getch(); | |
} | |
void runGame() | |
{ | |
using namespace std; | |
string input; | |
while (true) | |
{ | |
cin >> input; | |
/*********************** | |
****List of Commands**** | |
************************/ | |
if (input == "exit") break; | |
if (input == "new") setup(); | |
if (input == "skip") currentPlayer++; printBoard(); | |
//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 == White && board[fromLine][fromRow] < 0) | |
{ | |
cout << "You can't use the other person's pieces!\n"; | |
currentPlayer++; | |
} | |
else if (currentPlayer == Black && board[fromLine][fromRow] > 0) | |
{ | |
cout << "You can't use the other person's pieces!\n"; | |
currentPlayer++; | |
} | |
//if user enters move like d2d2, aka from the same square to the same square | |
else if (fromRow == toRow && fromLine == toLine) | |
{ | |
cout << "You didn't try to move the piece!\n"; | |
currentPlayer++; | |
} | |
//checks for whether the piece can move that way | |
else if (checkForCorrectMove(fromRow, fromLine, toRow, toLine, false) == false) | |
{ | |
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) == false) | |
{ | |
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 = White : currentPlayer = Black; | |
printBoard(); | |
} | |
//game ends | |
cout << "See you next time\n"; | |
_getch(); | |
} | |
int checkForCorrectMove(int row, int line, int toRow, int toLine, bool checkForCheck) { | |
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 || checkForCheck) | |
{ | |
if (toLine == 7) | |
{ | |
board[line][row] = 0; | |
queening(toRow); | |
currentPlayer++; | |
} | |
return true; | |
} | |
else return false; | |
} | |
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 && board[line + 1][row] == 0 && board[line + 2][row] == 0 && checkForCheck == false ) return true; | |
else return false; | |
} | |
else | |
{ | |
if (toLine > line && toLine - 1 == line && toRow == row && board[line + 1][row] == 0 && checkForCheck == false) | |
{ | |
if (toLine == 7) | |
{ | |
board[line][row] = 0; | |
queening(row); | |
currentPlayer++; | |
} | |
return true; | |
} | |
else return false; | |
} | |
} | |
else if (piece == -pawn) | |
{ | |
//checks if capture is possible or not | |
if ((toLine == line - 1 && (toRow == row + 1 || toRow == row - 1))) | |
{ | |
if (board[toLine][toRow] > 0 || checkForCheck) | |
{ | |
if (toLine == 0) | |
{ | |
board[line][row] = 0; | |
queening(toRow); | |
currentPlayer++; | |
} | |
return true; | |
} | |
else return false; | |
} | |
if (line == 6) | |
{ | |
//if on seventh rank you can move up to two squares | |
if (toLine < line && toLine - line >= -2 && toLine - line < 0 && toRow == row && board[line - 1][row] == 0 && board[line - 2][row] == 0 && checkForCheck == false) return true; | |
else return false; | |
} | |
else | |
{ | |
if (toLine < line && toLine + 1 >= line && toRow == row && board[line - 1][row] == 0 && checkForCheck == false ) | |
{ | |
if (toLine == 0) | |
{ | |
//checks for whether the pawn is at the lowest rank to allow queening | |
board[line][row] = 0; | |
queening(row); | |
currentPlayer++; | |
} | |
return true; | |
} | |
else return false; | |
} | |
} | |
else if (piece == knight || piece == -knight) | |
{ | |
if ((line + 2 == toLine && row + 1 == toRow) || (line + 2 == toLine && row - 1 == toRow) //Knight moves two up and left or right | |
|| (line - 2 == toLine && row + 1 == toRow) || (line - 2 == toLine && row - 1 == toRow) //Knight moves two down and left or right | |
|| (line + 1 == toLine && row + 2 == toRow) || (line - 1 == toLine && row + 2 == toRow) //Knight moves two right and up or down | |
|| (line + 1 == toLine && row - 2 == toRow) || (line - 1 == toLine && row - 2 == toRow)) //Knight moves two left and up or down | |
{ | |
return true; | |
} | |
else return false; | |
} | |
else if (piece == bishop || piece == -bishop) | |
{ | |
//checks if the user tries to move on a diagonal | |
if (abs(row - toRow) == abs(line - toLine)) | |
{ | |
//j increases to be added to (line +i). - j when i is below 0 and +j when above it that is | |
//because we want to check 1 square after the square we checked, but we want to check all squares after. | |
for (int j = 1; j < 7; j++) | |
{ | |
//i is the basic iteration to check in all directions in a diagonal | |
for (int i = -7; i < 8; i++) | |
{ | |
//checks for pieces in front from bottom left to top right | |
if (board[line + i][row + i] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row + i + (i == 0 ? 0 : i < 0 ? -j : j) == toRow) return false; | |
//checks for pieces in front from bottom right to top left | |
if (board[line + i][row - i] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row - i + (i == 0 ? 0 : i < 0 ? j : -j) == toRow) return false; | |
} | |
} | |
return true; | |
} | |
else return false; | |
} | |
else if (piece == rook || piece == -rook) | |
{ | |
//checks whether user tries to move on a horizontal or vertical line | |
if ((row == toRow && (line<toLine || line>toLine)) || (line == toLine && (row<toRow || row >> toRow))) | |
{ | |
//j is incremented to check for continous squares after a piece has been detected to prevent jumping over pieces | |
for (int j = 1; j < 7; j++) { | |
//i is the basic increment to check for moves in a specific direction, creates a cross with a horizontal and a vertical line | |
for (int i = -B_Height - 1; i <= B_Height; i++) | |
{ | |
//checks for the vertical line and whether there is a piece in the way | |
if (board[line + i][row] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row == toRow) return false; | |
//checks the horizontal row and whether there is a piece in the way | |
if (board[line][row + i] != 0 && line == toLine && row + i + (i == 0 ? 0 : i < 0 ? -j : j) == toRow) return false; | |
} | |
} | |
return true; //if no piece was found or the move is before the move and it is indeed a horizontal or vertical line | |
} | |
else return false; | |
} | |
else if (piece == queen || piece == -queen) | |
{ | |
if ((row == toRow && (line<toLine || line>toLine)) || (line == toLine && (row<toRow || row >> toRow)) || abs(row - toRow) == abs(line - toLine)) | |
{ | |
//j is incremented to check for continous squares after a piece has been detected to prevent jumping over pieces | |
for (int j = 1; j < 7; j++) { | |
//i is the basic increment to check for moves in a specific direction, creates a cross with a horizontal and a vertical line | |
for (int i = -B_Height - 1; i <= B_Height; i++) | |
{ | |
//checks for pieces in front from bottom left to top right | |
if (board[line + i][row + i] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row + i + (i == 0 ? 0 : i < 0 ? -j : j) == toRow) return false; | |
//checks for pieces in front from bottom right to top left | |
if (board[line + i][row - i] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row - i + (i == 0 ? 0 : i < 0 ? j : -j) == toRow) return false; | |
//checks for the vertical line and whether there is a piece in the way | |
if (board[line + i][row] != 0 && line + i + (i == 0 ? 0 : i < 0 ? -j : j) == toLine && row == toRow) return false; | |
//checks the horizontal row and whether there is a piece in the way | |
if (board[line][row + i] != 0 && line == toLine && row + i + (i == 0 ? 0 : i < 0 ? -j : j) == toRow) return false; | |
} | |
} | |
return true; | |
} | |
return false; | |
} | |
else if (piece == king || piece == -king) | |
{ | |
//checks whether player is trying to move exactly one square in either direction | |
if (abs(toLine - line) <= 1 && abs(toRow - row) <= 1) | |
{ | |
for (int i = 0; i < B_Height - 1; i++) | |
{ | |
for (int j = 0; j < B_Width - 1; j++) | |
{ | |
if (currentPlayer == White && board[i][j] < 0 && checkForCorrectMove(j, i, toRow, toLine, true)) return false; | |
else if (currentPlayer == Black && board[j][i] > 0 && checkForCorrectMove(j, i, toRow, toLine, true)) return false; | |
} | |
} | |
return true; | |
} | |
else return false; | |
} | |
} | |
int checkForSamePiece(int row, int line){ | |
switch (currentPlayer) | |
{ | |
case White: if (board[row][line] > 0) | |
{ | |
std::cout << "You can't capture your own piece!\n"; | |
currentPlayer++; | |
return true; | |
} | |
else return false; | |
case Black: if (board[row][line] < 0) | |
{ | |
std::cout << "You can't capture your own piece!\n"; | |
currentPlayer++; | |
return true; | |
} | |
else return false; | |
} | |
} | |
void queening(int row) { | |
std::string choice; | |
std::cout << "What piece do you want?\n" | |
<< "b=Bishop\nn=Knight\nr=Rook\nq=Queen\n"; | |
std::cin >> choice; | |
if (choice == "b") board[B_Height - 1][row] = (currentPlayer == White ? bishop : -bishop); | |
if (choice == "n") board[B_Height - 1][row] = (currentPlayer == White ? knight : -knight); | |
if (choice == "r") board[B_Height - 1][row] = (currentPlayer == White ? rook : -rook ); | |
if (choice == "q") board[B_Height - 1][row] = (currentPlayer == White ? queen : -queen ); | |
} | |
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; | |
} | |
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 == White? cout << "White.\n" : cout << "Black.\n"; | |
} | |
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 = White; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment