Skip to content

Commit

Permalink
Merge branch 'devel'
Browse files Browse the repository at this point in the history
  • Loading branch information
Wiljami committed Mar 17, 2020
2 parents 23e9c48 + ac8922f commit c8da0af
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
</activity>
<activity android:name=".AchievementActivity"/>
<activity android:name=".Game"/>
<activity android:name=".AnswerResultActivity"/>
</application>

</manifest>
39 changes: 39 additions & 0 deletions app/src/main/java/fi/tuni/parsta/AnswerResultActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package fi.tuni.parsta;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;


import androidx.appcompat.app.AppCompatActivity;

public class AnswerResultActivity extends AppCompatActivity {
TextView pepTalk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_answer);
pepTalk = (TextView) findViewById(R.id.answerText);
Bundle extras = getIntent().getExtras();
boolean wasAnswerRight;
if (extras != null) {
wasAnswerRight = extras.getBoolean("wasAnswerRight");
if(wasAnswerRight) {
pepTalk.setText("v jes hyvä sinä");
} else {
pepTalk.setText("parempi onni ensi kerralla (:");
}
}



}

public void nextClick(View view) {
Intent gameIntent = new Intent(this, Game.class);
startActivity(gameIntent);
}
}
49 changes: 46 additions & 3 deletions app/src/main/java/fi/tuni/parsta/Game.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package fi.tuni.parsta;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.os.Bundle;
Expand All @@ -10,6 +12,7 @@
import android.widget.Button;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
Expand Down Expand Up @@ -41,9 +44,14 @@ public class Game extends AppCompatActivity {
int level = 1;
ImageView questionImg;
GridLayout gridLayout;
TextView currentWins;
//Temporary
Boolean firstRound = true;
ArrayList<Button> buttonList = new ArrayList<>();
int rightAnswersInt = 0;
int clicks = 0;
String rightAnswerString;
SharedPreferences sharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
Expand All @@ -55,7 +63,15 @@ protected void onCreate(Bundle savedInstanceState) {
for(GameImage i : images) {
Log.d("GAMEIMAGETAG",i.name);
}

currentWins = (TextView) findViewById(R.id.currentWins);
sharedPreferences = getSharedPreferences("progress", MODE_PRIVATE);
clicks = sharedPreferences.getInt("clicks", 0);
rightAnswersInt = sharedPreferences.getInt("wins", 0);
currentWins.setText("Oikein: " + rightAnswersInt);
gameLoop();


}

protected void gameLoop() {
Expand All @@ -65,7 +81,14 @@ protected void gameLoop() {
int rndImage = Util.random(0, (images.length - 1));
GameImage newQuestion = images[rndImage];
String questionImgName = newQuestion.getName();
String rightAnswerString = questionImgName.substring(4);

//Edit questionImgName string (for answer button)
rightAnswerString = questionImgName.substring(5);
int firstUnderscorePosition = rightAnswerString.indexOf('_');
rightAnswerString = rightAnswerString.substring(0, firstUnderscorePosition);
Log.d("GAMEIMAGE", String.valueOf(firstUnderscorePosition));


Log.d("GAMEIMAGE", rightAnswerString);

//Set image to image view
Expand Down Expand Up @@ -152,11 +175,31 @@ public int getAnswerOptionsAmount(int level){

public void createButtonGrid(ArrayList<String> answerOptions){
for (int i = 0; i<answerOptions.size(); i++) {
Button myButton = new Button(this);
final Button myButton = new Button(this);
myButton.setWidth(900);
myButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
gameLoop();
// gameLoop();
SharedPreferences.Editor editor = sharedPreferences.edit();
if(rightAnswerString.contains(myButton.getText().toString())) {
rightAnswersInt++;
clicks++;
editor.putInt("clicks", clicks);
editor.putInt("wins", rightAnswersInt);
editor.apply();
currentWins.setText("Oikein: " + rightAnswersInt);
Intent gameIntent = new Intent(getApplication(), AnswerResultActivity.class);
gameIntent.putExtra("wasAnswerRight",true);
startActivity(gameIntent);
} else {
clicks++;
editor.putInt("clicks", clicks);
editor.apply();
Intent gameIntent = new Intent(getApplication(), AnswerResultActivity.class);
gameIntent.putExtra("wasAnswerRight",false);
startActivity(gameIntent);
}
}
});
myButton.setText(answerOptions.get(i));
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/fi/tuni/parsta/Util.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ public static boolean stringArrayContains(String[] arr, String targetValue) {
}
return false;
}

}
18 changes: 18 additions & 0 deletions app/src/main/res/layout/activity_answer.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/answerText"/>

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Seuraava"
android:onClick="nextClick"
android:id="@+id/start"
/>
</LinearLayout>
4 changes: 4 additions & 0 deletions app/src/main/res/layout/activity_game.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Mitä tunnetta ilme kuvastaa?"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/currentWins"/>

<ImageView
android:id="@+id/questionImg"
Expand Down

0 comments on commit c8da0af

Please sign in to comment.