-
-
Save t0code/ee76715c25c90a3f6f48 to your computer and use it in GitHub Desktop.
Having trouble reading account/password data. Where am I wrong?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Menu | |
@@Choices = ["New Game", "Load Game", "Options", "Help"] | |
@@VirtualChoice = [1,2,3,4,5,6,7,8,9,10,11,12] | |
def main_menu() | |
puts "Devil Soldiers .Beta" | |
choice = get_input(@@Choices) | |
case choice | |
when 1 | |
new_game() #add this | |
when 2 | |
load_game() #add this | |
when 3 | |
options_menu() #add this | |
when 4 | |
help_menu() #add this | |
else | |
puts "Invalid Command." | |
main_menu() | |
end | |
end | |
def get_input(options = []) | |
num = 0 | |
puts "Choose one:" | |
options.each do |option| | |
num += 1 | |
puts " #{num} - #{option}" | |
end | |
choice = gets.chomp.to_i | |
@@VirtualChoice.each do |option| | |
if choice == option.to_i | |
return choice | |
end | |
end | |
get_input(options) | |
end | |
def output(text) | |
puts text | |
end | |
end | |
module Entity | |
def attack() | |
return @stats["attack"] | |
end | |
def defend() | |
return @stats["armour"] | |
end | |
def item_stat() | |
@items.each do |key, value| | |
case key | |
when "Sword" | |
if value == 1 | |
@stats["attack"] += 50 | |
end | |
when "Armour" | |
if value == 1 | |
@stats["armour"] += 100 | |
end | |
when "Helmet" | |
if value == 1 | |
@stats["Helmet"] += 50 | |
end | |
end | |
end | |
end | |
def equip_item(item) | |
@items.each do |key, value| | |
if key == item | |
@items[item] = 1 | |
end | |
end | |
end | |
end | |
class Enemy | |
include Entity | |
def initialize() | |
@who = String.new | |
@level | |
@hp = 560 | |
@stats = { | |
"attack" => 80, | |
"armour" => 80 | |
} | |
@items = Hash.new | |
end | |
end | |
class Hero | |
include Entity | |
def initialize(who, type) | |
@who = who | |
@class = type | |
@hp = 880 | |
@stats = { | |
"attack" => 100, | |
"armour" => 100 | |
} | |
@items = { | |
"Sword" => 0, | |
"Armour" => 0, | |
"Helmet" => 0 | |
} | |
end | |
def who_is() | |
puts "#{@who}, #{@class}, #{@stats}, #{@items}" | |
end | |
end | |
def write_account(acct, pass, type) | |
require "open-uri" | |
File.open("account.txt", "a") {|x| x.puts "#{acct}#{pass}#{type}"} | |
end | |
def check_account(acct, pass) | |
file = File.open("account.txt", "r") | |
while !file.eof? | |
line = file.readline | |
if line.include?(acct) && line.include?(pass) | |
return 1 | |
else | |
return 0 | |
end | |
end | |
end | |
def load_game() | |
puts "Enter your Username:" | |
username = gets.chomp.to_s | |
puts "Enter your password:" | |
password = gets.chomp | |
if check_account(username, password) == 1 | |
puts "account loaded" | |
#load account here | |
else | |
puts "Account not found." | |
main_menu() | |
end | |
end | |
def new_game() | |
newClass = ["Warrior", "Mage", "Warlock", "Archer"] | |
puts "Choose a name:" | |
choiceName = gets.chomp.to_s | |
puts "Enter a password:" | |
choicePass = gets.chomp.to_s | |
choiceClass = case Menu.get_input(newClass) | |
when 1 | |
"Warrior" | |
when 2 | |
"Mage" | |
when 3 | |
"Warlock" | |
when 4 | |
"Archer" | |
end | |
write_account(choiceName, choicePass, choiceClass) | |
player = Hero.new(choiceName, choiceClass) | |
player.who_is() | |
end | |
include Menu | |
Menu.main_menu() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment