please change the coding below with using printf and scanf_s -------------------
ID: 3715506 • Letter: P
Question
please change the coding below with using printf and scanf_s
------------------------------------------------------------------------------------------
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>
using namespace std;
//Global variables
char coordX, coordY;
class MemoryMatchGame {
int value;
bool faceUp;
public:
MemoryMatchGame() { // Constructor
faceUp = false;
}
void SetValue(int v) {
value = v;
}
void TurnCard() {
faceUp = !faceUp;
}
int GetValue() {
return value;
}
bool GetFaceUp() {
return faceUp;
}
} deck[4][4];
void ShowBoard() { // Prints the board on screen
system("cls");
cout << " 1 2 3 4" << endl << endl;
for (int y = 0; y<4; y++) {
cout << y + 1 << " ";
for (int x = 0; x<4; x++) {
if (deck[x][y].GetFaceUp() == true) { // If the card is facing up show it
cout << deck[x][y].GetValue() << " ";
}
else { // If is facing down show an ASCII character (hidden card)
cout << (char)177 << " ";
}
}
cout << endl << endl;
}
cout << endl;
}
void SetCards() { // Set the cards in random order
int pairs[16] = { 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8 };
int a, b, temp;
for (int i = 0; i<30; i++) { // Swap 30 times
a = rand() % 16;
b = rand() % 16;
temp = pairs[a];
pairs[a] = pairs[b];
pairs[b] = temp;
}
for (int y = 0; y<4; y++) {
for (int x = 0; x<4; x++) {
deck[x][y].SetValue(pairs[x + (y * 4)]); // Puts swapped cards in the deck
}
}
}
void GetInput() { // Gets the user input
bool isAlreadyUp;
do {
do { // Insert X
cout << "Insert coordinate X: ";
cin >> coordX; // coordX/Y are char to prevent the program from crashing!
if (coordX < (1 + 48) || coordX >(4 + 48))
{
cout << "Invalid selection!" << endl;
}
} while (coordX < (1 + 48) || coordX >(4 + 48));
do { // Insert Y
cout << "Insert coordinate Y: ";
cin >> coordY;
if (coordY < (1 + 48) || coordY >(4 + 48)) {
cout << "Invalid selection!" << endl;
}
} while (coordX < (1 + 48) || coordX >(4 + 48));
if (deck[(int)(coordX - 48 - 1)][(int)(coordY - 48 - 1)].GetFaceUp() == true) { // If player inputs an already-matched card
cout << "You already found the match for this card!" << endl << endl;
isAlreadyUp = true;
}
else {
isAlreadyUp = false;
}
} while (isAlreadyUp == true);
// Adjust values (easier to work with arrays)
coordX--;
coordY--;
}
void Pause() { // Ugly but works fine
for (int i = 0; i<2000; i++) {
for (int j = 0; j<300000; j++);
}
}
int main() {
int holderX, holderY, remaining = 16, tries = 0;
char reply;
bool secondTurn = false;
srand(time(NULL));
while (1) { // Infinite loop
SetCards();
do { // Loop until player matched all cards
ShowBoard();
GetInput();
deck[(int)(coordX - 48)][(int)(coordY - 48)].TurnCard(); // Turns the selected card
if (secondTurn == true) { // If player turned the second card we have to look if they match
secondTurn = false;
tries++;
if ((((int)(coordX - 48)) == holderX) && ((int)(coordY - 48) == holderY)) { // The user selected the same card twice!
tries--;
cout << "You can't select the same card twice!" << endl;
Pause();
continue;
}
if (deck[(int)(coordX - 48)][(int)(coordY - 48)].GetValue() == deck[holderX][holderY].GetValue()) { // If cards match
ShowBoard();
cout << "You matched 2 cards!" << endl;
remaining -= 2;
}
else { // If cards are different
ShowBoard();
cout << "The cards are different!" << endl;
deck[(int)(coordX - 48)][(int)(coordY - 48)].TurnCard(); // Turn cards again
deck[holderX][holderY].TurnCard();
}
Pause();
}
else { // If this is the first card turned, we store the x,y coords for later use
holderX = (int)(coordX - 48);
holderY = (int)(coordY - 48);
secondTurn = true;
}
} while (remaining > 0);
cout << "Congratulations! You matched all the cards in " << tries << " tries!" << endl;
do { // Asks the user if he wants to play again
cout << "Do you want to play again? (y/n): ";
cin >> reply;
if (reply == 'n' || reply == 'N') {
return 0;
}
else if (reply != 'n' && reply != 'N' && reply != 'y' && reply != 'Y') {
cout << "Invalid character, try again." << endl << endl;
}
} while (reply != 'y' && reply != 'Y');
// Reset varables if player wants to play again
remaining = 16;
tries = 0;
for (int y = 0; y<4; y++) {
for (int x = 0; x<4; x++) {
deck[x][y].TurnCard(); // Turns all the cards face down
}
}
}
}
Explanation / Answer
#include "stdafx.h"
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <string>
//Global variables
char coordX, coordY;
class MemoryMatchGame {
int value;
bool faceUp;
public:
MemoryMatchGame() { // Constructor
faceUp = false;
}
void SetValue(int v) {
value = v;
}
void TurnCard() {
faceUp = !faceUp;
}
int GetValue() {
return value;
}
bool GetFaceUp() {
return faceUp;
}
} deck[4][4];
void ShowBoard() { // Prints the board on screen
system("cls");
printf(" 1 2 3 4 ");// " 1 2 3 4" );
for (int y = 0; y<4; y++) {
printf(" %d", y + 1 );// printf(y + 1 << " ";
for (int x = 0; x<4; x++) {
if (deck[x][y].GetFaceUp() == true) { // If the card is facing up show it
printf("%d ", deck[x][y].GetValue());
}
else { // If is facing down show an ASCII character (hidden card)
printf("'177'");
}
}
printf(" ");
}
printf(" ");
}
void SetCards() { // Set the cards in random order
int pairs[16] = { 1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8 };
int a, b, temp;
for (int i = 0; i<30; i++) { // Swap 30 times
a = rand() % 16;
b = rand() % 16;
temp = pairs[a];
pairs[a] = pairs[b];
pairs[b] = temp;
}
for (int y = 0; y<4; y++) {
for (int x = 0; x<4; x++) {
deck[x][y].SetValue(pairs[x + (y * 4)]); // Puts swapped cards in the deck
}
}
}
void GetInput() { // Gets the user input
bool isAlreadyUp;
do {
do { // Insert X
printf("Insert coordinate X: ");
scanf("%c",&coordX);
//scanf( coordX; // coordX/Y are char to prevent the program from crashing!
if (coordX < (1 + 48) || coordX >(4 + 48))
{
printf("Invalid selection! ");
}
} while (coordX < (1 + 48) || coordX >(4 + 48));
do { // Insert Y
printf("Insert coordinate Y: ");
scanf("%c",&coordY);
if (coordY < (1 + 48) || coordY >(4 + 48)) {
printf("Invalid selection! " );
}
} while (coordX < (1 + 48) || coordX >(4 + 48));
if (deck[(int)(coordX - 48 - 1)][(int)(coordY - 48 - 1)].GetFaceUp() == true) { // If player inputs an already-matched card
printf("You already found the match for this card! " );
isAlreadyUp = true;
}
else {
isAlreadyUp = false;
}
} while (isAlreadyUp == true);
// Adjust values (easier to work with arrays)
coordX--;
coordY--;
}
void Pause() { // Ugly but works fine
for (int i = 0; i<2000; i++) {
for (int j = 0; j<300000; j++);
}
}
int main() {
int holderX, holderY, remaining = 16, tries = 0;
char reply;
bool secondTurn = false;
srand(time(NULL));
while (1) { // Infinite loop
SetCards();
do { // Loop until player matched all cards
ShowBoard();
GetInput();
deck[(int)(coordX - 48)][(int)(coordY - 48)].TurnCard(); // Turns the selected card
if (secondTurn == true) { // If player turned the second card we have to look if they match
secondTurn = false;
tries++;
if ((((int)(coordX - 48)) == holderX) && ((int)(coordY - 48) == holderY)) { // The user selected the same card twice!
tries--;
printf("You can't select the same card twice! ");
Pause();
continue;
}
if (deck[(int)(coordX - 48)][(int)(coordY - 48)].GetValue() == deck[holderX][holderY].GetValue()) { // If cards match
ShowBoard();
printf("You matched 2 cards! ");
remaining -= 2;
}
else { // If cards are different
ShowBoard();
printf("The cards are different! ");
deck[(int)(coordX - 48)][(int)(coordY - 48)].TurnCard(); // Turn cards again
deck[holderX][holderY].TurnCard();
}
Pause();
}
else { // If this is the first card turned, we store the x,y coords for later use
holderX = (int)(coordX - 48);
holderY = (int)(coordY - 48);
secondTurn = true;
}
} while (remaining > 0);
printf("Congratulations! You matched all the cards in %d %d " ,tries,tries);// << << " tries! ");
do { // Asks the user if he wants to play again
printf("Do you want to play again? (y/n): ");
scanf( "%c",&reply);
if (reply == 'n' || reply == 'N') {
return 0;
}
else if (reply != 'n' && reply != 'N' && reply != 'y' && reply != 'Y') {
printf("Invalid character, try again." );
}
} while (reply != 'y' && reply != 'Y');
// Reset varables if player wants to play again
remaining = 16;
tries = 0;
for (int y = 0; y<4; y++) {
for (int x = 0; x<4; x++) {
deck[x][y].TurnCard(); // Turns all the cards face down
}
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.