2016-04-24 45 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 
    } 
} 

私はシンプルなシンボルチェスボードを作ろうとしています。行数/列数に応じてnumや文字を正しく設定するにはどうすればよいですか? 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); 
} 

出力: `refが何を

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

でこのように変換するとき、それがオーバーフローする可能性があることに留意してくださいません。

+0

ありがとう、試してみます。 – Kerbiter

+3

['std.conv.to'](http://dlang.org/phobos/std_conv.html#.to)を使用して、狭い変換でオーバーフローが起こることを警告します。 – rcorre

関連する問題