-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchess.rb
65 lines (45 loc) · 1.31 KB
/
chess.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
require './board'
class Chess
attr_accessor :board_object, :dictionary
def initialize
@board_object = Board.new
create_board_hash
end
def create_board_hash
@dictionary = Hash.new
columns = %w(a b c d e f g h)
8.times do |row_idx|
8.times do |col_idx|
dictionary[columns[col_idx] + (8 - row_idx).to_s] = [row_idx, col_idx]
end
end
nil
end
def play
system "clear"
@board_object.display
until @board_object.game_over?(@board_object.turn)
puts "#{@board_object.turn} Please move (start,finish)"
user_choice = gets.chomp.split(",")
choice = [@dictionary[user_choice.first], @dictionary[user_choice.last]]
moved_message = @board_object.move(*choice)
system "clear"
if moved_message == true
@board_object.move_tracker[@board_object.turn].unshift("#{user_choice}")
@board_object.display
else
@board_object.display
puts moved_message
end
@turn == :white ? previous_turn = :black : previous_turn = :white
puts "CHECK!" if @board_object.check?(previous_turn)
end
@board_object.toggle_turn
return "#{@board_object.turn} WINS!" if @board_object.check?(@board_object.turn)
"Draw"
end
end
if __FILE__ == $PROGRAM_NAME
game = Chess.new; nil
game.play
end