Skip to content
Snippets Groups Projects
Commit 35aa3711 authored by MAUGEZ Emeline's avatar MAUGEZ Emeline
Browse files

Génération aléatoire de cocktail (quantités et ingrédients)

parent e42c5997
No related branches found
No related tags found
No related merge requests found
package com.example.robotise.control;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import com.bluehomestudio.luckywheel.LuckyWheel;
import com.bluehomestudio.luckywheel.OnLuckyWheelReachTheTarget;
import com.bluehomestudio.luckywheel.WheelItem;
import com.example.robotise.R;
import com.example.robotise.model.IngredientItem;
import com.example.robotise.model.Ingredients;
import java.util.ArrayList;
import java.util.Arrays;
public class WheelActivity extends AppCompatActivity {
public Button mCommander;
public Button mRecommencer;
public TextView mIngredient1;
public TextView mIngredient2;
public TextView mIngredient3;
public ArrayList<TextView> quantites;
public int actualTarget;
public ArrayList<IngredientItem> toOrder;
public int quantiteRestante = 25;
@RequiresApi(api = Build.VERSION_CODES.P)
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.roue_activity);
setContentView(R.layout.wheel_activity);
LuckyWheel lw = (LuckyWheel) findViewById(R.id.lucky_wheel);
lw.setOutlineSpotShadowColor(ResourcesCompat.getColor(getResources(), R.color.purple_700, null));
lw.setOutlineAmbientShadowColor(ResourcesCompat.getColor(getResources(), R.color.purple_700, null));
lw.setDrawingCacheBackgroundColor(ResourcesCompat.getColor(getResources(), R.color.purple_700, null));
ArrayList<WheelItem> wheelItems = new ArrayList<>();
wheelItems.add(new WheelItem(Color.LTGRAY,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name), "text 1"));
wheelItems.add(new WheelItem(Color.BLUE,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name),
"text 2"));
wheelItems.add(new WheelItem(Color.BLACK,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name),
"text 3"));
wheelItems.add(new WheelItem(Color.GRAY,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name)
, "text 4"));
wheelItems.add(new WheelItem(Color.RED,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name),
"text 5"));
wheelItems.add(new WheelItem(Color.BLACK,
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name),
"text 6"));
int purple200 = ResourcesCompat.getColor(getResources(), R.color.purple_200, null);
int purple500 = ResourcesCompat.getColor(getResources(), R.color.purple_500, null);
ArrayList<Integer> colors = new ArrayList<Integer>(Arrays.asList(purple200, purple500));
ArrayList<IngredientItem> ings= Ingredients.getInstance().ings;
for(int i=0;i< ings.size();i++){
WheelItem wi = new WheelItem(colors.get(i%2),
BitmapFactory.decodeResource(getResources(), com.bluehomestudio.luckywheel.R.drawable.ic_action_name), ings.get(i).name);
wheelItems.add(wi);
}
mCommander = findViewById(R.id.wheel_activity_button_commander);
mCommander.setEnabled(false);
mCommander.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String msg = "";
String affichage = "Vous avez commander : ";
for(IngredientItem ingredientItem:toOrder){
int quantiteIngredient;
if (toOrder.indexOf(ingredientItem)==2){
quantiteIngredient=quantiteRestante;
}
else{
quantiteIngredient = randomQuantity(quantiteRestante);
}
quantiteRestante -= quantiteIngredient;
msg+="-1,"+ingredientItem.id+","+quantiteIngredient+"/";
affichage+=quantiteIngredient+"cl de "+ingredientItem.name+", ";
}
msg = msg.substring(0, msg.length()-1);
Toast.makeText(getApplicationContext(), affichage, Toast.LENGTH_LONG).show();
}
});
mRecommencer=findViewById(R.id.wheel_activity_button_recommencer);
mRecommencer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
recommencer();
}
});
mIngredient1 = findViewById(R.id.ingredient1);
mIngredient2 = findViewById(R.id.ingredient2);
mIngredient3 = findViewById(R.id.ingredient3);
quantites.add(findViewById(R.id.quantite1));
quantites.add(findViewById(R.id.quantite2));
quantites.add(findViewById(R.id.quantite3));
toOrder=new ArrayList<>();
lw.addWheelItems(wheelItems);
lw.setTarget(3);
actualTarget = (int)(Math.random()*(ings.size())+1);
lw.setTarget(actualTarget);
lw.setLuckyWheelReachTheTarget(new OnLuckyWheelReachTheTarget() {
@Override
public void onReachTarget() {
lw.setTarget((int)(Math.random()*(5)+1));
String nouvelIngredient = wheelItems.get(actualTarget-1).text;
if (mIngredient1.getText()==""){
mIngredient1.setText(nouvelIngredient);
int quantiteIngredient = randomQuantity(quantiteRestante);
quantiteRestante-=quantiteIngredient;
quantites.get(0).setText(""+quantiteIngredient);
}
else if (mIngredient2.getText()==""){
mIngredient2.setText(nouvelIngredient);
int quantiteIngredient = randomQuantity(quantiteRestante);
quantiteRestante-=quantiteIngredient;
quantites.get(1).setText(""+quantiteIngredient);
}
else if (mIngredient3.getText()==""){
mIngredient3.setText(nouvelIngredient);
mCommander.setEnabled(true);
int quantiteIngredient = quantiteRestante;
quantites.get(2).setText(""+quantiteIngredient);
}
else{
recommencer();
mIngredient1.setText(nouvelIngredient);
int quantiteIngredient = randomQuantity(quantiteRestante);
quantiteRestante-=quantiteIngredient;
quantites.get(0).setText(""+quantiteIngredient);
}
toOrder.add(ings.get(actualTarget-1));
actualTarget = (int)(Math.random()*(ings.size())+1);
lw.setTarget(actualTarget);
}
});
}
public int randomQuantity(int quantite){
return (int) Math.floor(Math.random()*quantite);
}
public void recommencer(){
mIngredient1.setText("");
mIngredient2.setText("");
mIngredient3.setText("");
mCommander.setEnabled(false);
toOrder = new ArrayList<>();
}
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:LuckyWheel="http://schemas.android.com/tools">
<com.bluehomestudio.luckywheel.LuckyWheel
android:id="@+id/lucky_wheel"
android:layout_width="290dp"
android:layout_height="290dp"
android:layout_centerInParent="true"
LuckyWheel:background_color="@color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:LuckyWheel="http://schemas.android.com/tools">
<com.google.android.material.button.MaterialButton
android:id="@+id/wheel_activity_button_recommencer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:theme="@style/Widget.Material3.Button.ElevatedButton"
android:text="Recommencer"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/lucky_wheel"/>
<com.bluehomestudio.luckywheel.LuckyWheel
android:id="@+id/lucky_wheel"
android:layout_width="290dp"
android:layout_height="290dp"
android:layout_centerInParent="true"
LuckyWheel:background_color="@color/purple_200"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="@+id/wheel_activity_grid_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="40dp"
android:textColor="@color/black"
android:orientation="horizontal"
app:layout_constraintTop_toBottomOf="@+id/lucky_wheel"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toTopOf="@+id/wheel_activity_button_commander">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginHorizontal="20dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text = "Ingredients : "
android:textColor="@color/black"/>
<TextView
android:id="@+id/ingredient1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text = ""
android:textColor="@color/black"/>
<TextView
android:id="@+id/ingredient2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text = ""
android:textColor="@color/black"/>
<TextView
android:id="@+id/ingredient3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text = ""
android:textColor="@color/black"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginHorizontal="20dp">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text = "Quantité : "
android:textColor="@color/black"/>
<TextView
android:id="@+id/quantite1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text = ""
android:textColor="@color/black"/>
<TextView
android:id="@+id/quantite2"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text = ""
android:textColor="@color/black"/>
<TextView
android:id="@+id/quantite3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_row="1"
android:layout_column="0"
android:text = ""
android:textColor="@color/black"/>
</LinearLayout>
</LinearLayout>
<com.google.android.material.button.MaterialButton
android:id="@+id/wheel_activity_button_commander"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text = "Commander"
android:theme="@style/Widget.Material3.Button.ElevatedButton"
app:layout_constraintTop_toBottomOf="@+id/wheel_activity_grid_layout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment