Range Error
Ruby The Roobster
michaeleverestc79 at gmail.com
Sun Apr 11 19:45:30 UTC 2021
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?
More information about the Digitalmars-d-learn
mailing list