www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Range Error

reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
So, here is the full code:

```d
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?
Apr 11 2021
next sibling parent reply Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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:
```d
square[i][j] = new Square(Color.none, sColor.white);
```
Apr 11 2021
parent martinm <martinmannes23312 gmail.com> writes:
On Sunday, 11 April 2021 at 19:48:42 UTC, Ruby The Roobster 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?
Nevermind. I messed up what line it was. It was actually this line:
```d
square[i][j] = new Square(Color.none, sColor.white);
```
This is maybe interesting for you: https://wiki.dlang.org/Component_programming_with_ranges
Apr 15 2021
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
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`. —Bastiaan.
Apr 11 2021
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Apr 11 2021
parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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.
Apr 12 2021
prev sibling next sibling parent Ruby The Roobster <michaeleverestc79 gmail.com> writes:
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: ```d 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.
Apr 12 2021
prev sibling parent reply kdevel <kdevel vogtner.de> writes:
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: ```d 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?: ```d 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); } } ```
Apr 12 2021
parent reply Imperatorn <johan_forsberg_86 hotmail.com> writes:
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: ```d 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?: ```d 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
Apr 12 2021
parent reply kdevel <kdevel vogtner.de> writes:
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); } ```
Apr 12 2021
parent Imperatorn <johan_forsberg_86 hotmail.com> writes:
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! ```d import std.stdio; [...]
Of course :D
Apr 12 2021