I just want to start off with, be gentle with me please. This is all new to me and I'm still learning.
I'm creating a slot game app, but I'm struggling with the coding on it recognizing patterns for winning lines. Here is the setup of my slot placement images:
|---reel1_symbol1--- reel2_symbol1--- reel3_symbol1--- reel4_symbol1---|
|---reel1_symbol2--- reel2_symbol2--- reel3_symbol2--- reel4_symbol2---|
|---reel1_symbol3--- reel2_symbol3--- reel3_symbol3--- reel4_symbol3---|
I attempted to teach it winning patterns with this block of code:
private fun checkWinningCombinations(finalSymbols: List<List<String>>): Long {
// Define the winning patterns
val winningPatterns = listOf(
// Single-line winning patterns
//Line of 2
listOf(
listOf("X", "X", "O", "O"),
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("X", "X", "O", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O"),
listOf("X", "X", "O", "O")
),
//Line of 3
listOf(
listOf("X", "X", "X", "O"),
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("X", "X", "X", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O"),
listOf("X", "X", "X", "O")
),
//Line of 4
listOf(
listOf("X", "X", "X", "X"),
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("X", "X", "X", "X"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("O", "O", "O", "O"),
listOf("X", "X", "X", "X")
),
// Multi-line winning patterns
listOf(
listOf("X", "O", "O", "X"),
listOf("O", "X", "X", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("X", "O", "O", "O"),
listOf("O", "X", "O", "O"),
listOf("O", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "X"),
listOf("O", "X", "O", "O"),
listOf("X", "O", "O", "O")
),
listOf(
listOf("O", "O", "O", "O"),
listOf("O", "X", "X", "O"),
listOf("X", "O", "O", "X")
)
)
val symbolMapping = mapOf(
"cherryblossom_girl1" to "X",
"cherryblossom_tower" to "X",
"cherryblossom_cat" to "X",
"cherryblossom_komono" to "X",
"cherryblossom_rice_chopsticks" to "X",
"cherryblossom_flower" to "X",
"cherryblossom_fan" to "X",
"cherryblossom_umbrella" to "X"
)
// Translate finalSymbols using the symbolMapping
val translatedFinalSymbols = finalSymbols.map { row ->
row.map { symbol ->
symbolMapping[symbol] ?: ""
}
}
// Iterate over each winning pattern and check if it matches the final symbols
for (pattern in winningPatterns) {
var matchCount = 0
for (i in pattern.indices) {
var matchFound = true
for (j in pattern[i].indices) {
if (translatedFinalSymbols[i][j] != pattern[i][j]) {
matchFound = false
break
}
}
if (matchFound) {
matchCount++
}
}
if (matchCount == pattern.size) {
return calculatePayout(pattern[0]) // Payout based on the first symbol of the pattern
}
}
// Return 0 if no winning combination is found
return 0
}
// Function to calculate the payout based on the final symbols displayed
private fun calculatePayout(finalSymbols: List<String>): Long {
val symbolValues = mapOf(
"cherryblossom_girl1" to 100L,
"cherryblossom_tower" to 80L,
"cherryblossom_cat" to 60L,
"cherryblossom_komono" to 40L,
"cherryblossom_rice_chopsticks" to 30L,
"cherryblossom_flower" to 20L,
"cherryblossom_fan" to 10L,
"cherryblossom_umbrella" to 5L
)
var winAmount = 0L
val winSymbols = mutableSetOf<String>()
for (symbol in finalSymbols) {
val value = symbolValues[symbol] ?: 0L
if (value > 0) {
winAmount += value
winSymbols.add(symbol)
}
}
// Determine the prize amount based on win symbols
return when {
winSymbols.size == 4 -> winAmount * 10L // Jackpot if all symbols are the same
winSymbols.size >= 3 -> winAmount * 5L // High win if three or more symbols are the same
winSymbols.size == 2 -> winAmount * 2L // Medium win if two symbols are the same
else -> winAmount // Low win or no win
}
}
Here is the xml layer if it helps
<!-- res/layout/activity_cherryblossom_slotgame.xml -->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.snggoeevee.slotswithfriends.HomeActivity">
<ImageView
android:id="@+id/cherryblossomslot_background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/cherryblossumslots_background" />
<!-- Reel 1 -->
<ImageView
android:id="@+id/reel1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="first reel"
android:layout_marginStart="1dp"
android:src="@drawable/cherryblossom_reel1" />
<!-- Reel 2 -->
<ImageView
android:id="@+id/reel2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="second reel"
android:layout_marginStart="1dp"
android:src="@drawable/cherryblossom_reel2" />
<!-- Reel 3 -->
<ImageView
android:id="@+id/reel3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="third reel"
android:layout_marginStart="1dp"
android:src="@drawable/cherryblossom_reel3" />
<!-- Reel 4 -->
<ImageView
android:id="@+id/reel4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="fourth reel"
android:layout_marginStart="1dp"
android:src="@drawable/cherryblossom_reel4" />
<!-- First/Top Payline Row -->
<LinearLayout
android:id="@+id/imgs"
android:layout_width="469dp"
android:layout_height="181dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:scaleType="centerCrop">
<ImageView
android:id="@+id/reel1_symbol1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel2_symbol1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="60dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel3_symbol1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="65dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="1dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel4_symbol1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="55dp"
android:layout_marginTop="110dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
</LinearLayout>
<!-- Second/Middle Payline Row -->
<LinearLayout
android:id="@+id/imgs2"
android:layout_width="485dp"
android:layout_height="69dp"
android:layout_below="@+id/imgs"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:scaleType="centerCrop">
<ImageView
android:id="@+id/reel1_symbol2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel2_symbol2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="60dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel3_symbol2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="65dp"
android:layout_marginEnd="1dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel4_symbol2"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="55dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
</LinearLayout>
<!-- Third/Bottom Payline Row -->
<LinearLayout
android:id="@+id/imgs3"
android:layout_width="486dp"
android:layout_height="70dp"
android:layout_below="@+id/imgs2"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:scaleType="centerCrop">
<ImageView
android:id="@+id/reel1_symbol3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel2_symbol3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="60dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel3_symbol3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="65dp"
android:layout_marginEnd="1dp"
android:src="@drawable/cherryblossom_cat" />
<ImageView
android:id="@+id/reel4_symbol3"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="55dp"
android:layout_marginEnd="0dp"
android:src="@drawable/cherryblossom_cat" />
</LinearLayout>
<ImageView
android:id="@+id/cherryblossomslot_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:src="@drawable/cherryblossumslots_frameboarder2" />
<ImageView
android:id="@+id/top_bar"
android:layout_width="match_parent"
android:layout_height="118dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:src="@drawable/top_banner" />
<ImageView
android:id="@+id/bottom_bar"
android:layout_width="865dp"
android:layout_height="79dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="@drawable/slots_spinbar" />
<LinearLayout
android:id="@+id/buttonsLayout"
android:layout_width="558dp"
android:layout_height="53dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="@+id/top_bar"
android:layout_marginTop="6dp">
<TextView
android:id="@+id/usernameTextView"
android:layout_width="197dp"
android:layout_height="match_parent"
android:background="@drawable/empty_bar"
android:gravity="center"
android:paddingStart="16dp"
android:paddingEnd="16dp"
android:text="Username"
android:textColor="@android:color/white"
android:textSize="19sp" />
<TextView
android:id="@+id/coin_balance"
android:layout_width="183dp"
android:layout_height="match_parent"
android:background="@drawable/coin_balance_bar"
android:gravity="center"
android:text="coinbalance"
android:textColor="@android:color/white"
android:textSize="19sp" />
<TextView
android:id="@+id/star_balance"
android:layout_width="183dp"
android:layout_height="wrap_content"
android:background="@drawable/star_balance_bar"
android:gravity="center"
android:text="starbalance"
android:textColor="@android:color/white"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottombuttonsLayout"
android:layout_width="181dp"
android:layout_height="74dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="@+id/bottom_bar">
<ImageView
android:id="@+id/spinbutton"
android:layout_width="183dp"
android:layout_height="match_parent"
android:gravity="center"
android:src="@drawable/spin_button" />
</LinearLayout>
<!-- Other UI elements can be added on top of the background if needed -->
<TextView
android:id="@+id/totalbidTextView"
android:layout_width="213dp"
android:layout_height="83dp"
android:layout_alignParentBottom="true"
android:background="@drawable/totalbid_bar"
android:gravity="center"
android:text="totalbid"
android:textColor="@android:color/white"
android:textSize="18sp" />
<ImageView
android:id="@+id/raisebid"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/totalbid_bar_plus"
android:gravity="center"
android:layout_marginStart="160dp"
android:layout_marginBottom="9dp"/>
<ImageView
android:id="@+id/lowerbid"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:background="@drawable/totalbidbar_minus"
android:gravity="center"
android:layout_marginBottom="9dp"/>
</RelativeLayout>
There are 8 different symbols that I will have spin. They are doing the spinning animation currently, so I'm happy there. I mean the animation isn't perfect, but for my first slot machine, I'm happy enough. However it's not recognizing any winning patterns. I think it's because the way I tried to code it, but I can't figure out how to fix it... If someone can fix this code for me and show me what I did wrong, I'd greatly appreciate it! TIA! <3 If you need more info, just me know!
I've tried to alter this section multiple times:
for (pattern in winningPatterns) {
var matchCount = 0
for (i in pattern.indices) {
var matchFound = true
for (j in pattern[i].indices) {
if (translatedFinalSymbols[i][j] != pattern[i][j]) {
matchFound = false
break
}
}
if (matchFound) {
matchCount++
}
}
if (matchCount == pattern.size) {
return calculatePayout(pattern[0]) // Payout based on the first symbol of the pattern
}
}
// Return 0 if no winning combination is found
return 0
}
I think this is where my problem is, but I can't figure it out.