D编程错误

时间:2016-11-04 23:00:43

标签: d

由于srand,我无法让我的程序工作。请帮忙。我用C ++编写了我的程序,但是将它转换为D.我不明白s中的srand如何工作。

main.d(18):错误:函数core.stdc.stdlib.srand(uint seed)不能使用参数类型调用(long)

import std.stdio;
import std.random;
import core.stdc.time;
import core.stdc.stdlib;


int criminals[8];
int a[8];
int b[8];
int c[8];
int countA,countB,countC;
int loserCount = 0;
int p1,p2,p3;

void main(string[] args)
{

srand(time(null));
int p1 = rand() % 7;
p1++;
int temp = rand() % 7;
while(temp+1 == p1)
{
    temp = rand() % 7;
}
p2 = temp+1;
temp = rand() % 7;
while(temp+1 == p1 || temp+1 == p2)
{
    temp = rand() % 7;
}
p3 = temp+1;
//writefln("criminals: ", p1, " ", p2," ", p3);
for(int i = 0 ; i < 8 ; i++)
{
    if(i == p1 || i == p2 || i == p3)
        criminals[i] = 1;
    else
        criminals[i] = 0;
    a[i] = 0;
    b[i] = 0;
    c[i] = 0;
}

writefln("\nLets Start the game. There are 7 criminals: 1,2,3,4,5,6,7 out of which 3 are the real perpetrators.");

while(true)
{

    writefln("3 random criminals: ");
    int p1 = rand() % 7;
    p1++;
    int temp2 = rand() % 7;
    while(temp2 + 1 == p1)
    {
        temp2 = rand() % 7;
    }
    p2 = temp2+1;
    temp2 = rand() % 7;
    while(temp2+1 == p1 || temp2+1 == p2)
    {
        temp2 = rand() % 7;
    }
    p3 = temp2+1;

    writefln(" ", p1, p2, p3);

    int tempCount = 0;
    if(criminals[p1] == 1)
        tempCount++;
    if(criminals[p2] == 1)
        tempCount++;
    if(criminals[p3] == 1)
        tempCount++;
    writefln("Out of these ", tempCount, " are real perpetrators");
    writefln("Does any player want to guess? (Y/N)");
    char ans;
    while(true)
    {

        if(ans == 'y' || ans == 'Y')
        {
            if(countA > -1)
            {
                writefln("Player 1 choice: ");
                int tempA;
                //cin>>tempA;
                if(criminals[tempA] && a[tempA] == 0)
                {
                    a[tempA] = 1;
                    countA++;
                }
                else if(criminals[tempA] == 0)
                {
                    countA = -1;
                    loserCount++;
                }
            }
            if(countB > -1)
            {
                writefln("Player 2 choice: ");
                int tempB;
                //cin>>tempB;
                if(criminals[tempB] && b[tempB] == 0)
                {
                    a[tempB] = 1;
                    countB++;
                }
                else if(criminals[tempB] == 0)
                {
                    countB = -1;
                    loserCount++;
                }
            }
            if(countC > -1)
            {
                writefln("Player 3 choice: ");
                int tempC;
                //cin>>tempC;
                if(criminals[tempC] && c[tempC] == 0)
                {
                    a[tempC] = 1;
                    countC++;
                }
                else if(criminals[tempC] == 0)
                {
                    countC = -1;
                    loserCount++;
                }
            }
            break;
        }
        else if(ans == 'N' || ans == 'n')
        {
            break;
        }
        else
            writefln("Wrong Input!!");
    }
    if(countA == -1)
        writefln("Player 1 has LOST");
    if(countB == -1)
        writefln("Player 2 has LOST");
    if(countC == -1)
        writefln("Player 3 has LOST");
    if(loserCount == 3)
    {
        writefln("\nAll Players have LOST\n\nGAME ENDS!");
        break;
    }
    if(countA == 3)
    {
        writefln("Player 1 WINS!!");
    }
    if(countB == 3)
    {
        writefln("Player 2 WINS!!");
    }
    if(countC == 3)
    {
        writefln("Player 3 WINS!!");
    }
    if(countA == 3 || countB == 3 || countC == 3)
    {
        writefln("GAME ENDS!!");
        break;
    }
}
}

当我尝试在DMD上编译时...它给我发信息:“OPTLINK:错误3:无法创建文件crimGame.exe”

2 个答案:

答案 0 :(得分:2)

错误消息表示srand(time(null)); srand想要一个uint(32位)整数作为参数,但time返回一个长整数(64位)。您需要将其转换为uint:

srand(cast(uint) time(null));

但是,在执行此操作之前,还应尝试在64位模式下进行编译。如果它在没有修改的情况下工作,则将转换更改为cast(size_t),因为它依赖于目标体系结构。

此外,您不应使用srandrand,而应使用uniform中的std.random,它是D中的随机函数。您不会甚至需要设置种子,因为它会自动将一些不可预测的值(如时间,进程ID,线程ID等)作为种子。

然后,您将rand() % 7调用uniform(0, 7)替换为inlist = [['1', '2', '3'],['8','9','10']] outlst = [] for lst in inlist: # For every inner list in the original list tmplst = [] # prepare a temporary empty list for converted chars for ch in lst: # and then for every char in that inner list tmplst.append(int(ch)) # perform the conversion and append it to temporary list outlst.append(tmplst) # Now the whole temporary list append to output list # (as its inner list of converted chars to int) print(inlist) print(outlst) ,它会产生一个0-6的随机值,因为它不会包含7.它也只会生成整数,因为两个参数都是是整数而不是浮点值)

答案 1 :(得分:0)

srand()需要一个无符号整数(32位)参数,但time()返回long(64位)