Created
March 12, 2014 15:26
-
-
Save alicht/9509184 to your computer and use it in GitHub Desktop.
Tic-Tac-Toe written in Ruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Establishing what the board pieces are now, so I can call it below during the introduction | |
board_pieces = ["X" , "0"] | |
@player1 = "X" | |
@player2 = "0" | |
# Introducing the players | |
puts "Welcome! Lets play Tic Tac Toe!" | |
puts "Player 1, what is your name?" | |
@player1_name = gets.chomp.capitalize | |
puts "Nice to meet you #{@player1_name} you will be the letter #{@player1}. " | |
puts "And player 2, what is your name?" | |
@player2_name = gets.chomp.capitalize | |
puts "And nice to meet you #{@player2_name} you will be the number #{@player2}. " | |
puts "Lets begin." | |
# Establishing what the slots are on the board, by creating a Hash with keys representing the positions on the board.The "@" signifies that we're working with an instance variable. @slots marks the areas of the board. ""=>"" is a hash rocket and will tell us which slot will get which mark (ie an "X" or an "0"). | |
@slots = { | |
"a1"=>" ", "a2"=>" ", "a3"=>" ", | |
"b1"=>" ", "b2"=>" ", "b3"=>" ", | |
"c1"=>" ", "c2"=>" ", "c3"=>" ", | |
} | |
# Defining what a sample board looks like so the players know how the board is labeled. | |
def sample_board | |
puts "------------------------------------------------------------------" | |
puts " a b c" | |
puts " " | |
puts " 1 #{@slots["a1"]}|#{@slots["b1"]}|#{@slots["c1"]}" | |
puts " ------" | |
puts " 2 #{@slots["a2"]}|#{@slots["b2"]}|#{@slots["c2"]}" | |
puts " ------" | |
puts " 3 #{@slots["a3"]}|#{@slots["b3"]}|#{@slots["c3"]}" | |
puts "------------------------------------------------------------------" | |
end | |
sample_board | |
puts "Each number and letter on the above board represents a position on the board." | |
# As the player makes a turn the values will be added to the corresponding keys. | |
# will ask the players to make their moves by selecting a position. | |
# here I combined my initial turn method (I was previously using turn1 and turn2) and combined them into function. I listed the variables after the method in the parentheses. | |
def turn(player_name, player) | |
puts "#{player_name} you are '#{player}' - it's your move!" | |
player_move = gets.chomp | |
while @slots[player_move] != " " | |
puts "Spot is already taken! Go again!" | |
player_move = gets.chomp | |
end | |
@slots[player_move] = player | |
end | |
def current_game | |
puts "------------------------------------------------------------------" | |
puts " a b c" | |
puts " " | |
puts " 1 #{@slots["a1"]}|#{@slots["b1"]}|#{@slots["c1"]}" | |
puts " ------" | |
puts " 2 #{@slots["a2"]}|#{@slots["b2"]}|#{@slots["c2"]}" | |
puts " ------" | |
puts " 3 #{@slots["a3"]}|#{@slots["b3"]}|#{@slots["c3"]}" | |
puts "------------------------------------------------------------------" | |
end | |
# I originally had 16 conditions here which outlined all possible winning combinations. However, 8 were just essentially redundant (ie 8 player1, and 8 player2). Here I also created a function, passed the argument to the function, from another function I'll call the winning _combinations (it's a function here, opposed to a method, because it's not associataed with a class). I also got rid of the "@" because it's no longer an instance variable | |
def winning_combinations(player_name, win) | |
if @slots.values_at('a1','b2','c3') == win #arguements in the function | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('a3','b2','c1') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('a1','b1','c1') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('a2','b2','c2') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('a3','b3','c3') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('a1','a2','a3') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('b1','b2','b3') == win | |
puts "#{player_name} wins!" | |
elsif @slots.values_at('c1','c2','c3') == win | |
puts "#{player_name} wins!" | |
else | |
return false #lowercase false is already defined constant variable | |
end | |
true #if i dont have the true the last statement is an if block which doesnt return anything. Wont return anything because puts doesnt return anything | |
end | |
# lastly, I also modified the game status here. I originally had 8 conditions to win, but decided to combine them and also make an i. p | |
def game_status | |
win1 = ["X", "X", "X"] | |
win2 = ["0", "0", "0"] | |
if winning_combinations(@player1_name, win1) #calling the winning combination function, and passing the correct name and win title to the winning combinations function. will go up and rename player name and continue step by step | |
then return true #return is special- ignore everything else in the fuction and go to the end | |
elsif winning_combinations(@player2_name, win2) | |
then return true #lowercase false is already defined constant variable | |
else | |
false | |
#if i dont have the true the last statement is an if block which doesnt return anything. Wont return anything because puts doesnt return anything | |
end | |
end | |
# created and added this game loop! | |
#replace by a for loop | |
#def game_loop | |
#for i in 1..9 | |
#turn1 | |
#puts | |
#end | |
def game_loop | |
for i in 1..9 | |
if i.odd? # "?" because of it's an instance where it's true/ false (without it would give me an error) | |
then turn(@player1_name, @player1) #we didnt have to do even because if its not odd by definition its even. when it goes to turn it'll also rename these 2 arguments. | |
else #is a catchall- doesnt take a condition (ie then turn(player2 etc)) | |
turn(@player2_name, @player2) | |
end | |
#based on i's value we'll know if it's an player1 move or player2 move | |
current_game | |
if game_status == true | |
then puts "Game Over" | |
break # here we're breaking the loop so that when the game is over the board notifies us. As long as i isnt 9 it'll keep looping. But break | |
elsif i == 9 | |
puts "Tie Game!" | |
end | |
end | |
end | |
game_loop #we're running this function that we defined above, here were calling the game loop that we just called above | |
#game loop is basically calling everything in my program; game status, turn, winning combinations all flow from it- kind of like a reverse pyramid. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment