Last active
June 25, 2022 14:08
-
-
Save LarsEckart/c4aa8c02d319bc4eecb5b6a584d25623 to your computer and use it in GitHub Desktop.
Chen Formula App
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.chenformula; | |
import java.util.HashMap; | |
import java.util.Map; | |
public class ChenCalculator { | |
public static final Map<String, Double> POINTS_BY_CARD = new HashMap<>(); | |
static { | |
POINTS_BY_CARD.put("A", 10.0); | |
POINTS_BY_CARD.put("K", 8.0); | |
POINTS_BY_CARD.put("Q", 7.0); | |
POINTS_BY_CARD.put("J", 6.0); | |
POINTS_BY_CARD.put("10", 5.0); | |
POINTS_BY_CARD.put("9", 4.5); | |
POINTS_BY_CARD.put("8", 4.0); | |
POINTS_BY_CARD.put("7", 3.5); | |
POINTS_BY_CARD.put("6", 3.0); | |
POINTS_BY_CARD.put("5", 2.5); | |
POINTS_BY_CARD.put("4", 2.0); | |
POINTS_BY_CARD.put("3", 1.5); | |
POINTS_BY_CARD.put("2", 1.0); | |
} | |
public static int calculate(String firstCard, String secondCard, boolean suited) { | |
double result = 0.0; | |
result += scoreHighestCardOnly(firstCard, secondCard); | |
if (isPair(firstCard, secondCard)) { | |
// what if less than 5!! | |
result = result * 2; | |
} | |
if (suited) { | |
result += 2; | |
} | |
result = result - pointsIfGapBetweenCards(firstCard, secondCard); | |
result += bonusPointIf0or1CardGapAndBothCardsLowerThanQ(firstCard, secondCard); | |
return Math.toIntExact(Math.round(result)); | |
} | |
private static double scoreHighestCardOnly(String firstCard, String secondCard) { | |
String higherCard = determineHigherCard(firstCard, secondCard); | |
return POINTS_BY_CARD.get(higherCard); | |
} | |
private static String determineHigherCard(String firstCard, String secondCard) { | |
String higherCard; | |
if (POINTS_BY_CARD.get(firstCard) > POINTS_BY_CARD.get(secondCard)) { | |
higherCard = firstCard; | |
} else { | |
higherCard = secondCard; | |
} | |
return higherCard; | |
} | |
private static boolean isPair(String firstCard, String secondCard) { | |
return firstCard.equals(secondCard); | |
} | |
private static int pointsIfGapBetweenCards(String firstCard, String secondCard) { | |
int higherCardNumber; | |
int lowerCardNumber; | |
if (determineHigherCard(firstCard, secondCard).equals(firstCard)) { | |
higherCardNumber = getCardValue(firstCard); | |
lowerCardNumber = getCardValue(secondCard); | |
} else { | |
higherCardNumber = getCardValue(secondCard); | |
lowerCardNumber = getCardValue(firstCard); | |
} | |
if (firstCard.equals(secondCard)) { | |
return 0; | |
} | |
int gap = higherCardNumber - lowerCardNumber - 1; | |
if (gap == 1) { | |
return 1; | |
} | |
if (gap == 2) { | |
return 2; | |
} | |
if (gap == 3) { | |
return 4; | |
} | |
if (gap >= 4) { | |
return 5; | |
} | |
return gap; | |
} | |
private static int getCardValue(String card) { | |
if ("J".equals(card)) { | |
return 11; | |
} | |
if ("Q".equals(card)) { | |
return 12; | |
} | |
if ("K".equals(card)) { | |
return 13; | |
} | |
if ("A".equals(card)) { | |
return 14; | |
} | |
return Integer.parseInt(card); | |
} | |
private static int bonusPointIf0or1CardGapAndBothCardsLowerThanQ(String firstCard, String secondCard) { | |
if (pointsIfGapBetweenCards(firstCard, secondCard) <= 1 | |
&& getCardValue(firstCard) < 12 | |
&& getCardValue(secondCard) < 12) { | |
return 1; | |
} | |
return 0; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<LinearLayout android:layout_height="match_parent" | |
android:layout_width="match_parent" | |
android:orientation="vertical" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<TextView | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:text="@string/headline" | |
android:textSize="30sp" | |
android:layout_margin="10dp" | |
android:gravity="center"/> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_margin="5sp"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:layout_margin="5sp"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/card_1_big" | |
android:textSize="25sp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/card_1_small" | |
android:textSize="15sp"/> | |
</LinearLayout> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_gravity="center" | |
android:hint="@string/card_value_hint" | |
android:id="@+id/card1" | |
android:autofillHints="2...A"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_margin="5sp"> | |
<LinearLayout | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:orientation="vertical" | |
android:layout_margin="5sp"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/card_2_big" | |
android:textSize="25sp"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/card_2_small" | |
android:textSize="15sp"/> | |
</LinearLayout> | |
<EditText | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
android:layout_gravity="center" | |
android:hint="@string/card_value_hint" | |
android:id="@+id/card2" | |
android:autofillHints="2...A"/> | |
</LinearLayout> | |
<LinearLayout | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:orientation="horizontal" | |
android:layout_margin="5sp"> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/suited" | |
android:textSize="25sp"/> | |
<CheckBox | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:id="@+id/suitedCheckbox"/> | |
</LinearLayout> | |
<Button | |
android:id="@+id/calculateButton" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="@string/calculate" | |
android:layout_gravity="center"/> | |
<TextView | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:text="5" | |
android:id="@+id/result" | |
android:textSize="25sp" | |
android:layout_margin="10sp" | |
android:layout_gravity="center"/> | |
</LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.chenformula; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.widget.Button; | |
import android.widget.CheckBox; | |
import android.widget.EditText; | |
import android.widget.TextView; | |
import androidx.appcompat.app.AppCompatActivity; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Button button = findViewById(R.id.calculateButton); | |
button.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
EditText card1EditText = findViewById(R.id.card1); | |
String card1Content = card1EditText.getText().toString(); | |
EditText card2EditText = findViewById(R.id.card2); | |
String card2Content = card2EditText.getText().toString(); | |
List<String> zugelasseneZahlen = createListForValidInputs(); | |
if (!zugelasseneZahlen.contains(card1Content)) { | |
card1EditText.setError("invalid!!!!"); | |
return; | |
} | |
if (!zugelasseneZahlen.contains(card2Content)) { | |
card2EditText.setError("invalid!!!!"); | |
return; | |
} | |
CheckBox checkbox = findViewById(R.id.suitedCheckbox); | |
boolean suited = checkbox.isChecked(); | |
int result = ChenCalculator.calculate(card1Content, card2Content, suited); | |
TextView resultTextView = findViewById(R.id.result); | |
resultTextView.setText(String.valueOf(result)); | |
} | |
}); | |
} | |
private List<String> createListForValidInputs() { | |
List<String> result = new ArrayList<>(); | |
result.add("2"); | |
result.add("3"); | |
result.add("4"); | |
result.add("5"); | |
result.add("6"); | |
result.add("7"); | |
result.add("8"); | |
result.add("9"); | |
result.add("10"); | |
result.add("J"); | |
result.add("Q"); | |
result.add("K"); | |
result.add("A"); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment