C++ Vector of structs, read access violation - c++

Edit: For loop didn't have a ending condition. Newbie mistakes.
I'm doing a school assignment for school, using MS VS, which has very specific
requirements. We're reading shape names and dimensions from a txt file, creating a struct for each shape with only the dimensions as members, and using a supporting function to calculate area/volume and output the results. Have to have 4 Loops:
The First will parse a txt file line by line,
check the type of shape, create a dynamic object and put it into a
generic bag.
The Second will process the bag and output the type of shape,
dimensions, and calculations to console.
The Third will do the same but output to another txt file.
The Last loop will delete all dynamic objects.
My program with only the code for squares:
#include <iterator>
#include <string>
#include <sstream>
#include <vector>
#include <iostream>
#include <fstream>
#include <map>
#include <cmath>
#include <cstdlib>
using namespace std;
int main()
{
string line, str;
double d1, d2, d3;
map < string, int > shapes;
vector<void*> myBag;
vector<char> myBagType;
shapes.insert(pair<string, int>("SQUARE", 1));
ifstream shapesin("TextFile1.txt");
ofstream shapesout("TextFile2.txt");
if (!shapesin || !shapesout)
{
cout << "Unable to open file\n";
}
while (getline(shapesin, line))
{
d1 = d2 = d3 = 0;
vector<string> token = parseString(line);
if (token.size() >= 1)
{
str = token[0];
switch (shapes[str])
{
case 1: //Square
{
Square* s = new Square;
myBag.push_back(s);
myBagType.push_back('S');
if (token.size() < 2)
{
s->side = 0;
break;
}
else
{
str = token[1];
d1 = atof(str.c_str());
s->side = d1;
}
break;
}
}
}
for (unsigned int i = 0; myBag.size(); i++)
{
if (myBagType[i] == 'S')
{
Square* aSquare = reinterpret_cast<Square*>(myBag[i]);
Square& bSquare = *aSquare;
outputSquare(cout, bSquare);
}
}
for (unsigned int i = 0; myBag.size(); i++)
{
if (myBagType[i] == 'S')
{
Square* aSquare = reinterpret_cast<Square*>(myBag[i]);
Square& bSquare = *aSquare;
outputSquare(shapesout, bSquare);
}
}
for (unsigned int i = 0; myBag.size(); i++)
{
if (myBagType[i] == 'S')
{
Square* aSquare = reinterpret_cast<Square*>(myBag[i]);
delete aSquare;
}
}
shapesin.close();
shapesout.close();
}
}
vector<string> parseString(string str)
{
stringstream s(str);
istream_iterator<string> begin(s), end;
return vector<string>(begin, end);
}
void outputSquare(ostream& shapesout, const Square& x)
{
double perim, area;
perim = (x.side * 4); //exception thrown here
area = (x.side * x.side);
shapesout << "SQUARE " << "side=" << x.side;
shapesout.setf(ios::fixed);
shapesout.precision(2);
shapesout << " area=" << area << " perimeter=" << perim << endl;
shapesout.unsetf(ios::fixed);
shapesout.precision(6);
}
Txt file input is:
SQUARE 14.5 344
SQUARE
RECTANGLE 14.5 4.65
DIMENSIONS
CIRCLE 14.5
BOX x 2 9
CUBE 13
BOX 1 2 3
CYLINDER 2.3 4 56
CANDY
SPHERE 2.4
CYLINDER 1.23
CYLINDER 50 1.23
TRIANGLE 1.2 3.2
PRISM 2.199 5
EOF
I know I have a problem with the way I'm accessing the struct member x.side but every other way I've tried won't compile, where as this will at least output the first line. I've read other, similar, questions but couldn't find one quite like this. I would really appreciate some assistance.

for (unsigned int i = 0; myBag.size(); i++)
No terminating condition
for (unsigned int i = 0; i < myBag.size(); i++)
Fixed

Related

How to find the item number of maximum profit and how to assign array[100000]???? help me

Questions – A file is given as “input.txt” name. In this file, you have 100000(N) items. Every item
has a fixed weight wi and price pi. You can take maximum 2036 weights. Find out the way to
take items, so that you can get maximum profit. You must print items which you have selected
to get maximum profit. See, the sample output.
Sample Input:
3 (N) 3 (W)
2 10
1 4
2 20
Sample Output:
Profit: 25
Item – 3: 2 = 20
Item – 1: 1 = 5
I already coded this program but here is a problem. here i can not set the array size array[100000]. if i do the the program automatically terminated.
Also, I have to show the item name as the sample output. Sample input file here you will find.
//Fractional Knapsack
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct iteam
{
double w,p,u;
};
bool compare( iteam a, iteam b)
{
return a.u>b.u;
}
int main()
{
int n,w;
iteam arry[100];
cin>>n>>w;
for(int i=0; i<n; i++)
{
cin>>arry[i].w>>arry[i].p;
arry[i].u = arry[i].p/arry[i].w;
}
sort(&arry[0],&arry[n],compare);
int p=0;
for (int i=0; i<n; i++)
{
if(w>arry[i].w)
{
p=p+arry[i].p;
w=w-arry[i].w;
}
else{
p=p+w*arry[i].u;
w=0;
}
}
cout<<"Total Profit: "<<p<<endl;
}
I'm not going to give you a straight answer, as this is either an assignment or a coding challenge, and you should be able to solve it yourself.
Instead I've tried to transform your code into some readable code, to show you how this can improve your understanding of your own code
//Fractional Knapsack
struct Item
{
double weight;
double profit;
double profitPerWeight;
};
bool operator> (Item const& lhs, Item const& rhs){
return lhs.profitPerWeight > rhs.profitPerWeight;
}
#include <iostream>
std::istream& operator>> (std::istream& in, Item& item) {
in >> item.weight >> item.profit;
return in;
}
#include <vector>
#include <algorithm>
int main()
{
int nrOfItems, maximumWeight;
std::cin >> nrOfItems >> maximumWeight;
std::vector<Item> items(nrOfItems);
for(auto& item : items)
{
std::cin >> item;
item.profitPerWeight = item.profit / item.weight;
}
// is this sort really needed? Think about it.
std::sort(begin(items), end(items), std::greater<>());
int totalProfit = 0;
for (auto const& item : items)
{
if(maximumWeight > item.weight)
{
totalProfit += item.profit;
maximumWeight -= item.weight;
}
else{
totalProfit += maximumWeight * item.profitPerWeight;
maximumWeight = 0;
}
}
std::cout << "Total Profit: " << totalProfit << '\n';
}
I used your literal implementation... If you read it like this, does it do what you want it to?

