Project Group Applied Functional Programming



Similar documents
Why HTML5 Tests the Limits of Automated Testing Solutions

Comparing Dynamic and Static Language Approaches to Web Frameworks

YouTrack MPS case study

Game Design From Concepts To Implementation

Course Information Course Number: IWT 1229 Course Name: Web Development and Design Foundation

game development documentation game development documentation: concept document

Information Technology Career Field Pathways and Course Structure

Advantages of PML as an iseries Web Development Language

COURSE SYLLABUS EDG 6931: Designing Integrated Media Environments 2 Educational Technology Program University of Florida

IE Class Web Design Curriculum

Web Development. Owen Sacco. ICS2205/ICS2230 Web Intelligence

WEB, HYBRID, NATIVE EXPLAINED CRAIG ISAKSON. June 2013 MOBILE ENGINEERING LEAD / SOFTWARE ENGINEER

separate the content technology display or delivery technology

testing EFFECTIVE TESTS FOR EVERY METRIC OF AN CAMPAIGN

Enterprise Mobile Application Development: Native or Hybrid?

Whitepaper. Trans. for Mobile

Team Members: Christopher Copper Philip Eittreim Jeremiah Jekich Andrew Reisdorph. Client: Brian Krzys

WHITE PAPER. FortiWeb and the OWASP Top 10 Mitigating the most dangerous application security threats

New Generation of Software Development

Business Process Management In An Application Development Environment

Client vs. Server Implementations of Mitigating XSS Security Threats on Web Applications

Lesson Overview. Getting Started. The Internet WWW

So today we shall continue our discussion on the search engines and web crawlers. (Refer Slide Time: 01:02)

Software Requirements Specification For Real Estate Web Site

COMPUTER SCIENCE/ COMPUTER NETWORKING AND TECHNOLOGIES (COSC)

ORACLE MOBILE APPLICATION FRAMEWORK DATA SHEET

Form Management Admin Guide

ORACLE ADF MOBILE DATA SHEET

Bypassing Internet Explorer s XSS Filter

php-crypto-params Documentation

How to Monitor and Identify Website Issues. 2013, SolarWinds Worldwide, LLC. All rights reserved. Share:

Dynamic website development using the Grails Platform. Joshua Davis Senior Architect Cognizant Technology Solutions

HTML5 : carrier grade

Actuate Business Intelligence and Reporting Tools (BIRT)

A new dimension of sound and vibration analysis

MarkLogic Server. Reference Application Architecture Guide. MarkLogic 8 February, Copyright 2015 MarkLogic Corporation. All rights reserved.

Cross Site Scripting Prevention

Web Design Competition College of Computing Science, Department of Information Systems. New Jersey Institute of Technology

A Model of the Operation of The Model-View- Controller Pattern in a Rails-Based Web Server

ORACLE APPLICATION EXPRESS 5.0

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Research on HTML5 in Web Development

Firefox for Android. Reviewer s Guide. Contact us: press@mozilla.com

Acunetix Website Audit. 5 November, Developer Report. Generated by Acunetix WVS Reporter (v8.0 Build )

Accelerating Wordpress for Pagerank and Profit

TATJA: A Test Automation Tool for Java Applets

Table of Contents Recommendation Summary... 3 Introduction... 4 Formatting Recommendations... 5 Creative:... 7 Deliverability & Infrastructure:...

Measure What Matters. don t Track What s Easy, track what s Important. kissmetrics.com

ASP.NET: THE NEW PARADIGM FOR WEB APPLICATION DEVELOPMENT

Business Rules in User Interfaces

38 Essential Website Redesign Terms You Need to Know

Client Side Filter Enhancement using Web Proxy

Portals and Hosted Files

How Web Browsers Work

Secure Authentication and Session. State Management for Web Services

A Monitored Student Testing Application Using Cloud Computing

Chapter 10: Multimedia and the Web

Monitoring Infrastructure (MIS) Software Architecture Document. Version 1.1

Mobile Game and App Development the Easy Way

Use Host Information in Policy Enforcement

Enduring Understandings: Web Page Design is a skill that grows and develops throughout the careful planning and study of software and design.

Where every interaction matters.

Basic tutorial for Dreamweaver CS5

International Journal of Engineering Technology, Management and Applied Sciences. November 2014, Volume 2 Issue 6, ISSN

IGW+ Certificate. I d e a l G r o u p i n W e b. International professional web design,

Bitrix Site Manager 4.1. User Guide

WebSphere Business Modeler

Web Pages. Static Web Pages SHTML

WompMobile Technical FAQ

MEng, BSc Applied Computer Science

DIPLOMA IN GRAPHIC WEB DESIGN AND WEB DEVELOPMENT COURSE INFO PACK

