Permalink
Browse files

Implemented an early version.

  • Loading branch information...
1 parent 689e899 commit 2e07ac820828335a3683b0c6bb472e3df55f5d1c @kaisellgren kaisellgren committed Aug 23, 2012
View
@@ -6,3 +6,6 @@ vendor
bin
.idea
composer.phar
+web/images/
+web/css/
+web/js
View
@@ -24,6 +24,6 @@ twig:
assetic:
debug: %kernel.debug%
use_controller: false
- bundles: [ ]
+ #bundles: []
filters:
cssrewrite: ~
@@ -1,13 +0,0 @@
-<?php
-
-namespace Loiste\MinesweeperBundle\Controller;
-
-use Symfony\Bundle\FrameworkBundle\Controller\Controller;
-
-class DefaultController extends Controller
-{
- public function indexAction()
- {
- return $this->render('LoisteMinesweeperBundle:Default:index.html.twig');
- }
-}
@@ -0,0 +1,38 @@
+<?php
+
+namespace Loiste\MinesweeperBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Session\Session;
+use Loiste\MinesweeperBundle\Model\Game;
+
+class GameController extends Controller
+{
+ public function startAction()
+ {
+ // Setup an empty game. To keep things very simple for candidates, we just store info on the session.
+ $game = new Game();
+
+ $session = new Session();
+ $session->start();
+ $session->set('game', $game);
+
+ return $this->render('LoisteMinesweeperBundle:Default:index.html.twig', array(
+ 'gameArea' => $game->gameArea
+ ));
+ }
+
+ public function makeMoveAction()
+ {
+ $row = $this->getRequest()->get('row'); // Retrieves the row index.
+ $column = $this->getRequest()->get('column'); // Retrieves the column index.
+
+ $session = new Session();
+ $session->start();
+ $game = $session->get('game'); /** @var $game Game */
+
+ return $this->render('LoisteMinesweeperBundle:Default:index.html.twig', array(
+ 'gameArea' => $game->gameArea
+ ));
+ }
+}
@@ -0,0 +1,34 @@
+<?php
+
+namespace Loiste\MinesweeperBundle\Model;
+
+/**
+ * This class represents a game model.
+ */
+class Game
+{
+ /**
+ * A two dimensional array of game objects.
+ *
+ * E.x.: $gameArea[3][2] instance of GameObject
+ *
+ * @var array
+ */
+ public $gameArea;
+
+ public function __construct()
+ {
+ // Upon constructing a new game instance, setup an empty game area.
+ $this->gameArea = array();
+
+ for ($row = 0; $row < 10; $row++) {
+
+ $temp = array();
+ for ($column = 0; $column < 20; $column++) {
+ $temp[] = new GameObject(floor(mt_rand(0, 2)));
+ }
+
+ $this->gameArea[] = $temp;
+ }
+ }
+}
@@ -0,0 +1,37 @@
+<?php
+
+namespace Loiste\MinesweeperBundle\Model;
+
+/**
+ * This class represents a game object.
+ */
+class GameObject
+{
+ const TYPE_UNDISCOVERED = 0;
+ const TYPE_MINE = 1;
+ const TYPE_EMPTY = 2; // Discovered.
+ const TYPE_NUMBER = 3; // Discovered.
+ const TYPE_EXPLOSION = 4; // Damn we hit a mine!
+
+ public $type;
+
+ public function __construct($type = 0)
+ {
+ $this->type = $type;
+ }
+
+ public function isMine()
+ {
+ return $this->type === GameObject::TYPE_MINE;
+ }
+
+ public function isNumber()
+ {
+ return $this->type === GameObject::TYPE_NUMBER;
+ }
+
+ public function isEmpty()
+ {
+ return $this->type === GameObject::TYPE_EMPTY;
+ }
+}
@@ -1,3 +1,7 @@
-LoisteMinesweeperBundle_index:
+LoisteMinesweeperBundle_start:
pattern: /
- defaults: { _controller: LoisteMinesweeperBundle:Default:index }
+ defaults: { _controller: LoisteMinesweeperBundle:Game:start }
+
+LoisteMinesweeperBundle_makeMove:
+ pattern: /makeMove
+ defaults: { _controller: LoisteMinesweeperBundle:Game:makeMove}
@@ -0,0 +1,24 @@
+html, body {
+ background: #eee;
+ font-family: "Segoe UI", sans-serif;
+ margin: 16px 32px;
+}
+
+.game-cell {
+ background: #ddd;
+ border: 1px solid #aaa;
+ cursor: pointer;
+ width: 64px;
+ height: 64px;
+}
+
+/* Center content inside game cells. */
+.game-cell > * {
+ display: table;
+ margin: 0 auto;
+}
+
+.game-number {
+ font-size: 32px;
+ font-weight: bold;
+}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -0,0 +1,18 @@
+/*!
+ * This file contains the front-end logic for the Minesweeper game.
+ */
+
+$(function() {
+ // Find out the route to the makeMove -action.
+ var routeMakeMove = $('#game').data('route-make-move');
+
+ $('.game-cell').click(function() {
+ // Find out the index of column & row.
+ var column = $(this).index();
+ var $tr = $(this).parents('tr');
+ var row = $tr.index();
+
+ // Make a move.
+ window.location = routeMakeMove + '?column=' + column + '&row=' + row; // Simple URL param concatenation.
+ });
+});
@@ -1 +1,28 @@
-Hello!
+{% extends 'LoisteMinesweeperBundle::base.html.twig' %}
+
+{% block body %}
+
+<h1>Minesweeper</h1>
+
+<table id="game" data-route-make-move="{{ path('LoisteMinesweeperBundle_makeMove') }}">
+ {% for row in 0..9 %}
+ <tr>
+ {% for column in 0..19 %}
+ <td class="game-cell">
+ {# Randomize what's in there... TODO: render the correct visual. #}
+ {% set type = gameArea[row][column].type %}
+
+ {% if type == constant('Loiste\\MinesweeperBundle\\Model\\GameObject::TYPE_NUMBER') %}
+ <div class="game-number">3</div>
+ {% endif %}
+
+ {% if type == constant('Loiste\\MinesweeperBundle\\Model\\GameObject::TYPE_MINE') %}
+ <img src="{{ asset('bundles/loisteminesweeper/images/mine.png') }}" />
+ {% endif %}
+ </td>
+ {% endfor %}
+ </tr>
+ {% endfor %}
+</table>
+
+{% endblock %}
@@ -0,0 +1,31 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <title>Loiste Minesweeper</title>
+ {% image
+ '@LoisteMinesweeperBundle/Resources/public/images/*'
+ %}
+ {% endimage %}
+
+ {% stylesheets filter="cssrewrite"
+ 'bundles/loisteminesweeper/css/*'
+
+ output='css/dist/dist.css'
+ %}
+ <link rel="stylesheet" href="{{ asset_url }}" />
+ {% endstylesheets %}
+
+ {% javascripts
+ '@LoisteMinesweeperBundle/Resources/public/js/jquery-1.8.0.min.js'
+ '@LoisteMinesweeperBundle/Resources/public/js/*'
+ %}
+ <script src="{{ asset_url }}"></script>
+ {% endjavascripts %}
+ </head>
+ <body>
+ {% block body %}
+ Nothing to show.
+ {% endblock %}
+ </body>
+</html>

0 comments on commit 2e07ac8

Please sign in to comment.