Class: NoughtsAndCrosses
- Inherits:
-
Game
- Object
- Game
- NoughtsAndCrosses
show all
- Defined in:
- lib/noughts_and_crosses.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods inherited from Game
#display, #reload_position
Constructor Details
Returns a new instance of NoughtsAndCrosses.
6
7
8
9
10
|
# File 'lib/noughts_and_crosses.rb', line 6
def initialize
@position = {:A1 => 0, :B1 => 0, :C1 => 0, :A2 => 0, :B2 => 0, :C2 => 0, :A3 => 0, :B3 => 0, :C3 => 0}
@next_player = 0
@move_list = []
end
|
Instance Attribute Details
#move_list ⇒ Object
Returns the value of attribute move_list.
4
5
6
|
# File 'lib/noughts_and_crosses.rb', line 4
def move_list
@move_list
end
|
#next_player ⇒ Object
Returns the value of attribute next_player.
4
5
6
|
# File 'lib/noughts_and_crosses.rb', line 4
def next_player
@next_player
end
|
#quiet ⇒ Object
Returns the value of attribute quiet.
5
6
7
|
# File 'lib/noughts_and_crosses.rb', line 5
def quiet
@quiet
end
|
#verbose ⇒ Object
Returns the value of attribute verbose.
5
6
7
|
# File 'lib/noughts_and_crosses.rb', line 5
def verbose
@verbose
end
|
#winner ⇒ Object
Returns the value of attribute winner.
4
5
6
|
# File 'lib/noughts_and_crosses.rb', line 4
def winner
@winner
end
|
Instance Method Details
#current_position ⇒ Object
93
94
95
|
# File 'lib/noughts_and_crosses.rb', line 93
def current_position
return @position
end
|
#drawn? ⇒ Boolean
132
133
134
135
136
137
138
139
140
|
# File 'lib/noughts_and_crosses.rb', line 132
def drawn?
unless legal_moves.count == 0
return false
end
if won?
return false
end
true
end
|
#legal_moves ⇒ Object
39
40
41
42
43
44
45
46
47
|
# File 'lib/noughts_and_crosses.rb', line 39
def legal_moves
legal_moves = []
@position.each do |move|
if move[1] == 0
legal_moves << move[0].to_s
end
end
legal_moves
end
|
#name ⇒ Object
12
13
14
|
# File 'lib/noughts_and_crosses.rb', line 12
def name
"NoughtsAndCrosses"
end
|
#play_move(player, move) ⇒ Object
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
85
86
87
88
89
90
91
|
# File 'lib/noughts_and_crosses.rb', line 49
def play_move(player, move)
sym_move = move.to_sym
unless player == @next_player
display "not player #{player}'s turn"
return false
end
unless @position[sym_move] == 0
display "illegal move by player #{player}"
return false
end
if won?
display "player #{winner} has already won"
return false
elsif drawn?
display "game was drawn"
return false
end
@move_list << [player,move]
if player == 1
@position[sym_move] = 1
else
@position[sym_move] = -1
end
if won?
display "player #{player} wins!"
@winner = player
elsif drawn?
display "game drawn"
end
if @next_player == 1
@next_player = 0
else
@next_player = 1
end
if @verbose
display show_board
display @position
end
true
end
|
#show_board ⇒ Object
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/noughts_and_crosses.rb', line 16
def show_board
board = " - - - \n"
i = 0
@position.each do |move|
i += 1
board += "|"
if move[1] == 0
board += " "
elsif move[1] == 1
board += "X"
else
board += "O"
end
if i == 3
board += "|\n _ _ _\n"
i = 0
end
end
board
end
|
#won? ⇒ Boolean
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
# File 'lib/noughts_and_crosses.rb', line 97
def won?
if @position[:A1] != 0
if @position[:A1] == @position[:A2] && @position[:A2] == @position[:A3]
return true
elsif @position[:A1] == @position[:B2] && @position[:B2] == @position[:C3]
return true
elsif @position[:A1] == @position[:B1] && @position[:B1] == @position[:C1]
return true
end
end
if @position[:B1] != 0
if @position[:B1] == @position[:B2] && @position[:B2] == @position[:B3]
return true
end
end
if @position[:C1] != 0
if @position[:C1] == @position[:C2] && @position[:C2] == @position[:C3]
return true
elsif @position[:C1] == @position[:B2] && @position[:B2] == @position[:A3]
return true
end
end
if @position[:A2] != 0
if @position[:A2] == @position[:B2] && @position[:B2] == @position[:C2]
return true
end
end
if @position[:A3] != 0
if @position[:A3] == @position[:B3] && @position[:B3] == @position[:C3]
return true
end
end
false
end
|