< Back to Blog

Code Wars – prepare to fire a broadside mateys!

Industry

9:00 am

A "code war" is a competition between teams to see who can write the best code. We are having one today where everyone in development (it was voluntary but everyone signed up) is competing to see who can write the best code. They are give the problem at 9:00am (ie now) and have until 5:00pm to turn in their code (all times mountain time). I created the problem and am administering the contest.

The Problem

The first issue I faced was what should the problem be. All of the initial problems I thought of would favor different groups here at Windward because it would match the expertise of that group. It's also critical to come up with a problem that does not have solutions all over the internet.

Well, after a couple of days I came up with it – writing an A.I. component for the board game Broadside. No one at work has any experience writing a computer A.I. so we have a level playing field. And this is a problem that cannot be brute-forced, working out even three turns for six teams to look at every combination will take too long. So it will require coming up with a clever way to determine what to do on each turn. I think this is more a contest of critical thinking than coding ability – and I like that.

I created a computer version of the game (yes horrible graphics but it gets the job done). It calls each player one at a time for their turn, and then the next player, and so on. So each team will write their code (I have a very poor AI implementation already for each to work from) and then all will give me their code and we'll run the game.

10:00 am

It took about 1/2 hour to go over the game with everyone and then another 1/2 hour as they got set up and hit me with a bunch of individual questions. I could tell from the reactions that it is definitely outside everyone's knowledge set. So I think it is a really good problem.

And it has never been quieter here - every door shut, every team totally focused on the problem with the world shut out. When productivity really matters, developers do know how to work :)

I came up with this idea because I played this game as a kid and had lots of fun doing so. It was a popular game so I figured the rules work well. But at the same time it's a very simple game so the logic of an A.I. player does not get wrapped up in complex maps, weird rules, etc.

12:30

We had pizza delivered and I've never seen people eat so fast. Lunch in under 5 minutes. Some of the teams grabbed their pizza and took it back to their offices - clearly still working through the problem in their mind as they grabbed the food.

One team said they have an awesome solution - but it apparently needs an infinite number of clock cycles. Two teams asked if they could have the weekend. And everyone is worried about what they can get done by 5:00. So I think it is a good problem for this kind of contest.

The Code

The program is downloadable below. It's broken up so that the player A.I. code does not have access to the internal structures. (The teams are on the honor system to not use reflection to hack the game.) So we have the game engine & UI project, the common API project, and a project for each player.

The art and rendering are atrocious. But for the purposes of the contest it is fine and doing this well would have added a lot of time and effort. I haven't touched DirectX in 15+ years so I would have had to learn it all over. And decent art would have cost a couple of hundred (I have zero artistic ability).

I also made one rookie mistake in the API. You can access the map either via a Point(X,Y) are as an array [Y][X]. Even with it being 15 years since I've written game code - this is horribly dumb. For anyone just starting in game programming keep it X,Y everywhere.

I also create a copy of all objects passed to a player on each turn so that they are getting a distinct object and it does not have any internal information. I think in a commercial game a better approach would be to keep the per player structures and update them. That would be a lot faster and have less memory fragmentation. But again, this works and I wrote this program in ~ 6 hours.

The A.I. I have in here is awful - it goes for any combat, even if it is one that the ship will lose. That gave me a fast way to test combat resolution and the end of the game logic.

The combat engine I think I did pretty good on. It walks each step of a ships move to determine if there is any combat and if so, assesses the results and sends the results as events to the players. Very straightforward and not that complex. I think it also does a good job of illustrating how to use LINQ to make code a lot simpler and more straightforward.

For the timer I used a Forms.Timer. It is not as accurate as alternatives and it has more overhead, but I am writing to the windows using Forms calls on each tick and so I need to be in the context of the UI thread. This is the most efficient way to get events in this situation. It also has a nice benefit that you do not have to handle re-entrancy. You may get another tick immediately upon returning from the event method - but you will not get an event in the middle of processing an event.

There is a tick for each sprite animation frame and then every N of those ticks, there is a move tick. The move tick will move a ship one map square (and assess any battery damage). If it is the final square, it assess ship fire and then the next move tick it gets the next player turn.

