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


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 :: 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++ :: 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 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++ :: Create Inventory System Using Classes And Objects?

Jan 13, 2015

I'm trying to create an inventory system in C++ using classes and objects. Here is what I have now

Item.h

#pragma once
#include <iostream>
#include <allegro5/allegro.h>
#include <allegro5/allegro_image.h>

[Code]....

Basically what I want my inventory system to become is this: [URL] Each slot can hold an item.

Basically this inventory system should be able to do what RS Inventory system can do. (Hold items, Use items, Equip Items (No need for moving item))

View 5 Replies View Related

C++ :: Create Base Class That Is Derived (inherited) By Three Other Classes?

Apr 30, 2013

how to create a base class that is derived (inherited) by three other classes?

View 12 Replies View Related

C++ :: Create A Class Type Structure Using Struct Instead Of Classes

Apr 16, 2013

I am trying to create a class type structure using struct instead of classes.

Code:
#include <iostream>
#include <stdlib.h>
using namespace std;
struct myclass {
int * array;
int nelements;

[Code] ....

Guess what I am asking is using the pointer in the first code section better or is there another way'. I don't know about making the second code work.

View 3 Replies View Related

C++ :: Array Of Classes With Classes Inside

Oct 5, 2013

I have an array of (Student)classes created in Manager.h, which contains a new instance of class Name (name),(in Student.h)How would I go about accessing the SetFirstName method in Name.cpp if I was in a class Manager.cpp? I have tried using Students[i].name.SetFirstName("name");

// In Manager.h
#include"Student.h"
class Manager
{

[Code]....

View 2 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++ :: How To Add A Counter For Comparisons

Apr 27, 2013

How do I add a counter for the comparisons so that it increments every time the two array value are compared?

// Session5.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
using namespace std;
const int NMAX =10;

[code].....

View 1 Replies View Related

C# :: Counter Won't Reset To 0

Jul 30, 2014

int counter won't reset to 0 when I click the start over button.

protected void startOverButton_Click(object sender, EventArgs e)
{
gameOverLabel.Visible = false;

[Code]....

View 3 Replies View Related

C/C++ :: Why Counter Keep Resetting To Zero

Feb 17, 2015

I am not sure why this is happening but I am simply trying to make rounds with my code and have it stop when one of two things, or both, happen.

#include <iostream>
#include "Classes.h"
#include "RNG.h"
#include "select.h"
#include "Fitnesschk.h"
using namespace std;
/*Global Variables************************************/
const int column = 32;

[Code] ....

I've tried resetting and renaming all variables but I don't see any conflict. I tried declaring in different areas but no dice. The other issue I have is that fittest always returns 0 even if the function returns 1. So it infinitely loops. When I go through looking at the variables. the fittest variable never changes even if the function returns 1 and round seems to reset after reproduce is called.

Here is the supporting code.

For Fittracker:

int fittracker(person man[], int row, int column){
for (int y = 0; y < row; y++){
int fit = 0;
for (int z = 0; z < column; z++){
if (man[y].data[z] == 1){

[Code] ....

View 10 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 :: How To Make A Char Counter

Apr 14, 2013

One that fills different char arrays the idea is to use a single char to represent a char such as 255 but if more than it then use a char array of two chars to represent the next char 00 because if only one is needed it would take less disk space than 0,255 as it would in an array.

My goal is to do this all the way up to an array that can hold 255,255,255,255 but only use the more complex array if needed Example: a char of 255 takes much less space than a char array of 4 chars so that instead of 0,0,0,255 it is written to disk as a single char whereas a char array of 200,0,0,1 would be written as a 4 byte char array. use single char to represent values from 0 to 255 use two char array to represent 0,1 to 255,255 three for 0,0,1 to 255,255,255 and four for 0,0,0,1 to 255,255,255,255

View 3 Replies View Related

C :: Unique Word Counter

Jun 18, 2013

I am trying to make a program which reads a text file, separates the strings into words, then finally saving and counting each different word.

When I comment out the code where it is supposed to search for the same word in the table, the program doesn't work.Am I doing something wrong with strcmp or the loop? The 'break' just exits out of that 'for' loop right?

Code:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define WORD_MAX 500
#define LIST_MAX 500
int main(){
int i, j, find;

[Code]...

View 6 Replies View Related







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