Currently I am programming Gin Rummy in C++11 and when dealing out cards my deck removes cards initially so the player gets a real like hand but when moving to dealing cards to the next player it gives that player the same Cards.
Here is a link to a github repo which contains my files. I will attach the output. If you wish to compile the code please you the ./compile script, it contains the necessary files and excludes the table.cpp. This is the output
Github Repo: Gin Rummy
Currently I am not including table.cpp to table.h to be compilied.
I am confused on why it is not removing the Card objects from the Deck vector.
Deck.cpp
#include <iostream>
#include <vector>
#include "deck.h"
#include "card.h"
using namespace std;
//Addes 52 cards to the deck
Deck::Deck() {
cardsInDeck = {};
}
//Remove top card from deck
Card Deck::drawCard(){
Card topCard = getCards()[cardsInDeck.size()-1];
popCard();
getCards().shrink_to_fit();
vector<Card> c2 = getCards();
int size = getCards().size();
cout << size << endl;
return topCard;
}
void Deck::popCard(){
this->cardsInDeck.pop_back();
}
//Add card to a deck
void Deck::addCard(Card addedCard) {
this->cardsInDeck.push_back(addedCard);
}
vector<Card> Deck::getCards(){
return cardsInDeck;
}
Main.cpp
#include <iostream>
#include <ctime>
#include "player.h"
#include "deck.h"
#include "card.h"
#include "computer.h"
using namespace std;
int main() {
srand(time(NULL));
Deck testDeck;
testDeck.newDeck();
testDeck.shuffleDeck();
cout << testDeck.deckSize() <<endl;
//testDeck.displayCards();
//cout << endl << endl;
Player player1;
player1.firstHand(testDeck);
player1.showHand();
cout << endl << testDeck.deckSize() << endl;
cout << endl << endl;
Player player2;
player2.firstHand(testDeck);
player2.showHand();
cout << endl << testDeck.deckSize() << endl;
return 0;
};
Related
I am new to OpenMP... Please help me with this dumb question. Thank you :)
Basically, I want to use OpenMP to speed up two for loops. But I do not know why it keeps saying: invalid controlling predicate for the for loop.
By the way, my GCC version is gcc (Ubuntu 6.2.0-5ubuntu12) 6.2.0 20161005, and OS I am using is Ubuntu 16.10.
Basically, I generate a toy data that has a typical Key-Value style, like this:
Data = {
"0": ["100","99","98","97",..."1"];
"1": ["100","99","98","97",..."1"];
...
"999":["100","99","98","97",..."1"];
}
Then, for each key, I want to compare its value with the rest of the keys. Here, I sum them up through "user1_list.size()+user2_list.size();". As for each key, the sum-up process is totally independent of other keys, which means this works for parallelism.
Here is my toy example code.
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include "omp.h"
using namespace std;
int main(){
// Create Data
map<string, vector<string>> data;
for(int i=0; i != 1000; i++){
vector<string> list;
for (int j=100; j!=0; j--){
list.push_back(to_string(j));
}
data[to_string(i)]=list;
}
cout << "Data Total size: " << data.size() << endl;
int count = 1;
#pragma omp parallel for private(count)
for (auto it=data.begin(); it!=data.end(); it++){
//cout << "Evoke Thread: " << omp_get_thread_num();
cout << " count: " << count << " / " << data.size() << endl;
count ++;
string user1 = it->first;
vector<string> user1_list = it->second;
for (auto it2=data.begin(); it2!=data.end(); it2++){
string user2 = it2->first;
vector<string> user2_list = it2->second;
cout << "u1:" << user1 << " u2:" << user2;
int total_size = user1_list.size()+user2_list.size();
cout << " total size: " << total_size << endl;
}
}
return 0;
}
If I generate random numbers with the following code:
#include <iostream>
#include <random>
int main()
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, 999);
for (int n=0; n<1000; ++n)
std::cout << dis(gen) << ' ';
std::cout << '\n';
}
is it possible to get the previously generated values in the reverse order (without saving them into an array, etc...) after the loop is finished, and do something like this:
for (int n=0; n<1000; ++n)
std::cout << GetPrev(dis, gen) << ' ';
std::cout << '\n';
?
If you seed the pseudo random engine with the same value, it will generate the same sequence of bits, which will translate in the distribution generating the same numbers. So you need to store the seed passed to the constructor of mt19937.
I understand that nearbyint allows me to round integers without throwing exceptions. It is possible to use feclearexcept to check for errors or see if rounding took place (which will always be the case for nearbyint).
Can anyone show me an example of when an exception would have been thrown which has been avoided by using nearbyint?
Here is an example of the normal use of the function:
#include <cfenv>
#include <cmath>
#include <iostream>
void test(const char* title, int round_mode)
{
std::feclearexcept(FE_ALL_EXCEPT);
std::fesetround(round_mode);
std::cout << title << std::endl;
std::cout << "nearbyint(2.5) = " << std::nearbyint(2.5) << std::endl;
std::cout << "nearbyint(-2.5) = " << std::nearbyint(-2.5) << std::endl;
std::cout << "FE_INEXACT = " << std::boolalpha << (std::fetestexcept(FE_INEXACT) != 0) << std::endl << std::endl; // This will always be true.
}
#define test(mode) test(#mode, mode)
int main()
{
#ifdef FE_DOWNWARD
test(FE_DOWNWARD);
#endif
#ifdef FE_TONEAREST
test(FE_TONEAREST);
#endif
#ifdef FE_TOWARDZERO
test(FE_TOWARDZERO);
#endif
#ifdef FE_UPWARD
test(FE_UPWARD);
#endif
}
I have this minimal code:
#include <mutex>
#include <iostream>
std::mutex themutex;
void f1()
{
std::cout << "1" << std::endl;
std::lock_guard<std::mutex> local_mutex(themutex);
std::cout << "2" << std::endl;
}
void f2()
{
std::cout << "3" << std::endl;
std::lock_guard<std::mutex> local_mutex(themutex);
std::cout << "4" << std::endl;
f1();
std::cout << "5" << std::endl;
}
int main(void)
{
f2();
return 0;
}
I compile and run with
g++ -std=c++11 test_mutex.cc -o test_mutex && ./test_mutex
and I get this output:
3
4
1
2
5
Why?
I expect the program to lock after printing "1" and never to return.
From 30.4.1 ("Mutex requirements"):
The expression m.lock() shall be well-formed and have the following semantics:
Requires: If m is of type std::mutex or std::timed_mutex, the calling thread does not own the mutex.
You're violating the requirements, and so you cannot expect any behaviour guaranteed by the standard.
I want to do something like the following (a and b are both vector<my_moveable_type>):
a.insert(a.end(), b.begin(), b.end());
But I want the operation to move b's elements into a instead of copying them. I have found std::vector::emplace but that is just for a single element, not a range.
Can this be done?
You can use std::make_move_iterator, so that accesses to the iterator returns rvalue references instead of lvalue references:
a.insert(a.end(), std::make_move_iterator(b.begin()), std::make_move_iterator(b.end()));
There is a std::move algorithm that appears to do what you want. In the following code the source std::vector is left with empty strings (the vector size doesn't change).
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
int main()
{
std::vector<std::string> one{"cat", "dog", "newt"};
std::vector<std::string> two;
std::move(begin(one), end(one), back_inserter(two));
std::cout << "one:\n";
for (auto& str : one) {
std::cout << str << '\n';
}
std::cout << "two:\n";
for (auto& str : two) {
std::cout << str << '\n';
}
}
Working code at ideone.com