2016-04-24 196 views
1
ref void init_board (ref int side, ref char[][] board) //make empty symbol chessboard 
{ 
    const char black = ' '; 
    const char white = 0xB0; 

    board[0][0] = ' '; 
    for (int i = 1; i <= side; i++) 
    { 
     board[i][0] = 0x30 + i; //Setting nums; "Error: Cannot convert int to char" 
     board[0][i] = 0x40 + i; //Setting letters; same here 
     for (int j = 1; j <= side; j++) 
      board[i][j] = (i+j)%2 == 0 ? black : white; //making black-white board 
    } 
} 

我想做一个简单的象征棋盘。如何正确设置数字和字母取决于或行数/列数? board[i][0] = 0x30 + i;(或0x40的)工作在C++,但不是在D.将int转换为char?

+0

是什么'裁判void'吗? – sigod

+0

@Kerbiter你为什么在那里使用ref?在任何一方面? 'ref int'当它只被读取时是一个完全的浪费,'ref char [] []'同样只是在这里增加了另一个间接的方法。 –

回答

6

你已经有了你需要的std.conv模块。 - 最好的是使用std.conv.to

import std.conv; 
import std.stdio; 

void main() { 
    int i = 68; 
    char a = to!char(i); 
    writeln(a); 
} 

输出:

D 
1
board[i][0] = cast(char)(0x30 + i); 

请记住,转换这样的时候,它可能溢出。

+0

谢谢,会尝试。 – Kerbiter

+3

使用['std.conv.to'](http://dlang.org/phobos/std_conv.html#.to)在缩小转换时发生溢出警告。 – rcorre