/
ticktack.rb
executable file
·84 lines (67 loc) · 1.56 KB
/
ticktack.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-
#マルペケゲーム
module TickTack
#審判
class Judge
WIN_PATTERN = [[1,2,3], [4,5,6], [7,8,9], [1,4,7], [2,5,8], [3,6,9], [1,5,9], [3,5,7]]
#判定、勝ちならtrue
def self.win?(player)
return false if player.hand.size < 3
WIN_PATTERN.each do |pattern|
return true if (player.hand.sort & pattern).size == 3
end
false
end
end
#プレイヤー
class Player
attr_accessor :name, :hand
def initialize(name)
@name = name
reset!
end
#出目を選び、印をつける。選択できたらtrueが返る
#盤は破壊していく(笑)
def choice(board)
return false if board.empty?
choiced = board.shuffle.first
if board.include? choiced
@hand.push(choiced) && board.delete(choiced)
else
choice(board)
end
true
end
def reset!
@hand = []
end
end
#ゲーム
class Game
#試合開始
#:p1, :p2, :draw のいずれかが返る
def self.play
pair, board = [Player.new(:p1), Player.new(:p2)], (1..9).to_a
# 決着着くまで◯を付け合う
until board.empty?
pair.each do |player|
return player.name if player.choice(board) && Judge.win?(player)
end
end
:draw
end
end
end
# 10000回ゲームさせて、計測
p1, p2, draw = 0, 0, 0
1.upto(10000) do
case TickTack::Game.play
when :p1
p1 += 1
when :p2
p2 += 1
when :draw
draw += 1
end
end
puts "p1:#{p1}, p2:#{p2}, draw:#{draw}"