-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4.rb
133 lines (116 loc) · 3.04 KB
/
connect4.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
require 'pry'
class ConnectFour
def initialize
@board = [
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "],
[" ", " ", " ", " ", " ", " ", " "]
]
@turn_count = 0
@position = []
display_board
game
end
def game
loop do
take_turn
if win?
puts "#{whos_turn} wins!"
break
end
end
end
def display_board
for i in [email protected] - 1
for j in 0..@board[0].length - 1
print "| #{@board[i][j]} "
end
print "|"
puts "\n"
end
end
def get_user_column #asks user for column
puts "It's #{@turn_count.even? ? marker = "X" : marker = "O"}'s turn!"
puts "Which column would you like to play? (1-7)"
answer = gets.chomp
end
def place_marker(marker, column)
column -= 1 #starts at index 0
x = @board.length - 1
until @board[x][column] == " "
x -= 1
end
@board[x][column] = marker
[x, column]
end
def play_move(marker)
column = 0
until column > 0 && column < 8
column = get_user_column.to_i
end
@position = place_marker(marker, column)
end
def whos_turn
@turn_count.even? ? marker = "O" : marker = "X"
end
def take_turn
marker = @turn_count.even? ? marker = "X" : marker = "O"
play_move(marker)
puts "Here's the current board:"
display_board
@turn_count += 1
end
def win?
horizontal || vertical || left_diagonal || right_diagonal
end
#checks if there's a horizontal win w/ passed in position
def horizontal #position comes in an array like [1, 1]
row = @position[0]
column = @board[row].join("")
column.include?("XXXX") || column.include?("OOOO") #true or false
end
#checks if there's a vertical win
def vertical
column_idx = @position[1]
column = ""
for i in [email protected]
column << @board[i][column_idx]
end
column.include?("XXXX") || column.include?("OOOO") #true or false
end
def left_diagonal #say, [3, 2]
upleft = [@position[0], @position[1]]
downright = [@position[0] + 1, @position[1] + 1]
string = ""
until upleft[0] < 0 || upleft[1] < 0
string += @board[upleft[0]][upleft[1]]
upleft[0] -= 1
upleft[1] -= 1
end
until downright[0] > 5 || downright[1] > 6
string += @board[downright[0]][downright[1]]
downright[0] += 1
downright[1] += 1
end
string.count("X") == 4 || string.count("X") == 4
end
def right_diagonal #say, [3, 2]
upright = [@position[0], @position[1]]
downleft = [@position[0] + 1, @position[1] - 1]
string = ""
until upright[0] < 0 || upright[1] > 6
string += @board[upright[0]][upright[1]]
upright[0] -= 1
upright[1] += 1
end
until downleft[0] > 5 || downleft[1] < 0
string += @board[downleft[0]][downleft[1]]
downleft[0] += 1
downleft[1] -= 1
end
string.count("X") == 4 || string.count("O") == 4
end
end