C++ :: How To Shuffle A Vector (cards)

Mar 2, 2014

I am trying to write a sub-program that takes a deck of 52 cards and will shuffle it up using this algorithm:

Start with n=51
Repeatedly:
Generate a random integer between 0 and n. Call it i.
Swap card i and card n in the deck.
Decrease n by 1

However the code that I've written,

vector<int> shuffleDeck(vector<int> deckVector)//deckVector is the original deck in order
{
int temp, n, i, s;
n = 51;

[Code].....

when I output the new vector, only produces this output(note- it is a random number every time, but it just repeats over and over):

8 8 8 8 8 8 8 8 0 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8

or:

22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 0 22 22 22 22
22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22 22

View 1 Replies


ADVERTISEMENT

C++ :: Create Array Of Playing Cards / Assign Values And Suits Then Shuffle The Array

Nov 24, 2014

I got this program to create an array of playing cards and assign the values and suits and shuffle the array. I'm at the point where I need to output the cards but I need to burn the first card by making it output "**" instead of the card. my cards[] is a constant so I can's assign the first card as such.

void showCards(const int cards[], int numCards, bool hideFirstCard) {
if (cards[0]) {
hideFirstCard=true;
cards[0] = '**';
} for(int a = 0; a <= numCards; a++) {
cout >> showCard(cards[a]);
} }

View 1 Replies View Related

C++ :: Cards Game - Seeding A Vector With 52 Unique Values

Dec 29, 2013

I am trying to seed a vector with 52 values ranging from 1 to 52. I already have the code written to seed the vector bu how to keep from repeating the same values from being seeded. This is for a five card stud game.

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

[Code] ....

View 3 Replies View Related

C :: 2D Array With 108 Strings - Shuffle Function?

Sep 7, 2013

I have a 2D array with 108 strings. How do I shuffle it correctly?

Code:
void Shuffle( char dest[208][13] )
{
// Initialize variables
char temp[13];

// Loop and shuffle(swap array values)

[Code] .....

The program force closes.

View 6 Replies View Related

C++ :: How To Shuffle A Word Coming From A File

Sep 24, 2013

HOW TO SHUFFLE A WORD COMING FROM A FILE?

FILE *DOCU;
DOCU = fopen ("file.txt", "r");
if (DOCU == 0) {
printf ("
Failed to open!

[Code] .....

View 4 Replies View Related

C/C++ :: Shuffle Songs Into Random Order

Sep 26, 2014

What's wrong with my shuffle function? It shuffles some songs, yet completely gets rid of others.

Here's the code:

void shuffle() //Shuffles songs into a random order. {
Music temp, temp2; //Temporary music file holders, 'Music' is a struct with string title;, string artist;, and int size;.
unsigned int currentTime = (unsigned)time(0);
srand(currentTime);
for (int i = 0; i < NUM_SONGS; i++)

[Code] .....

I want it to output this:

1. Title: Runaway2, Artist: Bon Jovi2, 2 MB
2. Title: Runaway5, Artist: Bon Jovi5, 5 MB
3. Title: Runaway1, Artist: Bon Jovi1, 2 MB
4. Title: Runaway4, Artist: Bon Jovi4, 1 MB
5. Title: Runaway7, Artist: Bon Jovi7, 5 MB
6. Title: Runaway6, Artist: Bon Jovi6, 5 MB
7. Title: Runaway3, Artist: Bon Jovi3, 5 MB
8. Title: Runaway8, Artist: Bon Jovi8, 1 MB

Or something along those lines, but instead I get something like this:

1. Title: Runaway7, Artist: Bon Jovi7, 1 MB
2. Title: Runaway2, Artist: Bon Jovi2, 2 MB
3. Title: Empty, Artist: Empty, 1856312448 MB
4. Title: Runaway4, Artist: Bon Jovi4, 1 MB
5. Title: Runaway5, Artist: Bon Jovi5, 5 MB
6. Title: Empty, Artist: Empty, 0 MB
7. Title: Runaway7, Artist: Bon Jovi7, 1 MB
8. Title: Empty, Artist: Empty, 1856312448 MB

View 2 Replies View Related

C++ :: Member Function To Shuffle Content?

Nov 14, 2014

Is there member functions for STL's like vector,list etc., to shuffle the content of that STL.

eg:

int arr[] = {1,2,3,4};
std::vector<int> v1(arr, arr+ sizeof(arr) / sizeof(int) );

now i need to shuffle the data randomly like

Now vector may contain {3,2,4,1} or {2,1,4,3} ...

I have taken vector as an example. let me know if there is any alternative way with other STL's

View 2 Replies View Related

C :: How To Shuffle Bits Of Plain Text According To DES Standard

Jul 14, 2014

I learned the bitwise operations in c, but how to shuffle the bits of the plain text accoring to DES's standard. Even with the key and the S-boxes. How DES is performed in C step by step. I have a fair knowledge in C.

View 3 Replies View Related

C++ :: Deck Of Cards - No Output

Mar 5, 2013

I'm still new at C++, so my syntax might be a little off for getting the correct output. Currently, there are no compiler errors or warnings, but there is also no output when I run the program. It seems as if it is not calling something correctly maybe the .toString or perhaps my DeckOfCards constructor is a little off, either way, I can't seem to figure out why there is no output.

Anyways, here is my code:

Card.h
#ifndef CARD_H
#define CARD_H
#include <string>
using namespace std;
class Card {

[Code] ....

View 3 Replies View Related

C++ :: How To Fill Cards In Deck

Sep 9, 2013

Here is my source code i am trying to write a function (fillAll) to fill all 52 cards in a deck and i would like to use my data struct for the function and make Deck one of my parameters.

enum suit{HEARTS, CLUBS, DIAMONDS, SPADES};
enum face{TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE};
struct Card{
suit suitType;
face faceType;

[Code] .....

View 10 Replies View Related

C# :: Listbox Loop - Getting Value Of Both Cards

Apr 3, 2015

I have a lstYourHand that has two cards in it, I loop through the listbox to get the values of both cards. I take the string value of the listbox item (strCardVal) and use a switch to give it an integer value (intCardVal). For some reason, when I run the code, the message Box at the end gives me the value 0 as a result, it does not register me giving it a value in the switch statement. My code is below:

Int32 intCardVal = 0;
String strCardVal;
Int32 intLoopCounter1;
for (intLoopCounter1 = 0; intLoopCounter1 == 1; intLoopCounter1++) {
strCardVal = lstYourHand.SelectedItem.ToString();

[Code] .....

View 3 Replies View Related

C++ :: How To Make Standard Deck Of Cards

Feb 6, 2014

How to make a standard deck of cards and being able to shuffle and distribute the cards. I have made an array to store each card and I can display them but I assigned them all in order according to the suit and rank. Is this a viable way to do it if I intend to have a shuffle effect? Or should I create an array, randomly generate card, check for duplicates, then put them in the array, then display the array? Each card is a structure with two void pointer members for suit and rank.

View 1 Replies View Related

C# :: Detect And Disable Network Cards

May 23, 2012

I have an application in c# that i don't want it to connect to the internet in a virtual environment, like virtualbox, vmware virtual pc and so on. Therefore i though that disabling the virtual network card inside the virtual machine would be a good idea.

View 3 Replies View Related

C :: Dealing A Deck Of Cards Using Structs?

Sep 9, 2013

I have a deck of 108 cards inside a 2D array and want to deal these card by 7 to 4 player. Each player had a id. The player and id are stored in a file while i've read in array each. Now I have to deal the cards.

Code:

struct card{
char color;
int rank;
char action[24];
char location[108]; Code: struct players{char name[10];
int id[5];
char hand;
};

View 2 Replies View Related

C :: Read 5 Cards Then Display A Text Value?

Jan 24, 2013

I want it to read 5 cards then display a text value

Code:
#include <stdio.h>
int main( void ) {
char inputtedhand[25];

[Code]....

View 3 Replies View Related

C/C++ :: Creating And Loading Array Of Cards?

Oct 6, 2014

I am working on a program that is supposed to eventually be able to evaluate bridge hands, but one of the first things I need to do is create and load an array. The program is supposed to read in hands from a file (each line in the file is a hand of 13 cards), evaluate these hands based on a list of rules, and display the results.

Here is the file that will be used for the program:

2C QD TC AD 6C 3D TD 3H 5H 7H AS JH KH
3C 4C 2D AC QC 7S 7C TD 9C 4D KS 8D 6C
2C 3C KC JC 4C 8C 7C QC AC 5C 9C 6C TC
5H 3S 4D KC 9S 3D 4S 8H JC TC 8S 2S 4C
2S 5D 6S 8S 9D 3C 2H TH
2H 6D %S 8S 7S 4D 3H 4S KS QH JH 5C 9S
2C QD TC AD 6C 3D TD 3C 5H 7H AS JH KD QS
2C QD TC AD 6C 3D TD 2C 5D 7H AS JH KD
2H 6D TS 8Z 7S 4D 3H 4S KS QD JH 5C 9S

I've gone through and wrote out what the program is supposed to do and what I think is needed and what steps to take to accomplish this, but am getting stuck on the array. If you can't tell I'm extremely new to this and am still learning. I'm not sure how to create and load an array like this since it seems to me there'd be two elements per index, a suit and a value.

Also, the instructions mention that I will need a data structure to hold the cards in an ordered manner. I'm really confused on this as well, if I'm using an array, how does the data structure come into play?

Can the data structure be used in place of the array?

Here is the code I have so far, but keep in mind that at the moment all it actually does is open the file (The "File is open" message will be taken out as the code progresses, I just threw that in there to make sure the file was actually being opened):

//#include <program3.h>
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main() {
char handsArray[];
ifstream bridgeFile;

[code]....

The input should be sorted by suit and the rank within the suit and be displayed in teh following format:

Clubs 10 6 2
Diamonds A Q 10 3
Hearts K J 7 5 3
Spades A
Points = 16

View 3 Replies View Related

C# :: Access SIM Cards And Send / Receive Messages Through PC Interface

Nov 2, 2013

I'm gonna start a project for my own purpose. In my project I need to access GSM sim cards and send & receive messages through a PC interface. What kind of GSM Interface support for my Requirements and in feature may extend Requirements. what kind of programming language will be simple for write an pc interface to access that GSM interface device.

View 6 Replies View Related

C++ :: Write A Program To Simulate Deck Of 52 Playing Cards

Oct 22, 2014

Write a program to simulate a deck of 52 playing cards.

Represent your deck as a 2D Array where the value in the cell is the position of the card in the deck. Represent the names for the suits and faces as an array of Strings.

Write an algorithm to shuffle the deck.

Display the deck. For Example:

1 Jack of Spades
2 Deuce of Diamonds
3 Three of Diamonds
4 King of Spades
5 Eight of Clubs
6 King of Diamonds
7 Six of Clubs
8 Six of Spades
9 Eight of Hearts

Deal two hands of five cards each. For Example:

****** HAND 1 ******
Jack of Spades
Three of Diamonds
Eight of Clubs
Six of Clubs
Eight of Hearts
...

Below is what I have so far. Dealing two hands and displaying it. How would I be able to do that?

#include <iostream>
#include <ctime>
#include <string>
using namespace std;
void shuffle(int[][2]);
int main()
{
int j,deck[52][2];

[Code]...

View 2 Replies View Related

C++ :: Write A Program To Simulate Deck Of 52 Playing Cards

Oct 26, 2014

Write a program to simulate a deck of 52 playing cards.

Represent your deck as a 2D Array where the value in the cell is the position of the card in the deck.

Represent the names for the suits and faces as an array of Strings.

#include "stdafx.h"
#include <iostream>
#include <string>

[Code].....

The problem: I am trying to connect the 1d arrays to the 2d arrays. Right now when I write: cout << deck[3][5] << endl; The output answer is 45. How do I populate the 2d array with the 1d arrays so that when I cout deck[3][5] I get it to say "Six of Spades"?

View 8 Replies View Related

C/C++ :: Shuffled Deck Of Cards - Corrupt Stack Around Variable

May 21, 2014

I am writing a console application that creates a shuffled deck of cards using pointers and arrays, but I get the error "Run-Time Check Failure #2 - Stack around the variable 'deck' was corrupted" What's the stack? What does this mean and what did I do to cause the stack to be corrupt?

#define _CRT_SECURE_NO_WARNINGS
#include "stdlib.h"
#include "time.h"
#include "stdio.h"
#include "conio.h"
typedef struct card{
int cval;

[Code] .....

View 8 Replies View Related

C/C++ :: Deal 2 Hands Of Cards - Print Deck After Dealing

Apr 2, 2014

My assignment is to create a program to print a deck of 52 cards, shuffle it, deal 2 hands of 5 cards, then print the deck after dealing the 2 hands with the cards that were dealt removed. this is my code so far...

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define DECK_SIZE 52
void init_deck( int deck[] , int size ) ;
void shuffle_deck( int deck[] , int size ) ;

[Code] ....

I am having trouble dealing 2 random hands and how to print the deck with the cards removed.

View 3 Replies View Related

C++ :: Using Vector Push Back Function To Output Contents Of Vector (similar To Array)

Feb 9, 2015

How to output vector contents using the push_back function. My program reads in values just fine, but it does not output anything and I've been stuck on why.

here is my code:

#include <iostream>
#include <array>
#include <vector>
using namespace std;
int duplicate( vector < int > &vector1, const int value, const int counter)

[Code].....

View 3 Replies View Related

C++ :: Open File And Read In Rows To String Vector And Return Vector

Jun 7, 2012

I have a cpp app that reads in a number of files and writes revised output. The app doesn't seem to be able to open a file with a ' in the file name, such as,

N,N'-dimethylethylenediamine.mol

This is the function that opens the file :

Code:
// opens mol file, reads in rows to string vector and returns vector
vector<string> get_mol_file(string& filePath) {
vector<string> mol_file;
string new_mol_line;
// create an input stream and open the mol file
ifstream read_mol_input;
read_mol_input.open( filePath.c_str() );

[Code] ....

The path to the file is passed as a cpp string and the c version is used to open the file. Do I need to handle this as a special case? It is possible that there could be " as well, parenthesis, etc.

View 9 Replies View Related

C++ :: Create Class Vector As Template And Define Operations On Vector?

May 13, 2013

I need to create a class vector as a template and define operations on vectors.

And this is what I made.

#include<iostream>
using namespace std;
template<class T>

[Code].....

View 2 Replies View Related

C++ :: Recursive Function 2 - Create Vector Of Vector Of Integers

Mar 26, 2013

Lets say that I have a vector of vector of integers. <1,2,3,4> , <5,6,7,8>, <10,11,12,13>

How do I make a function that creates vector of vector of every different integers?

<1,5,10> , <1,5,11>, <1,5,12>, <1,5,13>
<1,6,10> , <1,6,11>, <1,6,12>, <1,6,13>
<1,7,10> , <1,7,11>, <1,7,12>, <1,7,13>
<1,8,10>, <1,8,11>, <1,8,12>, <1,8, 13>
<2,5,10>, <2,5,11>, <2,5,12>, <2,5,13>

and so on...

View 2 Replies View Related

C++ :: Find Function To A Vector Of Structures Vector

Jul 5, 2013

I have asked a related question before, and it was resolved successfully. In the past, when I wanted to use std::max_element in order to find the maximum element (or even sort by using std::sort) of a vector of structures according to one of the members of the structure, all I had to do was to insert a specially designed comparison function as the third argument of the function std::max::element. But the latter comparison function naturally accepts two arguments internally.

For instance, here is a test program that successfully finds the maximum according to just one member of the structure:

Code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>

[Code] ....

And the output was this, as expected:
Maximum element S.a of vector<S> vec is at: 9
[I]max element of vec.a between slot 3 and slot 6 is: 6, and its index is: 6 vec[6].a = 6
[I]max element of vec.a between slot 4 and slot 7 is: 7, and its index is: 7 vec[7].a = 7
[I]max element of vec.a between slot 5 and slot 8 is: 8, and its index is: 8 vec[8].a = 8
[I]max element of vec.a between slot 6 and slot 9 is: 9, and its index is: 9 vec[9].a = 9

However, I now need to search and find an element of vector<myStruct> according to just one member of myStruct, instead of finding the maximum or sorting as before. This presents a problem because the function std::find does not accept such a comparison function as its third argument.

This was the description of the std::find function that I found: find - C++ Reference

Code:
template <class InputIterator, class T> InputIterator find (InputIterator first, InputIterator last, const T& val);

I could also find another function called std::find_if, but this only accepts a unary predicate like this: find_if - C++ Reference

Code:
template <class InputIterator, class UnaryPredicate> InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);

And once again this is either inadequate of I don't see how to use it directly, because for the third argument I would like to insert a function that takes two arguments with a syntax like this:

Code:
int x=7;
std::vector<S>::iterator result;
result = std::find(vec.begin(), vec.end(), []( const (int x, const S & S_1) { return ( x == S_1.a ) ; } ) ;

Is there another std function that I can use to make search and find an element according to just one member of myStruct?

Or perhaps there is a clever way to pass two arguments to the unary predicate.

View 4 Replies View Related







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