I am working on a program to code a game called Devil's Dice. I have the logic of code working perfectly, but I am struggling to print the scoreboard to show the games progress. The details of the assignment are below, followed by my code. I don't know where to begin printing the scoreboard as shown in the image I linked.
In our version of Devil's Dice the rules will be as follows:
The player repeatedly rolls a 6-sided die until either a 1 is rolled or the player decides to "hold".
If the player rolls a 1, they lose any points accumulated this turn and the devil gets a turn.
If the player rolls any other number, it is added to their turn total and the player's turn continues.
If a player chooses to "hold", their current turn total is added to their score and the devil gets a turn.
Devil's Logic
If the score is tied or the devil is winning, he will keep rolling until he has at least 21 points (unless he has already reached 100 points)
If the player is winning, the devil will keep rolling until he has at least 30 points (unless he has already reached 100 points)
If the devil rolls a 1, he also loses the points he has accumulated on his turn
If the player chooses to "forfeit", they lose and the game is over.
The player must score 100 points before the devil manages to do so in order to win.
For example, the player, Ann, begins a turn with a roll of 5. Ann could hold and score 5 points, but chooses to roll again. Ann rolls a 2, and could hold with a turn total of 7 points, but chooses to roll again. Ann rolls a 1, and must end her turn without scoring. The devil then rolls the sequence 4-5-3-5-5, chooses to hold, and adds his turn total of 22 points to his score.
There are a few other details about the game logic but i am confident i have it working correctly. I am just wondering how to get a menu with a moving arrow as shown below.
My code looks like this:
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;
int main() { //just for a school grading program//
#ifdef JARVIS
srand(0);
#else
srand(time(NULL));
#endif
// Call your functions to play your game here...
char userInput;
int diceRoll;
int turnScore = 0;
int totalScore = 0;
int devilScore = 0;
bool isItDevilsTurn = false;
bool winner = false;
int devilWins = 0;
int playerWins = 0;
cout << "---- Welcome to Devil's Dice! ----" << endl;
cout << "Hold[h], roll[r], or forfeit[f]: " << endl;
while (!winner) {
if (!isItDevilsTurn) {
cin >> userInput;
if (userInput == 'h')
{
totalScore = totalScore + turnScore;
cout << "You banked " << turnScore << " points and have a total of " << totalScore << " points" << endl;
isItDevilsTurn = true;
turnScore = 0;
}
if (userInput == 'r')
{
diceRoll = rand() % 6 + 1;
if (diceRoll > 1)
{
cout << "You rolled a " << diceRoll << "!" << endl;
turnScore = turnScore + diceRoll;
}
else
{
cout << "You rolled a 1 :(" << endl;
turnScore = 0;
isItDevilsTurn = true;
}
}
if (totalScore >= 100) {
winner = true;
cout << "You Win!" << endl;
playerWins = playerWins + 1;
}
if (userInput == 'f') {
cout << "Game Over!" << endl;
}
}
else
{
int devilGoal = 21;
if (totalScore > devilScore)
{
devilGoal = 30;
}
while (isItDevilsTurn)
{
diceRoll = rand() % 6 + 1;
if (diceRoll > 1 && turnScore < devilGoal)
{
cout << "devil rolled " << diceRoll << endl;
turnScore = turnScore + diceRoll;
}
else if (diceRoll > 1 && turnScore > devilGoal)
{
devilScore = devilScore + turnScore;
cout << "devil holds " << turnScore << " points and now has " << devilScore << " points" << endl;
isItDevilsTurn = false;
turnScore = 0;
}
else
{
cout << "devil rolled a 1" << endl;
isItDevilsTurn = false;
turnScore = 0;
}
if (devilScore >= 100)
{
cout << "Devil Wins!" << endl;
devilWins = devilWins + 1;
winner = true;
}
}
}
}
cout << "Total Wins: " << playerWins << endl;
cout << "Total Losses: " << devilWins << endl;
fstream output("games.txt");
output << playerWins << " " << devilWins << endl;
return 0;
}
I'm sorry, I'm not too good with tabs, so you just make it looks like you need to. I used a ternary operator to understand if I need to write number or a tab in cuurent line
printf("\tPlayer\t\t\tDevil\n");
printf("\t------\t\t\t-----\n");
for(int i = 10; i >= 0; --i){
printf((totalScore < (i+1)*10 && totalScore >=i*10) ? "%d >" : "\t", totalScore);
printf("--%d", i*10);
printf((turnScore < (i+1)*10 && turnScore >=i*10) ? "< %d" : "\t\t\t", turnScore);
printf((devilTotal < (i+1)*10 && devilTotal >=i*10) ? "%d >" : "\t", devilTotal);
printf("--%d", i*10);
printf((devilTurn < (i+1)*10 && devilTurn >=i*10) ? "< %d" : "\t\t", devilTurn);
printf("\n");
}
How it works? first printf() I evaluate totalScore, if it's less then next value and more than current I print it, otherwise just print tab. Second printf() prints current value. Third printf() evalutes turnScore, if it's less then next value and more than current I print it, otherwise just print 2 tabs.
Same for devil.
It would be the same with cin and cout, it's just that I feel more confident with printf(). If it looks right, all you need to do is work with alignments.
Related
I am trying to practice my C++ in order to grasp a better understanding in class, as I am a beginner and a little behind, and I came across a zombie game that someone posted online.
Anyways, I understand most of the code, however I do not understand "if(rand() & 67 < 10)".
I can interpret "rand() % 10 + 1", in which the code will generate a number, or in this case zombies, between 1 and 10.
Thinking at first that the zombies would cap at 67, given the player had chosen a high number, but I don't believe that is its function, but again, I'm not entirely sure...
Here is an example of the code I am looking at:
I'm sure its something simple, however I am still confused on its purpose. Even after trying to learn its function my running the game
Here is the whole code just in case:
#include <iostream>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>
using namespace std;
int createZombie() { // createZombie function OPEN
if(rand() % 67 < 10)
return 11;
else
return rand() % 10 + 1;
} // createZombie fuction CLOSED
// #################### MAIN FUNCTION ############################
int main() { // main function OPEN
srand(time(NULL));
char enter;
// game stats
int playerAlive = true;
int playerSkill = 9;
int playerScore = 1;
string playerName = "";
int zombieCount = 0;
int zombiesKILLed = 0;
// title
cout << "Welcome to Zombie War." << endl << "Press [ENTER] to start ";
cin.get();
// player name
cout << "Please enter your name: ";
cin >> playerName;
// ask how many zombies
cout << "How many zombies do you wish to fight? ";
cin >> zombieCount;
cout <<"Get ready to fight for you life, " << playerName << "!" << endl;
// main game loop
while(playerAlive && zombiesKILLed < zombieCount) { // while loop OPEN
// create a random zombie
int zombieSkill = createZombie();
// battle sequence
if(zombieSkill > 10) { // if statment OPEN
cout << endl << "Here comes a huge zombie! " << endl;
} // if statement CLOSED
else { // else statement OPEN
cout << endl << "Here comes Zombie " << zombiesKILLed + 1 << endl;
} // else statement CLOSED
cout << "Fighting... " << endl;
sleep(2);
// zombies killed the player
if(playerSkill < zombieSkill) { // if statement OPEN
playerAlive = false;
cout << "You have died." << endl;
} // if statement CLOSED
else { // else statment OPEN
// PLAYER KILLED THE ZOMBIE
if(playerSkill - zombieSkill > 7) { // if statement OPEN
cout << "You wasted the Zombie! " << endl;
playerScore = playerScore * 2;
} // if statment CLOSED
else if (playerSkill - zombieSkill > 5) { // else if statement OPEN
cout << "You decapitated the Zombie!" << endl;
playerScore = playerScore * 2;
} // else if statement CLOSED
else if (playerSkill - zombieSkill > 0) { // else
if statement OPEN
cout << "You Killed the Zombie!" << endl;
playerScore = playerScore * 2;
} // else if statment CLOSED
else { // else statment OPEN
cout << "You killed the zombie, but suffered injuries." << endl;
} // else statment CLOSED
zombiesKILLed++;
} // else statment CLOSE
cout << endl;
sleep(1);
} // while loop CLOSED
if(zombiesKILLed == zombieCount) { // if statement OPEN
// victory
cout <<"Your have survived the onslaught!" << endl;
} // if statement CLOSED
else { // else statement OPEN
// LOST
cout << "You did not survive the zombie war" << endl;
} // else statement CLOSED
cout << "Zombies killed: " << zombiesKILLed << endl;
cout << "Final score: " << playerScore << endl << endl;
} // main function CLOSED
Splitting the command into two parts for easy understanding:
Part I-
rand() % 67
= generate any random number from 0-66
= remainder of division of any number by 67
= N (say)
Part II
if( rand() % 67 < 10)
= if( N < 10)
So, I'm super new to C++ and am sharing a book with a friend. I'm creating a simple guessing game, where the user imagines a number, and the computer attempts to guess it. When I debug in Visual Studio, the project does make a guess, and properly prints "how did I do?". At this point, it should get user input for the 'feedback' variable. After the prompt, however, it seems as if it will only repeat everything before the 'while' statement. Does the problem concern the feedback char variable (maybe I should've just used 'cin' and integers?), or am I just missing something really obvious?
//Game that attempts to guess a number from one to twenty.
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
auto lowbound = 1;
auto highbound = 20;
auto guess = 10;
auto gamecont = true;
char feedback[1];
cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl;
cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << endl << endl;
cout << " Waiting on you..." << endl << " ";
cin.get();
while(gamecont)
{
cout << " I'm thinking your number is " << guess << "." << endl << endl;
cout << " How did I do?" << endl << endl << " ";
cin.get(feedback, 1);
if (feedback[1] == 1) // The guess was too low.
{
if (guess == 10)
{
guess = 15;
}
else if (guess >= 15)
{
guess++;
}
else if (guess < 10)
{
guess++;
}
}
else if (feedback[1] == 2) // The guess was too high.
{
if (guess == 10)
{
guess = 5;
}
else if (guess <= 5)
{
guess--;
}
else if (guess > 10)
{
guess--;
}
}
else if (feedback[1] == 3) // The guess was correct.
{
gamecont = false;
}
}
return 0;
}
Sorry if this question is stupid for whatever reason, and thanks in advance for reading.
a journey of a thousand miles begins with a single step, so here´s some aid for your first step:
using namespace std;
don´t do that. std:: is crowded with identifiers you might use too, problems are guaranteed.
char feedback[1];
You´ll never have input longer than 1, so
char feedback;
is more than appropriate. (besides: arrays are 0 based so it should have been char feedback[0]; instead of char feedback[1];)
cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << endl << endl;
std::endl flushes the buffer, no need to do that twice. Simply use '\n':
std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n";
you´ll get the character code of the key in feedback. '1' is not equal to 1, so
if (feedback == 1)
should be
if (feedback == '1')
Thats it. There still some work remaining to do for you, e.g. the guessing strategy is poor, but that should be a start.
//Game that attempts to guess a number from one to twenty.
#include <iostream>
int main()
{
auto lowbound = 1;
auto highbound = 20;
auto guess = 10;
auto gamecont = true;
char feedback;
std::cout << " Pick a number from one to twenty in you head and I'll guess it; no cheating!" << "\n\n";
std::cout << " If my guess is too low, just say (1). If too high, say (2). Say (3) if I've got it. It's (ENTER) to get going!" << "\n\n";
std::cout << " Waiting on you..." << "\n\n";
std::cin.get();
while(gamecont)
{
std::cout << " I'm thinking your number is " << guess << "." << "\n\n";
std::cout << " How did I do?" << "\n\n";
std::cin.ignore();
std::cin.get(feedback);
if (feedback == '1') // The guess was too low.
{
if (guess == 10)
{
guess = 15;
}
else if (guess >= 15)
{
guess++;
}
else if (guess < 10)
{
guess++;
}
}
else if (feedback == '2') // The guess was too high.
{
if (guess == 10)
{
guess = 5;
}
else if (guess <= 5)
{
guess--;
}
else if (guess > 10)
{
guess--;
}
}
else if (feedback == '3') // The guess was correct.
{
gamecont = false;
}
}
return 0;
}
score is an array of 10 scores in ascending order.
scoreName is an array of names associated with with each score.
I need to check if a new score is good enough to enter the the high score table and if so, insert it into the correct position and make sure scoreName is also updated to reflect any changes.
I am having problems with my current code:
//Table
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//Check to see if you made the highScore
if (diceTotal >= score[9])
{
cout << "Congrats, you have made the HighScore Table !\nEnter Your Name.";
cin >> playerName;
for (int i = 9; i < leaderBoardSize; i--)
{
if (diceTotal <= score[i])
{
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
scoreName[i] = playerName;
score[i] = diceTotal;
break;
}
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
}
}
Here is the entire code:
#include <iostream>
#include <string>
#include <time.h>
using namespace std;
int main()
{
//dice game variables
int dice1 = 0;
int dice2 = 0;
int diceTotal = 0;
int round = 0;
string choice;
bool isDone = true;
//Scoreboard
const int leaderBoardSize = 10;
int score[10] = { 40, 33, 29, 24, 22, 19, 15, 12, 11, 9 };
string scoreName[leaderBoardSize] = { "Jason", "Steve", "Bob", "Timberduck", "Eric", "Susan", "Tyler", "Nick", "NinjaDave", "RaidenGunfire" };
string playerName;
//random number seeder
srand((unsigned int)time(NULL));
//Game instructions
cout << "dice game\n---------\nCreated By: Darcy Tellier\n--------------------------\nInstructions:\nRoll 2 dices. Try to get as close to 50 without going over." << endl;
//The Game loop
do
{
//resets game variables
diceTotal = 0;
round = 1;
//in game match loop
do
{
// display round #, current dice total, ask user to quit or re-roll.
cout << "Round\n-----\n " << round << endl;
cout << "current total:" << diceTotal << endl;
cout << "Roll dice (y/n)?";
cin >> choice;
//Checks the users imput for invalid characters
while (choice != "Y" && choice != "y" && choice != "N" && choice != "n")
{
cout << "invalid option. Choose y/n:" << endl;
cin >> choice;
}
if (choice == "Y" || choice == "y")
{
//roll dice
round += 1;
dice1 = rand() % 6 + 1;
dice2 = rand() % 6 + 1;
diceTotal = diceTotal + dice1 + dice2;
cout << "you have rolled a " << dice1 << " and a " << dice2 << endl;
if (diceTotal > 50)
{
isDone = false;
}
}
else
{
//break was used because "isDone = false" does not work here. The debugger shows that when the variable is set to false, it still ignores it and skips to the next round.
break;
}
} while (isDone == true || diceTotal < 50);
//end of round
if (diceTotal > 50)
{
cout << "\nGameOver" << endl;
cout << "You went over in " << round << " turns. You Lose!!! " << endl;
}
else
{
cout << "You stopped at " << round << " turns. Final score: " << diceTotal << "." << endl;
system("pause");
system("cls");
}
//Table
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//Check to see if you made the highScore
if (diceTotal >= score[9])
{
cout << "Congrats, you have made the HighScore Table !\nEnter Your Name.";
cin >> playerName;
for (int i = 9; i < leaderBoardSize; i--)
{
if (diceTotal <= score[i])
{
scoreName[i] = playerName;
score[i] = diceTotal;
break;
}
}
}
//board display #2
for (int i = 0; i < leaderBoardSize; i++)
{
cout << scoreName[i] << "\t\t" << score[i] << endl;
}
system("pause");
//do you want to play again?
cout << "Do you want to play again";
cin >> choice;
while (choice != "Y" && choice != "y" && choice != "N" && choice != "n")
{
cout << "invalid option. Choose y/n:" << endl;
cin >> choice;
}
if (choice == "Y" || choice == "y")
{
system("cls");
isDone = true;
}
else
{
cout << "game over" << endl;
isDone = false;
}
} while (isDone);
system("pause");
return 0;
}
This is a copy of the assignment.
Note: I am not asking for you guys to do my work. I just want to figure out the highScore sorting thing.
The problem: arrays are not a good way to achieve what you want
You are trying to see if a score beat another score, and if so, replace that and move the rest of the scores down. I assume your scores are sorted, and that score is a int[10], but you have several problems:
1.
for (int i = 9; i < leaderBoardSize; i--)
You are attempting to iterate through your scores backwards, so you start at 9 (which I assume is the last index) and work your way down. Assuming leaderBoardSize is the total size of the leaderboard, and likely 10, i will always be less than leaderBoardSize and your for loop will go for a loooong time. You probably meant to say:
for (int i = 9; i >= 0; i--)
2.
scoreName[i = i + 1] = scoreName[i];
score[i = i + 1] = score[i];
This is assigning i to a new value, which will also ruin your for loop. You should only be doing
scoreName[i + 1] = scorename[i];
Trying to swap all the values in an array in cumbersome, and you are trying to do everything manually, so I give you:
The solution: use the standard library!
C++ is a great language, if for no other reason than containing a standard library: a library of functions and classes that solve many basic problems for you.
It is far easier to sort, insert and remove elements by using a standard container. Let's use std::vector:
// Simple class to handle a player score: a name and score
struct PlayerScore
{
std::string name;
unsigned int score;
};
// Create some test scores. I assume you have another means of storing high scores, perhaps in a file, but for this small purpose I am just hard-coding some scores:
std::vector<PlayerScore> hiScores = { {"ABC", 5000}, {"XJK", 10000}, {"FOO", 20000}, {"EGG", 4000}, {"HI", 50000} };
You may keep your scores sorted, but if, like mine they aren't sorted, you can sort them easily with std::sort:
std::sort(hiScores.begin(), hiScores.end(), [](PlayerScore ps1, PlayerScore ps2){ return ps1.score > ps2.score; });
With that out the way, you can proceed to play your game and obtain a name for the player and their score. I haven't bothered, and will just create a value for the score:
auto score = 10123u;
// Check if this score beats any of the current high scores:
auto it = std::find_if(hiScores.begin(), hiScores.end(), [score](PlayerScore ps){ return score > ps.score; });
if (it != hiScores.end())
{
// Yes! We beat a score!
std::cout << "Found score: " << it->score << std::endl;
// Insert this score before the other score
hiScores.insert(it, {"NewScore", score});
// Remove the last score:
hiScores.pop_back();
}
You no longer need to iterate and manually try to manipulate the positions of scores. Standard containers with random access such as std::vector allow you to just insert and access elements as you need to. Your 10 lines of code simply becomes 2 lines. std::vector also manages the size for you, so if you only have 3 high scores initially, it can grow easily to contain 10, 100 or many more scores.
std::find_if can find an element in your container without the need to manually iterate over every element. A predicate is passed in which acts as a find condition, which in our case we pass our score and check if it's greater than any PlayerScore in the container. The first element it's greater than is returned to us, and then we can insert our score in front of it, then remove the last score via pop_back
and if so, insert it into the correct position and make sure scoreName is also updated
This is a poor design. If scores and names need to stay together, you should make them a single entity. Define a struct or class to hold a name and associated score, and then store instances of that entity in a single array.
I decided for fun to try and make a simple program that "sort of" simulates blackjack in a dumbed down way. It's basically done, except for the fact that the randomly generated numbers are WAY too large. I don't care about the bias srand/rand has (for now) I just want to get it working properly.
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
int genRandInt (int low, int high) {
int randnum = low + (rand() % (high - low + 1));
return randnum;
}
int main()
{
srand(time(NULL));
int computerScore;
int score;
int card;
while (int playAgain = 1)
{
cout << "Enter 0 to hold or 1 to hit: ";
int play;
cin >> play;
if (play == 0)
{
computerScore = genRandInt(1, 31);
if (score < computerScore)
{
cout << "Your score is " << score << " and the computer's score is " << computerScore << "! You lose.\n";
}
if (score > 21)
{
cout << "Your score is " << score << " which is greater than 21. Bust!\n";
}
if (score > computerScore && score <= 21)
{
cout << "Your score is " << score << " and the computer's score is " << computerScore << "! You win!\n";
}
cout << "Would you like to play again? 1 for yes, 0 for no. : ";
cin >> playAgain;
}
if (play == 1)
{
card = genRandInt(1, 11);
score = score + card;
cout << "Your score is: " << score << "\n";
}
}
return 0;
}
Any ideas?
You use int score; uninitialized in
if (score < computerScore)
or
score = score + card;
depending on the if(play == 0) or if(play == 1) condition.
It happens to have some junk as its memory content, the compiler does not initializes to zero for you. In fact, it is undefined behaviour to use un-initialized variables. Initialize it before the first usage, preferable in the definition itself,
int score = 0;
Also, compile with warnings on (-Wall -Wextra for g++/clang++), since the compiler will easily warn about these mistakes.
Try running this and seeing if you have the same issues. I just added some print statements to try and debug it, and it stopped showing me really big numbers..
EDIT:
//ADD
int score = 0;
//
if (play == 1)
{
cout << "printing in the PLAY = 1 "<< score << endl;
card = genRandInt(1, 11);
score = score + card;
cout << "Your score is: " << score << "\n";
}
In this case, how would I solve this? I've looked at other posts with the same problem and I can't seem to apply it to this. I've solved this problem before but for some reason I can't remember how I did it.
#include <iostream>
#include <cstdlib>
#include <Windows.h>
#include<ctime>
using namespace std;
//Tristan Currie 11/20/14
//Dice Game
int main()
{
//variables
int die1, die2, dice_total, specialsum;
char rerun, reroll;
//intro
cout << "Welcome to the dice game! I will now explain the rules. \nIf you land on a 7 or 11 on the first role you win. \nIf your sum is 2, 3, or 12 you lose. \nAny other role becomes your special sum. If you roll your special sum before you roll a 7 then you win. \nIf when you roll you get a 7 before your special sum then you lose. ";
cout << "\n\nIf you would like to start the game, press enter to do your first roll. ";
cin.get();
cout << "\n\nRolling Dice...";
//Suspend program for 2 seconds
Sleep(2000);
//seed random number generator using the system clock
srand(static_cast<unsigned int>(time(0)));
//generate a random number between 1 and 6
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice_total = die1 + die2;
cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total;
cin.get();
if ((dice_total == 7) || (dice_total == 11)) {
cout << "Congratulations! You have won the game, press enter to end the program. ";
cin.get();
reroll = 'n';
}
else if ((dice_total == 2) || (dice_total == 3) || (dice_total == 12)) {
cout << "You lost. Press enter to exit. ";
cin.get();
reroll = 'n';
}
else if ((dice_total != 2) || (dice_total != 3) || (dice_total != 12) || (dice_total != 7) || (dice_total != 11)) {
cout << "This is your special sum: " << dice_total << endl;
dice_total = specialsum;
reroll = 'y';
}
while (reroll == 'y') {
cout << "\n\nRolling Dice...";
//Suspend program for 2 seconds
Sleep(2000);
//seed random number generator using the system clock
srand(static_cast<unsigned int>(time(0)));
//generate a random number between 1 and 6
die1 = rand() % 6 + 1;
die2 = rand() % 6 + 1;
dice_total = die1 + die2;
cout << "Done!" << endl << "Dice #1 = " << die1 << "\nDice #2 = " << die2 << "\nDice Total = " << dice_total;
cin.get();
if (dice_total == specialsum) {
cout << "Congratulations! You have won the game, press enter to end the program. ";
cin >> rerun;
cin.get();
reroll = 'n';
}
else if (dice_total == 7) {
cout << "What a shame, you have lost the game, press enter to exit the gane. ";
cin >> rerun;
cin.get();
reroll = 'n';
}
else {
reroll = 'y';
}
}
}
Well, I got an answer in the comments. I mixed up the order of this line: dice_total = specialsum;
int specialsum = 0;
Initializing integer variables to zero, or to a known value. Defining your variable like this, "int specialsum;" ,it gets lost.
Also try specialsum= dice_total; instead of dice_total=specialsum; because "dice_total" would be equal to zero all the time. Hope this helped.