Connect 4 Game (console)

Hi Everyone.
I am totally new to Ruby and i started out to do the connect four game and finished it. I need some suggestions on my code like if i am going on the right direction of Ruby.
Here is my code-
1.Board.rb

class Board

@@board = []
@@rows = 6
@@columns = 7

def build_board
	@@rows.times do |row|
		@@board[row] = []

		@@columns.times do
			@@board[row] << '.'
		end
	end
end

def print_board
	puts "----------\nWelcome to Connect 4\n----------"
	for row in @@board
		for col in row
			print "#{col}  "
		end
		puts "\n"
	end
end

def coin_insert(column, symbol)
	result_hash = {}
	row_value = self.position_chooser(column, @@rows-1)
	if row_value == -1
		puts "--------The Column is full.----------"
		result_hash["inserted"] = false
	else
		@@board[row_value][column] = symbol
		result_hash["inserted"] = true
		result_hash["row"] = row_value
		result_hash["column"] = column
	end
	return result_hash
end

#checkers
def position_chooser(column, row)
	while @@board[row][column]!='.' && row>-1
		row = row-1
	end
	if row <0
		return -1
	else
		return row
	end
end

def vertical_checker(column, symbol)
	array = []
	@@rows.times do |row|
		array[row] = @@board[row][column]
	end
	return connect4_checker(array, symbol)
end

def horizontal_checker(row, symbol)
	array = []
	@@columns.times do |col|
		array[col] = @@board[row][col]
	end
	return connect4_checker(array, symbol)
end


def connect4_checker(array, symbol)
	counter = 0
	array.size.times do |i|
		if array[i] == symbol
			counter+=1
			return true if counter == 4
		else
			counter = 0
		end
	end
	return false
end

def diagonal_checker(row, column, symbol)
	if positive_diagonal(row,column,symbol) || negative_diagonal(row,column,symbol)
		return true
	end
	return false
end

def positive_diagonal(row, column, symbol)
	counter = 0
	i = row
	j = column
	while i>=0 && j<@@columns
		if @@board[i][j] == symbol
			counter+=1
			i-=1
			j+=1
		else
			break
		end
	end
	row+=1
	column-=1
	while row<@@rows && column>=0
		if @@board[row][column] == symbol
			counter+=1
			row+=1
			column-=1
		else
			break
		end
	end
	if counter==4
		return true
	else
		return false
	end
end

def negative_diagonal(row, column, symbol)
	counter = 0
	i = row
	j = column
	while i<@@rows && j<@@columns
		if @@board[i][j] == symbol
			counter+=1
			i+=1
			j+=1
		else
			break
		end
	end
	row-=1
	column-=1
	while row>=0 && column>=0
		if @@board[row][column] == symbol
			counter+=1
			row-=1
			column-=1
		else
			break
		end
	end
	if counter==4
		return true
	else
		return false
	end
end

end

2.Player.rb

require './Board'

class Player
@@game = Board.new
@@win = false

def buildGame
	@@game.build_board
	@@game.print_board
end

def playerToggler
	toggler = true
	while !@@win
		if toggler
			if activePlayer(1, '+')
				toggler = !toggler
			end
		else
			if activePlayer(2, 'x')
				toggler = !toggler
			end

		end
	end
end

def activePlayer(player_id, player_symbol)
	puts "------------Player #{player_id} : Enter the Column value-------------"
	input = gets.to_i
	insert_outcome = @@game.coin_insert(input-1, player_symbol)
	if insert_outcome["inserted"]
		system "clear"
		@@game.print_board
		if win_checker(insert_outcome["row"], insert_outcome["column"], player_symbol)
			puts "!!!!!!!player #{player_id} won the game.!!!!!!!!"
			@@win = true
		end
		return true
	else
		return false
	end
end

def win_checker(row, column, symbol)
	if  @@game.horizontal_checker(row, symbol) || @@game.vertical_checker(column, symbol) || @@game.diagonal_checker(row, column, symbol)
		return true
	else
		return false
	end
end

end

play = Player.new
play.buildGame
play.playerToggler

Thanks!

1 Like