2016-04-29 10 views
2

コンソールでC++で機能する迷路ゲームを作成しました。C++の迷路の数が通る時間

プレイヤーが迷路を通過するのに必要な時間をカウントする機能を追加したいと思います。

迷路を通過した後に合計時間を表示することができました。

本当にありがとうございました。しかし、基本的に

do { 
    show(); // function do display maze , 2d array 
    cout << "Your position: " << x << " " << y << endl; 
    cout << "Coins gained: " << coins << endl; 
    cout << "blahblahblah" : "<<endl; 
    m = getche(); 
    cout << endl; 
    move(m); // function to recognize which way player want to go, including checking for not going through the wall 
    cout << endl; 
    system("CLS"); 
} while (x != 12 || y != 18 || coins < 10); //for pass the maze player have to move on these position and gain x coins 

system("CLS"); 
cout << "You Won!" << endl; 
cout << "Click enter to move on. \n"; 
+0

あなたの投稿には、実際に何か助けが必要であるかについて明確ではありません。 _specific_質問や、あなたが助けが必要な特定のものがありますか? \ –

+0

コンパイラがC++ 11をサポートしている場合は、 'std :: chrono'を起動する場所があります:http://en.cppreference.com/w/cpp/chrono – drescherjm

答えて

1
#include <time.h> 
#include <iostream> 

int main() { 
    int start, end, total; 
    start = time(NULL); 
//place loop here 
    //game ends, calc time 
    end = time(NULL); 
    total = end - start; 
    std::cout << "You completed the maze in " << total << " seconds."; 
    return 0; 
} 

、(秒)後の時点でカウントを停止、その後、ある時点での時間のカウントを開始何であるか、これが行われます。

メインのゲームループはそのような何かに見えますcinとgetch()、または入力を取得するためにプログラムを一時停止するものは、タイマーを停止させる可能性があります。特定のライブラリでは、システム時間を使用します。他のライブラリでは、実行時間が使用されます。このように注意してください、それが実行されている時間を利用している場合、第2の方法は、取得し、あなたのための時間を保持し、参照することによって、あなたの時間変数を渡すことによって保存されているように

int main(){ 

     time_t myTime,myTimeEnd; 
     time(&myTimeEnd); 
     myTime = myTimeEnd; 
    //code 
     time(&myTime); 
     int total = myTimeEnd - myTime; 
     std::cout<< "Time taken is " << total << " seconds."; 
     return 0; 
    } 

としてインプットメソッドを使用してください「時間」を取得する時間関数に変換します。

+0

ああ、私は以前にクロノを使っていましたそれほど難しいことではありません。手伝ってくれてありがとう。 – Matthaius