質問編集履歴

2

inputactivityとmainactivityの一部(共有プリファレンス、コンピューターの手の出し方)を追加しました。

2020/12/12 16:30

投稿

htnk_4a
htnk_4a

スコア3

test CHANGED
File without changes
test CHANGED
@@ -48,14 +48,86 @@
48
48
 
49
49
  ### 補足情報
50
50
 
51
+ ///inputactivity///
52
+
53
+
54
+
51
55
  package com.example.janken
52
56
 
53
57
 
54
58
 
59
+ import android.content.Intent
60
+
61
+ import androidx.appcompat.app.AppCompatActivity
62
+
55
63
  import android.os.Bundle
56
64
 
57
65
  import android.preference.PreferenceManager
58
66
 
67
+ import androidx.core.content.edit
68
+
69
+ import kotlinx.android.synthetic.main.activity_main.*
70
+
71
+ import android.view.View as View1
72
+
73
+
74
+
75
+ class MainActivity : AppCompatActivity() {
76
+
77
+
78
+
79
+ override fun onCreate(savedInstanceState: Bundle?) {
80
+
81
+ super.onCreate(savedInstanceState)
82
+
83
+ setContentView(R.layout.activity_main)
84
+
85
+
86
+
87
+ gu.setOnClickListener{ onJankenButtonTapped(it) }
88
+
89
+ choki.setOnClickListener { onJankenButtonTapped(it) }
90
+
91
+ pa.setOnClickListener { onJankenButtonTapped(it) }
92
+
93
+
94
+
95
+ val pref= PreferenceManager.getDefaultSharedPreferences(this)
96
+
97
+ pref.edit {
98
+
99
+ clear()
100
+
101
+ }
102
+
103
+ }
104
+
105
+ fun onJankenButtonTapped(view: View1?){
106
+
107
+ val intent = Intent(this, ResultActivity::class.java)
108
+
109
+ intent.putExtra("MY_HAND", view?.id)
110
+
111
+ startActivity(intent)
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ ///resultactivity///
120
+
121
+
122
+
123
+ package com.example.janken
124
+
125
+
126
+
127
+ import android.os.Bundle
128
+
129
+ import android.preference.PreferenceManager
130
+
59
131
  import androidx.appcompat.app.AppCompatActivity
60
132
 
61
133
  import androidx.core.content.edit
@@ -159,3 +231,119 @@
159
231
  saveData(myHand, comHand ,gameResult)
160
232
 
161
233
  }
234
+
235
+ private fun saveData(myHand: Int, comHand: Int, gameResult: Int) {
236
+
237
+ val pref = PreferenceManager.getDefaultSharedPreferences(this)
238
+
239
+ val gameCount = pref.getInt("GAME_COUNT", 0)
240
+
241
+ val winningStreakCount = pref.getInt("WINNING_STREAK_COUNT", 0)
242
+
243
+ val lastComHand = pref.getInt("LAST_COM_HAND", 0)
244
+
245
+ val lastGameResult = pref.getInt("GAME_RESULT", -1)
246
+
247
+
248
+
249
+ val edtWinningStreakCount: Int =
250
+
251
+ when {
252
+
253
+ lastGameResult == 2 && gameResult == 2 ->
254
+
255
+ winningStreakCount + 1
256
+
257
+ else ->
258
+
259
+ 0
260
+
261
+ }
262
+
263
+ val editor = pref.edit()
264
+
265
+ pref.edit {
266
+
267
+ putInt("GAME_COUNT", gameCount + 1)
268
+
269
+ putInt("WINNING_STREAK_COUNT", edtWinningStreakCount)
270
+
271
+ putInt("LAST_MY_HAND", myHand)
272
+
273
+ putInt("LAST_COM_HAND", comHand)
274
+
275
+ putInt("BEFORE_LAST_COM_HAND", lastComHand)
276
+
277
+ putInt("GAME_RESULT", gameResult)
278
+
279
+ }
280
+
281
+ }
282
+
283
+
284
+
285
+ private fun getHand(): Int {
286
+
287
+ var hand = (Math.random() * 3).toInt()
288
+
289
+ val pref = PreferenceManager.getDefaultSharedPreferences(this)
290
+
291
+ val gameCount = pref.getInt("GAME_COUNT", 0)
292
+
293
+ val winningStreakCount = pref.getInt("WINNING_STREAK_COUNT", 0)
294
+
295
+ val lastMyHand = pref.getInt("LAST_MY_HAND", 0)
296
+
297
+ val lastComHand = pref.getInt("LAST_COM_HAND", 0)
298
+
299
+ val beforeLastComHand = pref.getInt("BEFORE_LAST_COM_HAND", 0)
300
+
301
+ val gameResult = pref.getInt("GAME_RESULT", -1)
302
+
303
+
304
+
305
+ if (gameCount == 1) {
306
+
307
+ if (gameResult == 2) {
308
+
309
+ // 前回の勝負が1回目で、コンピュータが勝った場合、
310
+
311
+ // コンピュータは次に出す手を変える
312
+
313
+ while (lastComHand == hand) {
314
+
315
+ hand = (Math.random() * 3).toInt()
316
+
317
+ }
318
+
319
+ } else if (gameResult == 1) {
320
+
321
+ // 前回の勝負が1回目で、コンピュータが負けた場合
322
+
323
+ // 相手の出した手に勝つ手を出す
324
+
325
+ hand = (lastMyHand - 1 + 3) % 3
326
+
327
+ }
328
+
329
+ } else if (winningStreakCount > 0) {
330
+
331
+ if (beforeLastComHand == lastComHand) {
332
+
333
+ // 同じ手で連勝した場合は手を変える
334
+
335
+ while (lastComHand == hand) {
336
+
337
+ hand = (Math.random() * 3).toInt()
338
+
339
+ }
340
+
341
+ }
342
+
343
+ }
344
+
345
+ return hand
346
+
347
+ }
348
+
349
+ }

