Почему моя игра в фаркле не заканчивает игру в раунде и дает случайное число очков

Я создаю эту игру как финальный проект для класса. Игра запускается в первый раз, так как вы просите сохранить кубики по одному, а затем останавливается после 3 и печатает большое случайное отрицательное число. Затем переходите к следующему ходу игрока, но не отображает его имя.

#include "stdafx.h"#include <iostream>
#include <string>
#include <string.h>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>using namespace std;

int diceGame()
{
int dice[6], roll, keep[6], score;
char answer, done;

for (int i = 0; i < 6; i++)
{
dice[i] = rand() % 6 + 1;
cout << "You rolled " << (i + 1) << ". " << dice[i] << endl;
roll = dice[i];
}

for (int i = 0; i < dice[i]; i++)
{

cout << "Do you want to keep dice #" << (i + 1) << "? (Y/N)" << endl;
cin >> answer;

if (answer == 'Y' || answer == 'y')
{
keep[i] = dice[i];
}

if (keep[0] == 1 && keep[1] == 1 && keep[2] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[1] == 1 && keep[3] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[1] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[1] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[2] == 1 && keep[3] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[2] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[2] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[3] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[3] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[4] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[2] == 1 && keep[3] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[2] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[2] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[3] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[3] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[1] == 1 && keep[4] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[2] == 1 && keep[3] == 1 && keep[4] == 1)
{
score = score + 1000;
}
else if (keep[2] == 1 && keep[3] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[2] == 1 && keep[4] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[3] == 1 && keep[4] == 1 && keep[5] == 1)
{
score = score + 1000;
}
else if (keep[0] == 1 && keep[1] != 1 && keep[2] != 1 && keep[3] != 1 && keep[4] != 1 && keep[5] != 1)
{
score = score + 100;
}
else if (keep[1] == 1 && keep[0] != 1 && keep[2] != 1 && keep[3] != 1 && keep[4] != 1 && keep[5] != 1)
{
score = score + 100;
}
else if (keep[2] == 1 && keep[1] != 1 && keep[0] != 1 && keep[3] != 1 && keep[4] != 1 && keep[5] != 1)
{
score = score + 100;
}
else if (keep[3] == 1 && keep[1] != 1 && keep[2] != 1 && keep[0] != 1 && keep[4] != 1 && keep[5] != 1)
{
score = score + 100;
}
else if (keep[4] == 1 && keep[1] != 1 && keep[2] != 1 && keep[3] != 1 && keep[0] != 1 && keep[5] != 1)
{
score = score + 100;
}
else if (keep[5] == 1 && keep[1] != 1 && keep[2] != 1 && keep[3] != 1 && keep[4] != 1 && keep[0] != 1)
{
score = score + 100;
}
else if (keep[0] == 5 && keep[1] != 5 && keep[2] != 5 && keep[3] != 5 && keep[4] != 5 && keep[5] != 5)
{
score = score + 50;
}
else if (keep[1] == 5 && keep[0] != 5 && keep[2] != 5 && keep[3] != 5 && keep[4] != 5 && keep[5] != 5)
{
score = score + 50;
}
else if (keep[2] == 5 && keep[1] != 5 && keep[0] != 5 && keep[3] != 5 && keep[4] != 5 && keep[5] != 5)
{
score = score + 50;
}
else if (keep[3] == 5 && keep[1] != 5 && keep[2] != 5 && keep[0] != 5 && keep[4] != 5 && keep[5] != 5)
{
score = score + 50;
}
else if (keep[4] == 5 && keep[1] != 5 && keep[2] != 5 && keep[3] != 5 && keep[0] != 5 && keep[5] != 5)
{
score = score + 50;
}
else if (keep[5] == 5 && keep[1] != 5 && keep[2] != 5 && keep[3] != 5 && keep[4] != 5 && keep[0] != 5)
{
score = score + 50;
}
else if (keep[0] == keep[1] && keep[2] || keep[0] == keep[1] && keep[3] || keep[0] == keep[1] && keep[4] || keep[0] == keep[1] && keep[5])
{
score = score + (keep[0] * 100);
}
else if (keep[1] == keep[2] && keep[3] || keep[1] == keep[2] && keep[4] || keep[1] == keep[2] && keep[5])
{
score = score + (keep[1] * 100);
}
else if (keep[2] == keep[3] && keep[4] || keep[2] == keep[3] && keep[5])
{
score = score + (keep[2] * 100);
}
else if (keep[3] == keep[4] && keep[5])
{
score = score + (keep[3] * 100);
}cout << "Are you done this round? (Y/N)" << endl;
cin >> done;

if (done == 'y' || done == 'Y')
{
break;
}

}return score;
}int main()
{
int playerNum, score;
char rulesAnswer, doneAnswer;
string line;srand(time(NULL));

cout << "Welcome to FARKLE!!!" << endl;
cout << "Coded by: " << endl;
cout << endl;do {
cout << "Would you like to read the rules? (Y or N)" << endl; //rules option
cin >> rulesAnswer;
if (rulesAnswer == 'Y' || rulesAnswer == 'y')
{
ifstream ruleFile("farkleRules.txt"); //call file to be opened
if (ruleFile.is_open()) //if loop to check if the file is open
{
while (getline(ruleFile, line)) //while the file is open read line by line
{
cout << line << endl; //display the file
}
ruleFile.close(); // close the file
break;
}

else {
cout << "Unable to open the file" << endl;; //error warning if the file fails to open
}
}
else if (rulesAnswer == 'N' || rulesAnswer == 'n')
{
break;
}

else
{
cout << "Please enter Y or N only" << endl; //answer requirement
}

} while (rulesAnswer != 'Y' || rulesAnswer != 'y');

do {
cout << "How many players?" << endl; //getting numer of players
cin >> playerNum;

if (playerNum < 2) { //player requirement
cout << "Please add more players." << endl;
}

} while (playerNum < 2);

vector<string> playerName(playerNum);
ofstream ruleFile;for (int i = 0; i < playerNum; i++) //for loop to get name of all the players
{
cout << "Player " << i + 1 << " name?" << endl;
cin >> playerName[i];
}

for (int i = 0; i < playerNum; i++)
{
cout << playerName[i] << "'s turn." << endl;
do
{
score = diceGame();
cout << playerName[i] << "'s score is: " << score << endl;
cout << "Would you like to stop playing?" << endl;
cin >> doneAnswer;

if (doneAnswer == 'Y' || doneAnswer == 'y')
{
break;
}} while (doneAnswer != 'Y' || doneAnswer != 'y');
}return 0;
}

0

Решение

Задача ещё не решена.

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]