はじめに
神経衰弱ゲームを作ってみる。
実際のコード
#縦の数、横の数、プレイヤーの数を受け取る
h,w,n = gets.chomp.split(" ").to_a
#カードの並びを行列で入れる変数を宣言
cards = []
#高さの数だけ、受け取りを繰り返す。
h.to_i.times do
array = gets.chomp.split(" ").map &:to_i
cards.push(array)
end
#プレイヤーの数だけ、スコアの配列を用意する
score=[]
n.to_i.times do
score.push(0)
end
#実行回数
l = gets.chomp.to_i
#ターンの宣言
turn = 0
#ゲームの開始
l.times do
#実行回数分、めくったカードを配列で受け取る。
array = gets.chomp.split(" ").map &:to_i
#受け取った配列を、各回の行列に代入する。
a_1,b_1,a_2,b_2 =array.to_a
#めくったカードの指定をする
first = cards[a_1 -1][b_1 -1]
second = cards[a_2 -1][b_2 -1]
#カードが一致だったらスコアをあげる
if first == second
score[turn] += 2
#一致しなければ、次のターンに移る。
else
turn += 1
if score.count <= turn
turn=0
end
end
end
puts score