So I’m building a game of tic tac toe, and to select who will go first I
have the user’s choosing any number between 1-5, 5 being the lucky
number. If 5 isn’t selected then whichever user selects the number
closest to 5 wins. My approach is predicated on the fact that user(1)
will go first in selecting a number between 1-5, so I’ve laid out all
the possibilities where user(1)
would be the winner and when user(2) would be the winner. I’ve set it up
where if user(1) wins, he will be the active player on all the odds turn
and vice versa for user(2).
So how do I take their user inputs and reference it through an array of
all the possibilities looking for any match that would execute an
action.
Here is part of my code that goes over this feature
class Game
def initialize
@view = View.new
puts “\e[H\e[2J”
main_menu
end
def main_menu
@view.main_menu
selection = gets.chomp
if selection == "1"
puts "Player one what is your name?"
name = gets.chomp
player_one = name
puts "Player two what is your name?"
name = gets.chomp
player_two = name
puts "#{player_one} please select any number between 1-5"
odds1 = gets.chomp
x = odds1
puts "#{player_two} please select any number between 1-5"
odds2 = gets.chomp
y = odds2
# Possibilities where player_one would win and be assigned all the
odd rounds
if [x, y] == [[2, 1], [3, 1], [3, 2], [4, 1], [4, 2], [4, 3], [5,
1], [5, 2], [5, 3], [5, 4]]
@score[:rounds_played] % 2 != 0 ? @active_player = @player_two :
@active_player = @player_one
# Possibilities where player_two would win and be assigned all the
odd rounds
if [x, y] == [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3], [2, 4],
[2, 5], [3, 4], [3, 5], [4, 5]]
@score[:rounds_played] % 2 != 0 ? @active_player =
@player_one : @active_player = @player_two