2017-01-23 59 views
-1

我正在爲遊戲「Scattegories」編寫this程序,我想將某種字典(用於檢查是否存在單詞)連接到程序。 如果我這樣做,我該怎麼做?將程序連接到字典

// A program to keep track of points and time and to give a random letter for the game scattergories 
#include<iostream> 
#include<ctime> 
#include<string> 
#include <chrono> 
#include <thread> 
using std::cout; 
using std::cin; 
using std::string; 
using std::getline; 
using namespace std::chrono_literals; 
using std::this_thread::sleep_for; 

void ltr() //gives a random letter 
{ 
    srand(time(NULL)); //gives a differant pattern every time 
    char letter; 
    letter = rand() % 26 + 65;   //assigns a random letter in ascii code to a char (resulting in a random letter) 
    cout << "The letter is " << letter << "\n"; 
} 

void timer() 
{ 
    cout << "You got 1.5 minutes to finish\n"; //Changing the duration of the timer is done by changing the value of 'i' in the "for" loop 
    for (int i = 90; i > 0; i--) 
    { 
     if (i % 5 == 0) 
      cout << i << "\n"; 
     sleep_for(1s); 
    } 
    cout << "DING DONG!!! DING DONG!!! Time's up!!!\n"; 
} 

void table() 
{ 
    int plr, ctr; 
    string lst[5][20];   //first dimantion: how many players. second dimantion: how many catagories, third dimantion(if added) will be the round 
    cout << "How many players?"; 
    cin >> plr; 
    cout << "How many catagories?"; 
    cin >> ctr;  //parameters for later 
    cin.ignore();     //To avoid the "getline" reading the last input 
    for (int x = 0; x<plr; x++)  //the player changes only after the previus player finishes 
    { 
     cout << "Player number " << x+1<<":"; 
     timer();  //gives time to write the words. Optimaly it would run in the background while each player writes the words. 
     for (int i = 0; i<ctr; i++)  //changing catagory 
     { 
      getline(cin, lst[x][i]); 
     } 
     system("cls"); 
     cout << "Next player\n"; 
    } 
    for (int x = 0; x<plr; x++)     //this part (the whole "for" loop) is for confirming evreything is writen down 
    { 
     cout << "Player number " << x + 1 << ": "; 
     for (int i = 0; i<ctr; i++) 
     { 
      cout << lst[x][i] << " "; 
     } 
     cout << "\n"; 
    } 
    sleep_for(5s); 
} 

int points()  //points gained per round 
{ 
    int a, b, c, sum; 
    cout << "How many sections only you got?\n";   //worth 15 points 
    cin >> a; 
    cout << "How many words only you got?\n";  //worth 10 points 
    cin >> b; 
    cout << "How many words you and another person got?\n"; //worth 5 points 
    cin >> c; 
    sum = a * 15 + b * 10 + c * 5; 
    return sum;   //Note: It doesn't matter how many sections there are. 
} 

int act() //running the program 
{ 
    int Points; 
    ltr(); 
    table(); 
    Points = points(); 
    cout << "You have earned " << Points << " this round\n\n"; 
    return Points; 
} 

int main() 
{ 
    auto start = std::chrono::high_resolution_clock::now(); 
    int Points; 
    cout << "Starting in five seconds\n"; 
    sleep_for(5s); 
    Points = act(); 
    for (;;)   //inf loop 
    { 
     int ph; 
     cout << "Press 1 to continue or anything else to stop\n"; 
     cin >> ph; 
     if (ph == 1) 
     { 
      Points += act(); //keeping score of the rounds 
     } 
     else 
     { 
      auto end = std::chrono::high_resolution_clock::now(); 
      break; 
     } 
    } 
    cout << "You have earned a total of " << Points << " great job!"; 
    sleep_for(5s);  //time to read the last text 
    return 0; 
} 

/* 
    To do list: 
    -Convert to arduino 
    -Make timer work in background of of table 
    -Check if words in the table (for differant players) are the same and give points accordingly 
    -Check if words are actual words (connect an online dictonary?) 
    -Make interface? (if possible and I have time to learn how) 
    -Think of what to do with Hardwear 
    -Comment rest of the code 
    -Make a point count for each player 
    -change "srand" placement 
*/ 

請記住,我是相對較新的編程(近半年),所以請嘗試儘可能簡單地解釋它,謝謝。

+0

請仔細閱讀[旅遊](http://stackoverflow.com/tour)和[*怎樣問一個很好的問題?*](http://stackoverflow.com/help/how-to-ask) 。 – Biffen

+1

偏題:除非你真的有很好的理由,否則每個程序只能調用一次'srand'。它會重新啓動隨機數生成器,因此如果在同一秒鐘內調用,您可能會一遍又一遍地得到相同的數字。 – user4581301

+0

關於主題:[請閱讀'std :: set'](http://en.cppreference.com/w/cpp/container/set)。 – user4581301

回答

1

可以是一個有趣的練習來實現自己的字典。

使用大量字符來存儲單詞,按字母順序排序,每個字符都以null結尾。另外,保留一組索引,每個索引指向一個單詞的開始。

然後執行查找的二分搜索。這將是相當有效的,因爲你會在最Log(N)字符串比較後發現一個字,1L字符比較,其中L是字長之間每次服用。


基於散列法的解決方案可以更高效,但代碼有點困難。

+0

會使用二維數組,第一維按字母排序,第二維按字排序?我會檢查輸入的單詞的第一個字母,並僅在該維度中查找單詞。 – Sela12

+0

@ Sela12:不確定你的意思,但不是,二維數組無關緊要。檢查第一個字母與純二叉搜索是浪費時間。 –