-
Notifications
You must be signed in to change notification settings - Fork 0
Margaux/setup-to-gamescreen-connection #105
Changes from 15 commits
e939f16
c096e66
c21c210
94a2b92
044d504
a74cde7
7ea3f57
d480eda
3b8357a
7780f7f
b718a6f
800f006
70d4ddf
68b7513
1174f2c
ffbe705
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
/* | ||
@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 | ||
---|---|---|---|---|
|
@@ -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 { | ||||
|
@@ -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) | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||
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) | ||||
} | ||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would remove these print statements There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} | ||
|
@@ -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... | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
|
@@ -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 | ||
|
@@ -139,5 +146,4 @@ class GamescreenActivity: AppCompatActivity() { | |
|
||
startActivity(intent) | ||
} | ||
|
||
} |
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> |
There was a problem hiding this comment.
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!