My objectives:
My code:
MemoryMatchGame.h
#ifndef MEMORYMATCHGAME_H
#define MEMORYMATCHGAME_H
#include <string>
class MemoryMatchGame {
private:
std::string theme50Words[50];
std::string answerArray[8][8];
std::string displayArray[8][8];
int gridSize;
int timeInterval;
public:
void start();
void loadTheme(int theme);
void createGrid();
void populateAnswerGrid(int numTerms);
void displayCurrentGameState();
void playGame();
void revealTerm(int row, int col);
void compareTerms(int firstRow, int firstCol, int secondRow, int secondCol);
};
#endif // MEMORYMATCHGAME_H
MemoryMatchGame.cpp
#include <iostream>
#include <string>
#include <fstream>
#include <random>
#include <chrono>
#include <thread>
class MemoryMatchGame {
private:
std::string theme50Words[50];
std::string answerArray[8][8];
std::string displayArray[8][8];
int gridSize;
int timeInterval;
public:
void start();
void loadTheme(int theme);
void createGrid();
void populateAnswerGrid(int numTerms);
void displayCurrentGameState();
void playGame();
void revealTerm(int row, int col);
void compareTerms(int firstRow, int firstCol, int secondRow, int secondCol);
};
void MemoryMatchGame::start() {
int theme;
std::cout << "Choose a game theme (1-3):" << std::endl;
std::cout << "1. Car Brands" << std::endl;
std::cout << "2. Elements" << std::endl;
std::cout << "3. Team Names" << std::endl;
std::cin >> theme;
while (theme < 1 || theme > 3) {
std::cout << "Invalid theme. Choose a theme (1-3):" << std::endl;
std::cin >> theme;
}
loadTheme(theme);
int level;
std::cout << "Select level of play:" << std::endl;
std::cout << "1. 4 x 4 grid (Easy)" << std::endl;
std::cout << "2. 6 x 6 grid (Moderate)" << std::endl;
std::cout << "3. 8 x 8 grid (Difficult)" << std::endl;
std::cin >> level;
while (level < 1 || level > 3) {
std::cout << "Invalid level. Select level of play (1-3):" << std::endl;
std::cin >> level;
}
gridSize = (level + 1) * 2;
int speed;
std::cout << "Select speed of play:" << std::endl;
std::cout << "1. 2 seconds (Difficult)" << std::endl;
std::cout << "2. 4 seconds (Moderate)" << std::endl;
std::cout << "3. 6 seconds (Easy)" << std::endl;
std::cin >> speed;
while (speed < 1 || speed > 3) {
std::cout << "Invalid speed. Select speed of play (1-3):" << std::endl;
std::cin >> speed;
}
timeInterval = speed * 2;
createGrid();
populateAnswerGrid(gridSize * gridSize / 2);
displayCurrentGameState();
playGame();
}
void MemoryMatchGame::loadTheme(int theme) {
std::string filename;
switch (theme) {
case 1:
filename = "carBrands.txt";
break;
case 2:
filename = "elements.txt";
break;
case 3:
filename = "teamNames.txt";
break;
}
std::ifstream file(filename);
if (!file) {
std::cout << "Failed to open theme file." << std::endl;
return;
}
for (int i = 0; i < 50 && std::getline(file, theme50Words[i]); ++i) {
// Truncate words longer than 8 characters
if (theme50Words[i].length() > 8) {
theme50Words[i] = theme50Words[i].substr(0, 8);
}
}
file.close();
}
void MemoryMatchGame::createGrid() {
for (int i = 0; i < gridSize; ++i) {
for (int j = 0; j < gridSize; ++j) {
displayArray[i][j] = "FACE";
}
}
}
void MemoryMatchGame::populateAnswerGrid(int numTerms) {
std::random_device rd;
std::mt19937 gen(rd());
std::shuffle(theme50Words, theme50Words + 50, gen);
for (int i = 0; i < numTerms; ++i) {
answerArray[i / 2][i % 2] = theme50Words[i];
answerArray[(gridSize * gridSize - numTerms) + (i / 2)][(gridSize * gridSize - numTerms) + (i % 2)] = theme50Words[i];
}
}
void MemoryMatchGame::displayCurrentGameState() {
for (int i = 0; i < gridSize; ++i) {
for (int j = 0; j < gridSize; ++j) {
std::cout << displayArray[i][j] << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void MemoryMatchGame::playGame() {
int firstRow, firstCol, secondRow, secondCol;
int count = 0;
while (count < gridSize * gridSize / 2) {
std::cout << "Select the FIRST square (row, col): ";
std::cin >> firstRow >> firstCol;
while (firstRow < 0 || firstRow >= gridSize || firstCol < 0 || firstCol >= gridSize) {
std::cout << "Invalid selection. Select the FIRST square (row, col): ";
std::cin >> firstRow >> firstCol;
}
if (displayArray[firstRow][firstCol] == "FACE") {
revealTerm(firstRow, firstCol);
displayCurrentGameState();
std::cout << "Select the SECOND square (row, col): ";
std::cin >> secondRow >> secondCol;
while (secondRow < 0 || secondRow >= gridSize || secondCol < 0 || secondCol >= gridSize) {
std::cout << "Invalid selection. Select the SECOND square (row, col): ";
std::cin >> secondRow >> secondCol;
}
if (displayArray[secondRow][secondCol] == "FACE") {
revealTerm(secondRow, secondCol);
displayCurrentGameState();
compareTerms(firstRow, firstCol, secondRow, secondCol);
if (displayArray[firstRow][firstCol] == "FACE") {
displayArray[firstRow][firstCol] = "TERM";
displayArray[secondRow][secondCol] = "TERM";
}
displayCurrentGameState();
}
}
}
}
void MemoryMatchGame::revealTerm(int row, int col) {
displayArray[row][col] = answerArray[row][col];
}
void MemoryMatchGame::compareTerms(int firstRow, int firstCol, int secondRow, int secondCol) {
if (answerArray[firstRow][firstCol] == answerArray[secondRow][secondCol]) {
std::cout << "Match Found!" << std::endl;
}
else {
std::this_thread::sleep_for(std::chrono::seconds(timeInterval));
displayArray[firstRow][firstCol] = "FACE";
displayArray[secondRow][secondCol] = "FACE";
std::cout << "No Match. Terms hidden." << std::endl;
}
}
Timer.h:
#pragma once
#include <chrono>
class Timer
{
public:
Timer();
void reset();
double elapsed() const;
private:
std::chrono::time_point<std::chrono::high_resolution_clock> start_time_;
};
Timer.cpp:
#include "Timer.h"
Timer::Timer() : start_time_(std::chrono::high_resolution_clock::now()) {}
void Timer::reset() {
start_time_ = std::chrono::high_resolution_clock::now();
}
double Timer::elapsed() const {
auto end_time = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> duration = end_time - start_time_;
return duration.count();
}```
Main.cpp
#include
using namespace std;
#include ;
#include “MemoryMatchGame.h”
int main()
{
MemoryMatchGame Game1;
Game1.start();
}```
carBrands.txt:
Honda
Toyota
Chevrolet
Nissan
Ford
BMW
Audi
Lexus
Mercedes
Mazda
Hyundai
Kia
Subaru
Volvo
Jeep
Tesla
Acura
Dodge
Chrysler
GMC
Volkswag
Ferrari
Lamborgh
Porsche
Jaguar
LandRove
Mitsubis
Mini
AlfaRome
Buick
Cadillac
Lincoln
Infiniti
Renault
Peugeot
Suzuki
Fiat
Smart
Maserati
AstonM
Bentley
RollsRoy
Maybach
Lotus
McLaren
Pagani
Bugatti
Koenigse
Lada```