Created
September 11, 2014 00:47
-
-
Save anonymous/2c029e7e81288208ecc8 to your computer and use it in GitHub Desktop.
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
puts "Welcome to Tic Tac Toe, let's play!" | |
puts "Player 1, what is your name?" | |
player1 = gets.chomp | |
puts "Welcome #{player1}. Player 2, what is your name?" | |
player2 = gets.chomp | |
puts "Thanks #{player1} and #{player2}. #{player1}, make your first move by | |
typing the number that corresponds to the grid below" | |
[0,3,6].each do |item| | |
puts "#{1+item} | #{2+item} | #{3+item}\n" | |
end | |
box = { | |
'1' => ' ', | |
'2' => ' ', | |
'3' => ' ', | |
'4' => ' ', | |
'5' => ' ', | |
'6' => ' ', | |
'7' => ' ', | |
'8' => ' ', | |
'9' => ' ' | |
} | |
#keys | |
puts ' ' | |
def player2_winner?(box) | |
if box['1'] == 'X' and box['2'] == 'X' and box['3'] == 'X' | |
return true | |
elsif box['4'] == 'X' and box['5'] == 'X' and box['6'] == 'X' | |
return true | |
elsif box['7'] == 'X' and box['8'] == 'X' and box['9'] == 'X' | |
return true | |
elsif box['1'] == 'X' and box['4'] == 'X' and box['7'] == 'X' | |
return true | |
elsif box['2'] == 'X' and box['5'] == 'X' and box['8'] == 'X' | |
return true | |
elsif box['3'] == 'X' and box['6'] == 'X' and box['9'] == 'X' | |
return true | |
elsif box['1'] == 'X' and box['5'] == 'X' and box['9'] == 'X' | |
return true | |
elsif box['3'] == 'X' and box['5'] == 'X' and box['7'] == 'X' | |
return true | |
end | |
end | |
def player1_winner?(xyz) | |
if xyz['1'] == '0' and xyz['2'] == '0' and xyz['3'] == '0' | |
return true | |
elsif xyz['4'] == '0' and xyz['5'] == '0' and xyz['6'] == '0' | |
return true | |
elsif xyz['7'] == '0' and xyz['8'] == '0' and xyz['9'] == '0' | |
return true | |
elsif xyz['1'] == '0' and xyz['4'] == '0' and xyz['7'] == '0' | |
return true | |
elsif xyz['2'] == '0' and xyz['5'] == '0' and xyz['8'] == '0' | |
return true | |
elsif xyz['3'] == '0' and xyz['6'] == '0' and xyz['9'] == '0' | |
return true | |
elsif xyz['1'] == '0' and xyz['5'] == '0' and xyz['9'] == '0' | |
return true | |
elsif xyz['3'] == '0' and xyz['5'] == '0' and xyz['7'] == '0' | |
return true | |
end | |
end | |
[1,2,3,4,5,6,7,8,9].each do |item| | |
move = gets.chomp | |
if not box[move] == ' ' | |
puts 'Please try again!' | |
redo | |
end | |
if item.even? | |
box[move] = 'X' | |
elsif item.odd? | |
box[move] = '0' | |
end | |
if player1_winner?(box) and item > 3 | |
puts "#{player1} won! Congrats" | |
return | |
end | |
if player2_winner?(box) and item > 3 | |
puts "#{player2} won! Congrats" | |
return | |
end | |
[0,3,6].each do |item| | |
puts "#{box[(1+item).to_s]} | #{box[(2+item).to_s]} | #{box[(3+item).to_s]}\n" | |
end | |
end | |
puts 'A draw. Oh well.' | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment