I'm building a simple tic-tac-toe game and I've totally hit a wall with my grid.
I'd like to "puts @grid" including player input every time players have made their choice. How can I achieve this? I'm almost a total beginner with Ruby and have never build any games before. Any help is greatly appreciated, thanks in advance!
I tried making two different grids (grid and grid_with_markers) but couldn't figure out where to go from there. Looking back, having two grids also just seemed like a bad idea. I also tried having a Hash (marker_positions), but that seemed overly complicated compared to an Array.
Here is the grid
def initialize
@possible_choice = [1,2,3,4,5,6,7,8,9]
@marker_positions = [1,2,3,4,5,6,7,8,9]
@grid = "
|----|----|----|
| #{@marker_positions[0]} | #{@marker_positions[1]} | #{@marker_positions[2]} |
|----|----|----|
| #{@marker_positions[3]} | #{@marker_positions[4]} | #{@marker_positions[5]} |
|----|----|----|
| #{@marker_positions[6]} | #{@marker_positions[7]} | #{@marker_positions[8]} |
|----|----|----|
"
end
I'd like to use a method add_markers to show the grid with user input. So when a player selects number 1 @marker_positions[0] would be replaced as "X" (or "O"). Number 4 would replace @marker_positions[3] etc.
Edit: I realised the title of this post was misleading since I actually want to replace elements of @marker_positions Array with Strings ("X" or "O"). But the replaced element is selected based on user input, in other words, player_one and player_two Arrays.
def add_markers
puts @grid
end
Here's the player_one_turn method
def player_one_turn()
puts "Player One, make your choice:"
p @possible_choice
add_markers
@@player_one << @possible_choice.delete(gets.chomp.to_i)
p "Player One has chosen: #{@@player_one}"
end
And here's my entire tictactoe.rb file.
class Grid
WINNING_COMBOS = [
[1,2,3],[4,5,6],[7,8,9],
[1,4,7],[2,5,8],[3,6,9],
[1,5,9],[3,5,7]
]
attr_accessor :possible_choice
attr_accessor :marker_positions
attr_accessor :grid
def initialize
@possible_choice = [1,2,3,4,5,6,7,8,9]
@marker_positions = [1,2,3,4,5,6,7,8,9]
@grid = "
|----|----|----|
| #{@marker_positions[0]} | #{@marker_positions[1]} | #{@marker_positions[2]} |
|----|----|----|
| #{@marker_positions[3]} | #{@marker_positions[4]} | #{@marker_positions[5]} |
|----|----|----|
| #{@marker_positions[6]} | #{@marker_positions[7]} | #{@marker_positions[8]} |
|----|----|----|
"
end
def add_markers
puts @grid
end
end
class Game < Grid
@@player_one = Array.new
@@player_two = Array.new
def game
puts
puts "*** This is a tic-tac-toe game for two human players. ***"
puts
loop do
player_one_turn()
puts
if has_won?(@@player_one)
puts "The game has ended. Player One has won!"
puts
return
end
break if @@player_one.length == 5 || @@player_one.include?(nil)
player_two_turn()
puts
if has_won?(@@player_two)
puts "The game has ended. Player Two has won!"
puts
return
end
end
end
def player_one_turn()
puts "Player One, make your choice:"
p @possible_choice
add_markers
@@player_one << @possible_choice.delete(gets.chomp.to_i)
p "Player One has chosen: #{@@player_one}"
end
def player_two_turn()
puts "Player Two, make your choice:"
p @possible_choice
add_markers
@@player_two << @possible_choice.delete(gets.chomp.to_i)
p "Player Two has chosen: #{@@player_two}"
end
def has_won?(player)
WINNING_COMBOS.any? { |combo| (player & combo).size == combo.size}
end
end
new_game = Game.new
new_game.game
(I know it isn't very clean. Thank you for taking time to read all the way down here.)
@marker_positionsa 3x3 matrix, so that whenever a user inputs a position, you find the corresponding row and column, and update the value stored in that position?