-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.rb
53 lines (44 loc) · 1.29 KB
/
play.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
# The Game class starts the game and can save the game.
require 'json'
require "./lib/chess.rb"
class Game
def self.run
system("clear")
puts "Welcome!"
puts "Let's play chess!"
if File.exist?("save_file.json")
game = load_game
else
game = new_game
end
# If the play method returns true, save the game.
if game.play
save_state = game.to_json
save_file = File.open("save_file.json", "w") { |f| f.puts save_state}
puts "Your current game has been saved. See you again soon!"
end
end
def self.load_game
puts "Would you like to (1) load your saved game or (2) start a new game? \nPlease enter 1 or 2."
answer = gets.chomp
if answer == "1"
save_file = File.read("save_file.json")
save_state = JSON.parse(save_file)
File.delete("save_file.json")
game = Chess.load_game(save_state)
else
new_game
end
end
def self.new_game
puts "\n\nWhat is the first player's name?"
name_1 = gets.chomp
puts "\n\nWhat is the second player's name?"
name_2 = gets.chomp
random_names = [name_1, name_2].shuffle
new_state = File.read("new_state.json")
starting_board = JSON.parse(new_state)
game = Chess.new_game(random_names[0], random_names[1], starting_board)
end
end
Game.run