How to allow user to choose when to stop each of three columns from rotating

I'm writing a simple fruit machine slot in console. I have managed to create 3 separate arrays to simulate 'wheels' of the machine. Each array contains of 6 random generated characters which then 'move' upwards by swapping characters in the array. I would like to ask you for some sort of hint, how would you solve this problem. Thanks in advance.
I think the problem might be in the way I've done the 'wheels'. When I have while loop; wheels are spinning but I cannot figure out how to stop each wheel. Iv'e tried to use if statements with "GetAsyncKeyState(VK_SPACE))" inside the while loop, but once space was released it kept spinning again and this was solving only part of my problem because I have three wheels.
#include <iostream>
#include <windows.h>
#include <cstdlib> // system cls
#include <time.h>
#include <conio.h>
using namespace std;
void SetCursorPosition(int x, int y);
char printRandomChar(int n);
void wheel0();
void assignWheel0Char();
int const arraySize = 6;
int coursor_x = 1;
int coursor_y = 1;
char wheelArray[arraySize];
int main()
{
srand(time(NULL));
assignWheel0Char();
bool playing_game = true;
while (playing_game)
{
wheel0();
Sleep(500);
}
std::cin.get();
}
void SetCursorPosition(int x, int y)
{
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x,y };
SetConsoleCursorPosition(output, position);
}
void wheel0()
{
for (int i = 0; i < 3; i++)
{
SetCursorPosition(coursor_x, coursor_y);
cout << wheelArray[i];
coursor_y++;
}
char temp;
temp = wheelArray[0];
wheelArray[0] = wheelArray[1];
wheelArray[1] = wheelArray[2];
wheelArray[2] = wheelArray[3];
wheelArray[3] = wheelArray[4];
wheelArray[4] = temp;
coursor_y = 1;
}
void assignWheel0Char()
{
for (int i = 0; i < arraySize; i++)
{
wheelArray[i] = printRandomChar(1);
}
}
char printRandomChar(int n)
{
char alphabet[6] = { 'J', 'Q', 'K', 'W', 'L', 'D' };
char res{};
for (int i = 0; i < n; i++)
res = res + alphabet[rand() % 6];
return res;
}
I want to allow user to stop each wheel by using space bar so it looks like this:
1.Game starts wheels are spinning, user presses space bar, wheel1 stops ,
but other two wheels keeps spinning.
2. Another space bar press, now wheel1 is stoped and wheel2 stops while
wheel 3 keeps spinning.
3. Last space bar press stops wheel3.
4. Score is displayed if symbols match
5. User is asked if he wants to play again
I want to allow user to stop each wheel by using space bar so it looks
like this:
1.Game starts wheels are spinning, user presses space bar, wheel1 stops , but other two wheels keeps spinning. 2. Another space bar
press, now wheel1 is stoped and wheel2 stops while wheel 3 keeps
spinning. 3. Last space bar press stops wheel3. 4. Score is displayed
if symbols match 5. User is asked if he wants to play again
The following is an example implementation you can refer to:
#include <iostream>
#include <windows.h>
#include <cstdlib> // system cls
#include <time.h>
#include <conio.h>
using namespace std;
#define MAX_COLUMN_POSITION 3
#define SCORE_COLUMN_POSITION (MAX_COLUMN_POSITION + 1)
#define FINAL_COLUMN_POSITION (SCORE_COLUMN_POSITION + 1)
void SetCursorPosition(SHORT y, SHORT x = 1);
char printRandomChar(int n);
void wheel0();
void assignWheel0Char();
int const arraySize = 6;
char wheelArray[arraySize];
int spaceCnt = 0;
bool playing_game = true;
DWORD WINAPI GetUserInput(LPVOID lpThreadParameter)
{
while (playing_game)
{
char c = _getch();
switch (c)
{
case ' ':
{
spaceCnt++;
if (spaceCnt >= MAX_COLUMN_POSITION)
{
SetCursorPosition(SCORE_COLUMN_POSITION);
cout << "\n Score: ";
}
}
break;
case 'y':
case 'Y':
{
spaceCnt = 0;
}
break;
case 'n':
case 'N':
{
playing_game = FALSE;
}
break;
default:
break;
}
}
return 0;
}
int main()
{
srand(time(NULL));
assignWheel0Char();
CreateThread(NULL, 0, GetUserInput, NULL, 0, NULL);
cout << "Enter SPACE key to stop one wheel. Enter Y(Yes) to play again, enter N (No) to exit. \n";
while (playing_game)
{
wheel0();
Sleep(500);
}
SetCursorPosition(FINAL_COLUMN_POSITION);
cout << "\n Exiting...";
std::cin.get();
}
void SetCursorPosition(SHORT y, SHORT x)
{
HANDLE output = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = { x,y };
SetConsoleCursorPosition(output, position);
}
void wheel0()
{
for (SHORT i = 0, coursor_y = 1; i < MAX_COLUMN_POSITION; i++, coursor_y++)
{
if (spaceCnt > 0 && spaceCnt >= coursor_y)
continue;
SetCursorPosition(coursor_y);
cout << wheelArray[i];
}
char temp;
temp = wheelArray[0];
wheelArray[0] = wheelArray[1];
wheelArray[1] = wheelArray[2];
wheelArray[2] = wheelArray[3];
wheelArray[3] = wheelArray[4];
wheelArray[4] = temp;
}
void assignWheel0Char()
{
for (int i = 0; i < arraySize; i++)
{
wheelArray[i] = printRandomChar(1);
}
}
char printRandomChar(int n)
{
char alphabet[6] = { 'J', 'Q', 'K', 'W', 'L', 'D' };
char res{};
for (int i = 0; i < n; i++)
res = res + alphabet[rand() % 6];
return res;
}

Is there a way to display my prime number list output in 2 columns?

I am taking my first programming class and this is my first time posting. I have been able to find help on this site for previous projects when I got stuck, and I hope I am doing this right.
I have completed the program below to display only prime number between 0 and 100 for my intro to C++ class.
The only thing is it kinda bothers me that it is in a single column, I wanted to go the extra step and make it look all nice and display the numbers in a couple columns. I tried using "\t", but I can't get it to work right. Any ideas on what I might add to my code?
I think I could do it using an array but we have not covered it in class and I'm not supposed to use them yet.
the challenge was:
"Use the isPrime function that you wrote in Programming Challenge 21 in a program that stores a list of all the prime numbers from 1 through 100 in a file."
and here is my code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
bool isPrime(int);
int main()
{
static int num1=0;
cout<<"Listed below is all prime numbers from 1 through 100."<<endl<<endl<<endl;
do
{
num1++;
if (isPrime(num1))
{
cout<<num1<<endl;
}
}
while (num1<100);
cout<<endl;
return 0;
}
bool isPrime(int num1)
{
bool primeNum=true;
for (int i=2;i<num1;i++)
{
if (num1%i==0)
{
primeNum=false;
}
}
return primeNum;
}
Thanks in advance for any input,
Find cout.width()
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
bool isPrime(int);
int main()
{
static int num1 = 0;
cout << "Listed below is all prime numbers from 1 through 100." << endl << endl << endl;
int column = 0; // column variable
int width = 10; // column width size
do
{
num1++;
if (isPrime(num1))
{
cout.width(width); // set column's width
cout << num1;
if (column == 1) { // if prime number is printed in column 2
cout << endl; // add new line
column = 0; // set column to first
}
else {
column++; // increase column index
}
}
} while (num1<100);
cout << endl;
return 0;
}
bool isPrime(int num1)
{
// error: your isPrime returns true when num1 is 1 or 2. change it
if (num1 == 1 || num1 == 2) return false;
// your isPrime
bool primeNum = true;
for (int i = 2; i<num1; i++)
{
if (num1%i == 0)
{
primeNum = false;
}
}
return primeNum;
}
I just realized the question asked for me to STORE the list to a file. So I rewrote and here is my new code:
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
bool isPrime(int);
int main()
{
int num=0;
cout<<"This Program will store a list of only the prime numbers "<<endl;
cout<<"between 0 and 100 to the text file \"PrimeNumberList\"."<<endl<<endl;
cout<<"Find the list by using the file explorer to search for \"PrimeNumberList.txt\"."<<endl;
ofstream outFile;
outFile.open("PrimeNumberList.txt");
if (outFile.fail())
{
cout<<"Error opening \"PrimeNumberList.txt\" for output."<<endl;
return 1;
}
for (int i=1;i<100;i++)
{
if(isPrime(i))
{
outFile<<i<<endl;
}
}
return 0;
}
bool isPrime(int num1)
{
if (num1==1)return false;
bool primeNum=true;
for (int i=2;i<num1;i++)
{
if (num1%i==0)
{
primeNum=false;
}
}
return primeNum;
}

Call to function is ambiguous in C++. Candidate functions are the Prototype and the function itself

