-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.rb
137 lines (119 loc) · 3.03 KB
/
game.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require_relative 'board'
require 'yaml'
require 'io/console'
class Game
attr_reader :board
def initialize
filename = prompt_for_load
@board = filename ? Board.from_file(filename) : Board.new
play
end
def play
until board.over?
board.display
make_move
end
if board.won?
board.display
puts "Congrats! You win!"
store_time
display_leaderboard
else
board.render_losing_board
end
end
def store_time
username = prompt_for("username")
winners_lines = File.readlines('winners.txt').map(&:chomp)
winners_and_scores = winners_lines.map{|line| line.split(", ")}
winners_and_scores << [board.elapsed_time, username]
winners_and_scores = winners_and_scores.sort_by{|el| el[0].to_i}[0..9]
text = winners_and_scores.map {|el| el.join(", ")}.join("\n")
File.open('winners.txt', 'w+') {|f| f.puts(text)}
end
def display_leaderboard
system("clear")
winners_lines = File.readlines('winners.txt').map(&:chomp)
winners_and_scores = winners_lines.map{|line| line.split(", ")}
puts " LEADERBOARD:"
puts " Time (seconds), Username"
winners_and_scores.each_with_index do |winner, idx|
index = "#{idx + 1}".ljust(3)
time = "#{winner[0].to_i}".ljust(16)
name = "#{winner[1]}"
puts "#{index}#{time}#{name}"
end
end
def save_and_quit
filename = prompt_for("filename")
board.elapsed_time += Time.now - board.start_time
File.open("#{filename}.yaml", "w"){|f| f.puts(board.to_yaml)}
abort("Saved to #{filename}.yaml")
end
def prompt_for(name)
print "Please enter a #{name}: "
input = gets.chomp
until input.length > 0
print "Please enter a valid #{name}: "
input = gets.chomp
end
input
end
def prompt_for_load
print "Do you want to load from file (y/n)? "
action = gets.chomp.downcase
until action == "y" || action == "n"
print "Please choose either \"y\" or \"n\":"
action = gets.chomp.downcase
end
return nil if action == "n"
print "Please enter the filename to load: "
filename = gets.chomp.downcase
end
def read_char
STDIN.echo = false
STDIN.raw!
input = STDIN.getc.chr
if input == "\e" then
input << STDIN.read_nonblock(3) rescue nil
input << STDIN.read_nonblock(2) rescue nil
end
ensure
STDIN.echo = true
STDIN.cooked!
return input
end
def make_move
move_made = false
until move_made
move_made = move_highlight
end
end
def move_highlight
c = read_char
case c
when "\e[A"
board.move_highlight([-1, 0])
when "\e[B"
board.move_highlight([1, 0])
when "\e[C"
board.move_highlight([0, 1])
when "\e[D"
board.move_highlight([0, -1])
when "r"
board[*board.highlighted].reveal
return true
when "f"
board[*board.highlighted].flag
return true
when "q"
save_and_quit
end
system("clear")
board.display
false
end
end
if $PROGRAM_NAME == __FILE__
Game.new()
end