Class: Game

Inherits:
Object
  • Object
show all
Defined in:
lib/connect_four.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeGame

Returns a new instance of Game.



97
98
99
100
101
102
103
104
105
106
# File 'lib/connect_four.rb', line 97

def initialize
	@board = Board.new #initialize the board, along with all the cells
	@player1 = Player.new #initialize player 1
	@player2 = Player.new #initialize player 2
	@current_player = who_goes_first #this var will keep track of whose turn it is.
	@other_player = other_player
	@moves_remaining = 42 #counts down the number of moves remaining
	@game_over = false #
	@winner = nil
end

Instance Attribute Details

#boardObject

This class initialized instances of all the other classes, contains the main game loop, and handles IO.



95
96
97
# File 'lib/connect_four.rb', line 95

def board
  @board
end

#current_playerObject

This class initialized instances of all the other classes, contains the main game loop, and handles IO.



95
96
97
# File 'lib/connect_four.rb', line 95

def current_player
  @current_player
end

#other_playerObject

This class initialized instances of all the other classes, contains the main game loop, and handles IO.



95
96
97
# File 'lib/connect_four.rb', line 95

def other_player
  @other_player
end

#player1Object

This class initialized instances of all the other classes, contains the main game loop, and handles IO.



95
96
97
# File 'lib/connect_four.rb', line 95

def player1
  @player1
end

#player2Object

This class initialized instances of all the other classes, contains the main game loop, and handles IO.



95
96
97
# File 'lib/connect_four.rb', line 95

def player2
  @player2
end

Instance Method Details

#buffer_spaceObject



173
174
175
# File 'lib/connect_four.rb', line 173

def buffer_space
	puts "\n\n\n\n"
end

#check_for_winnerObject



207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/connect_four.rb', line 207

def check_for_winner
	#For each cell in current player's moves array (i.e. the cells on which they've made moves),
	#perform searches on each search path in that cell's list of valid search_paths.
	@current_player.moves.each do |move|
		#perform searches on each search_path
		move.search_paths.each do |path|
			#initiate a counter at 1. Travel along the given search path and increment the counter if the next cell
			#in the path is in that player's moves array.
			counter = 1
			for i in 1..3
				if @current_player.moves.any?{|x| x==@board.offset(move,
																   i*(@board.path_coords[path][0]),
																   i*(@board.path_coords[path][1]))}
					counter+=1
				else
				end
				if counter==4
					@game_over = true
					@winner = @current_player
					break
				else
				end
			end
		end
	end

	#check for a draw
	if (@game_over==false) && (@player1.moves.length+@player2.moves.length==42)
		@game_over = true
	end
end

#game_setupObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/connect_four.rb', line 131

def game_setup
	puts <<-eos
	
	WELCOME TO RUBY CONNECT FOUR


			eos

	print "\nPlayer 1, please enter your name: "
	@player1.name = gets.chomp
	print "\n\nPlayer 2, please enter your name: "
	@player2.name = gets.chomp

	set_player_symbols #randomly sets their symbols

	puts <<-eos

By luck of the draw, #{@current_player.name} will go first, and their symbol is #{@current_player.symbol}.
#{@other_player.name} will go second, and their symbol is #{@other_player.symbol}.

			eos
end

#playObject



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/connect_four.rb', line 239

def play
	buffer_space #put blank space at the start
	game_setup
	until @game_over #main game loop, continues until the board reports a winner, which it checks for after every move.
	show_board
	turn
	buffer_space
	end
	show_board
	if @winner==nil
		puts "The game is a draw!"
	else
	puts "Congratulations! #{@winner.name} is the winner. Bye bye!"
	end
end

#set_player_symbolsObject



108
109
110
111
# File 'lib/connect_four.rb', line 108

def set_player_symbols
	@player1.symbol = rand>0.5 ? "X".red : "O".blue  #randomly assigns player 1 red X's or blue O's as their symbol
	@player2.symbol = (@player1.symbol == "X".red) ? "O".blue : "X".red #assigns player 2 the other symbol.
end

#show_boardObject

prints a heredoc showing the board.



154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/connect_four.rb', line 154

def show_board #prints a heredoc showing the board.
	puts <<-eos
  ===============================
  || #{state("A1")} | #{state("A2")} | #{state("A3")} | #{state("A4")} | #{state("A5")} | #{state("A6")} | #{state("A7")} ||
  -------------------------------
  || #{state("B1")} | #{state("B2")} | #{state("B3")} | #{state("B4")} | #{state("B5")} | #{state("B6")} | #{state("B7")} ||
  -------------------------------
  || #{state("C1")} | #{state("C2")} | #{state("C3")} | #{state("C4")} | #{state("C5")} | #{state("C6")} | #{state("C7")} ||
  -------------------------------
  || #{state("D1")} | #{state("D2")} | #{state("D3")} | #{state("D4")} | #{state("D5")} | #{state("D6")} | #{state("D7")} ||
  -------------------------------
  || #{state("E1")} | #{state("E2")} | #{state("E3")} | #{state("E4")} | #{state("E5")} | #{state("E6")} | #{state("E7")} ||
  -------------------------------
  || #{state("F1")} | #{state("F2")} | #{state("F3")} | #{state("F4")} | #{state("F5")} | #{state("F6")} | #{state("F7")} ||
  ===============================
     1   2   3   4   5   6   7   
	eos
end

#state(a1) ⇒ Object



127
128
129
# File 'lib/connect_four.rb', line 127

def state(a1)
	@board.cell(a1).state
end

#switch_current_playerObject



121
122
123
124
125
# File 'lib/connect_four.rb', line 121

def switch_current_player
	buffer = @current_player
	@current_player = @other_player
	@other_player = buffer
end

#turnObject



177
178
179
180
181
182
183
184
# File 'lib/connect_four.rb', line 177

def turn
	#the procedure every turn
	print "It is #{@current_player.name}'s turn. Make your move by entering the desired column number: "
	input = gets.chomp.to_i #get the move as a string
	validate_turn(input)
	check_for_winner
	switch_current_player
end

#validate_turn(column) ⇒ Object

checks to see if the move is in bounds and on a vacant cell. If not, asks for another.



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'lib/connect_four.rb', line 186

def validate_turn(column) #checks to see if the move is in bounds and on a vacant cell. If not, asks for another.
	if (1..7).member?(column)
			for i in 0..5
			if (i==5)&&(@board.cell(0,column-1).state!=" ")
				print "Oops, that column is full! Pick a different column: "
				input = gets.chomp.to_i
				validate_turn(input)
			elsif @board.cell(5-i,column-1).state == " "
				@board.cell(5-i,column-1).state = @current_player.symbol #places their symbol in that cell
				@current_player.add_move(@board.cell(5-i,column-1)) #adds that cell into their moves array
				break
			else	
			end
		end			
	else
		print "Uh-oh! Please enter a valid column number:"
		input = gets.chomp.to_i
		validate_turn(input)	
	end
end

#who_goes_firstObject



113
114
115
# File 'lib/connect_four.rb', line 113

def who_goes_first
	rand>0.5 ? @player1 : @player2 #randomly decides who will go first
end