C/C++ :: Rolling Multiple Dice - Compare Total Sums

Feb 1, 2015

I am building the game risk and I want to start off with a dice rolling mechanism. In risk 5 die are needed so I want to know the most efficient way of churning out the code for this. Since the players have the options of rolling 1, 2 or 3 die (max 3 for attacker, and max 2 for defender) I was thinking something like this after searching the web for a few different options:

const int minNum = 1;
const int maxNum = 6;
int numberGenerator(){
int x;
x = minNum + rand() % (maxNum - minNum + 1);

[Code] ....

Since I will be asking the player the option to choose how many die to roll, is this the most efficient way? I want to compare the total sums after the die are rolled. If they choose 3 dice (the attacker) I will simply call the first 3 methods and either 1 or 2 for the defender.

View 14 Replies


ADVERTISEMENT

C :: If Statements For Dice Rolling

Oct 9, 2014

I'm very new to C Programming and am doing some homework for my intro to C class. "There is a game where one can play and bet money on. Two dice are rolled and the numbers that appear on the dice are added together. If the total is 7 or 11, then the user wins. If the total is 2 or 12, then the user loses. If a different total appears. then the user gets their money back."

I have written the program below:

Code:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
void main()
{
int d1,d2,sum;
d1=0;
d2=0;
}

[code]...

However, after finishing the program, I realized that I NEEDED to use random numbers!!! How do I make my program generate random numbers within a range of 1-12? Also, how do I make my program generate NEW random numbers EVERY TIME instead of ONCE??

View 1 Replies View Related

C++ :: Random Generator For Dice Rolling?

Oct 29, 2013

How would you code a simple random generator for dice rolling?

View 2 Replies View Related

C++ :: Create A Dice Rolling Counter Using Classes?

Dec 1, 2014

I'm trying to create a program that rolls a dice a few thousand times and print out how many times it lands on each face and then print out a graph using x's as a visual representation using classes.

output looks like this:
1 --- 2000 xxxxxxxxxxxxxxxxxxxxxxxxx
2 --- 1950xxxxxxxxxxxxxxxxxxxxxxxx
.
.
6 --- 2050 xxxxxxxxxxxxxxxxxxxxxxxxxxx

I need a method called int count(int face) inside my class that returns the number of times a particular face has appeared, I just don't know how to incorporate this. I think I should use a constructor and destructor but i'm new with those and knowing when to actually use them.

Here is what I have so far that shows how it should look when printed, just struggling to convert using a class.

#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
using namespace std;

[Code]....

View 2 Replies View Related

C :: Program Where A Computer And Player Take Turns Rolling Dice

Feb 19, 2013

I am making a program where a computer and player take turns rolling dice.

There seems to be an issue somewhere in my loop where when the player selects from the possible dice choices, it rolls for the computer and does not select the choice and it immediately becomes the player's turn again.

I am trying to get it to where when the computer rolls the dice, it should display the list of possible values, and ask the user to hit the enter key in order for the computer to choose which value to select using the following command:

Code:
while ( getchar() != '
' ) ; I've tried it a hundred times and no matter where I put the command it still skips the computer's turn.

Below is my code:

Code:
while (compscore < 99 && playerscore < 99){
printf("You have rolled the following pair of dice:
");
a =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
b =("%i ", rand() % (MAX_VAL - MIN_VAL + 1) + MIN_VAL);
multiply = a * b;

[Code] ....

View 11 Replies View Related

C/C++ :: Dice Rolling - Keeping Track Of How Many Times A Number Appears?

Sep 27, 2014

I wrote a code that mimics a dice, my code so far will ask for the user to input a number at which they want to hold or no longer throw the dice, it will also ask the user how many roll simulations they would like to do.

If a 1 is rolled the turn is over and the score is 0

After this input is recieved the code goes into a loop until the values have been met.

I would to add probability to my code so I need to know how many times a certain number was rolled and this is where I am stuck.

An example of how I would like my code to work, let's suppose hold at number is 17

We role the dice until we roll a 17 or larger, but if we roll a 1, then our score is 0.

Let's suppose the number of simulations is 5 and for simulation 1 we roll

2 + 5 + 6 + 4 = 17

and for simulation 2 we roll

5 + 4 + 1, but since we rolled a 1, the score is 0,

for simulation 3 we roll

3 + 4 + 5 + 6 = 18

for simulation 4 we roll

4 + 4 + 6 + 6 = 20

for simulation 5 we roll

3 + 4 + 5 + 5 = 17 so that means we got

0: 1 time 1/5 = .20
17: 2 times 2/5 = .40
18: 1 time 1/5 = .20
19: 0 times 0/5 = .00
20: 1 time 1/5 = .20
21: 0 times 0/5 = .00
22: 0 times 0/5 = .00

In other words the idea is to roll a 17 or larger (or whatever the hold number happens to be), but if you roll a 1, then your score is a 0.

#include <iostream>
#include <cstdlib>
#include <ctime>
using std::cout;
using std::cin;
using std::endl;
int dieRoll();

[Code] ....

View 4 Replies View Related

C Sharp :: Calculate The Total And Compare All Row In Datagridview?

Dec 3, 2012

I have a window form with datagridview,textbox,button and listbox. In datagridview i have added a column. In first row i have given the value like 7000,

second row 2500,
third row 7000,
fourth row 1000,
fifth 1300,
sixth 9000....n number of row:

if i enter 12000 in textbox1 then it will calculate the total in particular rows in datagridview.And the total should be less than 12000 what we have entered the value in textbox1.

The result will display in listbox..

like first greater value is 9000+2500=11500 ,
second greater value 7000+1000+1300=9500,
other third greater value is 7,000.

So i have given a sample code where i cannot get the solution ..

  double search;
            search = double.Parse(textBox1.Text);
            double sum = 0;
            for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
                double se2 = Convert.ToDouble(dataGridView1.Rows[i].Cells[0].Value.ToString());
                if (se2 == search) {

[code]....

View 5 Replies View Related

C :: Array Add Total Rows And Total Columns

Apr 16, 2013

Code: i am trying to display the information in the array as a table and add the total rows and the total colums separately

#include<stdio.h>
int main(void)
{
int row, col;

[Code].....

View 1 Replies View Related

C++ :: Generating Sums Of Random Numbers

Sep 19, 2014

I'm trying to create a code that generates random numbers and spits out a sum average and lowest and highest number. I am stuck on the sum however and once I get that I think the average will fall into place. Here's what I have.

#include <iostream>
#include <iomanip>
#include <ctime>
#include <math.h>
using namespace std;

[Code] ....

I've noticed that it's almost always off by 1 or 3 for whatever reason

View 9 Replies View Related

C++ :: Program Is Not Calculating Sums Correctly?

Jun 20, 2014

I am getting this weird error when calculating sums. Here is what my program does: it generates two random numbers and then randomly puts either an addition or subtraction sign between the two and then I am asked to enter what the correct answer for that question is. But on some of the subtraction questions when I give the answer it says I got it wrong after which the program shows the right answer. What's confusing is that the answer the computer calculates is the same that I entered. Anyways the complete program and a screenshot of the console window which shows the error are below. The function in question here is decAddSub at line 54. The error is shown in the first sum in the output window. It doesn't affect addition problems and only affects some of the subtraction problems.

#include<iostream>
#include<stdlib.h>
#include<time.h>
#include<random>

[Code].....

View 2 Replies View Related

C++ :: Adding Sums With Void Functions

May 3, 2013

1) This first function initializes an array of 30 components so that the first 15 components are equal to the square of the index value and the last 15 components are equal to the index value multiplied by 3.

2) The second function processes the array by finding the sum of the first 15 components and the sum of the last 15 components to determine which sum is bigger. The output to the screen should do the following:

a)State “The sum of the first 15 components is:” and then show the sum.
b)State “The sum of the last 15 components is:” and then show the sum.
c)State which of the two resulted in the greater sum or if the two sums were equal.

Function: The program uses two subroutines. One to initialize an array and the other to process the array and print to screen results:

#include <iostream>
#include <iomanip>
// Include any other header files you may need.

const int ARRAY_SIZE = 30;
void initialize ( double list[], int index );
void square ( double list[], int index );
void threeTimes ( double list[], int index );
void output ( const double list[], int index );

[Code] .....

View 4 Replies View Related

C/C++ :: 3x3 Magic Square That Sums Up To 15 On All Sides

Mar 10, 2015

I have an assignment that I have to make a 3x3 magic square that sums up to 15 on all sides. I can't use arrays, just Loops and If and Else statements.

My first attempt was like this

//Program for printing magic square that sums up to 15.

#include <iostream>
using namespace std;
int main() {
int a, b, c, d, e, f, g, h, i;
int r1, r2, r3;
int c1, c2, c3;

[Code] .....

I can't think of a way to loop that when it prints out, it will have a sum of 15 on all sides and/or print one of the 8 solution for a 3x3 magic square.

View 3 Replies View Related

C++ :: Divide Set Of Numbers Into Two Subsets Where Difference Between Sums Of Subset Is Minimal

Feb 28, 2013

The problem is that you have a set of numbers and you need to divide that set into two subsets where the difference between the sums of the subset is minimal.

Example: a set of numbers {1,5,9,3,8}, now the solution is two subsets, one subset with elements {9,3} and the other {8,5,1} the sum of the first one is 13 and the sum of the second is 13 so the difference between the sums is 0. The result shows the difference between the sums.

Another example: a set of numbers where the difference between the subsets cannot be zero, {9 51 308 107 27 91 62 176 28 6}, the minimal difference between the two subsets is 2.

I want to know how the function finds the two subsets, it works great because I've tested it for up to 300 inputs which sum adds up to 100,000.

Code:
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <limits.h>
using namespace std;
int BalancedPartition ( int a[] , int n ) {

[Code] ....

View 3 Replies View Related

C++ :: Dice Simulator - Floats Rounding Up

Oct 28, 2013

I made this dice simulator which basically throws the dice 1 million times and outputs the frequency and percentage average for each side (1 to 6).

Everything is working fine, except my averages (floats) seem to be rounding up, causing 4% being "unassigned" at the end of the million rolls. I'm outputting with a setprecision of 2, but I only get 0's and no fractional numbers.

View 5 Replies View Related

C/C++ :: Sum Function Of For Loop For Dice Generator

Oct 15, 2014

I have written the code below that allows the user to input any sided dice and any number of rolls. The program will print out the output in sequence. I need to sum these outputs into one number, but I am having trouble writing a sum function for this.

//This Program Creates the Sum of a Number of Dice Rolls of the Same Size  

#include <stdio.h> 
#include <math.h>
#include <time.h> 
int rolldie();   
int main(){ 
int x, n, a; //'x' n=number of rolls, a=number sided dice

[Code] ....

The output looks something like this currently:

Please Enter the Number Sided Dice Followed by the number of Rolls... 12 10
4 12 2 10 12 5 5 12 12 5 >Exit code: 0

View 2 Replies View Related

C :: Simulate Roll Of A Dice - How To Use Rand Function

Sep 23, 2014

I saw a program in which it uses rand like this:-

d1=rand() % 6+ 1;

where, d1 is any integer. The program is to simulate the roll of a dice. The whole program is this:-

Code:

#include<stdio.h>
#include<stdlib.h>
main() {
int i;
int d1, d2;
int a[13];

[Code] .....

View 10 Replies View Related

C++ :: Adding Up Values Of Dice Rolls In A While Loop

Feb 19, 2013

I'm having a problem adding up the values of the dice rolls in a while loop. The program will only store the last value so how would I get it to store multiples values? Here is my program thus far. I would try an array but we haven't gotten to that yet.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
int die_1=0, die_2=0, total_score1, total_score2, player1score;
char enter;

[Code] ....

What operators or equation I should use to accomplish this?

View 7 Replies View Related

C++ :: Five Dice Roll - Read Out Average And Sum Of Rolls

Feb 7, 2013

My issue is that my program dies whenever it attempts to readout the average and sum of the rolls. I think I need to include the size function in here somewhere but am not sure how to go about it. Possibly scrap the "do while" loop and go with something else, or just am completely missing something.

Here's the code:

#include<iostream>
#include<random>
#include<ctime>
#include<vector>
#include<cstdlib>
#include<iomanip>
using namespace std;
int sum5(int d1, int d2, int d3, int d4, int d5);
int DiceRoll();

[Code] ....

View 3 Replies View Related

C# :: Creating A Class And Methods For Dice Game?

Mar 21, 2015

The only difficulty im having is creating a class and methods & being able to access them from my win form app. Can i get a few tips on the do's and donts of creating classes / methods and accessing them from form app.

This is what i have put together so far.

public partial class Form1 : Form {
private Image[] dicePics;
private int[] diceNum;
private Random randomize;
public Form1() {
InitializeComponent();

[code]....

View 3 Replies View Related

C :: Snakes And Ladder Program - Dice Function To Switch Between Players

Mar 25, 2013

I am writing a snakes and ladder program and I'm almost finished, but I am struggling with the dice to work in the way I want it to work. I want the dice to work like this :

Before each player throw the dice they must start at 0.
Each player must throw a 6 on the dice to move on the board.
If a player threw a 6 on the dice, that player can throw again.

But I ended up with two seperate dice, one for each player (game is only for two players).And when I run the program, both players don't start at 0. And when I throw the dice, both players move at the same time but with different values.

If one of my players threw a 6, they just keep on throwing until someone wins the game. I tried to use a switch and if statements but I couldn't get it right. And so I did this :

Code:
#include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#define SpaceBar 32// 32 is an ASCII value for a spacebar

[Code] ....

View 5 Replies View Related

Visual C++ :: Dice Game - Creating Pass By Value Function Reference

Nov 27, 2014

i have a project where i create a dice game, the user rolls 2 dice and the computer roles 2 dice. the player that wins 3 out of 5 rolls wins the game. I have completed the requirements, but i wanted to create a pass by value function for "void Dice()", I'm not too sure how that works?

Code:

#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
//creating my variables and their values
int compinput;

[Code] .....

View 5 Replies View Related

C :: Simulating Random Dice Roll As A Basis For Chutes And Ladders Game

Jan 24, 2014

I am trying to simulate a random dice roll as a basis for a chutes and ladders game

Code:
int main {
int i = 0, diceroll;
while (i>5) {
diceroll = 1+rand()%6;
i++;
}
return 0;
}

However the numbers get don't seem to be random, it always starts off with 6,6,5.. then its random.

View 10 Replies View Related

C++ :: Dice Game - Receive Same Values When Roll Function Get Call Twice For Same Object

Nov 7, 2013

#include <iostream>
#include <cstdlib>
#include <time.h>
#include <conio.h>
using namespace std;
class Dice {

[Code] ....

Every time the roll_dice function get call twice for the same object, i receive the same dice values.

View 4 Replies View Related

C++ :: Dice (with Varying Number Of Sides) Program To Calculate Percentage Of Results

Oct 28, 2013

I am trying to code and compile a program that requests a uses to first inpu the number of sides they wish for a dice to have and secondly input the number of dice throws they would like to calculate the value for. The program will print an error message and default the number of sides the dice has to 5 if the number entered is below 4. Finally it will print out a percentage figure for each value that results from all the the dice rolls.

I have attached my header, driver and class files below along with accompanying error messages below them

// file name Exercise.cpp
#include <iostream>
#include "Die.h"
using namespace std;
int main() // the main program begins here
{
char restart = 'y'; // declare variables
double numRolls = 0.0;
int diceTot = 0;

[Code] .....

View 5 Replies View Related

C# :: Multiple Desktop Display (Multiple Explorer Windows)

Jul 31, 2014

I want to develop an application which can host multiple views of explorer (window), where as each window is totally separate from others. So that I can have multiple desktop views through my single explorer. Any built in feature in .NET ?

View 11 Replies View Related

C/C++ :: How To Make Multiple Subroutine And Return Multiple Arrays

Aug 17, 2014

how to make subroutines and return multiple arrays.Before that, i wrote my program on matlab. could my program to be structured as following?

<header>
initial value of program;
subroutine1() {
calculating the first work;
return 2 or 3 arrays;

[code].....

View 14 Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved