Code #2

 avatar
unknown
c_cpp
a year ago
4.9 kB
5
Indexable
#include <iostream>
#include <random>
#include <string>
using namespace std;

string player = "1";
int selectedx;
int selectedy;
int movex;
int movey;
bool checkmate = false;
string selectedobj;

const int constval = 8;
string arrayorg[constval][constval] = {    {"R2", "N2", "B2", "K2", "Q2", "B2", "N2", "R2"},
    {"P2", "P2", "P2", "P2", "P2", "P2", "P2", "P2"},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {"P1", "P1", "P1", "P1", "P1", "P1", "P1", "P1"},
    {"R1", "N1", "B1", "K1", "Q1", "B1", "N1", "R1"}};

string arrayi[constval][constval] = {    {"R2", "N2", "B2", "K2", "Q2", "B2", "N2", "R2"},
    {"P2", "P2", "P2", "P2", "P2", "P2", "P2", "P2"},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {"P1", "P1", "P1", "P1", "P1", "P1", "P1", "P1"},
    {"R1", "N1", "B1", "K1", "Q1", "B1", "N1", "R1"}};

string arrayy[constval][constval] = {    {"R2", "N2", "B2", "K2", "Q2", "B2", "N2", "R2"},
    {"P2", "P2", "P2", "P2", "P2", "P2", "P2", "P2"},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {". ", ". ", ". ", ". ", ". ", ". ", ". ", ". "},
    {"P1", "P1", "P1", "P1", "P1", "P1", "P1", "P1"},
    {"R1", "N1", "B1", "K1", "Q1", "B1", "N1", "R1"}};

void bgout(int type) {
    for(int i = 0; i < constval; i += 1) {
        int ty = 0;
        for(int u = 0; u < constval; u += 1) {
            ty += 1;
            if (ty < constval) {
                if (type == 1) {
                    cout << arrayi[i][u] << "  ";
                }
                else if (type == 2) {
                    cout << arrayy[i][u] << "  ";
                }
            }
            else {
                if (type == 1) {
                    cout << arrayi[i][u] << endl;
                }
                else if (type == 2) {
                    cout << arrayy[i][u] << endl;
                }
                ty = 0;
            }
        }
    }
}

void checkPiece(int x, int y) {
    if(arrayi[y-1][x-1] == ". ") {
        cout << "Invalid object, please select an object that exists." << endl;
        cout << "Please select your X position:" << endl;
        return;
    }
    else if(arrayi[y-1][x-1] != ". ") {
        string objchk = arrayi[y-1][x-1];
        bool invalid = false;
        string selectedobj2;
        if(objchk[1] != player[0]) {
            cout << "Invalid object, please select your pieces, not the other player's pieces." << endl;
            cout << "Please select your X position:" << endl;
            return;
        }
        else {
            if(objchk[0] == "K"[0]) {
                selectedobj2 = "King";
            }
            else if(objchk[0] == "R"[0]) {
                selectedobj2 = "Rook";
            }
            else if(objchk[0] == "B"[0]) {
                selectedobj2 = "Bishop";
            }
            else if(objchk[0] == "N"[0]) {
                selectedobj2 = "Knight";
            }
            else if(objchk[0] == "P"[0]) {
                selectedobj2 = "Pawn";
            }
            cout << selectedobj2 << endl;
        }
    }
}
void selectY(int x) {
    int y;
    cin >> y;
    if(y < 1 || y > 8) {
        cout << "Invalid Y position. Try again:" << endl;
        selectY(x);
    }
    else {
        checkPiece(x, y);
    }
}
void selectX() {
    int x;
    cin >> x;
    if(x < 1 || x > 8) {
       cout << "Invalid X position. Try again:" << endl;
       selectX();
    }
    else {
        cout << "Please select your Y position:" << endl;
        selectY(x);
    }
    if(checkmate == false) {
        selectX();
    }
}

int main() {
    cout << "Welcome to Chess++!" << endl;
    cout << "Here's some notes:\n 1. You have to scroll up to look at the checkboard.\n 2. You have to select an X position and Y position to select your objects.\n 3. When you select an object, it will only show the places where you can move.\n 4. To unselect an object, input 0.\n 5. In checkmate, the king is replaced with W and the number of the player.\n 6. This is a multi-player game, there is no AI.\n 7. You will start with player 1, who is on the bottom." << endl;
    cout << "Input anything below to start the game:" << endl;
    string inpot;
    cin >> inpot;
    cout << endl;
    bgout(1);
    cout << endl;
    cout << "You are now player 1!" << endl;
    cout << "Please select your X position:" << endl;
    selectX();
    return 0;
}


Editor is loading...
Leave a Comment