Web application security: automated scanning versus manual penetration testing.

Agile Business Suite: a 4GL environment for.net developers DEVELOPMENT, MAINTENANCE AND DEPLOYMENT OF LARGE, COMPLEX BACK-OFFICE APPLICATIONS

RNA Movies 2: sequential animation of RNA secondary structures

Front-End Performance Testing and Optimization

Flash Tutorial Part I

A Closer Look at BPM. January 2005

Integrated Network Vulnerability Scanning & Penetration Testing SAINTcorporation.com

SECURE APPLICATION DEVELOPMENT CODING POLICY OCIO TABLE OF CONTENTS

Criteria for web application security check. Version

MOVING THE SENIOR DEVELOPMENT CLASS FROM WEB DEVELOPMENT TO LIFE CYCLE DEVELOPMENT A CASE FOR VISUAL STUDIO 2005

CHAPTER 18 Programming Your App to Make Decisions: Conditional Blocks

Magento Security and Vulnerabilities. Roman Stepanov

1 File Processing Systems

Ball Aerospace s COSMOS Open Source Test System

What s new in Excel 2013

DM810 Computer Game Programming II: AI. Lecture 11. Decision Making. Marco Chiarandini

A Server and Browser-Transparent CSRF Defense for Web 2.0 Applications. Slides by Connor Schnaith

HTML5 Data Visualization and Manipulation Tool Colorado School of Mines Field Session Summer 2013

MEng, BSc Computer Science with Artificial Intelligence

Java Application Developer Certificate Program Competencies

Task-Model Driven Design of Adaptable Educational Hypermedia

How to achieve PCI DSS Compliance with Checkmarx Source Code Analysis

DIABLO VALLEY COLLEGE CATALOG

Subject knowledge requirements for entry into computer science teacher training. Expert group s recommendations

SQL INJECTION ATTACKS By Zelinski Radu, Technical University of Moldova

Transcription:

Project Group Applied Functional Programming Web Development with Haskell Meike Grewing, Lukas Heidemann, Fabian Thorand and Fabian Zaiser Informatik, Universität Bonn, Germany Abstract The goal of the project group Applied Functional Programming was to get an insight into web development using Haskell and the web framework Yesod. In this paper, we want to document our main project, a web implementation of the well-known game Battleships including an artificial intelligence. You can find the code on GitHub 1 and play the current production version 2. 1 Introduction Our main project was to develop a Battleships web application providing a neat graphical user interface and an adjustable AI to play against. We decided to use the following rules: Each player begins with a total of ten ships: one ship of length five, two of length four, three of length three and four of length two. When a shot hit a ship, the player may fire again. In addition to the usual rules, our version of the game includes some modifications: In addition to firing a shot, the player is allowed to move one of his ships forward or backward by one cell in each round (adhering to the placement regulations). As soon as one of your ships is completely sunk, other ships can be moved across the space it previously occupied. The number of turns is limited to prevent endless games. After the last turn, the player with the most remaining ships wins. 2 Architecture and Framework For our project, we used the Yesod Web Framework 3 developed by Michael Snoyman and others. Its goals are performance, type-safety and conciseness of code. For these purposes Yesod offers embedded DSLs 4 using template haskell for writing HTML, CSS and JavaScript code which allow using variables from Haskell code and are syntactically simpler (e.g. the DSL for HTML uses indentation instead of closing tags). There is also a lot of 1 https://github.com/zrho/afp/tree/master/battleships 2 http://www-pg.iai.uni-bonn.de/battleships 3 http://www.yesodweb.com/ 4 Domain Specific Languages 1

Figure 1: Typical game view. Shots are fired by clicking on the target. Figure 2: The ships can be placed randomly or via drag and drop. compile-time checking involved. URLs in Yesod are type safe, meaning that one specifies which parameters a certain route expects and their types. Whenever it is referenced, e.g. in hyperlinks or redirections, the compiler checks if the expected and actual data formats match. This makes 404 errors for internal links almost impossible. Furthermore, the type system guarantees that dynamic content inserted into HTML pages is verified or escaped, preventing common attacks like cross site scripting (XSS). Yesod also offers good support for internationalization. In fact, our project web site is available in English and German. The game logic resides on the server and is completely written in Haskell. On the client

