i have made this code that generates a combination of 3-5 of different symbols to result in a random float. And now i’m trying to make it into a board, where it shows up as paylines like the real slot machines, but i can’t figure out how to do it without it giving exceptions or looking really weird.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static test.Program;
namespace test
{
internal class Program
{
// Define a structure to represent a line and its payout
public struct Line
{
public string Symbol;
public int Length;
public float Payout;
public Line(string symbol, int length, float payout)
{
Symbol = symbol;
Length = length;
Payout = payout;
}
public override string ToString()
{
return $"{Symbol} {Length}x {Payout}";
}
}
static void Main(string[] args)
{
while (true)
{
// Define the paytable
List<Line> paytable = new List<Line>
{
new Line("J", 3, 0.1f),
new Line("J", 4, 0.3f),
new Line("J", 5, 0.6f),
new Line("Q", 3, 0.7f),
new Line("Q", 4, 1.5f),
new Line("Q", 5, 3.0f),
new Line("K", 3, 1.5f),
new Line("K", 4, 5.0f),
new Line("K", 5, 10.0f),
new Line("A", 3, 3.0f),
new Line("A", 4, 10.0f),
new Line("A", 5, 20.0f),
};
paytable.Reverse();
float maxWin = 280;
float target = GetRandomFloat(maxWin);
// List to hold the result
List<Line> result = new List<Line>();
// Find the combination
bool success = FindCombination(paytable, target, new List<Line>(), result);
if (success)
{
Console.WriteLine($"Combination found for {target}:");
foreach (var line in result)
{
Console.WriteLine(line);
}
}
else
{
Console.WriteLine($"No combination found for {target}.");
}
Console.ReadKey(true);
}
}
static float GetRandomFloat(float max)
{
Random random = new Random();
int scale = (int)(max * 10); // Scale to work with integers
int randomValue = random.Next(0, scale + 1);
return randomValue / 10f; // Convert back to float with 1 decimal
}
static bool FindCombination(List<Line> paytable, float target, List<Line> current, List<Line> result)
{
// Base case: if target is close enough to zero
if (Math.Abs(target) < 0.1f)
{
result.AddRange(current);
return true;
}
// Base case: if more than 20 lines are used
if (current.Count >= 20)
{
return false;
}
// Explore each line in the paytable
foreach (var line in paytable)
{
if (line.Payout <= target)
{
current.Add(line);
if (FindCombination(paytable, target - line.Payout, current, result))
{
return true;
}
current.RemoveAt(current.Count - 1);
}
}
return false;
}
}
}
2