-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtictactoe.rb
More file actions
142 lines (120 loc) · 3.55 KB
/
tictactoe.rb
File metadata and controls
142 lines (120 loc) · 3.55 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
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
131
132
133
134
135
136
137
138
139
140
141
142
def display_board(board)
puts " #{board[0]} | #{board[1]} | #{board[2]} "
puts "--------------"
puts " #{board[3]} | #{board[4]} | #{board[5]} "
puts "--------------"
puts " #{board[6]} | #{board[7]} | #{board[8]} "
end
# 驗證玩家輸入的是否正確的方法
def vaild_move?(board,position)
(0..8).include?(position) && board[position] == " "
end
def play(board, counter)
if current_player(counter) == "X" # 12/09 新增跟電腦輪流下棋
puts "選出你要下棋的位置,請輸入 1-9:"
input = gets.chomp
position = input.to_i - 1
else
puts "換電腦下棋"
position = computer_play(board)
end
if vaild_move?(board,position) == true
board[position] = current_player(counter)
display_board(board)
else
play(board, counter)
end
end
def turn(board)
counter = 1
while counter <= 9 && !won?(board) do
puts "第 #{counter} 回合, 輸到 #{current_player(counter)}"
play(board, counter)
counter += 1
end
if won?(board)
puts "#{winner(board)} 贏了!"
else
puts "平手!"
end
puts "遊戲結束!"
end
def current_player(counter)
if counter % 2 == 0
return "O"
else
return "X"
end
end
WIN_COMBINATIONS = [
[0, 1, 2],
[3, 4, 5],
[6, 7, 8],
[0, 3, 6],
[1, 4, 7],
[2, 5, 8],
[0, 4, 8],
[2, 4, 6]
]
def won?(board)
# 如果有滿足到贏的組合,就回傳 True
WIN_COMBINATIONS.each do |win_combo|
if (board[win_combo[0]] == "X" && board[win_combo[1]] == "X" && board[win_combo[2]] == "X" ) || (board[win_combo[0]] == "O" && board[win_combo[1]] == "O" && board[win_combo[2]] == "O" )
return win_combo
end
end
# 如果都沒人贏,就回傳 False
return false
end
=begin
def won?(board)
if (board[0] == "X" && board[1] == "X" && board[2] == "X" ) || (board[0] == "O" && board[1] == "O" && board[2] == "O" )
return true
elsif (board[3] == "X" && board[4] == "X" && board[5] == "X" ) || (board[3] == "O" && board[4] == "O" && board[5] == "O" )
return true
elsif (board[6] == "X" && board[7] == "X" && board[8] == "X" ) || (board[6] == "O" && board[7] == "O" && board[8] == "O" )
return true
elsif (board[0] == "X" && board[3] == "X" && board[6] == "X" ) || (board[0] == "O" && board[3] == "O" && board[6] == "O" )
return true
elsif (board[1] == "X" && board[4] == "X" && board[7] == "X" ) || (board[1] == "O" && board[4] == "O" && board[7] == "O" )
return true
elsif (board[2] == "X" && board[5] == "X" && board[8] == "X" ) || (board[2] == "O" && board[5] == "O" && board[8] == "O" )
return true
elsif (board[1] == "X" && board[4] == "X" && board[8] == "X" ) || (board[1] == "O" && board[4] == "O" && board[8] == "O" )
return true
elsif (board[2] == "X" && board[4] == "X" && board[6] == "X" ) || (board[2] == "O" && board[4] == "O" && board[6] == "O" )
return true
end
return false
end
=end
def winner(board)
win_combo = won?(board)
if win_combo
winner = board[win_combo[0]]
return winner
end
end
# 12/09 電腦選棋
def computer_play(board)
avail_position(board).sample
end
def avail_position(board)
avail_position = Array.new
#board.each_with_index do |input, index|
#if input == " "
for position in 0..8
if board[position] == " "
#avail_position << index
avail_position << position
end
end
avail_position
end
###################################################
# 執行程序
puts "你開啟了一局新的井字遊戲!"
puts "請以 1-9 數字對應棋盤位置"
display_board([1,2,3,4,5,6,7,8,9])
board = Array.new(9, " ")
turn(board)