如何更改此代码以使用文件 I/O 为我的游戏保存"High Score"文件



我现在使用YAML来存储数据,它可以创建和存储文件。我仍在尝试如何使用文本表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

我建议您考虑使用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: ''

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

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

最新更新