這裏是我所創建的功能:我想它選擇0或1隨機隨機選擇0和1之間
tJugador quienEmpieza(){
rand()
tJugador whoStarts = tJugador(rand() % 2); // randomly assigns player
return whoStarts;
。它每次選擇0。應該隨機選擇玩家角色或玩家自動機的遊戲。我只需要搞清楚如何使功能選擇0或1
這裏是整個方案與建議的更改幫助:
#include <iostream>
#include<ctime>
#include<cstdlib>
#include <string>
using namespace std;
const int Meta = 31;
enum tJugador { Automata, Persona };
tJugador quienEmpieza();
tJugador pasalacalculador();
int digitoAleatorio();
int digitoAutomata(int ultimo);
int digitoPersona();
int digitoPersona(int ultimo);
bool mismaFila(int ultimo, int nuevo);
bool mismaColumna(int ultimo, int nuevo);
bool digitoValido(int ultimo, int nuevo);
int main()
{
std::srand(time(NULL));
int pasalacalculadora();
int whoStarts = 0;
int d = 0;
int suma;
suma = 0;
string nom;
int ultimo;
ultimo = 0;
int x = 0;
int next = 0;
cout << "Bienvenido a Pasa La Calculadora!" << endl;
cout << "Como Te llamas?";
cin >> nom;
cout << "Hola:" << nom << endl;
cout << "Empiezas:";
if (whoStarts == 0)
{
std::cout << "Automata" << endl;
}
else if (whoStarts == 1)
{
std::cout << "Persona" << endl;
}
while ((x != 0) && (suma < Meta)) {
cout << "Introduce un numero (0 para abandonar): ";
cin >> d;
cout << "Suma =" << suma << endl;
if (whoStarts == Automata){
d = digitoAutomata(ultimo);
suma = suma + d;
ultimo = d;
next = Persona;
}
if (whoStarts == Persona){
x = digitoPersona();
suma = suma + x;
ultimo = x;
next = Automata;
}
}
cin.sync();
cin.get();
system("pause");
return next;
}
tJugador quienEmpieza(){
rand();
tJugador whoStarts = tJugador(rand() % 2); // randomly assigns player
return whoStarts;
}
int digitoAleatorio(){
return (rand() % 9) + 1; // randomly assigns a number
if (quienEmpieza == 0)
{
std::cout << "Introduce un digito:" << digitoAleatorio << endl;
std::cout << ("Suma:") << digitoAleatorio << endl;
}
}
int digitoAutomata(int ultimo){
bool validDigit = false;
int digitoAleatorio = 0;
int d = 0;
if (digitoAleatorio == validDigit) {
(d = true);
return d;
}
}
int digitoPersona(){
int x;
int whoStarts = 0;
if (whoStarts == 1)
{
cout << "Introduce un numero entre 0 y 9(0 para abandonar): ";
cin >> x;
}
if ((x >= 1) && (x <= 9))
{
cout << ("Suma:") << x << endl;
}
return x;
}
}
tJugador究竟做了什麼?發佈代碼。順便說一下,whoStarts的類型是'tJugador'而不是'int' – nbro 2014-12-06 16:39:18
它選擇了人物角色或自動機 – Create881 2014-12-06 17:08:58
我看不到你在哪裏調用函數'quienEmpienza'。在你的函數中,'digitoAleatorio'你有一個if語句來檢查'quienEmpieza == 0' ,但是這個全局變量在哪裏?也許你想調用這個函數?所以它會是'quienEmpieza()== 0'? – nbro 2014-12-06 17:19:33