-6
#include <iostream>
#include <cstdlib>
#include <cmath>
using namespace std;
void displayRules();
void play();
int shuffleCard(int cardPile[]);
int main()
{
int board[26] = {0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 0, 3, 0, 0, 0, 2, 0, 0, 0, 1, 0, 0, 0, 0, 0};
int cardPile[10] = {1, 1, 2, 2, 3, 3, 4, 4, 0, 5};
int player1 = 0;
int player2 = 0;
play();
return 0;
}
void play(){
displayRules();
shuffleCard(cardPile);
}
void displayRules(){
cout << "\nWelcome to GoHome! The main objective of this game is to reach Home"
" first." << endl;
cout << "The basic rules of the game are as follows:" << endl;
cout << "\n-To begin the player with the shortest name goes first." << endl;
cout << "-Each player picks a card that has a number on it and the player"
" must moves forward that many number of spaces." << endl;
cout << "-If a card says 'Lose A Turn', the player does nothing and the"
"turn moves to the next player." << endl;
cout << "-If a card says 'Switch Places', that player is allowed to switch"
" places with any player on the board." << endl;
cout << "-If a player lands on an obstacle, that player must move back that"
" many number of spaces." << endl;
cout << "-If a player lands another obstacle while moving backwards, then it"
" does not have to move backwards again.\n"<<endl;
}
int shuffleCard(int cardPile[]){
srand(time(0));
for(int i = 0; i < 10; i++){
int size = rand() % 10;
cout << cardPile[size] << endl;
}
}
我正在做一個家庭作業,其中我的教授特別提到他只想要主要調用play函數,而其他所有事情都應該在函數中完成。C++初學者可以在另一個函數中調用函數嗎?
基本上他想要函數調用其他函數。到目前爲止,我有兩個函數,一個叫做play,另一個叫做shuffleCard。我的問題是我不確定如何讓播放函數調用shuffleCard函數。 play函數調用displayRules函數時沒有問題,但是當我嘗試編譯時,我得到一個錯誤,說使用未聲明的標識符'cardPile'。
當然這是可能的。您可能錯過了指定前向聲明? – user0042
打開你的C++書,閱讀解釋「前向聲明」如何工作的章節。 –
當編譯器說'使用未聲明的標識符'cardPile''這意味着'play'內部使用名稱(標識符)'cardPile',但它找不到任何這樣的變量。你在'main'裏面有一個,但是不能從'main'之外訪問。你需要創建另一個局部變量'int cardPile [10] = {1,1,2,2,3,3,4,4,0,5};'或者將'cardPile'傳遞給'play',以便它可以將它傳遞給'shuffleCard'。 – nwp