1

ボタンの表示を試みている

2020/12/12 16:30

投稿

htnk_4a
htnk_4a

スコア3

test CHANGED
File without changes
test CHANGED
@@ -4,7 +4,9 @@
4
4
 
5
5
 
6
6
 
7
- andoroid studio を使用しています。じゃんけんアプリを作成しているのですが繰り返し処理の書き方がわかりません。手付かずなのでどなたかよろしくお願いします。
7
+ andoroid studio を使用しています。じゃんけんアプリを作成しているのですが繰り返し処理の書き方がわかりません。
8
+
9
+ このアプリはじゃんけんを連続でできない仕組みになっているのですが「次へ」のボタンを設置し連続でじゃんけんをできるようにしたいです。
8
10
 
9
11
 
10
12
 
@@ -12,7 +14,7 @@
12
14
 
13
15
  ### 発生している問題・エラーメッセージ
14
16
 
15
-
17
+ nextButton.setOnClickListener{ }の{}の中身がどう書けばいいか分かりません。
16
18
 
17
19
  ```
18
20
 
@@ -39,6 +41,8 @@
39
41
 
40
42
 
41
43
  forやwhile文を試しましたがコードが間違っているのかうまくいきませんでした。
44
+
45
+ 「次へ」のボタンをレイアウトエディタで設置しましたがbackButtonのようにうまく表示できない
42
46
 
43
47
 
44
48
 
@@ -145,3 +149,13 @@
145
149
  2 -> resultLabel.setText(R.string.result_lose) // 負けた場合
146
150
 
147
151
  }
152
+
153
+     nextButton.setOnClickListener{ }
154
+
155
+ backButton.setOnClickListener { finish() }
156
+
157
+
158
+
159
+ saveData(myHand, comHand ,gameResult)
160
+
161
+ }