Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <iomanip>
- using namespace std;
- void displayBoard(int [][8]);
- void displayMenu();
- void showAvailablePieces();
- void checkMovesBlack(int [][8]);
- void checkMovesRed(int [][8]);
- int jumpPiece(int [][8], int, int, int, int, bool);
- int main()
- {
- bool blackTurn;
- bool redTurn;
- int board [8][8]= { 1,0,1,0,1,0,1,0,
- 0,1,0,1,0,1,0,1,
- 1,0,1,0,1,0,1,0,
- 0,0,0,0,0,0,0,0,
- 0,0,0,0,0,0,0,0,
- 0,2,0,2,0,2,0,2,
- 2,0,2,0,2,0,2,0,
- 0,2,0,2,0,2,0,2};
- /*displayMenu();*/
- displayBoard(board);
- do{
- checkMovesBlack(board);
- blackTurn=0;
- redTurn=1;
- system("cls");
- checkMovesRed(board);
- redTurn=0;
- blackTurn=1;
- system("cls");
- } while(blackTurn || redTurn);
- system("pause");
- return 0;
- }
- void displayBoard(int matrix[][8])
- {
- for(int i = 8; i > 0; i--)
- for(int j = 0; j < 8; j++)
- if(matrix[j][i] == 1) cout << 'o'
- else if(matrix[j][i] == 2) cout << 'x';
- else if(matrix[j][i] == 0) cout << '-';
- }
- //void displayMenu()
- //{
- // string name1;
- // string name2;
- //
- // cout << setw(45) << "CHECKERS!!" << endl;
- // cout << "Player 1: ";
- // cin >> name1;
- // cout << "Player 2: ";
- // cin >> name2;
- //
- // system("cls");
- //}
- void checkMovesBlack(int matrix[][8])
- {
- bool availablePiece=0;
- bool availableMove=0;
- int startx=0;
- int starty=0;
- int nextx=0;
- int nexty=0;
- cout << endl;
- cout << "Black's turn!" << endl;
- cout << "Please enter the coordinates of the piece you would like to move: ";
- cin >> startx;
- cin >> starty;
- if (startx < 0 || startx >7 || starty < 0 || starty > 7)
- {
- cout << "Not valid coordinate. Please enter the coordinates of the piece you would like to move. " << endl;
- cin >> startx;
- cin >> starty;
- }
- else if (matrix[startx][starty]==2 && matrix[startx-1][starty-1]==0 || matrix[startx+1][starty-1]==0){availablePiece=1;}
- else {availablePiece=0;}
- if (availablePiece==0)
- {
- cout << "Not a valid coordinate. Please enter the coordinates of the piece you would like to move.";
- cin >> startx;
- cin >> starty;
- }
- else {
- cout << "Please enter the coordinates of the desired position: ";
- cin >> nextx;
- cin >> nexty;
- if (matrix[nextx][nexty]==1 && matrix[nextx-2][nexty-2]==0)
- {
- bool left=true;
- jumpPiece(matrix, startx, starty, nextx, nexty, left);
- }
- else if (matrix[nextx][nexty]==1 && matrix[nextx+2][nexty-2]==0) {
- bool left=false;
- jumpPiece(matrix, startx, starty, nextx, nexty, left);
- }
- else if (matrix[nextx][nexty]==2 && matrix[nextx-1][nexty-1]==0 || matrix[nextx+1][nexty-1]==0)
- {
- availableMove=1;
- }
- }
- if (availableMove) {
- cout << "Valid move!";
- startx=nextx;
- starty=nexty;
- }
- else {
- cout << "Not a valid move. " << endl;
- cin >> nextx;
- cin >> nexty;
- }
- cout << endl;
- }
- void checkMovesRed(int matrix[][8])
- {
- bool availablePiece=0;
- bool availableMove=0;
- int startx=0;
- int starty=0;
- int nextx=0;
- int nexty=0;
- cout << "Red's turn!" << endl;
- cout << "Please enter the coordinates of the piece you would like to move: ";
- cin >> startx;
- cin >> starty;
- if (startx < 0 || startx >7 || starty < 0 || starty > 7)
- {
- cout << "Not valid coordinate. Please enter the coordinates of the piece you would like to move. " << endl;
- cin >> startx;
- cin >> starty;
- }
- else if (matrix[startx][starty]==2 && matrix[startx-1][starty-1]==0 || matrix[startx+1][starty-1]==0){availablePiece=1;}
- else {availablePiece=0;}
- if (availablePiece==0)
- {
- cout << "Not a valid coordinate. Please enter the coordinates of the piece you would like to move.";
- cin >> startx;
- cin >> starty;
- }
- else {
- cout << "Please enter the coordinates of the desired position: ";
- cin >> nextx;
- cin >> nexty;
- if (matrix[nextx][nexty]==2 && matrix[nextx+1][nexty+1]==0 || matrix[nextx-1][nexty+1]==0)
- {
- availableMove=1;
- }
- }
- if (availableMove) {
- cout << "Valid move!";
- startx=nextx;
- starty=nexty;
- }
- else {
- cout << "Not a valid move. " << endl;
- cin >> nextx;
- cin >> nexty;
- }
- }
- int jumpPiece(int matrix[][8], int x, int y, int tempx, int tempy, bool passLeft)
- {
- if(passLeft)
- {
- matrix[x][y]=matrix[x-2][y-2];
- }
- if(!passLeft)
- {
- matrix[x][y]=matrix[x+2][y-2];
- }
- matrix[tempx][tempy]=0;
- return matrix[x][y];
- }
Add Comment
Please, Sign In to add comment