Calling the player code I wanted to make sure that a player taking too long to return or throwing a exception did not end the game. The exception part is easy, wrap each call in a try/catch. For the too long issue I make each call using an in-line Task object and then calling Task.Wait(1000). I really like how this works. Because it's in-line I can use local variables which keeps the code very simple. And if after a second they haven't provided a turn, the task is ended and I give them a random turn.

I don't know how useful this will be for others. I think the use of Task and LINQ is a good example. And the timer approach is exactly what you want if you are drawing to Forms objects. You are welcome to use the code in any way you wish.

5:41

And we have a winner. The criteria was you had to win twice. And the games went En-jay's team, Craig's team, and then En-jay's team again. So En-jay's team is the winner - congrats.

Everyone had a really good time. Most of the company watched the final play (the free beer didn't hurt) and there was lots of cheering and commentary as the game played. And the shot clock was really important. Without that each team would have stayed in their base waiting for others to fight it out.

We'll continue to do this. It's an incredible morale boost. Highly recommended.

Full source code including player AI's at: Download Broadside

Blog by Tomas about his team's effort - Tomas' Perspective, or What the F*ck was the Yellow Guy Thinking?

Tags Start & End

Tags Can Start & End Anywhere

Appendix B

.NET code for multi-page image output

Appendix A

Java code for multi-page image output

Data Bin Search

The Data Bin can now be searched to find a table, column, node or other piece of data without scrolling through it all.

Shrink to Fit

This will shrink the contents of a cell until it fits the defined cell size.

Time Zone Conversion

A new Windward macro has been added to help with converting dates and times from UTC time to the local time zone.

Image Output Format

New image output formats added.

PostScript Output Format

PostScript, commonly used with printers and printing companies, has been added as an additional output format.

New and Improved Datasets (Designer, Java Engine, .NET Engine)

Datasets have been re-written from scratch to be more powerful and easier to use.

Stored Procedure Wizard (Designer)

This works for all tag types that are connected to a SQL-based data source (Microsoft SQL Server, Oracle, MySQL, or DB2).

Boolean Conditional Wizard (Designer)

Before, conditional statements could only be written manually. Now they can also be built using our intuitive Wizard interface.

Reorganized Ribbon

The ribbon menus have been re-organized and consolidated to improve the report design workflow.

XPath 2.0 as Data Source

Adds various capabilities such as inequalities,descending sort, joins, and other functions.

SQL Select Debugger

SQL Select  Debugger

  • The look and feel was improved
  • Stored Procedure Wizard
  • Improved Exceptions pane

Tag Editor/Tag Selector

Added a Query tab as a field for typing or pasting in a select statement

  • Color Coding of Keywords
  • TypeAhead
  • Evaluate is now "Preview"

Rename a Datasource

All tags using that Data source will be automatically updated with that name.

Connecting to a Data Source

New single interface to replace 2 separate dialog boxes

Tag Tree

Displays of all the tags in the template, structured as they are placed in the template. This provides a simple & intuitive way to see the structure of your template. Also provides the capability to go to any tag and/or see the properties of any tag.

Added Javelin into the RESTful Engine

Support for Google Application Engine Integration

The ability to integrate the Windward Engine into Google’s cloud computing platform for developing and hosting web applications dubbed Google Applications Engine (GAE).

Additional Refinement for HTML Output

  • Improved indentation for ordered and unordered lists
  • Better handling of template header and footer images
  • Better handling for background images and colors

Redesigned PDF Output Support

This new  integration will allow for processing of complex scripts and bi-directional  text such as Arabic.  Your PDF output  will be much tighter and more closely match your template, and we’ll be able  to respond rapidly to PDF requests and fixes.

PowerPoint Support

Includes support for new ForEach and slide break handling, table header row repeat across slide breaks, and native Microsoft support for charts and images.

Tags are Color Coded

Tags are color coded in the template by type, making it easy to visually identify them.

Increased Performance

Version 13’s core code has been reworked and optimized to offer a reduced memory footprint, faster PDF generation and full documentation of supported features and limitations in the specifications for DOCX, XLSX and PPTX.

Advanced Image Properties

Documents can include advanced Word image properties such as shadows, borders, and styles.

Improved HTML Output

Windward has updated HTML output to reflect changing HTML standards.

Version 13 New Data Sources

Windward now works with a slew of new datasources: MongoDB, JSON, Cassandra, OData, Salesforce.com

Generate Code

The Generate Code tool in the designer allows you to open an existing template and, with a click of a button, automatically create a window with the code needed to run your current template with all data sources and variables. Simply copy this code and paste into your application's code in the appropriate place. You now have Windward integrated into your application.

You only need to do this once. You do not do this for each template. Instead, where it has explicit files for the template and output, change that to parameters you pass to this code. Same for the parameters passed to Windward. This example uses explicit values to show you what to substitute in where.

Pivot Tables Adjusted in Output

Any pivot tables in an XLSX template are carried over to the XLSX output. The ranges in the pivot ranges are adjusted to match the generated output. So your final XLSX will have pivot tables set as expected in the generated file.

This makes creating an XLSX workbook with pivot tables trivial.

Imported Template Can be Set to Match the Parent Styles

In an imported sub-template, if its properties for a style (ex. Normal) differ from the parent template's properties for the style, the use in the sub-template can be set to either use the properties in the sub-template, or the properties in the parent.

You set to retain when you don't want the child template's styling to change when imported. You set to use the parent when you want the styling of the imported template to match the styling in the parent.

Any explicit styling is always retained. This only impacts styling set by styles.

Tags can be Placed in Text Boxes

Tags can be placed in text boxes. Including linked text boxes. This gives you the ability to set the text in a textbox from your data.

Tags can be Placed in Shapes & Smart Art

Tags can be placed in shapes & smart art. This gives you the ability to set the text in a shape from your data.

HTML Output Supports Embedded Images

When generating HTML output, the engine can either write bitmaps as distinct files the generate HTML references, or it can embed the images in the HTML providing a single file for the output.

Footnotes & Endnotes can Have Tags

You can place tags in pretty much any part of a template, including in footnotes & endnotes.

Document Locking Supported in DOCX & XLSX

Any parts of a DOCX or XLSX (PowerPoint does not support this) file that are locked in the template, will be locked the same in the output.

Specify Font Substitution

If a font used in the template does not exist on the server generating a report, the font to substitute can be specified.
In addition, if a glyph to be rendered does not exist in the font specified, you can specify the replacement font. This can be set distinctly for European, Bi-Directional, and Far East fonts.

Process Multiple Datasources Simultaneously

If you need this - it's essential. And if you don't need it, it's irrelevant.

Windward enables you to build a document by applying multiple datasources to the template simultaneously. When Windward is merging the data into a template, it processes the template by handling each tag in order, and each tag pulls from different datasources. (As opposed to processing all of one datasource, then processing the next.)

This allows the select tag to use data from another datasource in its select. For example, if you are pulling customer information from one data source, you can then pull data from the sales datasource using the customer ID of the customer presently processing to pull the sales information for that customer. If you're interested in patching together your data from multiple datasources, read this post on our blog.

David Thielen

President/CEO at Windward Studios

From his early years as a Senior Developer at Microsoft, to legendary designer of the popular Enemy Nations strategy game, to reporting and document generation guru, Dave has never lost his passion for building superb software and teams.

david@windward.nethttps://www.linkedin.com/in/davethielen/
This blog was written by:_
David Thielen

For over 10 years, Windward has lead the industry with our world-class document generation platform that creates visually stunning, data-powered documents designed exactly the way users want and are created in a fraction of the time and cost compared to existing solutions. Proudly located in Boulder, Colorado, Windward Studios is the premier solution for developers and business users adding reporting and document generation capabilities to their applications in over 70 countries around the world.

© 2019 Windward Studios Inc.

Contact

Got questions about reporting and document generation? We've got answers—let's connect!
Send a note
messaging, phone, or email contact optionsclose out button