numbermemorygame's People
numbermemorygame's Issues
めも
[Inputs]
protocol MemoryGameViewModelInputs {
func numberTapped(num: Int)
func passButtonTapped()
func clearButtonTapped()
func updateState(to: MemoryGameState)
func viewDidLoad()
}
①各ボタンをタップした時
ボタンがタップされる(view)
数字を追加するもしくは一文字削除する(viewModel)
viewが更新される。
②viewDidLoadでViewModel内でもゲームを開始したい。
③Viewの更新が終わったら伝えたい。
例えば問題の数字をLabelに表示して1秒経過したことをViewModelに伝えたい。
[state]
enum MemoryGameState {
case showTarget //問題を表示する。
case trySolving //解答する
case gudgeResult //答え合わせをする。
}
このゲームの流れとして
①showTarget
問題を一定時間表示 --> ボタンのタップを制限、1秒表示して ②へ
②trySolving
入力を受け付ける。数字やクリア、パスなどを受け付ける。
必要長さの回答を得られた場合に -> ③へ
③gudgeResult
結果を判定しViewが更新される。(正解または間違っている部分を表示する 。)
Viewがn秒間結果を表示し終えると ①へと更新する。
[outputs]
protocol MemoryGameViewModelOutputs {
var targetString: Observable<String> { get set }
var currentAnswerString: Observable<String> { get set }
var result: Observable<GudgeResult> { get }
var tapEnabled: Observable<Bool> { get }
var gameFinished: Observable<Void> { get }
}
① targetString
問題が始まるときに更新され、ViewのLabelとbindする。
1秒間表示し終えるとviewModelにinputをする
②currentAnswerString
入力中の文字列。ボタンのタップで更新され、Viewに反映される。
③result
答えあわせ時に更新される。結果がViewに反映される。
④tapEnabled
問題表示中と結果を表示中(stateが.showTarget, .gudgeResult)の時はタップを制限したい。
⑤gameFinished
ゲームが終了した時に一度だけ流したいイベント。結果の画面に移動する。
viewModel.outputs
.nextTargetString
.bind(to: showTargetNumberForWhile)
.disposed(by: bag)
viewModel.outputs
.currentAnswerString
.bind(to: label.rx.text)
.disposed(by: bag)
viewModel.outputs
.result
.bind(to: showResultThenRequest)
.disposed(by: bag)
viewModel.outputs
.tapEnabled
.bind(to: baseView.rx.isActive)
.disposed(by: bag)
viewModel.outputs
.gameFinished
.bind(to: showGameResultVC)
.disposed(by: bag)
[所感]
UIEvent(ボタンのタップ、Viewの更新してN秒後)を入力
ViewModelのデータの変更をViewが反映できており
ViewModelは問題のデータや結果をGameManager(Model)的な存在とやり取りをしており
またViewModelはstateというView(ゲーム)の状態を持っており、ちゃんとMVVMになっていると思うのだがどうだろうか。
気になった点
- viewModel.input.updateState()と入力することもあればviewModel内で自動で更新される時もある。
こっちの方が複雑だが良いのか?
数字を入力すると文字列が増える。
文字数が8の場合に別の処理をしたい。
var answer = Variable("123456")
if 文で簡潔に
func numButtonTapped {
answer.value += "4"
if answer.value.count == 8 {
//do something
}
}
こっちの方が値を監視している感じがするが・・
func numButtonTapped {
answer.value += "4"
}
answer.asObserble().filter { $0.count >= 8 }.subscribe(onNext: {
}.dispose()
今回は前者で実装している
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.