teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

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

2020/12/12 16:30

投稿

htnk_4a
htnk_4a

スコア3

title CHANGED
File without changes
body CHANGED
@@ -23,10 +23,46 @@
23
23
  「次へ」のボタンをレイアウトエディタで設置しましたがbackButtonのようにうまく表示できない
24
24
 
25
25
  ### 補足情報
26
+ ///inputactivity///
27
+
26
28
  package com.example.janken
27
29
 
30
+ import android.content.Intent
31
+ import androidx.appcompat.app.AppCompatActivity
28
32
  import android.os.Bundle
29
33
  import android.preference.PreferenceManager
34
+ import androidx.core.content.edit
35
+ import kotlinx.android.synthetic.main.activity_main.*
36
+ import android.view.View as View1
37
+
38
+ class MainActivity : AppCompatActivity() {
39
+
40
+ override fun onCreate(savedInstanceState: Bundle?) {
41
+ super.onCreate(savedInstanceState)
42
+ setContentView(R.layout.activity_main)
43
+
44
+ gu.setOnClickListener{ onJankenButtonTapped(it) }
45
+ choki.setOnClickListener { onJankenButtonTapped(it) }
46
+ pa.setOnClickListener { onJankenButtonTapped(it) }
47
+
48
+ val pref= PreferenceManager.getDefaultSharedPreferences(this)
49
+ pref.edit {
50
+ clear()
51
+ }
52
+ }
53
+ fun onJankenButtonTapped(view: View1?){
54
+ val intent = Intent(this, ResultActivity::class.java)
55
+ intent.putExtra("MY_HAND", view?.id)
56
+ startActivity(intent)
57
+ }
58
+ }
59
+
60
+ ///resultactivity///
61
+
62
+ package com.example.janken
63
+
64
+ import android.os.Bundle
65
+ import android.preference.PreferenceManager
30
66
  import androidx.appcompat.app.AppCompatActivity
31
67
  import androidx.core.content.edit
32
68
  import kotlinx.android.synthetic.main.activity_result.*
@@ -78,4 +114,62 @@
78
114
  backButton.setOnClickListener { finish() }
79
115
 
80
116
  saveData(myHand, comHand ,gameResult)
81
- }
117
+ }
118
+ private fun saveData(myHand: Int, comHand: Int, gameResult: Int) {
119
+ val pref = PreferenceManager.getDefaultSharedPreferences(this)
120
+ val gameCount = pref.getInt("GAME_COUNT", 0)
121
+ val winningStreakCount = pref.getInt("WINNING_STREAK_COUNT", 0)
122
+ val lastComHand = pref.getInt("LAST_COM_HAND", 0)
123
+ val lastGameResult = pref.getInt("GAME_RESULT", -1)
124
+
125
+ val edtWinningStreakCount: Int =
126
+ when {
127
+ lastGameResult == 2 && gameResult == 2 ->
128
+ winningStreakCount + 1
129
+ else ->
130
+ 0
131
+ }
132
+ val editor = pref.edit()
133
+ pref.edit {
134
+ putInt("GAME_COUNT", gameCount + 1)
135
+ putInt("WINNING_STREAK_COUNT", edtWinningStreakCount)
136
+ putInt("LAST_MY_HAND", myHand)
137
+ putInt("LAST_COM_HAND", comHand)
138
+ putInt("BEFORE_LAST_COM_HAND", lastComHand)
139
+ putInt("GAME_RESULT", gameResult)
140
+ }
141
+ }
142
+
143
+ private fun getHand(): Int {
144
+ var hand = (Math.random() * 3).toInt()
145
+ val pref = PreferenceManager.getDefaultSharedPreferences(this)
146
+ val gameCount = pref.getInt("GAME_COUNT", 0)
147
+ val winningStreakCount = pref.getInt("WINNING_STREAK_COUNT", 0)
148
+ val lastMyHand = pref.getInt("LAST_MY_HAND", 0)
149
+ val lastComHand = pref.getInt("LAST_COM_HAND", 0)
150
+ val beforeLastComHand = pref.getInt("BEFORE_LAST_COM_HAND", 0)
151
+ val gameResult = pref.getInt("GAME_RESULT", -1)
152
+
153
+ if (gameCount == 1) {
154
+ if (gameResult == 2) {
155
+ // 前回の勝負が1回目で、コンピュータが勝った場合、
156
+ // コンピュータは次に出す手を変える
157
+ while (lastComHand == hand) {
158
+ hand = (Math.random() * 3).toInt()
159
+ }
160
+ } else if (gameResult == 1) {
161
+ // 前回の勝負が1回目で、コンピュータが負けた場合
162
+ // 相手の出した手に勝つ手を出す
163
+ hand = (lastMyHand - 1 + 3) % 3
164
+ }
165
+ } else if (winningStreakCount > 0) {
166
+ if (beforeLastComHand == lastComHand) {
167
+ // 同じ手で連勝した場合は手を変える
168
+ while (lastComHand == hand) {
169
+ hand = (Math.random() * 3).toInt()
170
+ }
171
+ }
172
+ }
173
+ return hand
174
+ }
175
+ }

1

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

2020/12/12 16:30

投稿

htnk_4a
htnk_4a

スコア3

title CHANGED
File without changes
body CHANGED
@@ -1,11 +1,12 @@
1
1
  ### 前提・実現したいこと
2
2
  三回勝つor三回負けるでプログラム終了(連勝、連敗で作成できる場合はそちらを教えていただきたいです。)
3
3
 
4
- andoroid studio を使用しています。じゃんけんアプリを作成しているのですが繰り返し処理の書き方がわかりません。手付かずなのでどなたかよろしくお願いします。
4
+ andoroid studio を使用しています。じゃんけんアプリを作成しているのですが繰り返し処理の書き方がわかりません。
5
+ このアプリはじゃんけんを連続でできない仕組みになっているのですが「次へ」のボタンを設置し連続でじゃんけんをできるようにしたいです。
5
6
 
6
7
 
7
8
  ### 発生している問題・エラーメッセージ
8
-
9
+ nextButton.setOnClickListener{ }の{}の中身がどう書けばいいか分かりません。
9
10
  ```
10
11
  エラーメッセージ
11
12
  ```
@@ -19,6 +20,7 @@
19
20
  ### 試したこと
20
21
 
21
22
  forやwhile文を試しましたがコードが間違っているのかうまくいきませんでした。
23
+ 「次へ」のボタンをレイアウトエディタで設置しましたがbackButtonのようにうまく表示できない
22
24
 
23
25
  ### 補足情報
24
26
  package com.example.janken
@@ -71,4 +73,9 @@
71
73
  0 -> resultLabel.setText(R.string.result_draw) // 引き分け
72
74
  1 -> resultLabel.setText(R.string.result_win) // 勝った場合
73
75
  2 -> resultLabel.setText(R.string.result_lose) // 負けた場合
74
- }
76
+ }
77
+     nextButton.setOnClickListener{ }
78
+ backButton.setOnClickListener { finish() }
79
+
80
+ saveData(myHand, comHand ,gameResult)
81
+ }