Jump to page: 1 2
Thread overview
Range Error
Apr 11, 2021
Ruby The Roobster
Apr 11, 2021
Ruby The Roobster
Apr 15, 2021
martinm
Apr 11, 2021
Bastiaan Veelo
Apr 12, 2021
Ruby The Roobster
Apr 12, 2021
Ruby The Roobster
Apr 12, 2021
kdevel
Apr 12, 2021
Imperatorn
Apr 12, 2021
kdevel
Apr 12, 2021
Imperatorn
April 11, 2021

So, here is the full code:

enum Color {none=0,red=1,black=2};
enum sColor {black=0,white=1};
class Square {
public:
    this(Color color, sColor Scolor) {
    this.color = color;
    this.Scolor = Scolor;
    }
    Color color;
    sColor Scolor;
}
import std.stdio;
void cPrintboard(Square[8][8] square)
{
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            final switch(square[i][j].color) {
            case Color.none:
                write(" n ");
                break;
            case Color.red:
                write(" r ");
                break;
            case Color.black:
                write(" b ");
                break;
            }
        }
        writeln("");
    }
    writeln("");
}
void sPrintboard(Square[8][8] square) {
    for(int i=0;i<8;i++)
    {
        for(int j=0;j<8;j++)
        {
            final switch(square[i][j].Scolor) {
            case sColor.white:
                write(" w ");
                break;
            case sColor.black:
                write(" b ");
                break;
            }
        }
        writeln("");
    }
    writeln("");
}
bool isEven(int a)
{
    return a&1;
}
void main()
{
    Square[8][8] square;
    for(int i=7;7>=0;i--)
    {
        for(int j=7;j>=0;j--)
        {
            if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j)))
                square[i][j] = new Square(Color.none, sColor.white);
            else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
                square[i][j] = new Square(Color.none,sColor.black);
            else
                assert(false);

        }
    }
    for(int i=2;i>=0;i--)
    {
        for(int j=7;i>=0;j--)
        {
            if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
                square[i][j].color = Color.red; //The program breaks here, with a range violation message
            else
            {

            }
        }
    }
    cPrintboard(square);
    sPrintboard(square);
}

What am I doing wrong here? Is it the 'for' loop?

April 11, 2021

On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:

>

What am I doing wrong here? Is it the 'for' loop?
Nevermind. I messed up what line it was. It was actually this line:

>
square[i][j] = new Square(Color.none, sColor.white);
April 11, 2021

On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:

>

What am I doing wrong here? Is it the 'for' loop?

Yes, there is a 7 where there should be an i on this line:

   for(int i=7;7>=0;i--)

This will go on forever, so you get a range error as soon as i < 0.

—Bastiaan.

April 11, 2021
On 4/11/21 4:41 PM, Bastiaan Veelo wrote:
> On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:
>> What am I doing wrong here? Is it the 'for' loop?
> 
> Yes, there is a `7` where there should be an `i` on this line:
> ```d
>    for(int i=7;7>=0;i--)
> ```
> This will go on forever, so you get a range error as soon as `i < 0`.

Also this code is wrong:

```d
   for(int i=2;i>=0;i--)
   {
       for(int j=7;i>=0;j--)
```

For that j loop, i will always be 2, so it will not terminate until the range error happens.

Should probably be:

for(int j=7; j >= 0; j--)

I would also suggest for more readable code, use foreach + iota or foreach_reverse:

```d
foreach_reverse(i; 0 .. 3) // or foreach(i; iota(2, -1, -1))
{
     foreach_reverse(j; 0 .. 8)
```

-Steve
April 12, 2021
On Monday, 12 April 2021 at 01:22:12 UTC, Steven Schveighoffer wrote:
> On 4/11/21 4:41 PM, Bastiaan Veelo wrote:
>> On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:
>>> What am I doing wrong here? Is it the 'for' loop?
>> 
>> Yes, there is a `7` where there should be an `i` on this line:
>> ```d
>>    for(int i=7;7>=0;i--)
>> ```
>> This will go on forever, so you get a range error as soon as `i < 0`.
>...

I fixed the code now. It works. Thanks for your help.
April 12, 2021

On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote:

>

On Sunday, 11 April 2021 at 19:45:30 UTC, Ruby The Roobster wrote:

>

What am I doing wrong here? Is it the 'for' loop?

Yes, there is a 7 where there should be an i on this line:

   for(int i=7;7>=0;i--)

This will go on forever, so you get a range error as soon as i < 0.

—Bastiaan.

I have fixed this.

April 12, 2021

On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote:
[...]

> >

What am I doing wrong here? Is it the 'for' loop?

Yes, there is a 7 where there should be an i on this line:

   for(int i=7;7>=0;i--)

This will go on forever, so you get a range error as soon as i < 0.

Can't these kinds of errors be eliminated by using foreach loops?:

    Square[8][8] square;
    foreach (int i, ref v; square)
    {
        foreach(int j, ref w; v)
        {
            if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j)))
                w = new Square(Color.none, sColor.white);
            else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
                w = new Square(Color.none,sColor.black);
            else
                assert(false);

        }
    }
April 12, 2021

On Monday, 12 April 2021 at 18:01:02 UTC, kdevel wrote:

>

On Sunday, 11 April 2021 at 20:41:35 UTC, Bastiaan Veelo wrote:
[...]

> >

[...]

Yes, there is a 7 where there should be an i on this line:

   for(int i=7;7>=0;i--)

This will go on forever, so you get a range error as soon as i < 0.

Can't these kinds of errors be eliminated by using foreach loops?:

    Square[8][8] square;
    foreach (int i, ref v; square)
    {
        foreach(int j, ref w; v)
        {
            if((isEven(i)&&isEven(j))^(!isEven(i)&&!isEven(j)))
                w = new Square(Color.none, sColor.white);
            else if((!isEven(i)&&isEven(j))^(isEven(i)&&!isEven(j)))
                w = new Square(Color.none,sColor.black);
            else
                assert(false);

        }
    }

Yup

April 12, 2021

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

>

Yup

D can be so much fun!

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);
}
April 12, 2021

On Monday, 12 April 2021 at 19:19:12 UTC, kdevel wrote:

>

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

>

[...]

D can be so much fun!

import std.stdio;

[...]

Of course :D

« First   ‹ Prev
1 2