2016-11-04 2 views
0

Ich kann mein Programm nicht wegen srand arbeiten. Bitte helfen Sie. Ich schrieb mein Programm in C++, aber Übergang zu D. Ich verstehe nicht, wie srand in D arbeitet.D Programmierfehler

main.d (18): Fehler: Funktion core.stdc.stdlib.srand (Uint Seed) ist nicht aufrufbar mit Argumenttypen (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; 
    } 
} 
} 

wenn ich auf DMD zu kompilieren versucht ... es gav mir diese msg: "OPTLINK: Fehler 3: Kann nicht Datei erstellen crimGame.exe"

Antwort

2

Die Fehlermeldung bedeutet in srand(time(null)); Srand will eine Uint (32 Bit) Integer als Argument aber time gibt eine lange (64 Bit) zurück. Sie müßten es zu einem uint wurf

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

Doch bevor Sie dies tun, sollten Sie auch in 64-Bit-Modus versuchen, zu kompilieren. Wenn es dann ohne Änderung funktioniert, ändere den Cast auf cast(size_t), weil es dann abhängig von der Zielarchitektur ist.

auch statt mit srand und rand Sie uniform von std.random verwenden sollen, ist es die Zufallsfunktion in D. Sie haben nicht einmal einen Samen dann setzen müssen, weil es einigen unvorhersehbaren Wert (wie Zeit, Prozess-ID nehmen, Thread-ID usw.) als Seed automatisch.

Sie würden dann ersetzen Sie rand() % 7 Anrufe mit uniform(0, 7) (die einen Zufallswert von 0-6 erzeugt, weil es nicht 7 enthalten wird Es wird auch nur ganze Zahlen erzeugen, da beide Argumente ganze Zahlen sind und nicht Gleitkommazahlen)

+0

danke. wenn ich versuchte, auf DMD zu kompilieren ... gav mir das msg: "OPTLINK: Fehler 3: Kann Datei creamCam nicht erstellen." – Concealment

+0

Visual Studio installiert? Schreibrechte in diesem Ordner? Ordnerpfadname nicht länger als ~ 200 Zeichen? Mit welchem ​​Befehl kompilieren Sie? – WebFreak001

0

srand() benötigt ein vorzeichenloses Integer (32-Bit) Argument, aber time() liefert lang (64-bit)

Verwandte Themen