Figure 3: Recap of the game. (animated) side, we use JavaScript for ship placement, handling of clicks on the boards and viewing the replay. It is important to note here that even the visual representation of the game is created in Haskell. The images of the current game state are generated using diagrams, a DSL for declaratively describing vector graphics. Then they are rendered to SVG and included in the web page. Thus, all the JavaScript code really does is display a sequence of graphics created by Haskell code. The game state is not stored on the server but is sent back and forth between client and server. To prevent manipulations on the client side the state is encrypted before being exposed. Of course we could have implemented the game with only HTML and JavaScript which would have made the transfer of the game state between client and server obsolete. On the other hand, that would have meant having to go without everything Haskell has to offer. For instance, the type system not only enforces writing code that works but also offers ways to clarify that the AI is not cheating (see section 3). Besides, Diagrams makes it pleasant to write the rendering code. In a non-declarative language like JavaScript this would have been a lot more frustrating and error-prone. Besides, our goal was not to write another version of Battleships but to learn about the advantages and disadvantages of using a functional programming language for web development. Battleships was merely a means to an end. 3 Demonstrating that the AI Plays Fair Of course, the AI should play fair and observe the same rules as the human player. Allowing ship movement makes it harder to check the AI for fair play (because one cannot observe its movements), so we tried our best to clarify that the AI cannot cheat : After the game ends, we produce an animated recap of the course of the game to help the player comprehend what happened at what time. We use Haskell s type system to make it easy to find the places in the code where the AI

does get information and interact with the game logic. This way, it is easier to check the code to convince oneself that the the AI cannot cheat. More precisely, every AI is an instance of the type class 5 AI which is defined as follows: class AI a where aiinit :: (MonadRandom m) => Rules -> m (a, FleetPlacement) aifire :: (MonadRandom m, MonadState a m) => m Pos airesponse :: (MonadRandom m, MonadState a m) => Pos -> HitResponse -> m () aimove :: (MonadRandom m, MonadState a m) => Fleet -> TrackingList -> m (Maybe (ShipID, Movement)) On the one hand, Haskell s type system restricts the AI to use the given random number generator (via MonadRandom) and its own state (via MonadState), in addition to the information given by the game rules (Rules), the result of the AI s last shot (HitResponse) and the AI s own fleet (Fleet) and the shots fired at the AI s fleet (TrackingList). The type system prevents the AI from having any other side effects (like IO etc.) and by using Safe Haskell (a subset of Haskell whithout functions to circumvent the type system) this constraint can be enforced by the compiler. On the other hand, the game engine is polymorphic in the AI type, so it can only interact with the AI via the functions prodived by the AI type class. While it may be possible to pass additional information to the AI via these functions by misusing the data types in the parameters, one can easily verify that this is not the case by looking at the places where the four aforementioned functions are invoked. 4 Battleships AI In addition to a simple AI which basically does everything at random, we implemented a much more sophisticated AI which we are going to describe in some detail here. 4.1 Scoring Cells When deciding on a cell to fire at, it is necessary to know which cells cannot currently be occupied by a ship. This is the case if we just found out that there is water at a certain position, or just sunk a ship there, or hit a ship at a position that is diagonally adjacent to the cell. When ships are movable, you often cannot tell for sure whether a cell is blocked or not. Ships may move over water cells or completely sunk ships. Thus, we model the probability for a cell to be water as exponential decay with factor 0.98. The AI selects its next target by computing a score for each position. A high score indicates a high probability to hit a ship at that position, so the AI chooses the position with the highest score as its next target. To make playing more interesting, we added some randomness to calculating the scores. The amount of randomness depends on the selected difficulty level; the scoring method depends on whether ships are movable or not. 5 Type classes in Haskell correspond very roughly to interfaces in OOP

Immovable Ships. Since ships will always stay at their position, the AI never has to hit a position more than once. Given that all ships have a minimum length of two, it is sufficient to apply a checkerboard pattern when searching for ships. For each position on the board the AI considers all potential ships that this position is part of. This way it can score the position according to the likeliness of hitting a ship there. Movable Ships. In this case there are two phases because the amount of turns is limited: During the beginning, the search phase, the AI follows a checkerboard pattern to find (and not sink!) all ships. Sinking is not beneficial at this stage because it allows ships to move around more freely. When the AI has found enough ships or there are only few turns left, the AI switches to the sink phase and tries to completely sink all the previously discovered ships, applying the same scoring function as in the immovable case. 4.2 Moving Ships In each turn, the AI generates all possible movements of all movable ships and chooses one at random, or also randomly decides not to move at all. 5 Conclusion Admittedly, web programming in Haskell still has some minor flaws (especially the use of Template Haskell in combination with a lack of proper documentation sometimes led to difficulties). This, however, is only due to Yesod being still under active development and not a general flaw with web development in Haskell by itself. Then again, Haskell s expressive type system and powerful abstraction mechanism are strong advantages when developing web applications, especially regarding security and maintainability. And using a declarative DSL for graphics simplifies the drawing process, as thinking about what should be drawn is easier than thinking about how it should be drawn.