class Tic_Tac_Toe

Public Instance Methods

displayBoard(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 12
def displayBoard(board)
    board.each do |row|
        row.each do |space|
            print "| #{space} |"
        end
        puts
    end
end
fullBoard(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 69
def fullBoard(board)
    gameOver = false
    count = 0
    board.each do |row|
        row.each do |space|
            if space == "X" || space =="O"
                count = count + 1
            end
        end
    end
    if count == 9
        gameOver = true
    end
    return gameOver
end
initBoard(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 2
    def initBoard(board)
    pos = 1
    for row in 0..2 do
        for col in 0..2 do
            board[row][col] = pos
            pos = pos + 1
        end
    end
end
turnPlayed(board,player,move) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 21
def turnPlayed(board,player,move)
    if move < 0 || move > 8
        puts "Wrong move! Try again."
        return false
    end
    col = move % 3
    row = (move - col) / 3
    if board[row][col].to_i.between?(1,9)
        board[row][col] = player
        return true
    else
        puts "Wrong Move! Try again."
        return false
    end
end
winningCols(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 48
def winningCols(board)
    gameOver = false
    for col in 0..2 do
        if board[0][col] == board[1][col] && board[1][col] == board[2][col]
            gameOver = true
        end
    end
    return gameOver
end
winningDiagonals(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 58
def winningDiagonals(board)
    gameOver = false
    if board[0][0]==board[1][1] && board[1][1]==board[2][2]
        gameOver = true
    end
    if board[0][2]==board[1][1] && board[1][1]==board[2][0]
        gameOver = true
    end
    return gameOver
end
winningRows(board) click to toggle source
# File lib/tic_tac_toe_cn.rb, line 38
def winningRows(board)
    gameOver = false
    for row in 0..2 do
        if board[row][0] == board[row][1] && board[row][1] == board[row][2]
            gameOver = true
        end
    end
    return gameOver
end