Untitled

 avatar
unknown
plain_text
a year ago
3.6 kB
6
Indexable
#include <iostream>
#include <version>
using namespace std;

int main() {
    char inletter;
    int innumber;
    char outletter;
    int outnumber;

    cout << "Welcome to the world of chess" << endl;
    const int width = 8;
    const int height = 8;
    char board[height][width] =
           {{'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'}};


    for(int i = 0; i < height; ++i) {
        for(int j = 0; j < width; ++j) {
            cout << board[i][j] << " ";
        }
        cout << endl;
    }

    cout << "Input the letter position (A-H): ";
    cin >> inletter;
    cout << "Input the number position (1-8): ";
    cin >> innumber;

    int col = toupper(inletter) - 'A';
    int row = 8 - innumber;

    if(row >= 0 && row < height && col >= 0 && col < width) {
        cout << "The piece at " << inletter << innumber << " is: " << board[row][col] << endl;
        if(board[row][col] == 'P') {
            cout << "Enter the letter to where you want to go" << endl;
            cin >> outletter;
            cout << "Enter the number to where you want to go" << endl;
            cin >> outnumber;
            int col2 = toupper(outletter) - 'A';
            int row2 = 8 - outnumber;
            if (outletter == inletter) {
                if(outnumber - innumber <=2) {
                    if(board[outnumber][innumber] == '-') {
                        board[row2][col2] = board[row][col];
                        board[row][col] = '-';
                        for(int i = 0; i < height; ++i) {
                            for(int j = 0; j < width; ++j) {
                                cout << board[i][j] << " ";
                            }
                            cout << endl;
                        }
                    } else {
                        cout << "Invalid move" << endl;
                    }
                } else {
                    cout << "The pawn can only move up to 2 blocks only in the first move" << endl;
                }
            }else {
                cout << "You can't choose a piece and move another" << endl;
            }
        }else if(board[row][col] == 'N') {
            cout << "Enter the letter where you want to go" << endl;
            cin >> outletter;
            cout << "Enter the number where you want to go" << endl;
            cin >> outnumber;
            int col2 = toupper(outletter) - 'A';
            int row2 = 8 - outnumber;
            if(outnumber - innumber == 2) {
                if(outletter - inletter == 1 || outletter - inletter == -1) {
                    if(board[outnumber][innumber] == '-') {
                        board[row2][col2] = board[row][col];
                        board[row][col] = '-';
                        for(int i = 0; i < height; ++i) {
                            for(int j = 0; j < width; ++j) {
                                cout << board[i][j] << " ";
                            }
                            cout << endl;
                        }
                    }else {
                        cout << "Invalid move";
                    }
                } else {
                    cout << "Invalid move";
                }
            }else {
                cout << "Invalid move";
            }
        }// Here we add the other pieces
    }
}
Editor is loading...
Leave a Comment