Skip to content

Instantly share code, notes, and snippets.

@boy-jer
Last active April 20, 2016 21:47
Show Gist options
  • Save boy-jer/5a4cd9172420fc996e1b5122cd6b263c to your computer and use it in GitHub Desktop.
Save boy-jer/5a4cd9172420fc996e1b5122cd6b263c to your computer and use it in GitHub Desktop.
class Board
attr_reader :grid
def initialize(input_array_hash = {})
@grid = input_array_hash.fetch(:grid, board_default_grid)
end
def fetch_cell(x, y)
grid[y][x]
end
def set_cell(x, y, value)
fetch_cell(x, y).value = value
end
def game_finished
return :winner if winner?
return :draw if draw?
false
end
def join_grid_rows
grid.each do |row|
puts row.map { |cell| cell.value.empty? ? "__|" : cell.value }.join(" ")
end
end
private
def board_default_grid
Array.new(3) { Array.new(3) { Cell.new } }
end
def draw?
k = grid.flatten.map { |cell| cell.value }
#any? here is used to check if any elements of the array are empty and false otherwise.
k.any? {|s| s.to_s.empty?}
end
def winner?
winning_positions.each do |winning_position|
#.all? is checking is all_empty?
next if winning_position_values(winning_position).all? { |element| element.to_s.empty? }
#all? here is used to check if all the array rows are thesame
#return true if winning_position_values(winning_position).all? { |element| element == winning_position_values[0] }
w = winning_position_values(winning_position)
return true if w.all? { |element| element == w[0] }
end
false
end
def winning_position_values(winning_position)
winning_position.map { |cell| cell.value }
end
def winning_positions
grid + # rows
grid.transpose + # columns
diagonals # two diagonals
end
def diagonals
[
[fetch_cell(0, 0), fetch_cell(1, 1), fetch_cell(2, 2)],
[fetch_cell(0, 2), fetch_cell(1, 1), fetch_cell(2, 0)]
]
end
end
################
class Game
attr_reader :players, :board, :current_player, :other_player
def initialize(players, board = Board.new )
@players = players
@board = board
#@current_player, @other_player = players.shuffle
@current_player = players
end
def switch_players
@current_player, @other_player = @other_player, @current_player
end
def single_player
@current_player
end
def solicit_move
"#{current_player.name}: Enter a number between 1 and 9 to make your move"
end
def get_move(human_move = gets.chomp)
human_move_to_coordinate(human_move)
end
def game_over_message
return "#{current_player.name} won!" if board.game_finished == :winner
return "The game ended in a tie" if board.game_finished == :draw
end
def play
#puts "#{current_player.name} has randomly been selected as the first player"
puts "#{current_player.name}"
while true
board.join_grid_rows
puts ""
puts solicit_move
x, y = get_move
board.set_cell(x, y, current_player.color)
board.join_grid_rows
#these 4 lines added by me
puts solicit_move
x, y = get_move
board.set_cell(x, y, current_player.color)
board.join_grid_rows
#these 4 lines added by me
puts solicit_move
x, y = get_move
board.set_cell(x, y, current_player.color)
if board.game_finished
puts game_over_message
board.join_grid_rows
return
else
#switch_players
single_player
end
end
####
class Player
attr_reader :color, :name
def initialize(player_hash)
@color = player_hash.fetch(:color)
@name = player_hash.fetch(:name)
end
end
################
class Cell
attr_accessor :value
def initialize(value='')
@value = value
end
end
########################
puts "Welcome to tic tac toe"
mike = Ttt::Player.new({color: "X", name: "mike"})
#david = Ttt::Player.new({color: "O", name: "david"})
#players = [mike, david]
#Ttt::Game.new(players).play
Ttt::Game.new(mike).play
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment