You are viewing our Forum Archives. To view or take place in current topics click here.
[C++] Need some help with arrays
Posted:
[C++] Need some help with arraysPosted:
Status: Offline
Joined: Jul 14, 201410Year Member
Posts: 1,720
Reputation Power: 71
Status: Offline
Joined: Jul 14, 201410Year Member
Posts: 1,720
Reputation Power: 71
I'm creating a UNO game for a project for school, and I need some help.
Basically what I did so far was create an array that has all 100 elements stored into it. I then have a line of code that "shuffle" this deck. If you run the program as it is, it will ask you to name 4 players and then it will give you 7 random cards for those players.
I need help with creating a loop that will take the 7 random array values and then store them for the player's hand. Each player will need a hand so that will result in 4 arrays.
The other thing that I will need help with is adding x amount of cards to the array. and a separate thing that will remove the card from the array when the player types it out. (check code)
Any help in the right direction will be appreciated.
CODE:
Basically what I did so far was create an array that has all 100 elements stored into it. I then have a line of code that "shuffle" this deck. If you run the program as it is, it will ask you to name 4 players and then it will give you 7 random cards for those players.
I need help with creating a loop that will take the 7 random array values and then store them for the player's hand. Each player will need a hand so that will result in 4 arrays.
The other thing that I will need help with is adding x amount of cards to the array. and a separate thing that will remove the card from the array when the player types it out. (check code)
Any help in the right direction will be appreciated.
CODE:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <ctime>
#include <string>
using namespace std;
string players[4];
string playercard;
class Card // might not need some of this
{
public:
enum face {ZERO, ONE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, SKIP, DRAWTWO, DRAWFOUR, REVERSE, WILD};
enum color {RED, BLUE, GREEN, YELLOW, BLACK};
//Overload
friend ostream& operator<<(ostream& os, const Card& aCard);
Card(face f = ZERO, color c = RED, bool iac = false); // Card constructor, iac = is action card
//Returns value - Might not need this
int GetAction() const;
//Flips card - Might not need this
void Flip();
//private:
face m_Face;
color m_Color;
bool m_IsActionCard;
}; // Class for the card
Card::Card(face f, color c, bool iac) : m_Face(f), m_Color(c), m_IsActionCard(iac)
{}
int Card::GetAction() const // This SOB took a bit to get working. We might change it. So it is more automatic.
{
char pChoice;
//int cChoice;
string Card;
if (m_Face == SKIP || DRAWTWO || DRAWFOUR || REVERSE || WILD)
{
cout << " You have a special card! Would you like to use it? (Y/N) " << endl;
cin >> pChoice;
if (pChoice == 'Y' || pChoice == 'y')
{
cout << " What type of card is it? " << endl;
cin >> Card;
if (Card == "DRAWTWO" || Card == "drawtwo")
{
cout << " Next player gets 2 cards added to their hand! " << endl;
}
else if (Card == "SKIP"|| Card =="skip")
{
cout << " Next player is skipped! " << endl;
}
else if (Card == "DRAWFOUR"|| Card =="drawfour")
{
cout << " Next player draws four " << endl;
}
else if (Card == "REVERSE"|| Card == "reverse")
{
cout << " The order has been flipped! " << endl;
}
else if (Card == "WILD" || Card == "wild")
{
cout << " Please pick a color " << endl;
}
}
else
{
cout << " The card will be saved " << endl;
}
}
return 0;
} // Checks if the player has an action card
int deck() // This is an array for the deck
{
int i;
const int cardAmount = 100; // states that the array wil contain 100 elements
string deck[cardAmount] = { "Wild", "Draw4", "Wild", "Draw4", "Wild", "Draw4", "Wild", "Draw4",
"R0", "R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9",
"R1", "R2", "R3", "R4", "R5", "R6", "R7", "R8", "R9",
"B0", "B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9",
"B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8", "B9",
"G0", "G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9",
"G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8", "G9",
"Y0", "Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8", "Y9",
"Y1", "Y2", "Y3", "Y4", "Y5", "Y6", "Y7", "Y8", "Y9",
"RSKIP", "RSKIP", "BSKIP", "BSKIP", "GSKIP", "GSKIP", "YSKIP",
"YSKIP", "R-REVERSE", "R-REVERSE", "B-REVERSE", "B-REVERSE",
"G-REVERSE", "G-REVERSE", "Y-REVERSE", "Y-REVERSE"
};
random_shuffle(deck, deck + 100); // This 'shuffles' the deck
for (i = 0; i < 7; i++) // outputs 7 random cards|| This is just to test
{
cout << deck[i] << ", ";
}
return 0;
}
int names() // Test for player naming loop, and seeing them out to the screen
{
cout << " Please insert 4 names for your players" << endl;
cin >> players[0];
cin >> players[1];
cin >> players[2];
cin >> players[3];
cout << "Welcome: " << players[0] << " , " << players[1] << " , " << players[2] << " , " << players[3] << endl;
return 0;
}
int playerLoop() // This loop doesn't seem to work. When playercard is reverse or REVERSE nothing changes.
{
bool reverse = false;
int i;
while (reverse = true)
{
if (playercard != "REVERSE" || playercard != "reverse")
{
for (i = 0; i != 4; i++)
{
cout << " It is " << players[i] << "'s turn to play!" << endl;
deck(); // This assignes 7 new cards each loop. Just for test
cout << players[i] << "'s cards" << endl;
cout << " Type a card you want to play " << endl;
cout << "" << endl;
cout << " Current Card: " << playercard << endl;
cin >> playercard;
}
}
else if (playercard == "REVERSE" || playercard == "reverse")
{
for (i = 0; i != 4; i--)
cout << " It is " << players[i] << "'s turn to play!" << endl;
cout << " Type a card you want to play " << endl;
cout << "" << endl;
cout << " Current Card: " << playercard << endl;
cin >> playercard;
}
}
return 0;
}
int main() // This is jsut so I can test somethings code wise while I make them
{
/*
Card test1; // Makes a card instance
test1.m_Face == 14; // Assigns a special card. Check the enum 14 is a wild
test1.GetAction(); // Checks the card to see if it is special.
*/
names();
playerLoop();
system("pause");
return 0;
}
#2. Posted:
Status: Offline
Joined: Jun 12, 200915Year Member
Posts: 8,515
Reputation Power: 520
ProJimmyRustler wrote I need help with creating a loop that will take the 7 random array values and then store them for the player's hand. Each player will need a hand so that will result in 4 arrays.
I would have a player object which contains a hand member (like std::vector<Card> Hand;) which takes 7 random vector elements and adds them to each players (create 4 player objects) hand. Though that would require rewriting a lot of your code lol
Why vectors instead of arrays? Read below.
ProJimmyRustler wrote The other thing that I will need help with is adding x amount of cards to the array. and a separate thing that will remove the card from the array when the player types it out. (check code)
Unless your professor has given you a certain specification for this project, I would rather use std::vector<type> instead of std::array. I don't use arrays often in C++ so I'm probably not even going to be any help to you here but I know for sure that with std::vector you can add (push_back) and delete (erase) elements from the vector itself since it is a dynamic array in a sense unlike arrays which have a fixed size after compiling (if you're familiar with C#, C++ vectors work similar to a C# list).
- 1useful
- 0not useful
#3. Posted:
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
Status: Offline
Joined: Aug 19, 201014Year Member
Posts: 5,243
Reputation Power: 532
For your "playerLoop" function, you have declared the reverse boolean and initialised it to false.
This means that every time the function is called, the while loop won't execute because the value is already false.
This means that every time the function is called, the while loop won't execute because the value is already false.
- 0useful
- 0not useful
#4. Posted:
Status: Offline
Joined: Jul 14, 201410Year Member
Posts: 1,720
Reputation Power: 71
Status: Offline
Joined: Jul 14, 201410Year Member
Posts: 1,720
Reputation Power: 71
-Deano wrote For your "playerLoop" function, you have declared the reverse boolean and initialised it to false.
This means that every time the function is called, the while loop won't execute because the value is already false.
I have changed the code slightly since then. A member in my group did that.
Ant wroteProJimmyRustler wrote I need help with creating a loop that will take the 7 random array values and then store them for the player's hand. Each player will need a hand so that will result in 4 arrays.
I would have a player object which contains a hand member (like std::vector<Card> Hand;) which takes 7 random vector elements and adds them to each players (create 4 player objects) hand. Though that would require rewriting a lot of your code lol
Why vectors instead of arrays? Read below.
ProJimmyRustler wrote The other thing that I will need help with is adding x amount of cards to the array. and a separate thing that will remove the card from the array when the player types it out. (check code)
Unless your professor has given you a certain specification for this project, I would rather use std::vector<type> instead of std::array. I don't use arrays often in C++ so I'm probably not even going to be any help to you here but I know for sure that with std::vector you can add (push_back) and delete (erase) elements from the vector itself since it is a dynamic array in a sense unlike arrays which have a fixed size after compiling (if you're familiar with C#, C++ vectors work similar to a C# list).
This helps. There is nothing specific that was assigned for this project, it is a final project for the class. I don't know much about vectors, because it is an intro class and we havent gotten that far. I plan to do some research on them. If you don't mine, can I pm you when I get some research done?
- 0useful
- 0not useful
#5. Posted:
Status: Offline
Joined: Jun 12, 200915Year Member
Posts: 8,515
Reputation Power: 520
Status: Offline
Joined: Jun 12, 200915Year Member
Posts: 8,515
Reputation Power: 520
ProJimmyRustler wrote This helps. There is nothing specific that was assigned for this project, it is a final project for the class. I don't know much about vectors, because it is an intro class and we havent gotten that far. I plan to do some research on them. If you don't mine, can I pm you when I get some research done?
I'm not the best at explaining this sort of stuff but yeah sure thing.
- 0useful
- 0not useful
You are viewing our Forum Archives. To view or take place in current topics click here.