Archive for October, 2010

Posted by kent at 25 October 2010

Category: Game review

Tags: , , , ,

This is a simple howto on how to beat the virus infection in the Brotherhood of Steel datastore.

1) Talk to Scribe Ibsen about the virus infection and ask if you could help him.

2) After much talking Scribe Ibsen will ask you to wait until he says the virus has jumped. Do a quicksave here just before Scribe Ibsen tells you the virus has jumped.

3) Place yourself in front of one of the terminals.

4) When Scribe Ibsen gives you GO, search for the virus in the computers. Do a systematic search. And remember what computers have the virus. If you fail the first attempt, reload the quicksave.

5) Go to the computers you had the virus in the previous attempt and isolate them there.

6) Continue the search for additional infected computers.

7) If unsuccessful, goto 4.

Scribe Ibsen at the terminal

Scribe Ibsen at the terminal

More screenshots in the next post.

VN:F [1.9.13_1145]
Rating: 5.0/5 (3 votes cast)
Share

Posted by kent at 20 October 2010

Category: c++

Tags: , , , , ,

This is a reminder to myself.

To sort a collection of non-POD types (not Plain Old Data) with STL we need to tell it how to sort these objects.

To the std::sort method we simply supply a predicate! It’s just a method taking 2 arguments of that type and apply our logic to tell if the first argument is lesser than the last argument. With templates it becomes a little more complex, but still it’s very simple when you know how it works.

Here is some sample code:

#include <iostream>     // Provides cout
#include <vector>       // Provides our basic container
#include <algorithm>    // Provides std::sort

using namespace std;

// Structures I'll sort with 1 template function
//
//
struct A
{
    A(size_t i, size_t t) : id(i), time(t), other_data(false){}
    size_t  id;
    size_t  time;
    bool    other_data;
};

struct B
{
    B(size_t i, size_t t) : id(i), time(t), other_data("string data"){}
    size_t  id;
    size_t  time;
    string  other_data;
};

struct C
{
    C(size_t i, size_t t) : id(i), time(t), other_data("character array data"){}
    size_t  id;
    size_t  time;
    char*   other_data;
};

// Our predicate
//
template<typename CLASS>
bool timeSortPredicate( const CLASS &a, const CLASS &b )
{
    return a.time < b.time;
}

template<typename CLASS>
bool idSortPredicate( const CLASS &a, const CLASS &b )
{
    return a.id < b.id;
}

// Our collection printer
//
template<typename CLASS>
void print( const CLASS &c )
{
    typedef typename CLASS::const_iterator cit;

    for ( cit it = c.begin(); it != c.end(); ++it )
    {
        cout << it->id << " " << it->time << endl;
    }
}

int main()
{
    // Construct the objects ... tedious I know
    A a1(1,15), a2(2,20),   a3(3,14);
    B b1(3,4),  b2(2,10),   b3(1,12);
    C c1(11,0), c2(12,0),   c3(13,0);

    // Construct collections
    vector<A> alist;
    alist.push_back(a1);
    alist.push_back(a2);
    alist.push_back(a3);

    vector<B> blist;
    blist.push_back(b1);
    blist.push_back(b2);
    blist.push_back(b3);

    vector<C> clist;
    clist.push_back(c1);
    clist.push_back(c2);
    clist.push_back(c3);

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    cout << "Sorting collections by time" << endl;

    sort(alist.begin(), alist.end(), timeSortPredicate<A> );
    sort(blist.begin(), blist.end(), timeSortPredicate<B> );
    sort(clist.begin(), clist.end(), timeSortPredicate<C> );

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    cout << "Sorting collections by id" << endl;

    sort(alist.begin(), alist.end(), idSortPredicate<A> );
    sort(blist.begin(), blist.end(), idSortPredicate<B> );
    sort(clist.begin(), clist.end(), idSortPredicate<C> );

    // Print stuff
    cout << "Collection A" << endl;
    print(alist);

    cout << "Collection B" << endl;
    print(blist);

    cout << "Collection C" << endl;
    print(clist);

    return 0;
}

Compile with:

g++ sortpredicate.cpp -o sortpredicate

The output should be:

Collection A
1 15
2 20
3 14
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by time
Collection A
3 14
1 15
2 20
Collection B
3 4
2 10
1 12
Collection C
11 0
12 0
13 0
Sorting collections by id
Collection A
1 15
2 20
3 14
Collection B
1 12
2 10
3 4
Collection C
11 0
12 0
13 0
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by Tatyana at 17 October 2010

Category: Dev, Java

Tags: , , ,

It’s uncommon to see methods like this>

public Person findPerson() {
   if(someService) [
     return someService.getPerson();
   }
   return null;
}

There is no reason to make a Person object where there are no persons found. But doing so requires extra code in the client to handle the null value. This becomes a problem if a client is, for example, a GUI:

somePage.jsp
<%
  ...
  Person person = myService.findPerson();
%>
<table>
   <tr>
      <td>${person.name}</td>
      <td>${person.address}</td>
      <td>${person.numberOfPurchases}</td>
   </tr>
</table>

The result of such code in cases where the person cannot be found is Server error. So, there is no reason ever to return null from an object-valued method instead of returning an empty object. Especially if you deal with arrays and collections.

VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share

Posted by Tatyana at 14 October 2010

Category: Dev, Java

Tags: , ,

How many times we write a program like this:

public void doSmth() {
    Salary s = calculateSalary(Person p);
    ...
}

private Salary calculateSalary(Person p) {
   return 100;
}

And so we hear from our sjef that we can’t have same salary for male and female. So we do this:

public void doSmth() {
    Salary male = calculateSalary(Person p, true);
    Salary female = calculateSalary(Person p, false);
    ...
}

private Salary calculateSalary(Person p, boolean sex) {
   if(sex) return 100;
   else return 50;
}

Yes, but what if we have more options later on? Or if we look just at the doSmth() function? What do these true/false mean?

An enum type in such cases makes your code easier to read and to write, especially if you are using an IDE that supports autocomplition. Also, it makes it easy to add more options later.

So here’s how we should do it:

public enum Sex {MALE, FEMALE}

public void doSmth() {
    Salary male = calculateSalary(Person p, Sex.MALE);
    Salary female = calculateSalary(Person p, Sex.FEMALE);
    ...
}

private Salary calculateSalary(Person p, Sex sex) {
   if(sex.equals(Sex.MALE) return 100;
   else return 50;
}
VN:F [1.9.13_1145]
Rating: 0.0/5 (0 votes cast)
Share