Skip to content

Instantly share code, notes, and snippets.

@harrisonmalone
Last active October 30, 2020 08:59
Show Gist options
  • Save harrisonmalone/1ecfcde62e573309fab481dcf6767d07 to your computer and use it in GitHub Desktop.
Save harrisonmalone/1ecfcde62e573309fab481dcf6767d07 to your computer and use it in GitHub Desktop.
# board begins its life as an array of empty strings
board = [" ", " ", " ", " ", " ", " ", " ", " ", " "]
# method for displaying the tictactoe board
def display_board(board)
puts " 1 2 3"
puts "A #{board[0]} | #{board[1]} | #{board[2]}"
puts " ----------"
puts "B #{board[3]} | #{board[4]} | #{board[5]}"
puts " ----------"
puts "C #{board[6]} | #{board[7]} | #{board[8]}"
end
# method to determine if player is an "X" or an "O"
def player_token(num)
if num == 1
player_token = "X"
else
player_token = "O"
end
return player_token
end
# method for checking if there is a winner or draw
def check_for_winner(board, num)
win = false
# by returning false everytime we maintain our main in game until loop, we use the exit method to exit out of the game if need be
winner_combinations = [
[board[0], board[1], board[2]],
[board[0], board[4], board[8]],
[board[0], board[3], board[6]],
[board[3], board[4], board[5]],
[board[6], board[7], board[8]],
[board[6], board[4], board[2]],
[board[1], board[4], board[7]],
[board[2], board[5], board[8]]
]
# we loop through the different winning combinations to see if a player has won yet
winner_combinations.each do |item|
if item[0] == "X" && item[1] == "X" && item[2] == "X"
puts ""
puts "#{toggle_player(num)} wins!"
exit
elsif item[0] == "O" && item[1] == "O" && item[2] == "O"
puts ""
puts "#{toggle_player(num)} wins!"
exit
end
end
return win
end
def add_mark(board, num)
# we get a new token ("X", "O") each time we run this method, it uses the num we assign to both players
token = player_token(num)
# this running variable keeps our while loop running until it's set as false when we assign a token to a place on the board
running = true
# we use this positions array to extract the index value of what the position.gets.chomp is
positions = ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]
while running do
puts ""
puts "#{toggle_player(num)}"
puts "Pick a square (eg A1)"
print "> "
position = gets.chomp.downcase
puts ""
# this where we grab the index so we know where to put the token in the board array
index_of_position = positions.index(position)
# this checks for correct formatting, if the positions array doesn't include the value stored in the gets.chomp then this returns true and we start the loop again
if !positions.include?(position)
puts ""
display_board(board)
puts ""
puts "Incorrect formatting of pick. Please choose a square on board above."
# this checks to see if a token already exists on the board based on what the gets.chomp was, we use the index_of_position variable on board, so if that spot on board doesn't include an empty string then it returns true and we start the loop again
elsif !board[index_of_position].include?(" ")
puts ""
puts "That spot has already been taken! Try again"
# if the conditionals above both return false then we know that the formatting is correct and that place on the board hasn't been taken, therefore we can just add to our array using the index_of_position and the token we created above
else
board[index_of_position] = token
running = false
end
end
# we display the board after every add_mark method call, so after every player turn an updated board is displayed
display_board(board)
# here we return the check_for_winner method which will always return false unless one the condtionals inside of our winner_combinations block returns true at which point the program will exit
return check_for_winner(board, num)
end
def draw(board)
# a draw is returned if there are no squares on board that are empty strings
if !board.include?(" ")
puts ""
puts "It's a draw!"
exit
end
end
# we use this method for string interpolation, tells us whether it's player 1 or player 2's turn
def toggle_player(num)
if num == 1
return "Player 1"
else
return "Player 2"
end
end
# start of the game
puts "Let's play Tic Tac Toe!"
puts ""
display_board(board)
# here we create a loop until there is a win or a draw, check_for_winner will return false unless it gets through one of the condtionals where we use the exit keyword
until check_for_winner(board, num = nil)
# nil needs a default as otherwise it's undefined in the first loop
add_mark(board, 1)
# we check for the draw here as player 1 has one extra turn, if it was to be draw it would be player 1 having the final turn
draw(board)
add_mark(board, 2)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment