This is about the same as the code in my previous post, but I removed some
unnecessary stuff, used more descriptive variable names, and slightly
improved the computer's strategy.
There is no error checking, so don't input numbers that are not available
or letters, etc.
Sorry, no hashes or 2D arrays.


x_moves, o_moves = [],[]
open_spaces,grid = (1..9).to_a, (0..9).to_a
puts
p [grid[8],grid[1],grid[6]].map{|f| f.to_s}
p [grid[3],grid[5],grid[7]].map{|f| f.to_s}
p [grid[4],grid[9],grid[2]].map{|f| f.to_s}
puts
puts "You are x. Please go first. Choose a numbered position."
x = gets.chomp.to_i
x_moves << x
grid[x] = "x"
open_spaces -= [x]
p [grid[8],grid[1],grid[6]].map{|f| f.to_s}
p [grid[3],grid[5],grid[7]].map{|f| f.to_s}
p [grid[4],grid[9],grid[2]].map{|f| f.to_s}
puts

4.times do
  #Random selection
  o = open_spaces.sample

  o = 5 if o_moves.size==0 && x_moves[-1] != 5
  o = 2 if o_moves.size==0 && x_moves[-1] == 5

  # If there is an open direction, go for it.
  open_direction = []
  open_direction = open_spaces.combination(2).to_a.select{|e,f|
o_moves[-1]+e+f==15}.flatten if o_moves.size>0
  o = open_direction[0] if open_direction.size>0

  # If 2 x's in line, block it.
  block_it = x_moves.combination(2).to_a.map{|u,w| 15-u-w}.select{|e| e>0
&& grid[e]==e}
  o = block_it[0] if block_it.size>0

  # If 2 o's in line, choose third in line for the win.
  win = o_moves.combination(2).to_a.map{|u,w| 15-u-w}.select{|e| e>0 &&
grid[e]==e}
  o = win[0] if win.size > 0

  o_moves << o
  grid[o] = "o"
  open_spaces -= [o]
  puts
  p [grid[8],grid[1],grid[6]].map{|f| f.to_s}
  p [grid[3],grid[5],grid[7]].map{|f| f.to_s}
  p [grid[4],grid[9],grid[2]].map{|f| f.to_s}
  puts
  if o_moves.combination(3).to_a.map{|a,b,c| a+b+c}.include?(15)
    p "o wins"
    break
  end

  puts "Your turn."
  x = gets.chomp.to_i
  x_moves << x
  grid[x] = "x"
  open_spaces -= [x]
  if x_moves.combination(3).to_a.map{|a,b,c| a+b+c}.include?(15)
    p "x wins"
    p [grid[8],grid[1],grid[6]].map{|f| f.to_s}
    p [grid[3],grid[5],grid[7]].map{|f| f.to_s}
    p [grid[4],grid[9],grid[2]].map{|f| f.to_s}
    break
  end
end



Harry