-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
152 lines (124 loc) · 4.25 KB
/
main.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
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
143
144
145
146
147
148
149
150
151
152
# class for orfanising plyers
class Players
@@players = []
@@limit = 3
@@player_moves = []
def initialize(name)
if @@players.length < @@limit
@name = name
@@players.push(@name)
player_active
else
(raise 'Only two players can play the game')
end
end
def self.info
puts "THE PLAYERS ARE #{@@players}"
puts "\n#{@@players[0]} is #{@@player_moves[0]}"
puts "#{@@players[1]} is #{@@player_moves[1]}"
end
protected
def player_active
puts "\n#{@name} has entered the game"
if @@players.length <= 1
@@player_moves.push(choose_move)
else
if @@player_moves[0] == 'X'
@@player_moves.push('O')
else
@@player_moves.push('X')
end
end
end
def choose_move
move = ""
until move == 'X' || move == 'O'
puts "#{@name} choose your move: `X` or `O`"
move = gets.chomp.upcase
end
move
end
end
# class for playing game
class Game < Players
def initialize
@position = [0, 1, 2, 3, 4, 5, 6, 7, 8]
@columns1 = Array.new()
@columns2 = Array.new()
@columns3 = Array.new()
@rows1 = Array.new()
@rows2 = Array.new()
@rows3 = Array.new()
@diagonal1 = Array.new()
show_board
play_rounds
end
private
def show_board
board = "\n---------------------\n"
board += "#{@position[0]} | #{@position[1]} | #{@position[2]}\n"
board += "---+---+---\n"
board += "#{@position[3]} | #{@position[4]} | #{@position[5]}\n"
board += "---+---+---\n"
board += "#{@position[6]} | #{@position[7]} | #{@position[8]}\n"
board += "---------------------\n"
print board
end
def play_rounds
while true
puts "#{@@players[0].upcase} choose position to play [0 - 8]"
player_one_move = gets.chomp
if @position.index('X') != player_one_move.to_i && @position.index('O') != player_one_move.to_i
@position[player_one_move.to_i] = @@player_moves[0]
show_board
else
puts "invalid input...bad luck loser"
end
puts "#{@@players[1].upcase} choose position to play [0 - 8]"
player_two_move = gets.chomp
if @position.index('X') != player_two_move.to_i && @position.index('O') != player_two_move.to_i
@position[player_two_move.to_i] = @@player_moves[1]
show_board
else
puts "invalid input...bad luck loser"
end
@columns1 = [@position[0], @position[3], @position[6]]
@columns2 = [@position[1], @position[4], @position[7]]
@columns3 = [@position[2], @position[5], @position[8]]
@rows1 = [@position[0], @position[1], @position[2]]
@rows2 = [@position[3], @position[4], @position[5]]
@rows3 = [@position[6], @position[7], @position[8]]
@diagonal = [@position[0], @position[4], @position[8]]
if @rows1.all? {|pos| pos == @@player_moves[0]} || @rows2.all? {|pos| pos == @@player_moves[0]} || @rows3.all? {|pos| pos == @@player_moves[0]}
puts "\n#{@@players[0].upcase} won the game bro"
break
elsif @columns1.all? {|pos| pos == @@player_moves[0]} || @columns2.all? {|pos| pos == @@player_moves[0]} || @columns3.all? {|pos| pos == @@player_moves[0]}
puts "\n#{@@players[0].upcase} won the game bro"
break
elsif @diagonal.all? {|pos| pos == @@player_moves[0]}
puts "\n#{@@players[0].upcase} won the game bro"
break
end
if @rows1.all? {|pos| pos == @@player_moves[1]} || @rows2.all? {|pos| pos == @@player_moves[1]} || @rows3.all? {|pos| pos == @@player_moves[1]}
puts "\n#{@@players[1].upcase} won the game bro"
break
elsif @columns1.all? {|pos| pos == @@player_moves[1]} || @columns2.all? {|pos| pos == @@player_moves[1]} || @columns3.all? {|pos| pos == @@player_moves[1]}
puts "\n#{@@players[1].upcase} won the game bro"
break
elsif @diagonal.all? {|pos| pos == @@player_moves[1]}
puts "\n#{@@players[1].upcase} won the game bro"
break
end
end
end
end
puts "Welcome to Tic-Tac-Toe"
puts "\nEnter your name player 1"
name_one = gets.chomp
Players.new(name_one)
puts "\nEnter your name player 2"
name_two = gets.chomp
Players.new(name_two)
Players.info
print "\nBelow are the options you have to play as 'X' or 'O' from position 0 - 8\n"
Game.new