Blog do projektu Open Source JavaHotel

sobota, 2 czerwca 2018

Civilization The Board Game, next version

Introduction
I deployed a new version of my computer implementation of  Civilization The Board Game. The implementation consists of three parts:
New features implemented
  • Great Persons, only resources augment is implemented, not Great Person card abilities
  • Event Card, cards only, events are not implemented
Great Person
While advancing on culture track, the player is receiving Great Person.

The Great Person can be placed immediately on the map or can be held in reserve and placed on the map later during Start Of Turn phase.
After placing the Great Person on the map, its resource capabilities augment the capacity of the adjacent city.
You are welcome, Great General!

Culture Cards
Culture Cards are collected (like Great Persons) during advancing in culture. Culture Cards Events are not implemented yet.

If a number of Culture Cards exceeds the size of the hand, the players should reject one of the cards.
Next steps
Capabilities of civilizations.

wtorek, 29 maja 2018

DB2 and user-defined aggregate functions

Introduction
It is hard to believe but in DB2 there was not a feature to create user-defined aggregate function. It caused problems while migrating from Oracle to DB2 and to overcome them required sometimes a lot of effort.
Finally, this long-awaited option has been shipped in DB2 11.1. It is good news. But there also a bad news, it is very scanty documented. The only information I was able to find was an article in official DB2 Knowledge Centre.
So I decided to fill the gap and to create my own port of exemplary Oracle aggregate function.
The source code of Oracle UDF and DB2 equivalent is available here (db2aggr branch). Unfortunately, the custom aggregates are less flexible than standard and there is no way to apply additional argument, for instance, the delimiter character. This applies to DB2 and Oracle. In the example, hardcoded coma is used.
Aggregate function
The aggregate function is similar to standard LISTAGG function. But it also removes duplicates and sorts the result.
Assuming the table:
CREATE TABLE "TESTID" 
   "ID" VARCHAR2(20 BYTE)
  ); 
  INSERT INTO TESTID VALUES('G');  
  INSERT INTO TESTID VALUES('H');  
  INSERT INTO TESTID VALUES('A');
  INSERT INTO TESTID VALUES('B');
  INSERT INTO TESTID VALUES('C');
  INSERT INTO TESTID VALUES('C');
  
  INSERT INTO TESTID SELECT * FROM TESTID;
The expected result of running:
  SELECT LISTAGGDUPL(ID) FROM TESTID;
is:
A,B,C,G,H
Oracle
Oracle PL/SQL code of the aggregate function is available here. It simply collects values in the nested table and the final removing duplicates and sorting are done by running:

FOR Result IN ( SELECT DISTINCT O.COLUMN_VALUE FROM TABLE(self.gCollect) O ORDER BY 1 )

DB2
User-defined aggregate function in DB2 are Java or C++, there is no way to develop it in pure DB2 SQL/PL. So the implementation consists of two parts: Java code and SQL/PL code for defining the signature and deployment.
  • Java project is accessible here. That is very simple and straightforward, no comments are required.
  • SQL/PL code is available here.
  • Test code, the same for Oracle and DB2.
Additional remarks
  • In order to run the function in DB2 it is recommended to increase the Java heap space. Example: db2 update dbm cfg using JAVA_HEAP_SZ 76800
  • I was unable to trigger "MERGE PROCEDURE" phase. So I'm not sure of the Java code implementing it is valid.
  • During the test, I discovered that INITIALIZE and FINALIZE code is run twice, I do not understand why. 

czwartek, 3 maja 2018

Civilization The Board Game, next version

Introduction
I deployed a new version of my computer implementation of  Civilization The Board Game. The implementation consists of three parts:
New features implemented 
  • Culture track
  • Spend culture tokens to advance a culture level
  • Devote city to arts
  • MetalWorking
  • Code Of Laws
  • Currency
  • Irrigation
  • Navigation
Devote city to arts
The player can choose a city to devote it to arts to collect culture tokens. Culture tokens can be also sent from scout or scouts occupying an appropriate square.




Of course, the noble sacrifice is not in vain, it is piled in the resource panel.

Advance culture level, culture track
If the player stocks enough culture tokens can spend them to advance culture.


The cost depends on the culture level, in the beginning, it is 3 culture tokens. After spending culture, the player is climbing up one step on the culture ladder.


The player can advance only once using the dialog window but it can be repeated several times in the same City Management phase.
Important: the current implementation supports only marking the progress on the culture track. The culture cards, great persons, and culture victory are still pending.
MetalWorking technology
During the battle, the iron token can be used to increase the attack strength. Also, iron harvested from hut and villages can be consumed. The current implementation spends firstly iron token. If the iron token is not gathered, the hut or village is used. In the future implementation, the player will have a choice which iron to spend. 
Code Of Law technology
After winning the battle, the economy is increased and a coin is added to the technology card.

Currency technology
The player can spend incense to gain three culture tokens. Explicit incense token can be used or incense collected from hut or village. The player has an option to point it.

Irrigation technology
Irrigation allows building the third city. Research this technology as quickly as you can, it is the must in this game.
Navigation technology
After researching this technology, the player can cross the water but cannot stop in it. It is also the must in water covered area.

Next steps
  • Great persons
  • More technologies

niedziela, 29 kwietnia 2018

KVM, HortonWorks and IBM Spectrum Scale

