-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rb
More file actions
54 lines (51 loc) · 1006 Bytes
/
Copy pathmain.rb
File metadata and controls
54 lines (51 loc) · 1006 Bytes
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
require 'json'
require_relative 'lib/board.rb'
class Game
def self.play()
file = 'save.txt'
if File.exist?(file)
data = File.open(file, 'r')
json = data.read
p json
board = Board.from_json(json)
data.close()
else
board = Board.new()
end
p "Welcome to chess!"
switch = 0
color = "white"
p "It's #{color}'s turn."
loop do
puts board.render_board()
p "This game uses long algebraic notation."
p "I.E. Bc1-f4 will move the bishop in c1 to f4."
move = gets.chomp()
if move.downcase() == "save"
data = board.to_json
savefile = File.open(file, 'w')
savefile.write(data)
savefile.close
p "Game saved!"
exit
end
if board.make_move(move, color)
puts board.render_board()
if board.checkmate?()
break
end
switch += 1
if switch % 2 == 0
color = "white"
else
color = "black"
end
p "It's #{color}'s turn."
else
next
end
end
p "Game ended! #{color} won!"
end
end
Game.play()