Last active
August 29, 2015 14:24
-
-
Save ERuban/56a05bb87c1d668abe52 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
@hh = {'1' => ' ', '2' => ' ', '3' => ' ', | |
'4' => ' ', '5' => ' ', '6' => ' ', | |
'7' => ' ', '8' => ' ', '9' => ' '} | |
def print_table | |
puts '' | |
puts "\t\tChoose" | |
puts " #{@hh['1']}|#{@hh['2']}|#{@hh['3']}\t\t1|2|3" | |
puts " -----\t\t-----" | |
puts " #{@hh['4']}|#{@hh['5']}|#{@hh['6']}\t\t4|5|6" | |
puts " -----\t\t-----" | |
puts " #{@hh['7']}|#{@hh['8']}|#{@hh['9']}\t\t7|8|9" | |
end | |
def user_move | |
print 'Choose place: ' | |
@coord = gets.strip | |
end | |
def comp_move | |
@coord = rand(1..9).to_s | |
end | |
def check_win | |
if @hh['1'] == @hh['4'] && @hh['4'] == @hh['7'] && @hh['7'] != ' ' | |
puts "#{@hh['1']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['2'] == @hh['5'] && @hh['5'] == @hh['8'] && @hh['8'] != ' ' | |
puts "#{@hh['2']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['3'] == @hh['6'] && @hh['6'] == @hh['9'] && @hh['9'] != ' ' | |
puts "#{@hh['3']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['1'] == @hh['2'] && @hh['2'] == @hh['3'] && @hh['3'] != ' ' | |
puts "#{@hh['1']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['4'] == @hh['5'] && @hh['5'] == @hh['6'] && @hh['6'] != ' ' | |
puts "#{@hh['4']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['7'] == @hh['8'] && @hh['8'] == @hh['9'] && @hh['9'] != ' ' | |
puts "#{@hh['7']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['1'] == @hh['5'] && @hh['5'] == @hh['9'] && @hh['9'] != ' ' | |
puts "#{@hh['1']} wins!" | |
print_table | |
puts '' | |
exit | |
elsif @hh['3'] == @hh['5'] && @hh['5'] == @hh['7'] && @hh['7'] != ' ' | |
puts "#{@hh['3']} wins!" | |
print_table | |
puts '' | |
exit | |
end | |
end | |
print 'Which symbol U will play? (X/O): ' | |
@user_sym = gets.strip.capitalize | |
if @user_sym == 'X' | |
@comp_sym = 'O' | |
else | |
@comp_sym = 'X' | |
end | |
loop do | |
print_table | |
puts "Comp symbol: #{@comp_sym}" | |
puts "User symbol: #{@user_sym}" | |
while true | |
user_move | |
puts @hh[@coord] | |
if @hh[@coord] == ' ' | |
@hh[@coord] = @user_sym | |
break | |
else | |
puts 'Wrong coordinates! Try again!' | |
end | |
end | |
check_win | |
print_table | |
while true | |
comp_move | |
if @hh[@coord] == ' ' | |
@hh[@coord] = @comp_sym | |
break | |
end | |
end | |
check_win | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment