DouglasQ DouglasQ - 2 months ago 9
Android Question

Building a Martingale Apps, and the code is just now working, didn't know why

Good Day, i'm a just a beginner to android & java. i'm trying to build an app the will tell you if you win or lose base on the Martingale on gambling.

My concept is this, you can set your money, target and the minimum bet.

for example is i set my current money is 1000, and my target is to get 1100, and the minimum bet is 100, the app will show me whether i win or not.

(actually my idea was to build this app to calculate the win rate for this, but i didn't know how, therefore i just try to build the simpler app first and further modify if i can get it.)

but when i try to run the app, the app keeps showing "You Lose", even i try to create a situation that i have really low chance to lose, still it show me "You Lose"

My Java Code Here

package com.example.android.gambling;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void seeRate(View view) {

EditText cMoney = (EditText) findViewById(R.id.money);
double currentMoney = Double.parseDouble(cMoney.getText().toString());

EditText target = (EditText) findViewById(R.id.target);
double theTarget = Double.parseDouble(target.getText().toString());

EditText bet = (EditText) findViewById(R.id.bet);
double minBet = Double.parseDouble(bet.getText().toString());

String findRate = calRate(currentMoney, theTarget, minBet);

TextView textview = (TextView)findViewById(R.id.textView);
textview.setText("You " + findRate);



}

public String calRate(double currentMoney, double theTarget, double minBet) {

while (currentMoney>minBet){
boolean win = winRate();
if (win){
currentMoney += minBet;
minBet = minBet;
}
else {
currentMoney -= minBet;
minBet *= 2;
}
if (currentMoney>=theTarget){
break;
}

}

return "Lose";
}

private boolean winRate() {
double d = Math.random();
if (d < 0.5)
return true;
else
return false;

}
}


and the xml is here

<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android.gambling.MainActivity">

<EditText
android:id="@+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:hint="Current Money"
android:width="300dp"/>

<EditText
android:id="@+id/target"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/money"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:hint="Target"
android:width="300dp"/>

<EditText
android:id="@+id/bet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/target"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp"
android:hint="Minimun Bet per Time"
android:width="300dp"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Check Rate"
android:id="@+id/button"
android:layout_centerHorizontal="true"
android:onClick="seeRate"
android:layout_marginTop="32dp"
android:layout_below="@+id/bet" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Result Here"
android:id="@+id/textView"
android:layout_alignParentBottom="true"
android:layout_alignEnd="@+id/button"
android:layout_marginBottom="60dp" />


</RelativeLayout>

Answer

Change your code:

public String calRate(double currentMoney, double theTarget, double minBet) {
    while (currentMoney > minBet) {
        boolean win = winRate();
        if (win) {
            currentMoney += minBet;
        } else {
            currentMoney -= minBet;
        }
        if (currentMoney >= theTarget) {
            return "Won";
        }
    }
    return "Lose";
}