I am working through Stanford CS106B C++ assignments and I have a 'semantic issue' with an assignment.
It seems as if the compiler cannot deduce whether the call is to a function or to the prototype of the function. I don't understand why a call would ever be made to the prototype. How can I make it so that the call is made to the function rather than the prototype? The error message I get it "Call to 'humansTurn' is ambiguous".
The error messages relate to the calls of the humansTurn(Lexicon,Lexicon) function, within the humansTurn(Lexicon,Lexicon) function, at the bottom of the page. The prototype for this function is above the main function.
Any help would be greatly appreciated.
Kind regards,
Mehul
/*
* File: Boggle.cpp
* ----------------
*/
#include <iostream>
#include "gboggle.h"
#include "graphics.h"
#include "grid.h"
#include "vector.h"
#include "lexicon.h"
#include "random.h"
#include "simpio.h"
using namespace std;
/* Constants */
const int BOGGLE_WINDOW_WIDTH = 650;
const int BOGGLE_WINDOW_HEIGHT = 350;
const string STANDARD_CUBES[16] = {
"AAEEGN", "ABBJOO", "ACHOPS", "AFFKPS",
"AOOTTW", "CIMOTU", "DEILRX", "DELRVY",
"DISTTY", "EEGHNW", "EEINSU", "EHRTVW",
"EIOSST", "ELRTTY", "HIMNQU", "HLNNRZ"
};
const string BIG_BOGGLE_CUBES[25] = {
"AAAFRS", "AAEEEE", "AAFIRS", "ADENNN", "AEEEEM",
"AEEGMU", "AEGMNN", "AFIRSY", "BJKQXZ", "CCNSTW",
"CEIILT", "CEILPT", "CEIPST", "DDLNOR", "DDHNOT",
"DHHLOR", "DHLNOR", "EIIITT", "EMOTTT", "ENSSSU",
"FIPRSY", "GORRVW", "HIPRRY", "NOOTUW", "OOOTTU"
};
/* Function prototypes */
void welcome();
void giveInstructions();
// Create random board
static Grid <char> randomBoard();
// Create custom board
static Grid<char> customBoard();
static void drawAndFillBoard(Grid<char>);
static void humansTurn(Lexicon,Lexicon);
int main() {
initGraphics(BOGGLE_WINDOW_WIDTH, BOGGLE_WINDOW_HEIGHT);
welcome();
giveInstructions();
string custom = getLine("Type y to create custom board:" );
Grid<char> gridData;
if (custom=="y"){
gridData = customBoard();
} else {
gridData = randomBoard();
}
drawAndFillBoard(gridData);
Lexicon english("EnglishWords.dat");
// Lexicon holds words previously encountered
Lexicon previousWords;
humansTurn(english, previousWords);
return 0;
}
/*
* Function: welcome
* Usage: welcome();
* -----------------
* Print out a cheery welcome message.
*/
void welcome() {
cout << "Welcome! You're about to play an intense game " << endl;
}
/*
* Function: giveInstructions
* Usage: giveInstructions();
* --------------------------
* Print out the instructions for the user.
*/
void giveInstructions() {
cout << endl;
cout << "The boggle board is a grid onto which I ";
cout << "or triple your paltry score." << endl << endl;
cout << "Hit return when you're ready...";
getLine();
}
static Grid<char> randomBoard(){
Vector<string> standardCubes;
for(int i = 0; i<16;i++){
standardCubes.add(STANDARD_CUBES[i]);
}
// Shuffle cubes
for (int i = 0; i < standardCubes.size(); i++) {
int r = randomInteger(i, standardCubes.size()-1);
if (i!=r){
string stringToMove1 = standardCubes.get(i);
string stringToMove2 = standardCubes.get(r);
standardCubes.set(r, stringToMove1);
standardCubes.set(i, stringToMove2);
}
}
// Update grid with random side of cube
Grid<char> gridData(4, 4);
int counter = 0;
for (int columnNo = 0; columnNo <4; columnNo++){
for (int rowNo = 0; rowNo<4; rowNo++) {
string s = standardCubes.get(counter);
int r = randomInteger(0, 5);
gridData[columnNo][rowNo] = s[r];
counter++;
}
}
return gridData;
}
static Grid<char> customBoard(){
Grid<char> gridData(4,4);
string s = getLine("Please enter 16 characters to make up the custom board. Characters will fill the board left to right, top to bottom: ");
for (int i = 0; i < s.length(); i++) {
s[i] = toupper(s[i]);
}
if (s.length()<16){
cout << "String has to be 16 characters long, try again" << endl;
customBoard();
}
int i =0;
for (int columnNo = 0; columnNo <4; columnNo++){
for (int rowNo = 0; rowNo<4; rowNo++) {
gridData[columnNo][rowNo] = s[i];
i++;
}
}
return gridData;
}
static void drawAndFillBoard(Grid<char> gridData){
drawBoard(4, 4);
for (int columnNo = 0; columnNo <4; columnNo++){
for (int rowNo = 0; rowNo<4; rowNo++) {
labelCube(rowNo, columnNo, gridData[rowNo][columnNo]);
}
}
}
static void humansTurn(Lexicon englishWords, Lexicon &previousWords){
/*
Human’s turn (except for finding words on the board). Write the loop that allows the user to enter words. Reject words that have already been entered or that don’t meet the minimum word length or that aren’t in the lexicon. Use the gboggle functions to add words to the graphical display and keep score.
*/
string humanGuess = getLine("Please enter your guess: ");
for (int i = 0; i < humanGuess.length(); i++) {
humanGuess[i] = tolower(humanGuess[i]);
}
if (humanGuess.length()<4){
cout << "Min guess length is four characters" << endl;
humansTurn(englishWords, previousWords);
}
if (!englishWords.contains(humanGuess)) {
cout << "That word is not English, please try another word" << endl;
humansTurn(englishWords, previousWords);
}
if (previousWords.contains(humanGuess)){
cout << "That word has already been guessed, please try another word" << endl;
humansTurn(englishWords, previousWords);
}
// check if word can be made using data on board
}
Your function humansTurn definition has different signature with declaration
function declaration:
static void humansTurn(Lexicon,Lexicon);
Function definition:
static void humansTurn(Lexicon englishWords, Lexicon &previousWords)
^^
//Here

