Question
So, I'm having a problem with passing my 2d array I only have one workaround and it looks kinda weird. which is at reading my
So, I'm having a problem with passing my 2d array I only have one workaround and it looks kinda weird. which is at reading my file function, but in my project it is says to pass a 2d array? So I'm trying to figure out how I can use a loop for the first getline(file, input) to have them match ( a pair of each letter since on txt file it has only = ABCDEFGH so 8 letters ) in every matrix than having to do it twice. I'm trying to just do letters right since once I have that done the numbers, animals, and objects will be easy. Also is my shuffle okay? I know I still have the issues of not using both of the cols and rows but as far as the question on the project and my code, does it look fine? And help me to do displayboard() please, also I know I haven't made my boolean 4x4 array it's only because I haven't gotten to that part yet. Everything else I will try to get up to and if you like to help more than that, that's fine. I appreciate your help since I'm trying to get better at c++.
(I haven't used .h file yet too btw)
My code:
#include
#include
#include
#include
#include
#include "CheckInput.h"
int getFileChoice();
using namespace std;
#define rows 4
#define cols 4
void displayBoard(string array[rows][cols]);
void readFile(string array[rows][cols], int choice);
void shuffle(string array[rows][cols]);
int main(){
string array[rows][cols] = {{"1", "2", "3", "4"},
{"5", "6", "7", "8"},
{"9", "10", "11", "12"},
{"13", "14", "15", "16"}};
for(int i =0; i
for( int j =0; j
cout
}
cout
}
cout
int GetNum;
GetNum = getFileChoice();
if(GetNum == 1){
int find = 1;
readFile(array, find);
for(int i =0; i
for( int j =0; j
cout
}
cout
}
cout
shuffle(array);
for(int i =0; i
for( int j =0; j
cout
}
cout
}
}
if(GetNum == 2){
}
if(GetNum == 3){
}
if(GetNum == 4){
}
}
void shuffle(string array[rows][cols]){
srand(time(0));
int large = 100;
for(int i =0; i
int index = rand() % 16;
string temp = array[0][cols];
array[0][cols] = array[0][index];
array[0][index] = temp;
}
}
// the work around for array
void readFile(string array[rows][cols], int choice){
if( choice == 1){
string array1[rows];
string array2[rows];
int i =0;
int j =0;
int k =1;
int p =1;
fstream file;
string input;
string inputp;
file.open("letters.txt", ios::in);
if(file){
while( getline(file, input)){
array[0][i] = input;
i++;
}
file.close();
} else {
cout
}
file.open("letters.txt", ios::in);
if(file){
while( getline(file, input)){
array[2][j] = input;
j++;
}
file.close();
} else {
cout
}
}
}
int getFileChoice(){
int get;
cout
cout
cout
cout
get = getIntRange(1, 4);
return get;
}
int getIntRange(int low, int high) {
int input = 0;
bool valid = false;
while(!valid) {
if(cin >> input) {
if(input >= low && input
valid = true;
} else {
cout
}
} else {
cin.clear();
string invalid;
cin >> invalid;
cout
}
}
return input;
}
project :
Create the children's game Memory. Initialize the deck using a user selected file of card values, then shuffle and display the cards face down. The user selects a card and it is displayed face up. Then the user selects a second card, and is again displayed face up. If they are a match leave the cards face up for the rest of the game, if they aren't, flip them back over when the user chooses the next card. After the user has matched all eight cards they win and the game is over. Create a 4x4 array of strings to store the values of each of the cards, and a 4x4 array of booleans to keep track of whether a card is face up or face down. Create the following functions to use for your program: 1. getFileChoice - display a menu listing the files for the types of card values, prompt the user for their choice and return their selection. 2. readFile - pass in the user's file selection and the 2D array of strings. Read in the file's contents and place two of each card value in the array. 3. shuffle - pass in the 2D array of strings, then repeatedly (-100 times) choose two random locations in the array and swap the card values. 4. display Board - pass both 2D arrays, display the board in a 4x4 configuration similar to the example output below. If the card is face up, display the card's value, if it is face down, display the card's number (1-16). 5. getChoice - gets the user's input for which card they choose. Return their selection. Check that the input is an integer within the range 1-16. 6. flipChoice - pass in the user's choice, and the 2D array of booleans, then flip over that card. 7. isMatch - pass in the two cards the user chose and the 2D array of strings, return true if they are a match. 8. check Flipped - pass in the user's choice and the 2D array of booleans, return true if the card is already flipped up. The user should not be able to enter any invalid values for their card choice (ie. strings, or numbers outside of the range 1-16. They also should not be able to choose a card that has already been flipped over. Note: the card values in the files all have exactly 4 characters (shorter words are already padded with spaces). This will make displaying them on the cards easier. Hint: You need to convert the user's choice (1-16) into a row and column location for the 2D arrays. You can use / and % to find the row and column. Extra Credit (5 points) - after the user has found all 8 matches and the game ends, ask the user if they would like to play again. If so, reset the arrays and start again. +----+ +----+ +----+ +----+ Example Output: Memory Game 1. Letters 2. Numbers 3. Animals 4. Objects Enter Choice: 1 +----+ +----+ +----+ - | 8 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ - | 10 | 11 | 12 | | 1 | 2 | 3 | 4 | - + +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ + +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 H | 15 | 16 || | 5 +----+ | 6 | 7 | 8 | LL LL +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Not A Match Please enter choice: 16 +----+ +----+ +----+ +----+ | 9 | 10 | 11 | 12 | | | 2 | 3 | 4 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 | 14 | 15 | 16 | | 5 | 6 | 7 | 8 | +----+ +----+ +----+ +----+ Please enter choice: X Invalid Input +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 9 | 10 | 11 12 | Please enter choice: 22 Invalid Value +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 14 +----+ +----+ +----+ +----+ | 13 | 14 | 15 | LA | 3 | 4 | +----+ | 1 | 2 1 L L +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 6 +----+ +----+ +----+ +----+ | 5 | 6 | 7 | 8 | | 1 | 2 3 | 4 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | | | 9 +----+ +----+ | | 10 +----+ +----+ | | | 11 | +----+ +----+ | 12 | | 5 | A 7 | 8 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 | H | 15 | 16 | | 9 | | +----+ +----+ | 10 | | +----+ +----+ | 11 | +----+ +----+ | 12 | | | +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 14 Location already entered. | 13 | 14 | 15 | LA Please enter choice: 6 +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Match Found Please enter choice: | 1 | 2 | 3 | 4 | L L LL LL +----+ +----+ +----+ +----+ getFileChoice & getChoice methods readFile method Prompts user Uses Checklnput class Checks that input is an integer 1-4 & 1-16 Returns valid input Passes in user selection and String array Uses Scanner to read correct file Adds card values to array (2 each) Displays 4x4 grid of cards _Displays card number if card is not flipped Displays card value if card is flipped over Chooses two random cards Swaps the values of the cards Repeats -100 times Flips over the card at the location the user specified Face down flips to face up and face up flips to face down display Board method shuffle method flip Choice method is Match method checkFlipped method Returns true if the two cards the user chose are a match Returns false if the two cards are not a match Returns true if the card is already flipped up Returns false if the card is flipped down Created 2D array of strings for card values Created 2D array of booleans for card state Has a repeating game loop Gets user input for location of first card to flip Gets user input for location of second card to flip Checks if inputs are valid Checks if cards have already been flipped Displays board after each flip Displays if there was a match or not Keeps matched cards face up Mismatched cards are flipped back down Player wins when all 8 matches are made main method letters.txt Inamonds IQTMDow> Create the children's game Memory. Initialize the deck using a user selected file of card values, then shuffle and display the cards face down. The user selects a card and it is displayed face up. Then the user selects a second card, and is again displayed face up. If they are a match leave the cards face up for the rest of the game, if they aren't, flip them back over when the user chooses the next card. After the user has matched all eight cards they win and the game is over. Create a 4x4 array of strings to store the values of each of the cards, and a 4x4 array of booleans to keep track of whether a card is face up or face down. Create the following functions to use for your program: 1. getFileChoice - display a menu listing the files for the types of card values, prompt the user for their choice and return their selection. 2. readFile - pass in the user's file selection and the 2D array of strings. Read in the file's contents and place two of each card value in the array. 3. shuffle - pass in the 2D array of strings, then repeatedly (-100 times) choose two random locations in the array and swap the card values. 4. display Board - pass both 2D arrays, display the board in a 4x4 configuration similar to the example output below. If the card is face up, display the card's value, if it is face down, display the card's number (1-16). 5. getChoice - gets the user's input for which card they choose. Return their selection. Check that the input is an integer within the range 1-16. 6. flipChoice - pass in the user's choice, and the 2D array of booleans, then flip over that card. 7. isMatch - pass in the two cards the user chose and the 2D array of strings, return true if they are a match. 8. check Flipped - pass in the user's choice and the 2D array of booleans, return true if the card is already flipped up. The user should not be able to enter any invalid values for their card choice (ie. strings, or numbers outside of the range 1-16. They also should not be able to choose a card that has already been flipped over. Note: the card values in the files all have exactly 4 characters (shorter words are already padded with spaces). This will make displaying them on the cards easier. Hint: You need to convert the user's choice (1-16) into a row and column location for the 2D arrays. You can use / and % to find the row and column. Extra Credit (5 points) - after the user has found all 8 matches and the game ends, ask the user if they would like to play again. If so, reset the arrays and start again. +----+ +----+ +----+ +----+ Example Output: Memory Game 1. Letters 2. Numbers 3. Animals 4. Objects Enter Choice: 1 +----+ +----+ +----+ - | 8 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ - | 10 | 11 | 12 | | 1 | 2 | 3 | 4 | - + +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ + +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 H | 15 | 16 || | 5 +----+ | 6 | 7 | 8 | LL LL +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Not A Match Please enter choice: 16 +----+ +----+ +----+ +----+ | 9 | 10 | 11 | 12 | | | 2 | 3 | 4 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 | 14 | 15 | 16 | | 5 | 6 | 7 | 8 | +----+ +----+ +----+ +----+ Please enter choice: X Invalid Input +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 9 | 10 | 11 12 | Please enter choice: 22 Invalid Value +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 14 +----+ +----+ +----+ +----+ | 13 | 14 | 15 | LA | 3 | 4 | +----+ | 1 | 2 1 L L +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 6 +----+ +----+ +----+ +----+ | 5 | 6 | 7 | 8 | | 1 | 2 3 | 4 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | | | 9 +----+ +----+ | | 10 +----+ +----+ | | | 11 | +----+ +----+ | 12 | | 5 | A 7 | 8 | +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ | 13 | H | 15 | 16 | | 9 | | +----+ +----+ | 10 | | +----+ +----+ | 11 | +----+ +----+ | 12 | | | +----+ +----+ +----+ +----+ +----+ +----+ Please enter choice: 14 Location already entered. | 13 | 14 | 15 | LA Please enter choice: 6 +----+ +----+ +----+ +----+ +----+ +----+ +----+ +----+ Match Found Please enter choice: | 1 | 2 | 3 | 4 | L L LL LL +----+ +----+ +----+ +----+ getFileChoice & getChoice methods readFile method Prompts user Uses Checklnput class Checks that input is an integer 1-4 & 1-16 Returns valid input Passes in user selection and String array Uses Scanner to read correct file Adds card values to array (2 each) Displays 4x4 grid of cards _Displays card number if card is not flipped Displays card value if card is flipped over Chooses two random cards Swaps the values of the cards Repeats -100 times Flips over the card at the location the user specified Face down flips to face up and face up flips to face down display Board method shuffle method flip Choice method is Match method checkFlipped method Returns true if the two cards the user chose are a match Returns false if the two cards are not a match Returns true if the card is already flipped up Returns false if the card is flipped down Created 2D array of strings for card values Created 2D array of booleans for card state Has a repeating game loop Gets user input for location of first card to flip Gets user input for location of second card to flip Checks if inputs are valid Checks if cards have already been flipped Displays board after each flip Displays if there was a match or not Keeps matched cards face up Mismatched cards are flipped back down Player wins when all 8 matches are made main method letters.txt Inamonds IQTMDow>
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started