-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRubyTicTacToe.rb
More file actions
134 lines (119 loc) · 3.61 KB
/
RubyTicTacToe.rb
File metadata and controls
134 lines (119 loc) · 3.61 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
VERSION = "1.0.2"
class Board
attr_accessor :board, :turn_counter, :winner
def initialize
@board = [[" ", " ", " "],[" ", " ", " "],[" ", " ", " "]]
@turn_counter = 0
@winner = 0
end
def print_board
puts " A B C"
puts "1 #{@board[0][0]} | #{@board[1][0]} | #{@board[2][0]}"
puts " --+---+--"
puts "2 #{@board[0][1]} | #{@board[1][1]} | #{@board[2][1]}"
puts " --+---+--"
puts "3 #{@board[0][2]} | #{@board[1][2]} | #{@board[2][2]}"
end
def status
for i in (0..2) do
if @board[i][0] == @board[i][1] && @board[i][0] == @board[i][2] && @board[i][0] != " "
return @board[i][0]
elsif @board[0][i] == @board[1][i] && @board[0][i] == @board[2][i] && @board[0][i] != " "
return @board[0][i]
end
end
if @board[0][0] == @board[1][1] && @board[0][0] == @board[2][2] && @board[0][0] != " "
return @board[0][0]
elsif @board[2][0] == @board[1][1] && @board[2][0] == @board[0][2] && @board[2][0] != " "
return @board[2][0]
end
if @board.count(" ") == 9
return 'Nobody'
end
return false
end
private
def spin_board
new_board = [[@board[2][0],@board[2][1],@board[2][2]],[@board[1][0],@board[1][1],@board[1][2]],[@board[0][0],@board[0][1],@board[0][2]]]
@board = new_board
end
end
def welcome
100.times { puts "" }
puts "TIC-TAC-TOE v#{VERSION}"
puts "by CSTAICH"
puts "======================"
$game = Array.new
end
def menu
puts "Enter command ( new_game :: score )"
input = gets.chomp.downcase
case input
when "new_game"
$game << Board.new
play_game
when "score"
x_score = 0
o_score = 0
$game.each do |x|
if x.winner == 'X'; x_score += 1
elsif x.winner == 'O'; o_score += 1
end
end
puts ""
puts "Current score is X:#{x_score} to O:#{o_score}."
puts ""
return
else
puts "unknown command"
end
end
def play_game
$game[-2] != nil && $game[-2].status == 'X' ? turn = 'O' : turn = 'X'
while $game.last.status == false && $game.last.turn_counter != 9
puts ""
$game.last.print_board
puts "#{turn}'s turn. Enter input in form: A1"
input = gets.downcase.split(//)
case input[0]
when "a"; col = 0
when "b"; col = 1
when "c"; col = 2
end
row = input[1].to_i - 1
if $game.last.board[col][row] == " "
$game.last.board[col][row] = turn
if turn == 'X'
turn = 'O'
else
turn = 'X'
end
$game.last.turn_counter += 1
else
puts ""
puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
puts "You can't go there, that space is taken."
puts "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
end
end
if $game.last.status == false
winner = "Nobody"
else
winner = $game.last.status
$game.last.winner = winner
end
10.times { puts "" }
3.times { puts "= = = = = = = = = =" }
puts "Game Over! #{winner} wins!"
3.times { puts "= = = = = = = = = =" }
3.times { puts "" }
$game.last.print_board
5.times { puts "" }
end
welcome
while true do
menu
end
$game << Board.new
$game.last.print_board
$game.last.status