Range Error

kdevel kdevel at vogtner.de
Mon Apr 12 19:19:12 UTC 2021


On Monday, 12 April 2021 at 18:13:38 UTC, Imperatorn wrote:
[...]
> Yup

D can be so much fun!

```d
import std.stdio;

enum Color {none = " n ", red = " r ", black = " b "};
enum sColor {black= " b ", white= " w "};
class Square {
public:
     this(Color color, sColor Scolor) {
         this.color = color;
         this.Scolor = Scolor;
     }
     Color color;
     sColor Scolor;
}

void Printboard (alias Q) (Square[8][8] square) {
     foreach (int i, ref v; square)
     {
         foreach(int j, ref w; v)
         {
            string s = Q (w);
            write (s);
         }
         writeln;
     }
     writeln;
}

bool isEven(int a)
{
     return a&1;
}

void main()
{
     Square[8][8] square;
     foreach (int i, ref v; square)
         foreach(int j, ref w; v)
         {
             auto color = (i + j).isEven
                ? sColor.black
                : sColor.white;
             w = new Square(Color.none, color);
         }
     foreach (int i, ref v; square)
     {
         if (i > 2)
             continue;
         foreach (int j, ref w; v)
             w.color = (i + j).isEven
                ? Color.red
                : w.color;
     }
     Printboard!(w => w.color) (square);
     Printboard!(w => w.Scolor) (square);
}
```


More information about the Digitalmars-d-learn mailing list