Body:
I’m trying to implement logic for checking diagonal matches in a 3×5 2D string vector that looks like this:
[0,0][0,1][0,2][0,3][0,4]
[1,0][1,1][1,2][1,3][1,4]
[2,0][2,1][2,2][2,3][2,4]
Based on this matrix, the diagonals from top-left to bottom-right are:
[0,0], [1,1], [2,2]
[0,1], [1,2], [2,3]
[0,2], [1,3], [2,4]
I’m struggling to create the diagonal matching logic for my slot machine game. If there is a diagonal match, I want to add it to the total payout and then print the total payout. Once I get the forward direction, I think it will be easier to implement the reverse direction.
Code:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
// Function to print a 2D vector (for debugging purposes)
void print2D_Vector(const vector<vector<string>>& vec) {
for (const auto& row : vec) {
for (const auto& elem : row) {
cout << elem << " ";
}
cout << endl;
}
}
int main() {
vector<vector<string>> reels = {
{"cherry", "bell", "7", "bell", "diamond"},
{"diamond", "cherry", "7", "bell", "$"},
{"7", "diamond", "cherry", "$", "bell"}
};
print2D_Vector(reels);
int total_Payout = 0;
// Check for diagonal matches (top-left to bottom-right)
for (int col = 0; col <= 2; ++col) {
if (reels[0][col] == reels[1][col + 1] && reels[1][col + 1] == reels[2][col + 2]) {
string match = reels[0][col];
if (match == "cherry") {
total_Payout += 5;
} else if (match == "bell") {
total_Payout += 10;
}
}
}
// Check for reverse diagonal matches (bottom-left to top-right)
for (int col = 0; col <= 2; ++col) {
if (reels[2][col] == reels[1][col + 1] && reels[1][col + 1] == reels[0][col + 2]) {
string match = reels[2][col];
if (match == "cherry") {
total_Payout += 5;
} else if (match == "bell") {
total_Payout += 10;
}
}
}
cout << "Your payout is " + to_string(total_Payout) << endl;
return 0;
}
1