Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting > Code Gallery
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 03-22-2008, 11:49 PM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Roman numbers

Well, gives the ability to convert numbers into roman numbers and vice versa:

PHP Code:
public function romanToNumber(roman) {
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.0;
  for(
temp.0roman.length(); ++) {
    if (
roman.substring(i2in table_l) {
      
+= table_n[table_l.index(roman.substring(i2))];
      
++;
    }else 
+= table_n[table_l.index(roman.charat(i))];
  }
  return 
n;
}
public function 
numberToRoman(number) {
  
temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"};
  
temp.out ""temp.number2 number;
  for(
temp.table_n) {
    while(
number2 >= n) {
      
number2 -= n;
      
out @= table_l[table_n.index(n)];
    }
  }
  return 
out;

(Credits to Ian )

Example:

PHP Code:
function onCreated() {
  echo(
numberToRoman(947));
  echo(
romanToNumber("CMXLVII"));
  
/*
  CMXLVII
  947
  */
  
echo(numberToRoman(2008));
  echo(
romanToNumber("MMVIII"));
  
/* 
  MMVIII
  2008
  */
  
echo(numberToRoman(romanToNumber("MMVIII")));
  
// MMVIII

anyways, enjoy?
__________________
Reply With Quote
  #2  
Old 03-23-2008, 12:03 AM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
Ace, this will be pretty cool for implementing a sort of RP thing with numbers. I don't really see it making a big gameplay impact as not many know how to translate roman numerals, but I guess it's still cool, and new!
Reply With Quote
  #3  
Old 03-23-2008, 01:00 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
PHP Code:
  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 
PLEASE use multi-dimensional arrays
__________________
Reply With Quote
  #4  
Old 03-23-2008, 01:24 AM
Programmer Programmer is offline
Coder
Programmer's Avatar
Join Date: Jan 2008
Location: -78.464422, 106.837328
Posts: 449
Programmer has a spectacular aura aboutProgrammer has a spectacular aura about
Send a message via AIM to Programmer Send a message via MSN to Programmer Send a message via Yahoo to Programmer
Quote:
Originally Posted by cbkbud View Post
PHP Code:
  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 
PLEASE use multi-dimensional arrays
Aw be quiet :/ ... its good enough for what it's doing.
__________________
- Iᴀɴ Zɪᴍᴍᴇʀᴍᴀɴ
Reply With Quote
  #5  
Old 03-23-2008, 01:34 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Quote:
Originally Posted by cbkbud View Post
PHP Code:
  temp.table_n = {1000,900,500,400,100,90,50,40,10,9,5,4,1};
  
temp.table_l = {"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}; 
PLEASE use multi-dimensional arrays
naw, I don't see a reason for it..
__________________
Reply With Quote
  #6  
Old 03-23-2008, 01:39 AM
cbk1994 cbk1994 is offline
the fake one
cbk1994's Avatar
Join Date: Mar 2003
Location: San Francisco
Posts: 10,718
cbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond reputecbk1994 has a reputation beyond repute
Send a message via AIM to cbk1994
Quote:
Originally Posted by Chompy View Post
Naw. Using multi-dimensional arrays in this case makes it just alot more complicated since you have to divide the index by two for find the correct number, then plus 1 to find the letter( and by using multi dimensional arrays you can't use the
for(member : array) loop because of indexes being wrong, and you to use an extra variable to count the index), so... Nope. Using multi-dimensional arrays in this case just makes it more complicated then it needs too.
What?

PHP Code:
temp.table = { { 1000"M" }, { 900"CM" } };
for ( 
temp.temp.table )
{
  
temp.num temp.n[0];
  
temp.letter temp.n[1];

I think you misunderstand the concept of multi-dimensional arrays.
__________________
Reply With Quote
  #7  
Old 03-23-2008, 01:42 AM
Tolnaftate2004 Tolnaftate2004 is offline
penguin.
Join Date: Jul 2004
Location: Berkeley, CA
Posts: 534
Tolnaftate2004 is a jewel in the roughTolnaftate2004 is a jewel in the rough
Send a message via AIM to Tolnaftate2004
Quote:
Originally Posted by Chompy View Post
Naw. Using multi-dimensional arrays in this case makes it just alot more complicated since you have to divide the index by two for find the correct number, then plus 1 to find the letter( and by using multi dimensional arrays you can't use the
for(member : array) loop because of indexes being wrong, and you to use an extra variable to count the index), so... Nope. Using multi-dimensional arrays in this case just makes it more complicated then it needs too.
He means {{int,int, ...},{str,str,...}}.
And you can certainly use for (needle: haystack) with a multidimensional array.
__________________
◕‿‿◕ · pfa · check yer syntax! · src

Killa Be: when i got that locker in 6th grade the only thing in it was a picture of a midget useing a firehose :/
Reply With Quote
  #8  
Old 03-23-2008, 01:43 AM
Chompy Chompy is offline
¯\(º_o)/¯
Chompy's Avatar
Join Date: Sep 2006
Location: Norway
Posts: 2,815
Chompy is just really niceChompy is just really niceChompy is just really nice
Send a message via MSN to Chompy
Ninja edit btw (err, I started to think about something I did earlier, then I realised you meant {{foo}, {bar}} arrays (multi-dimensional arrays), realised that after like 10 secs after posting that post xD)

Anyways

PHP Code:
public function numberToRoman(number) {
  
temp.table = {
    {
1000,900,500,400,100,90,50,40,10,9,5,4,1},
    {
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}
  };
  
temp.out ""temp.number2 number;
  for(
temp.table[0]) {
    while(
number2 >= n) {
      
number2 -= n;
      
out @= table[1][table[0].index(n)];
    }
  }
  return 
out;
}
public function 
romanToNumber(roman) {
  
temp.table = {
    {
1000,900,500,400,100,90,50,40,10,9,5,4,1},
    {
"M","CM","D","CD","C","XC","L","XL","X","IX","V","IV","I"}
  };
  
temp.0;
  for(
temp.0roman.length(); ++) {
    if (
roman.substring(i2in table[1]) {
      
+= table[0][table[1].index(roman.substring(i2))];
      
++;
    }else 
+= table[0][table[1].index(roman.charat(i))];
  }
  return 
n;


And, the reason why I posted that I posted was because I thought I did something like that, but I did:

PHP Code:
temp.table = {
       
1"I",
       
5"V",
      
10"X",
      
50"L",
     
100"C",
     
500"D",
    
1000"M"
  
}; 
and that popped into my mind when I saw your post Chris, sorry for the misunderstanding xD

And, some other version of it (romanToNumber()) under:

PHP Code:
public function romanToNumber(number) {
  
temp.table = {
       
1"I",
       
4"IV",
       
5"V",
       
9"IX",
      
10"X",
      
40"XL",
      
50"L",
      
90"XC",
     
100"C",
     
400"CD",
     
500"D",
     
900"CM",
    
1000"M"
  
};
  
temp.number2 number;
  
temp.out "";
  for(
temp.table.size()-1> -1-= 2;) {
    
temp.number3 0;
    while(
number2 >= table[i-1]) {
      if (
number2 0) {
        
number2 -= table[i-1];
        
out @= table[i];
      }
    }
  }
return 
out;

__________________

Last edited by Chompy; 03-23-2008 at 02:32 AM..
Reply With Quote
  #9  
Old 03-23-2008, 04:10 AM
Rapidwolve24 Rapidwolve24 is offline
North*
Join Date: Oct 2007
Location: Massachusetts
Posts: 178
Rapidwolve24 is on a distinguished road
Send a message via AIM to Rapidwolve24 Send a message via MSN to Rapidwolve24
Nice idea, .
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +2. The time now is 09:08 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.