Linking/Integration Issue. I need a direction of where to go?

So I have a 2 games running side by side.
proj2/main.cpp (main that gets run that then can call either game)
proj2/spades/display.cpp
proj2/spades/gameplay.cpp
proj2/spades/otherFiles.cpp
proj2/hearts/display.cpp (this is the same display class as in the spades game with the same functions just sightly altered.)
proj2/hearts/hearts.cpp
proj2/hearts/otherFiles.cpp
What basically happened is I am trying to integrate 2 games, 1 I created and another that uses the same display.cpp and display.h files slightly altered. In order to do this I created 2 seperate namespaces, hearts and spades. Everything I have compiles fine but a few of the .cpp files do not link correctly and I get errors such as the following.
./gamePlay.o: In function `__static_initialization_and_destruction_0(int, int)':
gamePlay.cpp:(.text+0x396): undefined reference to `spades::display::display()'
./gamePlay.o: In function `__tcf_3':
gamePlay.cpp:(.text+0x480): undefined reference to `spades::display::~display()'
./gamePlay.o: In function `spades::gamePlay::storeBid(std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >&)':
gamePlay.cpp:(.text+0x1507): undefined reference to `spades::display::captureInput()'
What am I doing wrong here?
Below are some of the files I am using. Let me know if there are anymore I should add. (Dont want to bloat this post anymore than I have to)
gameplay.cpp
#include <iostream>
#include <sys/ioctl.h>
#include <cstdio>
#include <unistd.h>
#include <locale.h>
#include <ncursesw/ncurses.h>
#include <fstream>
#include "gamePlay.h"
#include <cstdlib>
#include <sstream>
#include <ctime>
namespace spades {
vector <spades::player> players(4);
vector <spades::card> deck(52);
spades::display monitor;
spades::card center[4];
vector <spades::card> gamePlay::getDeck(){return deck;}
vector <spades::player> gamePlay::getplayers(){return players;}
//sorts the cards in the players hand into diamonds, clubs, hearts, spades
void gamePlay::handSort(){
int spades[13];
int hearts[13];
int clubs[13];
int diamonds[13];
int index;
int i;
for(i=0; i<13; i++){ //determines the card number and places them into corresponding indexes
index = (players.at(0).hand.at(i).getCardNum()+11)%13; //cause the cards to be placed based on their number with 2 being index 0 and 1(ace) being index 12
switch(players.at(0).hand.at(i).getSuit())
{
case 1: spades[index] = 1;
break;
case 2: hearts[index] = 1;
break;
case 3: clubs[index] = 1;
break;
case 4: diamonds[index] = 1;
break;
default: mvprintw(3,2,"!!!!!!!we have a problem!!!!!!!!!!");
break;
}
}
i = 0;
while(i<13){ //being placing the ordered cards back into the players hand
int j = 0;
while(j < 13){
if(diamonds[j] == 1){ //if a card has been placed in this index for the diamonds only array
if(j+2 == 14) //if the card is an ace
players.at(0).hand.at(i).setCardNum(1);
else
players.at(0).hand.at(i).setCardNum(j+2); //add 2 to each index to get the card number
players.at(0).hand.at(i).setSuit(4);
i++;
}
j++;
}
j = 0;
while(j < 13){
if(clubs[j] == 1){
if(j+2 == 14)
players.at(0).hand.at(i).setCardNum(1);
else
players.at(0).hand.at(i).setCardNum(j+2);
players.at(0).hand.at(i).setSuit(3);
i++;
}
j++;
}
j = 0;
while(j < 13){
if(hearts[j] == 1){
if(j+2 == 14)
players.at(0).hand.at(i).setCardNum(1);
else
players.at(0).hand.at(i).setCardNum(j+2);
players.at(0).hand.at(i).setSuit(2);
i++;
}
j++;
}
j = 0;
while(j < 13){
if(spades[j] == 1){
if(j+2 == 14)
players.at(0).hand.at(i).setCardNum(1);
else
players.at(0).hand.at(i).setCardNum(j+2);
players.at(0).hand.at(i).setSuit(1);
i++;
}
j++;
}
}
}
//compares the center pile of 4 played cards to determine who wins the trick
int gamePlay::compareCenter(int leadplayer){
int highest = center[leadplayer].getCardNum();
if(center[leadplayer].getCardNum() == 1)
highest = 14;
int suit = center[leadplayer].getSuit();
int player = leadplayer;
for(int i = leadplayer+1; i < leadplayer+4; i++)
{
if(center[i%4].getSuit() == 1)
setSpadesBroken(true);
if((suit != 1) && (center[i%4].getSuit() == 1))
{
player = i%4;
suit = 1;
highest = center[i%4].getCardNum();
}
if(suit == center[i%4].getSuit()){
if(center[i%4].getCardNum() == 1){
player = i % 4;
highest = 14;
}
if(highest < center[i%4].getCardNum())
{
player = i%4;
highest = center[i%4].getCardNum();
}
}
}
players.at(player).setTricksTaken(players.at(player).getTricksTaken()+1); //increments the trick count of the winning player
return player; //return the player who won to determine who goes first next turn
}
//Create the deck of 52 cards by setting the suit and number of each card to a nonzero integer
void gamePlay::createDeck() {
for(int j = 0; j < 52; j++)
{
deck.at(j).setCardNum((j%13)+1);
deck.at(j).setSuit((j/13)+1);
}
random_shuffle(deck.begin(), deck.end());
}
//deal out 13 cards to each player by setting the
void gamePlay::deal(vector <spades::card> &newdeck, vector <spades::player> &newplayers){
for(int i = 0; i<52; i++){
newplayers.at(i/13).addCard(newdeck.at(i));
newdeck.at(i).setSuit(0);
newdeck.at(i).setCardNum(0);
}
}
//determines if the player still has a card of the same suit in their hand as the leading card played
bool gamePlay::containSuit(spades::card lead, spades::player players){
bool suit = false;
for(int i = 0; i < players.getHand().size(); i++){
if(lead.getSuit() == players.getHand().at(i).getSuit())
suit = true;
}
return suit;
}
//determines if the player has only spades cards left in their hand
bool gamePlay::onlySpade(spades::player play){
for(int i = 0; i<play.getHand().size(); i++){
if(play.getHand().at(i).getSuit()!=1)
return false;
}
return true;
}
//determines if the position the player is clicking on the screen actually points to a playable card
//and then returns the position of that card based on the player's hand vector of type <card>
int gamePlay::handCheck(int xevent, int yevent, vector <spades::player> players, int trickStart){
int i = xevent/6;
//first check to find the card on the display
if(i>=0 && i<players.at(0).getHand().size() && yevent>17 && yevent<23 &&
players.at(0).getHand().at(i).getSuit() != 0 &&
players.at(0).getHand().at(i).getCardNum() != 0)
{
spades::card playedCard = players.at(0).getHand().at(i);
//check to see if leading the current round or not and if spades are "broken"
if(trickStart==0 && !getSpadesBroken()){
if(onlySpade(players.at(0)))
return i;
else if(playedCard.getSuit() != 1)
return i;
else
return (-1);
}
if(trickStart == 0 && getSpadesBroken())
return i;
//if not leading, then call the containsuit method to see if your hand contains one of similar suit
if(trickStart > 0 && containSuit(center[trickStart],players.at(0))){
if(playedCard.getSuit()==center[trickStart].getSuit())
return i;
}
if(trickStart > 0 && !containSuit(center[trickStart],players.at(0)))
return i;
else
return (-1);
}
mvprintw(4,3,"invalid card");
return (-1);
}
//draws the cards in the player's hand if it contains a valid card, erase the card otherwise if invalid
void gamePlay::displayHand(){
int offset = 0;
for(int i =0; i<players.at(0).getHand().size(); i++){
if(players.at(0).getHand().at(i).getSuit() != 0)
monitor.displayCard(offset, 17, players.at(0).getHand().at(i).getSuit(), players.at(0).getHand().at(i).getCardNum(), 0);
else
monitor.eraseBox(offset, 17, 6, 5);
offset+=6;
}
}
void gamePlay::displayAdd(){
vector <string> lines;
int count12 = 0;
string line;
ifstream myfile ("advertisement.txt");
if ( myfile.is_open() )
{
while ( ! myfile.eof() )
{
getline (myfile, line);
lines.push_back(line);
count12++;
}
myfile.close();
int random1 = rand() % 6;
monitor.bannerBottom(lines.at(random1));
}
else{
monitor.bannerBottom("Unable to open Advertisement file.");
}
}
//determins the position of a mouse click and sends that to handcheck(), and then sends the card to the center pile array to be scored
void gamePlay::humanPlay(int trickStart){
int xevent, yevent;
for(;;){
mvprintw(3,2,"Please choose a card to play.");
int key = monitor.captureInput();
// if a mouse click occurred
if (key == -1) {
xevent = monitor.getMouseEventX();
yevent = monitor.getMouseEventY();
int handCh = handCheck(xevent, yevent, players, trickStart);
if(handCh != (-1)){ //if the card is valid
spades::card played = players.at(0).getHand().at(handCh);
players.at(0).hand.at(handCh).setCardNum(0);
players.at(0).hand.at(handCh).setSuit(0);
center[0]= played;
monitor.displayCard(39, 12, center[0].getSuit(), center[0].getCardNum(), 0);
displayHand();
//playedCards++; //Update Global Variable---- NEED TO HAVE THIS INITIATED GLOBALLY.
break;
}
else
mvprintw(4,3,"invalid card");
}
}
}
//loops through a computer players hand and checks to see if the random card is playable within the rules of the game
void gamePlay::CPUplay(int trickStart, int CPU){
bool goodCard = false;
spades::card playedCard =players.at(CPU).getHand().at(0);
int i;
for(i = 0; i < players.at(CPU).getHand().size(); i++){
if(players.at(CPU).getHand().at(i).getSuit() != 0 &&
players.at(CPU).getHand().at(i).getCardNum() != 0){
playedCard = players.at(CPU).getHand().at(i);
//check to see if leading or not
if(trickStart==CPU && !getSpadesBroken()){
if(onlySpade(players.at(CPU)))
break;
if(playedCard.getSuit()!=1)
break;
}
if(trickStart == CPU && getSpadesBroken())
break;
//if not leading use contains suit function
if(trickStart != CPU && containSuit(center[trickStart], players.at(CPU)))
if(playedCard.getSuit()==center[trickStart].getSuit())
break;
if(trickStart != CPU && !containSuit(center[trickStart], players.at(CPU)))
break;
}
}
players.at(CPU).hand.at(i).setCardNum(0);
players.at(CPU).hand.at(i).setSuit(0);
center[CPU]= playedCard;
if(CPU==1)
monitor.displayCard(29, 7, center[CPU].getSuit(), center[CPU].getCardNum(), 0);
if(CPU==2)
monitor.displayCard(39, 2, center[CPU].getSuit(), center[CPU].getCardNum(), 0);
if(CPU==3)
monitor.displayCard(49, 7, center[CPU].getSuit(), center[CPU].getCardNum(), 0);
}
//scores each team with the player being teamed with CPU 2 and the other team being CPU 1 and CPU 3
void gamePlay::score(spades::player &play, spades::player &play2){
int trickOver = play.getTricksTaken()-play.getBid(); // Calculate the difference between bid and actually taken.
if(play.getBid() == NULL)
trickOver = 0;
int trickOver2 = play2.getTricksTaken()-play2.getBid(); // Calculate the difference between bid and actually taken.
int totalBid = play.getBid()+play2.getBid();
int totalTrick = trickOver+trickOver2;
//Bidding Double Nil (if gets it 200 points other wise -200 points)
if(play.getDoubleNil()){
if(play.getTricksTaken()==0) //player did get Double Nil successfully
play.setScore(play.getScore()+200); // add 200 points
else
play.setScore(play.getScore()-200);
}
else if(play.getBid()==0){ //Bidding Nil (if gets it 100 points other wise -100 points)
if(play.getTricksTaken()==0) //player did get Nil successfully
play.setScore(play.getScore()+100); //add 100 points
else //player didnt get Nil
play.setScore(play.getScore()-100); //take away 100 points
}
if(totalTrick < 0){ //player bids more than number of tricks won
play.setScore(play.getScore()+(totalBid*(-10))); //decrease score by 10 poitns for every overtrick
}
else if(totalTrick >= 0){ //player bids less then number of tricks won
play.setSandBag(play.getSandBag() + totalTrick); //increase sandbag by 1
play.setScore(play.getScore()+totalTrick+(10*totalBid)); //increase 10 points per trick bid on and 1 point per trick over
}
if(play.getSandBag()>10){ //check for sandbagging
play.setScore(play.getScore()-100);
play.setSandBag(play.getSandBag()-10);
}
play.setBid(NULL); //reset players bid to NULL
play2.setBid(NULL); //reset players bid to NULL
play.setTricksTaken(0);
play2.setTricksTaken(0);
play.setDoubleNil(false); //Player has not yet bid double NILL.
}
//loops infinitely until a mouse click or key input is detected, then sets bid accordingly
void gamePlay::storeBid(stringstream &msg){
int xevent;
int yevent;
for(;;){
mvprintw(3,2,"Click the box bid Double Nil or type out your bid now. \n Use the keys 1-9, a for 10, b for 11, c for 12, and d for 13.");
int key = monitor.captureInput();
monitor.drawBox(2, 5, 3, 2, 0);
xevent = monitor.getMouseEventX();
yevent = monitor.getMouseEventY();
if (key == -1)
if((xevent>=2 && xevent<4)&&(yevent>=5 && yevent <7)){
msg.str("");
msg << "Your bid: double nil";
monitor.bannerTop(msg.str());
players.at(0).setDoubleNil(true);
break;
}
else{
msg.str("");
msg << "Your bid is horrible. Bid again!!!!";
monitor.bannerTop(msg.str());
}
if ((key-48) >= 0 && (key-48) <= 9){
msg.str("");
msg << "Your bid: " << (key-48);
monitor.bannerTop(msg.str());
players.at(0).setBid(key-48);
break;
}
else if(key>=97 && key<=100){
msg.str("");
msg << "Your bid: " << (key-87);
monitor.bannerTop(msg.str());
players.at(0).setBid(key-87);
break;
}
else if(key!=0 && key!=-1){
msg.str("");
msg << "Your bid is horrible. Bid again!!!!";
monitor.bannerTop(msg.str());
}
}
monitor.eraseBox(1, 1, 62, 16);
}
//main function that runs the entire game
void gamePlay::runGame(){
double startTime = clock();
stringstream messageString;
srand(time(NULL));
int count;
int handStart = 0;
int trickStart;
while(players.at(0).getScore()<500 && players.at(1).getScore()<500)
{
createDeck();
deal(deck, players); //deal out 13 cards to each player
handSort(); // sort the human players hand
displayHand();
storeBid(messageString); //asks for bid
//sets the bid randomly between 2-4 for each computer player
players.at(1).setBid(rand()%3+2);
players.at(2).setBid(rand()%3+2);
players.at(3).setBid(rand()%3+2);
//displays the current bid and scores of each team
messageString << " Partner: " << (players.at(2).getBid())
<< " CPU 1: " << players.at(1).getBid()
<< " CPU 3: " << players.at(3).getBid()
<< " Team Score = " << players.at(0).getScore()
<< " Enemy Score = " << players.at(1).getScore();
monitor.bannerTop(messageString.str());
displayAdd();
trickStart = handStart;
count = 0;
setSpadesBroken(false);
while(count<13){ // simulates the different cases where the human player plays 1st, 2nd, 3rd, last
bool success = true;
switch (trickStart)
{
case 0: humanPlay(trickStart);
CPUplay(trickStart,1);
CPUplay(trickStart,2);
CPUplay(trickStart,3);
break;
case 3: CPUplay(trickStart,3);
humanPlay(trickStart);
CPUplay(trickStart,1);
CPUplay(trickStart,2);
break;
case 2: CPUplay(trickStart,2);
CPUplay(trickStart,3);
humanPlay(trickStart);
CPUplay(trickStart,1);
break;
case 1: CPUplay(trickStart,1);
CPUplay(trickStart,2);
CPUplay(trickStart,3);
humanPlay(trickStart);
break;
default: mvprintw(3,2,"!!!!we have a problem!!!!");
success = false;
break;
}
monitor.eraseBox(3, 2, 20, 2);
for(;;){//pause to wait for an acknowledgment mouse click
int key = monitor.captureInput();
monitor.eraseBox(3, 2, 30, 4);
mvprintw(3,2,"click anywhere to continue");
if(key == -1){
break;
}
}
//if no errors appear, display the tricks taken
if(success){
monitor.eraseBox(1, 1, 62, 16);
trickStart = compareCenter(trickStart);
messageString.str("");
messageString << "Your Tricks= " << players.at(0).getTricksTaken()
<< " Partner= " << players.at(2).getTricksTaken()
<< " CPU 1= " << players.at(1).getTricksTaken()
<< " CPU 3= " << players.at(3).getTricksTaken();
displayAdd();
monitor.bannerAboveBottom(messageString.str());
count++;
}
}
//score the round and tally up for each team
for(int n =0; n<2;n++)
score(players.at(n),players.at(n+2));
messageString.str("");
monitor.bannerTop(messageString.str());
messageString << "Your Tricks = " << players.at(0).getTricksTaken()
<< " Partner = " << players.at(2).getTricksTaken()
<< " CPU 1 = " << players.at(1).getTricksTaken()
<< " CPU 3 = " << players.at(3).getTricksTaken();
handStart = (handStart +1) % 4;
}
double endTime = clock();
}
}
gameplay.h
#include "player.h"
#include "display.h"
#ifndef gamePlay_H
#define gamePlay_H
namespace spades {
using namespace std;
class gamePlay{
bool spadesBroken;
public:
vector <spades::card> getDeck();
vector <spades::player>getplayers();
bool getSpadesBroken() {return spadesBroken;}
void setSpadesBroken(bool b){spadesBroken = b;}
int compareCenter(int leadplayer);
void createDeck();
void deal(vector <spades::card> &deck, vector <spades::player> &players);
void handSort();
bool containSuit(spades::card lead, spades::player players);
bool onlySpade(spades::player play);
int handCheck(int xevent, int yevent, vector <spades::player> players, int trickStart);
void displayHand();
void displayAdd();
void humanPlay(int trickStart);
void CPUplay(int trickStart, int CPU);
void score(spades::player &play, spades::player &play2);
void storeBid(stringstream &msg);
void runGame();
};
}
#endif
player.h
#include <iostream> // Stream declarations
#include <vector> //Vectors used to store deck and players hands
#include <string> //String declarations
#include <algorithm> //Shuffle Method
#include "card.h"
#ifndef PLAYER_H
#define PLAYER_H
namespace spades {
using namespace std;
class player {
int score; //total score
int bid; //bid for that round
int tricksTaken; //score for thast round
int sandBag; //the number of points you win, more than what you bid, every 10th bag = -100
bool doubleNil;
//vector <card> hand;
public:
player();
vector <card> hand;
int getSandBag(){return sandBag;}
bool getDoubleNil() {return doubleNil;}
int getScore() {return score;}
int getBid() {return bid;}
int getTricksTaken() {return tricksTaken;}
vector<card> getHand() {return hand;}
void setSandBag(int i) {sandBag = i;}
void setBid(int i) {bid = i;}
void setTricksTaken(int i) {tricksTaken=i;}
void setScore(int i) {score = i;}
void setDoubleNil(bool i) {doubleNil = i;}
//add card to end of vector
void addCard(card b);
void removeCard(card a);
};
}
#endif
player.cpp
#include <iostream> // Stream declarations
#include <vector> //Vectors used to store deck and players hands
#include <string> //String declarations
#include <algorithm> //Shuffle Method
#include <sys/ioctl.h>
#include <cstdio>
#include <unistd.h>
#include <locale.h>
#include <ncursesw/ncurses.h>
#include "player.h"
namespace spades {
using namespace std;
player::player() {
score =0; //total score
bid = NULL; //bid for that round
tricksTaken = 0; //score for thast round
sandBag = 0; //the number of points you win, more than what you bid, every 10th bag = -100
doubleNil = false;
for(int i=0; i<13; i++)
hand.push_back(card());
}
void player::addCard(spades::card b){
for (int i=0; i<hand.size(); i++){
//compare card being played to the ones in your hand to search and determine which one to erase
if((hand.at(i).getCardNum() == 0) &&
(hand.at(i).getSuit() == 0))
{
hand.at(i).setCardNum(b.getCardNum());
hand.at(i).setSuit(b.getSuit());
return;
}
}
}
void player::removeCard(spades::card a) {
for (int i=0; i<hand.size(); i++){
//compare card being played to the ones in your hand to search and determine which one to erase
if((hand.at(i).getCardNum() == a.getCardNum()) &&
(hand.at(i).getSuit() == a.getSuit()))
{
hand.at(i).setCardNum(0);
hand.at(i).setSuit(0);
return;
}
}
}
}
What does your link line look like? It looks like when you're linking, you're missing spades.o?
If that's not it, use the nm command to see what symbols spades.o is providing - it's always possible
you've typo'd a namespace name or something:
nm -C spades.o | grep 'display::.*display'
(The -C option should put nm into C++/demangled mode - if it doesn't for your nm, add a "c++filt" or "dem" step to the pipe chain before grep).