Software Development and Programming Careers (Official Discussion Thread)

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
I plan on making a simple rpg, so one of the first things I have to do is create a system to lay out tiles. I've been playing around with arrays. Here is a small demo that takes an array with number values and then outputs a 'symbol' in it's place. Eventually I'll have to change that to a 'tile'. I find it very helpful to break bigger problems down into smaller ones, so this is merely a textual example, but I'll eventually go to blocks, and then actual graphics tiles.

Code:
//Input data
var a =
[[1, 2, 3, 3, 1],
[1, 1, 1, 2, 1],
[1, 3, 2, 1, 2]];


//Create representation map array
var map = [[], [], []];

for(i=0; i<3; i++)
{
  for(j=0; j<a[i].length; j++)
  {
  if(a[i][j] === 1)
  {
  map[i].push("X");
  }
  else if(a[i][j] === 2)
  {
  map[i].push("0");
  } else if(a[i][j] === 3)
  {
  map[i].push("#");
  }

  } //end inner for
}

console.log(map[0]);
console.log(map[1]);
console.log(map[2]);

Update:
The code is quite ugly right now and I'll refactor soon, but I finally got it working. What is it exactly? It's basically a program that outputs tiles based upon the array you put in. each number in the array corresponds to a different tile color. I'll eventually replace the colored tiles with graphics.
http://codepen.io/kevm3/full/EaLOZa/
 
Last edited:
Joined
Jan 21, 2015
Messages
53
Reputation
20
Daps
114
C program with command line argument for num input. overflow & invalid input checking.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

void even_or_odd(long n);
int main (int argc, char **argv)
{
    long num = 0;
    char *tmp;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <num>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    num = strtol(argv[1], &tmp, 10);
    if (errno == ERANGE)
        fprintf(stderr, "Error: overflow.\n");
    else if (*tmp)
        fprintf(stderr, "Error: invalid input.\n");
    else
        even_or_odd(num);

    return 0;

}

void even_or_odd(long n)
{
    long x = n;
    if (n == 0)
        printf("%ld is even.\n", n);
    else if (n == 1 || n < 0)
        printf("%ld is odd\n", n);
    else {
        x >>= 1;
        x <<= 1;
        if (x == n)
            printf("%ld is even.\n", n);
        else
            printf("%ld is odd.\n", n);
    }
}

edit: actually you can simplify the function by omitting the if tests. the bit shifting covers those cases

Code:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

