An object oriented tictactoe game

Just doing a basic Ruby tictactoe game using some basic oop.  Feel free to copy and paste this code and give it a go.

# tic tac is a 2 player board game played on 3 x 3 grid.
# players take turns marking a square, the 1st player to mark 3 in a row
# wins.

# Nouns: player, board, square, grid
# Verbs: play, mark

# board
# square
# player
# – mark
# – play
require ‘pry’

class Board
attr_accessor :board, :squares, :marker
WINNING_LINES = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] +
[[1, 4, 7], [2, 5, 8], [3, 6, 9]] +
[[1, 5, 9], [3, 5, 7]]

def initialize
@squares = {}
reset
end

def reset
(1..9).each { |key| @squares[key] = Square.new }
end

def get_square_at(key)
@squares[key]
end

def set_square_at(key, marker)
@squares[key].marker = marker
end

def []=(num, marker)
@squares[num].marker = marker
end

def unmarked_keys
@squares.keys.select { |key| @squares[key].unmarked? }
end

def marked_keys
@squares.keys.select { |key| @squares[key].marked? }
end

def full?
unmarked_keys.empty?
end

def someone_won?
!!winning_marker
end

def count_human_marker(squares)
squares.collect(&:marker).count(TTTGame::HUMAN_MARKER)
end

def count_computer_marker(squares)
squares.collect(&:marker).count(TTTGame::COMPUTER_MARKER)
end

def three_identical_markers?(squares)
markers = squares.select(&:marked?).collect(&:marker)
return false if markers.size != 3
markers.min == markers.max
end

def winning_marker
WINNING_LINES.each do |line|
squares = @squares.values_at(*line)
if three_identical_markers?(squares)
return squares.first.marker
end
end
nil
end

# rubocop:disable Metrics/AbcSize
def draw
puts “”
puts ” | |”
puts ” #{@squares[1]} | #{@squares[2]} | #{@squares[3]}”
puts ” | |”
puts “—–+—–+—–”
puts ” | |”
puts ” #{@squares[4]} | #{@squares[5]} | #{@squares[6]}”
puts ” | |”
puts “—–+—–+—–”
puts ” | |”
puts ” #{@squares[7]} | #{@squares[8]} | #{@squares[9]}”
puts ” | |”
end
end

class Square
attr_accessor :marker
INITIAL_VALUE = ‘ ‘

def initialize(marker=INITIAL_VALUE)
@marker = marker
end

def to_s
@marker
end

def unmarked?
marker == INITIAL_VALUE
end

def marked?
marker != INITIAL_VALUE
end
end

class Player
attr_accessor :marker, :player_win_count, :computer_win_count

def initialize(marker)
@marker = marker
@player_win_count = 0
@computer_win_count = 0
end
end

class TTTGame
HUMAN_MARKER = “X”
COMPUTER_MARKER = “O”
FIRST_TO_MOVE = HUMAN_MARKER

attr_reader :board, :human, :computer

def initialize
@board = Board.new
@human = Player.new(HUMAN_MARKER)
@computer = Player.new(COMPUTER_MARKER)
@current_marker = FIRST_TO_MOVE
end

def game_winner_found
if human.player_win_count == 5
puts “Congrats, you won against the machine!”
elsif computer.computer_win_count == 5
puts “Skynet wins.”
end
end

def play
clear
display_welcome_message

loop do
display_board

loop do
current_player_moves
break if board.someone_won? || board.full?
clear_screen_and_display_board
end
display_result
break if game_winner_found
break unless play_again?
reset
display_play_again_message
end

display_goodbye_message
end

private

def display_welcome_message
puts “Welcome to tic tac toe!”
puts “”
end

def display_goodbye_message
puts “Thanks for playing Tic tac toe! Goodbye!”
end

def clear
system ‘clear’
end

def clear_screen_and_display_board
clear
display_board
end

def display_board
puts “You’re a #{human.marker}. Computer is a #{computer.marker}”
puts “”
board.draw
puts “”
end

def joinor(arr, delimiter, word = “or”)
arr[-1] = “#{word} #{arr.last}” if arr.size > 1
arr.join(delimiter)
end

def human_moves
puts “Choose a square (#{joinor(board.unmarked_keys, ‘, ‘)}):”
square = nil
loop do
square = gets.chomp.to_i
break if board.unmarked_keys.include?(square)
puts “Sorry, that’s not a valid choice.”
end
board.[]=(square, HUMAN_MARKER)
end

def defense(line)
if board.squares.values_at(*line).join(‘, ‘).count(‘X’) == 2
board.squares.select{ |k,v| line.include?(k) && v.marker == ‘ ‘ }.keys.first
else
nil
end
end

def offense(line)
if board.squares.values_at(*line).join(‘, ‘).count(‘O’) == 2
board.squares.select{ |k,v| line.include?(k) && v.marker == ‘ ‘ }.keys.first
else
nil
end
end

def computer_moves
square = false

# defense
Board::WINNING_LINES.each do |line|
square = defense(line)
break if square
end

# offense
# if !square
# Board::WINNING_LINES.each do |line|
# square = offense(line)
# break if square
# end
# end

if !square
# square = board.[]=(board.unmarked_keys.sample, COMPUTER_MARKER)
square = board.unmarked_keys.sample
end

# board.squares[square].marker = COMPUTER_MARKER
board.[]=(square, COMPUTER_MARKER)
end

def current_player_moves
if @current_marker == HUMAN_MARKER
human_moves
@current_marker = COMPUTER_MARKER
else
computer_moves
@current_marker = HUMAN_MARKER
end
end

def reset
board.reset
@current_marker = FIRST_TO_MOVE
clear
end

def display_result
binding.pry
clear_screen_and_display_board
case board.winning_marker
when human.marker
puts “You won!”
human.player_win_count += 1
when computer.marker
puts “Computer won!”
computer.computer_win_count += 1
else
puts “It’s a tie”
end
end

def play_again?
answer = ”
loop do
puts “Would you like to play again? (y/n)”
answer = gets.chomp.downcase
break if %w(y n).include? answer
puts “Sorry, must be y or n”
end

answer == ‘y’
end

def display_play_again_message
puts “Let’s play again!”
puts “”
end
end

game = TTTGame.new
game.play

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: