2014-09-23 43 views
0
#include<iostream> 
#include<cstdlib> 
#include<time.h> 
#include<ctime> 
#include<iomanip> 
using namespace std; 

const int End=70; //constant fixed integer for the entire game. 

void MoveTurtoise (int *); 
void MoveHare (int *); 
void PrintPosition (int *, int*); 



int main() 
{ 
int Tortoise = 1; 
int Hare = 1; 
int Time = 0; 

srand(time(0)); 

cout<<"BANG!!!!!\n" 
    <<"AND THEY'RE OFF !!!!!\n"; 

while(Tortoise != End && Hare != End) 
{ 
    srand(time(0)); 
    MoveTurtoise (&Tortoise); 
    MoveHare (&Hare); 
    PrintPosition (&Tortoise,&Hare); 
    Time++; 

} 

if (Tortoise==Hare) 
     cout<<"It's a tie."<<endl; 
    else if (Tortoise>Hare) 
     cout<<"Tortoise wins."<<endl; 
    else if (Hare>Tortoise) 
     cout<<"Hare wins."<<endl; 

system("PAUSE"); 
return 0; 
} 

void MoveTurtoise (int *Tortoise) 
{ 
srand(time(0)); 
int p = 1+ rand()%10; // 1 <= i <= 10 

if (1<=p && p<=5) //Fast plod 
    Tortoise+=3; //3 squares right 
else if (p>=6 && p<=7) //Slip 
    Tortoise-=6;//6 squares left 
else //Slow plod 
    ++Tortoise; //1 square right 

if (*Tortoise<1) 
    *Tortoise=1; 



} 

void MoveHare (int *Hare) 
{ 
srand(time(0)); 
int p = 1+ rand()%10; // 1 <= p <= 10 

if (1<= p && p<=2); //Sleep 
        //No move 
else if (p>=3 && p<=4) //Big hop 
    Hare+=9;//9 squares right 
else if (p==5) //Big Slip 
    Hare-=12;// 12 squares left 
else if (p>=6 && p<=8) // Small hop 
    ++Hare;// 1 square right 
else if (p>=9 && p<=10)// Small Slip 
    Hare-=2; // 2 squares left 

if (*Hare<1) 
    *Hare=1; 


} 

void PrintPosition (int *Tortoise, int *Hare) 
{ 
if (Tortoise==Hare) 
    cout<<"OUCH!!!"<<endl; 
else if (Tortoise<Hare) 
    { 
    cout<<setw(*Tortoise)<<"T"<<endl; 
    cout<<setw(Hare-Tortoise)<<"H"<<endl; 
    } 
else if (Hare<Tortoise) 
{ 
    cout<<setw(Tortoise-Hare)<<"T"<<endl; 
    cout<<setw(*Hare)<<"H"<<endl; 

} 




} 

大家好。我只是用C++編寫了一個用於Tortoise和Hare模擬遊戲的代碼。我有一個問題發現是什麼導致我的程序「不」終止。它一直在繼續,並取得了相同的結果。我假設有一個循環錯誤和錯誤的使用srand()..但我仍然沒有線索...烏龜和野兔模擬終止問題

+0

你試過調試過嗎? 「烏龜」和「野兔」是否增加?他們是否達到70?超過70? – 2014-09-23 16:47:34

+0

我調試,我看到的是T和H重複相同的結果。 – user3543568 2014-09-23 16:50:03

+1

我不認爲你需要在每個函數中使用'srand'。只需在'main'中使用它,並在循環中刪除'srand',因爲在它之前已經有一個。 – 2014-09-23 17:00:13

回答

1

在函數MoveTurtoiseMoveHare,您正在增加指針而不是它們的值。

void MoveTurtoise (int *Tortoise) 
{ 
    srand(time(0)); 
    int p = 1+ rand()%10; // 1 <= i <= 10 

    if (1<=p && p<=5) //Fast plod 
     Tortoise+=3; //3 squares right 
     // This makes Tortoise point to a different location. 
     // It does not change the value of what Tortoise points to. 
     // Similarly for the next two clauses. 

    else if (p>=6 && p<=7) //Slip 
     Tortoise-=6;//6 squares left 
    else //Slow plod 
     ++Tortoise; //1 square right 

    if (*Tortoise<1) 
     *Tortoise=1; 
} 

你需要什麼:

void MoveTurtoise (int *Tortoise) 
{ 
    srand(time(0)); 
    int p = 1+ rand()%10; // 1 <= i <= 10 

    if (1<=p && p<=5) //Fast plod 
     (*Tortoise) += 3; //3 squares right 
    else if (p>=6 && p<=7) //Slip 
     (*Tortoise) -= 6;//6 squares left 
    else //Slow plod 
     ++(*Tortoise); //1 square right 

    if (*Tortoise<1) 
     *Tortoise=1; 
} 

MoveHare需要被固定在一個類似的方式。

這是更好的解決方案。更改更改參數類型爲int&,然後代碼將更喜歡你所擁有的。

​​3210

而且,你的PrintPosition實現是,如果參數爲:

void PrintPosition (int Tortoise, int Hare); 

這是一個比你有什麼更好的。更改

void PrintPosition (int *Tortoise, int *Hare); 

void PrintPosition (int Tortoise, int Hare); 
0

是,user3543568198 你忘了使用std ::運輸及工務局局長添加頁眉庫;並且您在打印語句中添加了一個else if(Hare < = Tortoise),因爲如果... else執行相同的操作,但您的代碼通過引用傳遞,除了您的Tortoise函數缺少raceEnd =外,其他所有代碼都很好。 ..在頭髮和烏龜功能....只是通過用下面的功能替換你的程序功能,它實際上會移動屏幕上的單位......哦一定要添加 const int RACE_END = 70; in header

void MoveTurtoise (int *Tortoise) 
{ 
int x = 1 + rand() % 10; 
// determine which move to make 
if (x >= 1 && x <= 5)  // fast plod 
    *Tortoise += 5; 

else if (x == 9) // slip 
    *Tortoise -= 12; 

else       // slow plod 
    ++(*Tortoise); 
if (*Tortoise < 1) 
    *Tortoise = 1; 

else if (*Tortoise > RACE_END) 
    *Tortoise = RACE_END; 

} 



void MoveHare(int *Hare) 
{ 
int y = 1 + rand() % 10; 
/* Write statements that move hare */ 
// determine which move to make 
if (y >= 1 && y <= 5)  // fast plod 
    *Hare += 3; 

else if (y == 6 || y == 7) // slip 
    *Hare -= 6; 
else       // slow plod 
    ++(*Hare); 

/* Write statements that test if hare is before 
the starting point or has finished the race */ 
if (*Hare < 1) 
    *Hare = 1; 

else if (*Hare > RACE_END) 
    *Hare = RACE_END; 
}