class Anakhi::TicTacToe

Public Class Methods

new() click to toggle source

initialize

# File lib/anakhi.rb, line 8
def initialize
    # set up the board
    @board = Board.new

    # set up the players
    @player_x = Player.new("Madame X", :x, @board)
    @player_y = Player.new("Mister Y", :y, @board)

    # assign the starting player
    @current_player = @player_x
end

Public Instance Methods

check_draw() click to toggle source

check_draw?

# File lib/anakhi.rb, line 60
def check_draw
    # If Board says we've filled up
    if @board.full?
        # display draw message
        puts "Bummer, you've drawn..."
        true
    else
        false
    end
end
check_game_over() click to toggle source

check_game_over?

# File lib/anakhi.rb, line 40
def check_game_over
    # check for victory
    # check for draw
    check_victory || check_draw
end
check_victory() click to toggle source

check_victory?

# File lib/anakhi.rb, line 47
def check_victory
    # IF Board says current player's piece has
    # a winning_combination?
    if @board.winning_combination?(@current_player.piece)
        # then output a victory message
        puts "Congratulations #{@current_player.name}, you win!"
        true
    else
        false
    end
end
play() click to toggle source

play

# File lib/anakhi.rb, line 21
def play

    # loop infinitely
    loop do
        # call the board rendering method
        @board.render

        # ask for coordinates from the current player
        @current_player.get_coordinates

        # check if game is over
        break if check_game_over

        # switch players
        switch_players
    end
end
switch_players() click to toggle source

switch_players

# File lib/anakhi.rb, line 72
def switch_players
    if @current_player == @player_x
        @current_player = @player_y
    else
        @current_player = @player_x
    end
end