1

I have a board game that has spaces (has numeral values 1,2,3, etc.) starting from 1 and 16 pieces of pawns; four for each player.

I want to show the result of my board game at some point. I tried the method below but that will make my code extremely long.

i have 16 pieces and 100 spaces that i have to repeat that code with 100 space that would take forever. the code below is just for one space (the first space) Any idea how to show my result in a short way? Thanks in advance!

Here is my old-fashioned way:

//space 1                                                                       
if (bpiece1->value == 1)
{
    cout << "            bpiece1";
}
else if (bpiece2->value == 1)
{
    cout << "            bpiece2";
}
else if (bpiece3->value == 1)
{
    cout << "            bpiece3"; 
}
else if (bpiece4->value == 1)
{
    cout << "            bpiece4";
}
else if (gpiece1->value == 1)
{
    cout << "            gpiece1";
}
else if (gpiece2->value == 1)
{
    cout << "            gpiece2";
}
else if (gpiece3->value == 1)
{
    cout << "            gpiece3";
}
else if (gpiece4->value == 1)
{
    cout << "            gpiece4";
}
else if (ypiece1->value == 1)
{
    cout << "            ypiece1";
}
else if (ypiece2->value == 1)
{
    cout << "            ypiece2";
}
else if (ypiece3->value == 1)
{
    cout << "            ypiece3"; 
}
else if (y4->value == 1)
{
    cout << "            y4";
}
else if (rpiece1->value == 1)
{
    cout << "            rpiece1";
}
else if (rpiece2->value == 1)
{
    cout << "            rpiece2";
}
else if (rpiece3->value == 1)
{
    cout << "            rpiece3";
}
else if (rpiece4->value == 1)
{
    cout << "            rpiece4";
}
else
{
    cout << "            01";       
}
  • You need to learn the basics of C++. That is too broad a need for SO to address, but here is a [reading list](https://stackoverflow.com/q/388242/1362568) – Mike Kinghan Jul 22 '17 at 11:15
  • Create an array of size 100 that contains values from 0 to 16 (0 for an empty space). Then write a loop to go over that array. – interjay Jul 22 '17 at 11:26
  • @interjay do you want to give an answer ? –  Jul 22 '17 at 11:31
  • You should look into the book "Professional C++" as it walks you through how to make a very simple chess game with modern c++ – Matthew Jul 22 '17 at 23:07

1 Answers1

1

C++ is an object-oriented language. Therefore, we start by creating a class that stores your board and implements all functions on it. Like

//Board.h

#include <array>
using std::array;

enum class Figure { None, Pawn };

class Board {

private:
    array<array<Figure, 8>, 8> fields; //8x8 if it was a chess board

public:
    void print() const;
};

//Board.cpp

#include "Board.h"

#include <iostream>
using std::cout;
using std::endl;

#include <string>
using std::string;

inline string to_string(const Figure figure){
    switch(figure){
        case Figure::None:
            return " ";
        case Figure::Pawn:
            return "p";
    }
    //throw error here
    return "";
}

void Board::print() const {

    for(size_t i = 0; i < fields.size(); i++){
        for(size_t j = 0; j < fields[i].size(); j++){
            cout << to_string(fields[i][j]);
        }
        cout << endl;
    }
    cout << endl;
}

If this is new to you, you should really read the basic tutorials first and make sure that you understand each line I wrote in the end.

Important here is: Representation, represenation, representation. Don't think in "1 is a pawn", think in "a pawn is a pawn". Everything that has a function which you can think of should probably be a class, a structure or an enum.

Aziuth
  • 3,652
  • 3
  • 18
  • 36
  • @T_T Your "one issue" is based on you not using the possibilities that C++ offers. You try to solve your problem in a procedural way. This will lead to more and more problems, especially regarding overview. See, one could solve your problem procedurally. But I wouldn't consider a bad design as a good answer. That said, my solution has everything you need and which you would still need in a procedural solution - that is, a storage (the array^2) and code that works on it (the for loops). – Aziuth Jul 23 '17 at 10:57