Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,563 questions

56,778 answers

573 users

How to write the Tic-Tac-Toe game in Ruby

1 Answer

0 votes
#
# A simple Tic‑Tac‑Toe game written in Ruby.
#
# Highlights:
#   - The board is represented as an Array of 9 characters.
#   - Functions handle printing, move validation, win checking,
#     and turn progression.
#   - The game loop is clear and easy to follow.
#   - Comments explain the reasoning behind each part of the program.
#

#---------------------------------------------------------------
# Print the board in a 3×3 layout
#---------------------------------------------------------------
def print_board(board)
  puts
  board.each_with_index do |cell, i|
    print cell
    if (i + 1) % 3 == 0
      puts
    else
      print " | "
    end
  end
  puts
end

#---------------------------------------------------------------
# Check if a player has won
#---------------------------------------------------------------
def check_win(board, player)
  wins = [
    [0,1,2], [3,4,5], [6,7,8],   # rows
    [0,3,6], [1,4,7], [2,5,8],   # columns
    [0,4,8], [2,4,6]             # diagonals
  ]

  wins.any? { |a, b, c| board[a] == player && board[b] == player && board[c] == player }
end

#---------------------------------------------------------------
# Check if the board is full (draw)
#---------------------------------------------------------------
def board_full(board)
  board.none? { |c| c == " " }
end

#---------------------------------------------------------------
# Attempt to place a move; return true if successful
#---------------------------------------------------------------
def place_move(board, pos, player)
  return false unless pos.between?(0, 8)
  return false unless board[pos] == " "

  board[pos] = player
  true
end

#---------------------------------------------------------------
# Main game loop
#---------------------------------------------------------------
board = Array.new(9, " ")
current_player = "X"

puts "Tic‑Tac‑Toe"
print_board(board)

loop do
  print "Player #{current_player}, enter position (0‑8): "
  input = gets&.strip

  unless input =~ /^\d+$/
    puts "Invalid input. Try again."
    next
  end

  pos = input.to_i

  unless place_move(board, pos, current_player)
    puts "Invalid move. Try again."
    next
  end

  print_board(board)

  if check_win(board, current_player)
    puts "Player #{current_player} wins!"
    break
  end

  if board_full(board)
    puts "It's a draw!"
    break
  end

  current_player = current_player == "X" ? "O" : "X"
end

__END__


=begin
run:

Tic‑Tac‑Toe

  |   |  
  |   |  
  |   |  

Player X, enter position (0‑8): 3

  |   |  
X |   |  
  |   |  

Player O, enter position (0‑8): 5

  |   |  
X |   | O
  |   |  

Player X, enter position (0‑8): 8

  |   |  
X |   | O
  |   | X

Player O, enter position (0‑8): 1

  | O |  
X |   | O
  |   | X

Player X, enter position (0‑8): 0

X | O |  
X |   | O
  |   | X

Player O, enter position (0‑8): 7

X | O |  
X |   | O
  | O | X

Player X, enter position (0‑8): 6

X | O |  
X |   | O
X | O | X

Player X wins!

=end

 



answered Aug 27 by avibootz
...