Creating a console based text game and can't get the cursor to pop up a line into the proper location (Ruby) - ruby

Edit: using the Paint gem.
So I'm re-writing the shop system in my text based game I made in ruby. I'm attempting to get multiple colors onto a single line of text. I found a method that works but for some reason the first string (which includes the item id and the item name) is getting concated at 11 characters all the time.
each item value is an array. value[0] contains the name, and so on.
Here is the code that draws each item for the shop menu:
shopbag.each { |id, value|
if id != 0
string1 = "(" + id.to_s + ")" + " " + value[0]; str1 = string1.size
string2 = "ATTACK: #{value[1]}"; str2 = string2.size
string3 = "SPEED: #{value[2]}"; str3 = string3.size
pa string1; pa "\033[1A \033[#{str1}C #{string2}", :red, :bright; pa "\033[1A \033[#{str2}C #{string3}", :green, :bright
end
}
Here is a screenshot of what it looks like:
shop bug (click for image)
Note you can see a string size before each item, as I had a print line in there to verify that the size was correct. It is not present in my code snippet here. If it was working properly, it would show the full names before the attack and speed values.

Related

Syntax error ... Jython/Python

Hi I'm doing my first programming assignment (it's with Jython, i.e. Python using Java) and I've run into a syntax error on line 14 (bolded below). I've tried changing the variable to something less useful like "L" or "I" but it still gives the error. It's annoying because it makes no sense. I have tried indenting again and adding comments around it.
This is a program that outputs a picture of a soccer ball factory. It's as much as an artistic project as a comp sci project. So the printing looks complicated but is only like long checklist for building.
def prettyPic():
#building materials and parts
spacer = " "
ceiling_part = "-"
ball = "o"
wheel = ""
door_joint = "#"
left_half_arch = "/"
right_half_arch = "\\"
ladder = "\\"
wall = "|"
glass = (
#biox
**left_box = "u"
right_box = "u"**
#begin printing
print (spacer*30 + ceiling_part*30)
print (spacer*32 + wall*1) + (spacer*47 + wall*1)
#three balls, leaving space for drop
print (spacer*32 + wall*1) + (ball*27) + (wall*1)
#arches, not touching ceiling
etc, etc
The problem is in this line:
glass = (
This means that to glass variable, you assign tuple just like:
glass = (1, 2, 'some string')
Python interpreter searches for termination of just opened tuple but it finds only Python code that is not correct in this context.
Remove or comment out line with glass, or assign to glass variable some value.

Ruby + Prawn PDF: How to add space prefix in string inside of a table?

I have a two dimensional array:
line_items = []
line_item.product.book_versions.each do |book_version|
data = []
data << ""
data << " #{book_version.book.title} - #{book_version.isbn}" #<-- notice the extra spaces in the beginning of the string
data << "#{line_item.quantity}"
line_items << data
end
And I load this data into my table with pdf.table line_items ... do ... end
However, the extra spaces in my 2nd column don't show up. How would I escape these spaces so that they aren't stripped?
Depending on what you want to do, you can also use the constant Prawn::Text::NBSP. If it is purely blank space, then the column padding is what you want. However, I had a situation where I had to simulate a "checkmark space" such that an X character was underlined. My table looked like:
table([["<u>X</u>", "Do you agree to these terms and conditions?"]]) do
columns(0).style(:inline_format => true)
end
However, this produced a simple X with an underline. I wanted to underlined section to be wider, i.e., space (blank) characters that still received an underline. So I changed the table data to be
table([["<u>#{Prawn::Text::NBSP*3} X #{Prawn::Text::NBSP*3}</u>", ...]]) do
Then in the PDF, it looked like I wanted: ___X___ (with the X obviously underlined too).
You can do a hack with a character that prawns does not understand
I was able to do it with adding: ⁔ character before the text.
Prawns did not understand it, and ended up giving me a space :)
Your best bet will probably be to use a custom padding for that column. Something like this:
pdf.table line_items do
column(1).padding = [5, 5, 5, 30] # Default padding is 5; just increase the left padding
...
end
I don't think it is an escaping problem, maybe you should use a more formal way for spacing, try using the \t character instead of spaces. It is intended for that use.
line_items = []
line_item.product.book_versions.each do |book_version|
data = []
data << ""
data << "\t\t#{book_version.book.title} - #{book_version.isbn}"
data << "#{line_item.quantity}"
line_items << data
end

RegEx to remove new line characters and replace with comma

I scraped a website using Nokogiri and after using xpath I was left with the following string (which is a few td's pushed into one string).
"Total First Downs\n\t\t\t\t\t\t\t\t359\n\t\t\t\t\t\t\t\t274\n\t\t\t\t\t\t\t"
My goal is to make this into an array that looks like the following(it will be a nested array):
["Total First Downs", "359", "274"]
The issue is creating a regex equation that removes the escaped characters, subs in one "," but does not sub in a "," after the last set of integers. If the comma after the last set of integers is necessary, I could use #compact to get rid of the nil that occurs in the array. If you need the code on how I scraped the website here it is: (please note i saved the webpage for testing in order for my ip address to not get burned during the trial phase)
f = File.open('page')
doc = Nokogiri::HTML:(f)
f.close
number = doc.xpath('//tr[#class="tbdy1"]').count
stats = Array.new(number) {Array.new}
i = 0
doc.xpath('//tr[#class="tbdy1"]').each do |tr|
stats[i] << tr.text
i += 1
end
Thanks for your help
I don't fully understand your problem, but the result can be easily achieved with this:
"Total First Downs\n\t\t\t\t\t\t\t\t359\n\t\t\t\t\t\t\t\t274\n\t\t\t\t\t\t\t"
.split(/[\n\t]+/)
# => ["Total First Downs", "359", "274"]
Try with gsub
"Total First Downs\n\t\t\t\t\t\t\t\t359\n\t\t\t\t\t\t\t\t274\n\t\t\t\t\t\t\t".gsub("/[\n\t]+/",",")

ruby: building string with length constraint composed from many variable length strings

I thought I'd throw out this problem to see what elegant solutions folk
could come up with and, in the process, hopefully learn some new ruby
tricks.
I'll set the problem in the context of producing a twitter message,
which has a maximum length of 140 characters. I'm looking for a concise
function that will deliver a tweet no longer than 140 characters from
three inputs: text_a (mandatory), text_b (optional), boolean that
triggers a function that returns a string (optional).
(I've used the twitter-text gem to take byte, char, and encoding issues
out of play, as that is not the focus of the problem.)
The main constraint is that to achieve the required maximum length, it
is text_a that must be truncated.
Here's some long-winded sample code (working, I think) that hopefully
makes the requirement clear.
# encoding: utf-8
require 'twitter-text'
def tweet(text_a, text_b=nil, suffix=false)
text = "fixed preamble #{text_a}"
text << " #{text_b}" if text_b
text << get_suffix if suffix
return text unless Twitter::Validation.tweet_invalid?(text) == :too_long
excess_length = Twitter::Validation.tweet_length(text) - Twitter::Validation::MAX_LENGTH
text_a = text_a[0..-(excess_length + 1)]
text = "fixed preamble #{text_a}"
text << " #{text_b}" if text_b
text << get_suffix if suffix
text
end
def get_suffix
" some generated suffix"
end
It's ugly, especially with the duplication. Ideas?
Why not build the string properly in the first place?
def tweet(text_a, text_b=nil, suffix=false)
text = ""
text << " #{text_b}" if text_b
text << get_suffix if suffix
space = Twitter::Validation::MAX_LENGTH - Twitter::Validation.tweet_length(text)
raise "too long" unless space > 0
"fixed preamble #{text_a}"[0, space] + text
end

VB6 getting ride of large spaces

Hey all, i am trying to replace large spaces between text with just one. My output looks like this right now:
5964215">
This is just the first example of the spaces
5964478">
This would be the 2nd example of showing how many spaces this thing has in each sentence.
5964494">
That comes from a textbox that has multi-line to true. Here is what it looks like when it doesn't have multi-line to true.
http://www.june3rdsoftware.com/forums/vb6.jpg
I can not seem to get the spaces to go away! BTW, this text is from a webpage if that makes any difference.
David
According to the suggestion of MvanGeest, here is some VB code to replace blocks of white spaces:
Sub test()
Dim x As String, y As String
x = "abcd defg 1233"
Dim re As New RegExp
re.Pattern = "\s+"
re.Global = True
y = re.Replace(x, " ")
Debug.Print y
End Sub
To make this work, you will have to add a reference to "Microsoft VBScript Regular Expresssions" to your project.
Assuming no regex support, why not set up a simple state machine that will set the state=1 when a space is found and set state=0 once a non-space is encountered. You can move char by char when state=0 (thus copying over only 1 space per series of spaces).
Also assuming no regex, you could try something like
str = "long text with spaces "
i = LenB(str)
str = Replace(str, " ", " ")
Do While LenB(str) <> i
i = LenB(str)
str = Replace(str, " ", " ")
Loop
Of course this code could be optimized for long space sequences but it might be all you need as well

Resources