Skip to content
This repository has been archived by the owner on Jan 31, 2024. It is now read-only.

Margaux/setup-to-gamescreen-connection #105

Merged
merged 16 commits into from
Mar 30, 2022
30 changes: 30 additions & 0 deletions app/src/androidTest/java/ch/sdp/vibester/BuzzerScoreUpdaterTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package ch.sdp.vibester

import org.hamcrest.CoreMatchers.equalTo
import org.hamcrest.MatcherAssert.assertThat
import org.junit.Test

public class BuzzerScoreUpdaterTest {

// FIXME: Uncomment this when the infinite loop issue in the class is fixed
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The infinite loop problem! you forgot to increment i in your buzzerCode:

var i = 0
while (i < scores.size) {
theMap.put(ids[i], scores[i])
}

Happens to all of us!!! I hope it helped you!

/*
@Test
fun constructorTestWithSameSizedArrays() {
val idArray = arrayOf(0, 1, 2, 3)
val scoreArray = arrayOf(0, 0, 0, 0)
val expectedMap = LinkedHashMap<Int, Int>()
expectedMap.put(0, 0)
expectedMap.put(1, 0)
expectedMap.put(2, 0)
expectedMap.put(3, 0)
val testUpdater = BuzzerScoreUpdater(scoreArray, idArray)
assert(testUpdater.getMap()==(expectedMap)) // value or reference?
}

@Test
fun arrayUpdateTest() {

}

*/
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class GameSetupActivityTest {
fun checkIntentOnProceedDefault() { //FILLER TEST
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(GamescreenActivity::class.java.name))
intended(hasExtra("Number of players", "One"))
intended(hasExtra("Number of players", 1))
}

@Test
Expand All @@ -86,7 +86,7 @@ class GameSetupActivityTest {
onData(Matchers.anything()).atPosition(0).perform(click())
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(GamescreenActivity::class.java.name))
intended(hasExtra("Number of players", "One"))
intended(hasExtra("Number of players", 1))
}

@Test
Expand All @@ -95,7 +95,7 @@ class GameSetupActivityTest {
onData(Matchers.anything()).atPosition(1).perform(click())
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(GamescreenActivity::class.java.name))
intended(hasExtra("Number of players", "Two"))
intended(hasExtra("Number of players", 2))
}

@Test
Expand All @@ -104,7 +104,7 @@ class GameSetupActivityTest {
onData(Matchers.anything()).atPosition(2).perform(click())
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(GamescreenActivity::class.java.name))
intended(hasExtra("Number of players", "Three"))
intended(hasExtra("Number of players", 3))
}

@Test
Expand All @@ -113,6 +113,7 @@ class GameSetupActivityTest {
onData(Matchers.anything()).atPosition(3).perform(click())
onView(withId(R.id.nb_players_selected)).perform(click())
intended(hasComponent(GamescreenActivity::class.java.name))
intended(hasExtra("Number of players", "Four"))
intended(hasExtra("Number of players", 4))
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,30 +50,26 @@ class GamescreenActivityTest {
fun answerIsPresentButInvisibleOnStartup() {
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE)))
}

/*
@Test
fun clickingBuzzerMakesAnswerVisible() {
var i = 0
while (i < 4) {
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE)))
onView(withId(i)).perform(click())
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
i = i + 1
onView(withId(R.id.buttonCorrect)).perform(click())
}
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE)))
onView(withId(R.id.buzzer_0)).perform(click()) // why does it not find the buzzer???
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.VISIBLE)))
onView(withId(R.id.buttonWrong)).perform(click())
}


@Test
fun clickingAnswerButtonsMakesAnswerInvisible() {
val buttonIdArray = arrayOf(R.id.buttonCorrect, R.id.buttonWrong)
for (butId in buttonIdArray) {
onView(withId(0)).perform(click()) // make answer visible first
onView(withId(R.id.buzzer_0)).perform(click()) // make answer visible first
onView(withId(butId)).perform(click())
onView(withId(R.id.answer)).check(matches(withEffectiveVisibility(Visibility.INVISIBLE)))
}
}


*/

/*
* Currently testing with the *static* values. Change to *dynamic* once the game is correctly
Expand Down
55 changes: 51 additions & 4 deletions app/src/main/java/ch/sdp/vibester/activity/GameSetupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package ch.sdp.vibester.activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import android.widget.*
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.children
import ch.sdp.vibester.R

class GameSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {
Expand All @@ -29,13 +28,61 @@ class GameSetupActivity : AppCompatActivity(), AdapterView.OnItemSelectedListene

override fun onItemSelected(parent: AdapterView<*>, view: View?, position: Int, id: Long) {
text = parent.getItemAtPosition(position).toString()
updatePlayerNameVisibility(textToNumber(text), R.id.namePlayer2)
updatePlayerNameVisibility(textToNumber(text), R.id.namePlayer3)
updatePlayerNameVisibility(textToNumber(text), R.id.namePlayer4)
// update linear layout's visibility, add linear layout with certain visible number of rows
// or just make 4 rows at first and update that later
}

/**
* Converts the spinner text for the number of players into an Int
* @param
* text: the string to be converted
*/
fun textToNumber(text: String): Int {
when(text) {
"One" -> return 1
"Two" -> return 2
"Three" -> return 3
"Four" -> return 4
}
return 1
}

override fun onNothingSelected(parent: AdapterView<*>) {text = "One"}

/**
* Updates visibility of player name entry fields according to number of players selected in the spinner
* @param
* n: number of players selected in the spinner
* id: the id of the field to update
*/
fun updatePlayerNameVisibility(n: Int, id: Int) {
var i = when(id) {
R.id.namePlayer2 -> 2
R.id.namePlayer3 -> 3
R.id.namePlayer4 -> 4
else -> 0
}
findViewById<EditText>(id).visibility = if (n>=i) android.view.View.VISIBLE else android.view.View.INVISIBLE
}

fun proceedToGame(view: View) { //FILLER INTENT
val intent = Intent(this, GamescreenActivity::class.java)
intent.putExtra("Number of players", text)
//intent.putExtra("Number of players", text)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
//intent.putExtra("Number of players", text)

val players = findViewById<LinearLayout>(R.id.playerNames).children.filter { child: View -> child.visibility==android.view.View.VISIBLE }
val pNameArray = arrayOfNulls<String>(players.count())
if (players.count()>0) {
intent.putExtra("Number of players", players.count())
} else {intent.putExtra("Number of players", 1)}
val editTextIdArray = arrayOf(R.id.namePlayer1, R.id.namePlayer2, R.id.namePlayer3, R.id.namePlayer4)
var i = 0
for (playerView in players) {
pNameArray[i] = findViewById<EditText>(editTextIdArray[i]).text.toString()
i = i + 1
}
intent.putExtra("Player Names", pNameArray)
startActivity(intent)
}
}
36 changes: 21 additions & 15 deletions app/src/main/java/ch/sdp/vibester/activity/GamescreenActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,25 @@ class GamescreenActivity: AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_gamescreen)

/* number of players for the game
currently hardcoded as a placeholder
will be retrieved from intent launched from the game setup screen
*/
val players = arrayOf("Kamila", "Jiabao", "Arda", "Laurynas")
val getIntent = intent.extras
val nPlayers = getIntent?.getInt("Number of players")

val answer = findViewById<LinearLayout>(R.id.answer)
val answerText = findViewById<TextView>(R.id.answerText)
answerText.text= "The song was Demo by The Placeholders"

// hardcoded test values
val song = "Demo"
val artist = "The Placeholders"
val allPoints = nPlayers?.let { Array<Int>(it, { i -> 0 }) }

answerText.text= "The song was $song by $artist"
val playersFull = getIntent?.getStringArray("Player Names")
val players = nPlayers?.let { playersFull?.copyOfRange(0, it) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move getting intent extras at one place. Is there a reason why you copy from playerfull to players? I would assume the lengthof player names is according to number of players


val allPoints = arrayOf(1, 2, 3, 4)
println(nPlayers)
print(players) // test
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove these print statements

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, they're test statements I forgot to remove. Will do!


buildScores(players, allPoints)
buildBuzzers(players, answer)
val buzIds = players?.let { fetchBuzIdArray(it.size) }

if (players != null && allPoints != null) { buildScores(players, allPoints) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for buildScore function: I am not sure if you need allPoints here, since it is initialization and it is always 0

if (players != null && buzIds != null) { buildBuzzers(players, buzIds, answer) }
setAnswerButton(answer, findViewById(R.id.buttonCorrect))
setAnswerButton(answer, findViewById(R.id.buttonWrong))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe instead of setting setAnswerButton function for buttons here, you can set them in xml file. For each button have android:onclick and set setAnswerButton function there.

}
Expand Down Expand Up @@ -75,12 +75,19 @@ class GamescreenActivity: AppCompatActivity() {

i = i + 1
}

}

private fun fetchBuzIdArray(size: Int): Array<Int> {
var array = arrayOf(R.id.buzzer_0, R.id.buzzer_1, R.id.buzzer_2, R.id.buzzer_3, R.id.buzzer_4, R.id.buzzer_5) // replace magic number here!
return array.copyOfRange(0, size) // is "size" index included or not
}

/*
Programmatically builds the buzzers according to the number and names of players.
*/
private fun buildBuzzers(players: Array<String>, answer: LinearLayout) {
// the correct number of buzzers are built but the names don't show up...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The names are displayed correctly in my screen:
image

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to remove this comment after fixing that issue. 😅 It's all good now

private fun buildBuzzers(players: Array<String>, buzIds: Array<Int>, answer: LinearLayout) {

val buzzers = findViewById<LinearLayout>(R.id.buzzersLayout)
val buttons = arrayOfNulls<Button>(players.size)
Expand All @@ -90,7 +97,7 @@ class GamescreenActivity: AppCompatActivity() {
for (pName in players) {

val button = Button(this)
button.id = i
button.id = buzIds[i]
button.text = pName
button.width = 100
button.height = 0
Expand Down Expand Up @@ -139,5 +146,4 @@ class GamescreenActivity: AppCompatActivity() {

startActivity(intent)
}

}
49 changes: 49 additions & 0 deletions app/src/main/res/layout/activity_game_setup_screen.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,53 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nb_player_spinner"
app:layout_constraintVertical_bias="1.0" />

<LinearLayout
android:id="@+id/playerNames"
android:layout_width="0dp"
android:layout_height="231dp"
android:layout_marginStart="1dp"
android:layout_marginEnd="1dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/nb_player_spinner">

<EditText
android:id="@+id/namePlayer1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name of Player 1" />

<EditText
android:id="@+id/namePlayer2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name of Player 2"
android:visibility="invisible"/>

<EditText
android:id="@+id/namePlayer3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name of Player 3"
android:visibility="invisible"/>

<EditText
android:id="@+id/namePlayer4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:hint="Name of Player 4"
android:visibility="invisible"/>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
9 changes: 9 additions & 0 deletions app/src/main/res/values/buzIds.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="buzzer_0" type="id"/>
<item name="buzzer_1" type="id"/>
<item name="buzzer_2" type="id"/>
<item name="buzzer_3" type="id"/>
<item name="buzzer_4" type="id"/>
<item name="buzzer_5" type="id"/>
</resources>