IBM Spectrum Scale (former GPFS) is a clustered file system giving single access point to distributed parallel nodes. HDP is Hadoop implementation created and supported by HortonWorks. Both work together smoothly, GPFS can replace HDFS in a transparent way.
If you are entitled to use IBM Spectrum Scale, it is tempting to install IBM Spectrum Scale locally and explore its capabilities without setting up huge infrastructure.
Unfortunately, although there is plenty of ample information available, it is not easy to make the first step and just install it.
So I decided to fill the gap and install HDP HortonWorks on the top of IBM Spectrum Scale (GPFS) using KVM virtualization. I'm not paying too much attention to stuff like tunning, mirroring, replication, configuration etc. Just install using a default setting as much as possible to give the first taste of this fantastic feature.
More details can be found here.
Before you start:
  • Make sure you are entitled to use IBM Spectrum Scale that way, review license agreement. IBM Spectrum Scale is not publicly available.
  • Do not start unless you at least 32GB memory, otherwise, 4 KVM with at least 6 GB memory will kill your machine.





czwartek, 5 kwietnia 2018

Civilization The Board Game, next version

Introduction
I deployed a new version of my computer implementation of  Civilization The Board Game. The implementation consists of three parts:
New features
  • "Pottery" technology is enabled and you can gain coins by spending resources on them.
  • Economy progress is scored.
Pottery and economy
"Pottery" technology is the first to be enabled so far.


When technology is researched and you have at least two resources hoarded you have the opportunity to transform wheat or incense into gleaming gold.



  • "Use Technology" 
  • Choose technology you want to use, here Pottery only
  • Point the city where the action will be performed
  • Select two resources to spend
After spending resources you will be two resources lighter but one gold coin heavier.

Also "Pottery" card weights one coin now.

Although you can collect coins, the economic victory is not implemented yet.
Next step
  • Score culture progress.


niedziela, 25 marca 2018

Find missing number or numbers

Exercise
There is an exercise in Cracking the Code Interview.
Missing Two: You are given an array with all the numbers from 1 to N appearing exactly once,
except for one number that is missing. How can you find the missing number in O(N) time and
0( 1) space? What if there were two numbers missing?
The solution is quite simple, just sum up all numbers from 0 to N using well-known formula, then sum up all numbers in the table and the difference is the number missing.
In case of two numbers missing, this method gives back the sum of the numbers. So it is necessary to conduct additional calculation, for instance, to sum square of the numbers. This method we receive the sum of the squares of the missing number and after resolving quadratic formula, we come up with the numbers.
But this solution has a weak point. If the number is big enough, there is a risk of overflow and the whole calculation is wrong. The threshold is around the square root of the maximum value of the integer involved. For 32 bit integer, it is around 46341.
Alternative solution for one missing
The alternative solution for one number missing is to calculate the number of ones and zeros in a binary representation of all numbers and the same for the table with missing one. Comparing which bits are missing, we can reconstruct the number lost.
The C++ solution is available here. Only number of ones should be calculated.
It does not break the time and space complexity requirement. Although we have to enumerate through bits for every number, the loop is still constant, 32 for 32 integer. 32*N is still O(N). Also, we need additional space for storing the number of bits but it is also constant and keeps O(1).
Solution for two missing
A similar solution can be applied to two numbers missing but only the sum can be reconstructed. If the difference between ones for the whole sum and the table is 0, it means that both missing numbers have zeros at the position. If the difference is 2, we can assume ones. But if the difference is 1, it means a combination of one and zero. Unfortunately, we cannot assign them to the numbers. But in case of the sum, it is not important because addition is commutative so we can assign them as we like.
uint ReconstructMissingSum(ByteCounter &all) const {
   uint missing1 = 0;
   uint missing2 = 0;
   for (int i=BN-1; i>=0; i--) {
 missing1 <<= 1;
 missing2 <<= 1;
 if (all.one_sum[i] != one_sum[i]) missing1 += 1;
 // Sum have two ones at the position. Move the second 1 to the second number.
 // We are calculating the sum, the order does not matter
 if (all.one_sum[i] - one_sum[i] == 2) missing2 += 1;
 }
   // it is not the reconstruction of numbers but the sum only.
   return missing1 + missing2;
}
But, as above, we need an additional equation to extract the exact values of the numbers. The same method can be applied for squares of the numbers. Unfortunately, this solution falls under the curse of overflow. Maybe there is a method to improve it but I was unable to find it so far.

środa, 21 marca 2018

Civilization The Board Game, next version

Introduction
I deployed a new version of my computer implementation of  Civilization The Board Game. The implementation consists of three parts:
New features
  • Wonders of The World.
You can buy wonders and spend money on them. Unfortunately, the functionality of the wonders is not implemented yet, will be added later.
Wonders are displayed in the separate panel. 
No graphic is attached, only the first letter of the Wonder name is exposed. It would be difficult to prepare several dozens of different and meaningful images. Only in the background a symbol of Ancient, Medieval or Modern is added.
When the player piles up the stack of money big enough, is allowed to buy a wonder. Discount for discovering a proper technic is granted also.
One player put aside enough to buy Pyramids or Great Lighthouse. After selecting a wonder, a square where the wonder is to be positioned should be selected. If square already had a building or another wonder atop, the previous content will be replaced.
The wonder is built and the world is staring at it with mouth open.
Next step

  • Economy, coins gathering
  • Culture progress