如何更改此代码以使用文件I / O为我的游戏保存“高分”文件

时间:2013-07-02 14:39:09

标签: ruby file-io

我现在使用YAML来存储数据,它可以用于创建和存储文件。我仍在尝试弄清楚如何使用text-table gem以正确的格式将表格打印到终端。这是代码:

def highscore
if File.exists?('highscore.txt')
    hs = YAML.load_file("highscore.txt")
else
    hs = {
        1 =>  { player: '', score: 0, date: '' },
        2 =>  { player: '', score: 0, date: '' },
        3 =>  { player: '', score: 0, date: '' },
        4 =>  { player: '', score: 0, date: '' },
        5 =>  { player: '', score: 0, date: '' },
        6 =>  { player: '', score: 0, date: '' },
        7 =>  { player: '', score: 0, date: '' },
        8 =>  { player: '', score: 0, date: '' },
        9 =>  { player: '', score: 0, date: '' },
        10 => { player: '', score: 0, date: '' },
        }
end

(1..10).each do |rank|
  t = Time.now
  if @grand_total > hs[rank][:score]
    hs[rank][:score] = @grand_total
    hs[rank][:date] = "#{t.month}/#{t.day}/#{t.year}" 
    puts "Congratulations you set a new HIGH SCORE! Enter your initials."
    initials = gets.chomp.upcase
    hs[rank][:player] = initials
    break
  else
    puts "Sorry, you didn't get a high score. Try again!"
  end
end

File.write('highscore.txt', hs.to_yaml)

puts hs.to_table
end

1 个答案:

答案 0 :(得分:0)

我建议您考虑使用YAML数据序列化程序。它创建了一个格式良好且可读的文件,可以被其他语言轻松阅读。

require 'pp'
require 'yaml'

hs = [["RANK", "PLAYER", "SCORE", "DATE"],
      ["1st", "-", 0, "-"],
      ["2nd", "-", 0, "-"],
      ["3rd", "-", 0, "-"],
      ["4th", "-", 0, "-"],
      ["5th", "-", 0, "-"],
      ["6th", "-", 0, "-"],
      ["7th", "-", 0, "-"],
      ["8th", "-", 0, "-"],
      ["9th", "-", 0, "-"],
      ["10th", "-", 0, "-"]]

puts hs.to_yaml

看起来像:

---
- - RANK
  - PLAYER
  - SCORE
  - DATE
- - 1st
  - '-'
  - 0
  - '-'
- - 2nd
  - '-'
  - 0
  - '-'
- - 3rd
  - '-'
  - 0
  - '-'
- - 4th
  - '-'
  - 0
  - '-'
- - 5th
  - '-'
  - 0
  - '-'
- - 6th
  - '-'
  - 0
  - '-'
- - 7th
  - '-'
  - 0
  - '-'
- - 8th
  - '-'
  - 0
  - '-'
- - 9th
  - '-'
  - 0
  - '-'
- - 10th
  - '-'
  - 0
  - '-'

保存到文件很简单:

File.write('path/to/file.yaml', hs.to_yaml)

阅读它很简单:

hs = YAML.load_file('path/to/file.yaml')

阅读“Yaml Cookbook”以获取有关Ruby和YAML结构如何相关的更多信息。

那就是说,我建议你的高分表使用不同的数据结构。而不是访问难以访问的数组数组,使用哈希散列:

hs = {
  1 =>  { player: '', score: 0, date: '' },
  2 =>  { player: '', score: 0, date: '' },
  3 =>  { player: '', score: 0, date: '' },
  4 =>  { player: '', score: 0, date: '' },
  5 =>  { player: '', score: 0, date: '' },
  6 =>  { player: '', score: 0, date: '' },
  7 =>  { player: '', score: 0, date: '' },
  8 =>  { player: '', score: 0, date: '' },
  9 =>  { player: '', score: 0, date: '' },
  10 => { player: '', score: 0, date: '' },
}

这将使YAML文件更易于理解:

---
1:
  :player: ''
  :score: 0
  :date: ''
2:
  :player: ''
  :score: 0
  :date: ''
3:
  :player: ''
  :score: 0
  :date: ''
4:
  :player: ''
  :score: 0
  :date: ''
5:
  :player: ''
  :score: 0
  :date: ''
6:
  :player: ''
  :score: 0
  :date: ''
7:
  :player: ''
  :score: 0
  :date: ''
8:
  :player: ''
  :score: 0
  :date: ''
9:
  :player: ''
  :score: 0
  :date: ''
10:
  :player: ''
  :score: 0
  :date: ''

并将简化您对代码中表的访问。

通常,您的代码需要进行重大清理。编写代码的一个非常强大的原则是“干”AKA“不要重复自己”。您的代码中有大量冗余,可以大大简化。这需要在一个单独的问题中处理,但是,基本上你的得分处理代码可以简化为case语句和一种在哈希值中存储值的方法。