void even_or_odd(long n);
int main (int argc, char **argv)
{
    long num = 0;
    char *tmp;

    if (argc != 2) {
        fprintf(stderr, "Usage: %s <num>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    num = strtol(argv[1], &tmp, 10);
    if (errno == ERANGE)
        fprintf(stderr, "Error: overflow.\n");
    else if (*tmp)
        fprintf(stderr, "Error: invalid input.\n");
    else
        even_or_odd(num);

    return 0;

}

void even_or_odd(long n)
{
    long x = n;
        x >>= 1;
        x <<= 1;
        if (x == n)
            printf("%ld is even.\n", n);
        else
            printf("%ld is odd.\n", n);
}

edit again: doh, dunno why I had it in mind that numbers < 0 are odd. Second version should be correct.
 
Last edited:

Nomadum

Woke Dreamer
Joined
Dec 23, 2014
Messages
4,622
Reputation
-705
Daps
9,074
Reppin
Nothing
I really don't think there is anything inherent to the rpg genre that makes it more complex than others.

I'm on the outside looking in so forgive my ignorance if I say something wrong,

I'm assuming @Responsible Allen Iverson was assuming @kevm3 mean's producing a full-fledged RPG. meaning he will have to program the enemy a.i., any dialog or interaction with npc characters, the recognition system for hits received and sent, stages, etc. etc. I think that's why he was saying it's nothing "simple" about them:yeshrug:
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
I'm on the outside looking in so forgive my ignorance if I say something wrong,

I'm assuming @Responsible Allen Iverson was assuming @kevm3 mean's producing a full-fledged RPG. meaning he will have to program the enemy a.i., any dialog or interaction with npc characters, the recognition system for hits received and sent, stages, etc. etc. I think that's why he was saying it's nothing "simple" about them:yeshrug:

I'm going to keep it simple. No super complex battle system or any of that.
 

Spatial Paradox

All Star
Supporter
Joined
May 16, 2012
Messages
2,291
Reputation
1,120
Daps
12,156
Reppin
Brooklyn
Write a function to determine if a number is even without using multiplication, division or modulo (*, /, %). Return true if it is even, false if not. For simplicity, it has to be a non-negative integer input into the function, i.e.:

int num= 987;
result = isEven( num);

And then print the result

My solution
C++11
Code:
#include <iostream>

bool isEven(const uint value)
{
  auto bitmask = 0x00000001;
  return (bitmask & value) == 0 ? true : false;
}

int main(int argc, const char * argv[]) {
  uint value;
  std::cout << "Input a value to test: ";
  std::cin >> value;

  isEven(value) ? std::cout << value << " is even.\n" : std::cout << value << " is odd.\n";

  return 0;
}

If you know binary, then you likely know that all even numbers will have a zero in their rightmost bit. So you can just use 1 (specifically 1 in hex in this case) as a bitmask and perform a bitwise AND operation to test for whether the last bit is 0 or 1. If the result is 0, then the number is even and true is returned. If the result is 1, then the number is odd and false is returned.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Quite a diverse set of solutions in here. I know I learned a few things seeing how others presented a solution.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Ok I have a challenge. This one is fairly simple in concept, but it will require you to do a bit of research. Create a basic calculator for all 4 operations: *, +, -, / that a user can select which operation to perform out of a menu between two numbers. Make sure the program doesn't exit unless the user selects the exit option, so you will have to create a program loop. For example
Which operation do you want to perform?
1. Addition
2. Multiplication
3. Division
4. Subtraction
5. Exit

User enters 1 and the program says:
You have chosen Addition.
Enter number A:
5
Enter number B:
10
The output is:
15

After the program performs the operation, ask them if they want to perform another operation, and if they choose "Y", have it go back to the main program menu. If they choose N, have the program exit. Don't worry about implementing multiple operations. Just have it do the basic operation and return to the menu.

Where the challenge comes in is do this in a language you have little to no familiarity in. If you really want to challenge yourself, do it in a different paradigm than you are acustomed to. For example, if you always do imperative style programming, choose functional. If you are familiar with the C family, some candidates might be Clojure, Scala, Go, Ruby, Javascript, Haskell, etc.

Purpose of this exercise? To get out of your go-to programming language and comfort zone and to be comfortable doing research in order to implement a solution in an unfamiliar language in a time-critical manner. You may end up having a job where you are required to learn a new language on a whim.
 
Last edited:

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,300
Reputation
5,571
Daps
83,590
Ruby Solution (WIP) Still have to implement the program loop. I'm going to start showing different versions as I refactor just to show how much more efficient and concise refactoring can be
Version 1 - The initial version was actually much longer but I accidentally replaced it with this one
Code:
def input(choice)
  puts "What is the first number?"
  num1 = gets.to_i
  puts "What is the second number?"
  num2 = gets.to_i
 
  case choice
  when 1
  ans = num1 + num2
  when 2
  ans = num1 * num2
  when 3
  ans = num1 / num2
  when 4
  ans = num1 - num2
  end
 
  return ans
end
puts "Please choose an option:"
puts "1. Addition"
puts "2. Multiplication"
puts "3. Division"
puts "4. Subtraction"
user_choice = gets.to_i
 
  print "You have chosen "
  case user_choice
  when 1
  puts "Addition"
  ans = input(1)
  puts "The result is: #{ans}"
 
  when 2
  puts "Multiplication"
  ans = input(2)
  puts "The result is: #{ans}"
 
  when 3
  puts "Division"
  ans = input(3)
  puts "The result is: #{ans}"
  when 4
  puts "Subtraction"
  ans = input(4)
  puts "The result is: #{ans}"
  else
  puts "test"
  end

Version 2
Code:
#####################################################
## Functions
def input(choice)
  puts "What is the first number?"
  num1 = gets.to_i
  puts "What is the second number?"
  num2 = gets.to_i
 
  case choice
  when 1
  ans = num1 + num2
  when 2
  ans = num1 * num2
  when 3
  ans = num1 / num2
  when 4
  ans = num1 - num2
  end
 
  return ans
end
####################################################
## Main Program
#set the program flag to on
progchoice = 1
while(progchoice != 0) do
#start the program loop
puts "Please choose an option:"
puts "1. Addition"
puts "2. Multiplication"
puts "3. Division"
puts "4. Subtraction"
user_choice = gets.to_i
 
result = input(user_choice)
puts "The result is: #{result}"
 
####################################################
## Quit or Continue
  puts "Type 5 to quit"
  decision = gets.to_i
  puts decision
 
  if decision == 5
  progchoice = 0
  puts "Quitting the program"
  end
end
 
Last edited:

KingBowser

Rookie
Joined
Jul 14, 2012
Messages
47
Reputation
10
Daps
152
Reppin
NULL
Let me jump in this too since it looks like fun. My solution for the even / odd problem.

Code:
#include<iostream>
#include<string>

std::string isEven(const int &num);

int main()
{
    int num = -1;

    while (num < 0)
    {
          std::cout << "Enter a positive number: ";
          std::cin >> num;

          if(std::cin.fail() || num < 0)
          {
              std::cin.clear();
              std::cin.ignore(INT_MAX,'\n');
              std::cout << "Please enter an positive number .\n";
          }
    }
    std::cout << "The number you entered " << std::to_string(num) << " is " << isEven(num) << std::endl;
}

std::string isEven(const int &num)
{
     if(num & 1)
        return "odd.";
    
     return "even.";
}
 
Top