RT @WealthWitness: One of the best tools for Online Marketing offered Free until May 20th! Limited Amount Available. #makemoney #freesoftwa…
Keiser Report: Happy Hacking! (ft. Richard Stallman) http://t.co/fh1vVyfBzP #Facebook #freesoftware #FSF
Tracciati, livelli, maschere. Vettoriale o raster. Si fa tutto anche con software libero #shareitbaggio #freesoftware #venerdi17
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
James Hunt: A simple two-player QML game for Ubuntu Touch using the Ubuntu SDK: noughts and crosses (aka tic-tac-toe)!
But it doesn't stop there. The Design Team have done some incredible work in creating the Ubuntu SDK components: not only do they look fantastic (if you have the ubuntu-ui-toolkit-examples package installed, try running /usr/lib/ubuntu-ui-toolkit/demos/launch_componentshowcase), they are also extremely flexible and powerful.
As Rick has mentioned, it does take a while to grok the "QML-ish" way of doing things. And if like me you spend most of your time writing in imperative languages, initially you just think "all this QML is wonderful, but where do I actually put the code?". But then you have the epiphany moment when you realise you're already writing "the code" - in many cases, you don't need anything beyond the declarative QML itself.
I Need an Itch to Scratch
The only real way to learn a new language is to use it. But what to do? I wanted to code something simple and fun, like a game. There are already few games on the Collections page so I needed to think of a really simple one that is also fun to play. How about a game that even children can appreciate? Of course - Noughts and Crosses (aka tic-tac-toe)!
Note that the code is pretty rudimentary right now, but it's just about usable ;-)
Design This is a simple game so we only need a few objects: Cell, Game and MainView.
The MainView is the container for the application and includes a Page and the actual Game object. The only property we specify for the game is the boardSize of 3 giving us a 3x3 board. Technically, we don't actually even need to specify this since -- as we're about to see -- 3x3 is the default board size anyway. So, the Game object could be specified minimally as "Game {}". However, I've left it specified as a reminder to myself that ultimately I'd like to pass a variable to allow the board size to be specified at game creation time.
Here is a slightly simplified version of the MainView (noughts-and-crosses.qml):
import QtQuick 2.0 import Ubuntu.Components 0.1 MainView { Page { title: "Noughts and Crosses" id: page Game { // change this to whatever value you want for an NxN-sized board boardSize: 3 } } }
The Game object is a Column and comprises a Label, to show some text relating to the game, and a Grid to actually represent the game. There is some magic going on in the grid as it uses the very cool Repeater object to make laying out the grid easy: for a 3x3 board it creates 9 Cell objects and packs them into the grid. Here's a cut-down version of the Game object:
Column { property alias boardSize: gameGrid.boardSize Label { id: text text: "Noughts goes first" } Grid { id: gameGrid // Default to a 3x3 board (we only support square boards). property real boardSize: 3 // toggled between "O" and "X". The value specified below denotes // which side goes first. property string player: "O" columns: boardSize rows: boardSize // layout the appropriate number of cells for the board size Repeater { id: gridRepeater model: boardSize * boardSize Cell { width: 100 height: width } } } }
Note the property alias for boardSize in the Column object - it exposes a boardSize variable which is just a way to access the real variable of the same name within the Grid object. Note too that we tell the Grid object its dimensions by setting its columns and rows properties.
The Game object also contains a chunk of Javascript in the form of the checkForWin() function to determine whether a move resulted in the game being won.
The Cell object is the most interesting object. A Cell represents an individual location on the board. It is constructed from a Rectangle and comprises a Text value. The text value is either a middle-dot (to denote the cell has not yet been selected), a "O" or a "X". It also includes a MouseArea that specifies the new cell state to apply when the cell is clicked. Initially, the state is middle-dot but when the cell is clicked, the state is changed to the value of the parent (Game) objects player property. The Cell object specifies 3 states to represent every possible value a Cell can display. What's neat here is that changing the cells state also toggles the parent (Game) objects player property which allows the game to proceed with each player taking a turn. Clicking a cell also calls the checkForWin()function to determine if a particular turn results in the game being won. Here's the complete Cell object:
Rectangle { id: cell state: gameGrid.defaultText property alias textColor: cellText.color Text { id: cellText text: parent.state color: "silver" anchors.horizontalCenter: parent.horizontalCenter anchors.verticalCenter: parent.verticalCenter font.pointSize: 48 } states: [ State { name: cell.parent.defaultText PropertyChanges { target: gameGrid; player: "" } }, State { name: "O" PropertyChanges { target: gameGrid; player: cell.state } }, State { name: "X" PropertyChanges { target: gameGrid; player: cell.state } } ] // when clicked, MouseArea { anchors.fill: parent onClicked: { cell.state = (gameGrid.player == "O" ? "X" : "O"); gameGrid.numberTurns += 1 gameGrid.checkForWin(); } } }
Winning Algorithm The approach I've taken is very simplistic: just scan each row, column and diagonal looking for a winning run. This isn't particularly efficient (we're scanning the board multiple times) but that's not a problem for small board sizes. However, it has two fairly compelling attributes:
- It's simple to understand
- It works for arbitrary-sized boards.
My favourite alternative algorithm is to make use of the properties of Magic Squares. Using these, you can scan the board a single time to determine if a player has one. This is achieved by determining if a cell has been selected by a player and if so incrementing their counter based on the magic square value for that index. For a 3x3 board, if a players total equals 15, they win!
Screenshots So, what does it look like at this early beta stage...?
Start of a new game:
We have a winner!
Another winner on a 7x7 board (the person playing crosses needs more practice me thinks :-):
What's Next
- The javascript code is currently horrid and needs to be refactored with dynamite.
- Add ability to play "the computer".
- Config option to allow variable-sided playing grids.
- Once the game is stopped, we need to disallow further board clicks.
- Leverage more QML facilities to simplify the code further.
- Visual improvements (animation for a winning run maybe?)
- Ability to change player that starts.
- Score-keeping and "best of 'n' games " support (particularly useful when the kids beat you repeatedly ;-)
- Menu to start new game.
In Conclusion My "clean-room" implementation is far from perfect at the moment, but it's been a fantastic learning exercise so far and a lot of fun!
There are of course other QML noughts-and-crosses games out there. They come with varying licenses, some use C++ for the game logic, and most -- if not all -- are hard-coded to produce a 3x3 board only. Additionally, they generally use graphical representations for the noughts and crosses whereas here, I'm just using styled text. If you're interested, compare my github code with, for example, the Qt version to see the different approaches taken:
See Also
Rick Spencer: Dogfood Update
At the end of April, we set the goal to have Ubuntu Touch be dogfoodable on the Nexus and Nexus 4 phones. By that we mean, the goal is to make it so that we can use our phones exclusively as our phones. Today I chatted with some of the engineering managers involved to see how much progress we have made towards that. I am happy to say that it looks like we are still on track for this goal. However, there do appear to be some risky parts, so I am keeping my fingers crossed.
- You can make and receive phone calls: Done!
- You can make and receive sms messages: Done!
- You can browse the web on 3g data: Tony had been blocked on some technical issues, but thinks he's through them, so is in the debugging phase. He expects to have this done by end of May as per the dogfooding goal. For me, personally, this is the only missing part for me to be able to use the phone as my main phone around town. So, if Tony cracks this nut, then I will put away my old phone and start using my Ubuntu Phone exclusively.
- You can browse the web on wifi: Done! This has actually been done for quite a while.
- You can switch between wifi and 3g data: There are 2 parts to this work. There is low level networking code to get done, and then there is UI to enable it. That means that the Phone Foundations team and the Desktop team both have work to do. Both teams expect to get it done for May, but the work is not started yet.
- The proximity sensore dims the screen when you lift the phone to talk on it: There are two parts to this also. Gather the sensor data and then making the phone app use the sensor data. Work has not started for this part either.
- You can import contacts from somewhere, and you can add and edit contacts: There is some work done on this that imports from a *.csv file. I expect there will be some crude support for this in time for the May goal. It might be fun for someone to try out a more elegant implementation. Ubuntu Phone is using Evolution Data Server for the contacts store, so there may be folks out there who already have the experience to do this easily.
- When you update your phone your user data is retained, even if updating with phablet-flash: Done! This part being done makes the contacts import less important to me because as I add contacts they won't get blown away. On the other hand, it means it is worth it to import contacts, since you won't have to re-important as you update your phone each day (while it is in development).
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
Mozilla delays turning on third-party cookie killer in Firefox http://t.co/EaT3KYR6WO #freesoftware
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
Motorola Files Reply Brief in Appeal of Judge Posner's FRAND Decision in Apple v. Motorola-~pj
Motorola defends the value of its patents and then tells the Federal Circuit that RAND patent holders have to be able to seek injunctions against "intransigent" licensees like Apple. Otherwise, they'll take advantage, delaying by litigation any reckoning for years while benefiting from the technology without paying for it.
What exactly should happen to a company that refuses to pay and won't accept an offered rate or a court-set rate? The RAND patent holder *still* can't do a thing? No injunction? Nothing? Apple began its infringement, Motorola points out, in 2007. It's now 2013, and it still hasn't paid a dime. "Motorola should have the opportunity to seek an injunction to stop Apple's six years of ill-gotten gains from stretching into a decade or more," Motorola says.
Vreau ca toate instituțiile de stat să ruleze programe libere #gnu #linux #romania #freesoftware #programelibere #linuxromania
Linux Mint 15 brings prettier desktop, new software and driver managers http://t.co/TfBWbxAxuH #freesoftware
RT @sonjdol: I finally stopped using @itunes and switsched to @banshee. #freesoftware is better than #apple
Colin King: Kernel tracing using lttng
sudo apt-get update sudo apt-get install lttng-tools LTTng was already recently added into the Ubuntu 13.10 Saucy kernel, however, with earlier releases one needs to install the LTTng kernel driver using lttng-modules-dkms as follows:
sudo apt-get install lttng-modules-dkms It is a good idea to sanity check to see if the tools and driver are installed correctly, so first check to see the available kernel events on your machine:
sudo lttng list -k And you should get a list similar to the following:
Kernel events: ------------- mm_vmscan_kswapd_sleep (loglevel: TRACE_EMERG (0)) (type: tracepoint) mm_vmscan_kswapd_wake (loglevel: TRACE_EMERG (0)) (type: tracepoint) mm_vmscan_wakeup_kswapd (loglevel: TRACE_EMERG (0)) (type: tracepoint) mm_vmscan_direct_reclaim_begin (loglevel: TRACE_EMERG (0)) (type: tracepoint) mm_vmscan_memcg_reclaim_begin (loglevel: TRACE_EMERG (0)) (type: tracepoint) .. Next, we need to create a tracing session:
sudo lttng create examplesession ..and enable events to be traced using:
sudo lttng enable-event sched_process_exec -k One can also specify multiple events as a comma separated list. Next, start the tracing using:
sudo lttng start and to stop and complete the tracing use:
sudo lttng stop sudo lttng destroy and the trace data will be saved in the directory ~/lttng-traces/examplesession-[date]-[time]/. One can examine the trace data using the babeltrace tool, for example:
sudo babeltrace ~/lttng-traces/examplesession-20130517-125533 And you should get a list similar to the following:
[12:56:04.490960303] (+?.?????????) x220i sched_process_exec: { cpu_id = 2 }, { filename = "/usr/bin/firefox", tid = 4892, old_tid = 4892 } [12:56:04.493116594] (+0.002156291) x220i sched_process_exec: { cpu_id = 0 }, { filename = "/usr/bin/which", tid = 4895, old_tid = 4895 } [12:56:04.496291224] (+0.003174630) x220i sched_process_exec: { cpu_id = 2 }, { filename = "/usr/lib/firefox/firefox", tid = 4892, old_tid = 4892 } [12:56:05.472770438] (+0.976479214) x220i sched_process_exec: { cpu_id = 2 }, { filename = "/usr/lib/libunity-webapps/unity-webapps-service", tid = 4910, old_tid = 4910 } [12:56:05.478117340] (+0.005346902) x220i sched_process_exec: { cpu_id = 2 }, { filename = "/usr/bin/ubuntu-webapps-update-index", tid = 4912, old_tid = 4912 } [12:56:10.834043409] (+5.355926069) x220i sched_process_exec: { cpu_id = 3 }, { filename = "/usr/bin/top", tid = 4937, old_tid = 4937 } [12:56:13.668306764] (+2.834263355) x220i sched_process_exec: { cpu_id = 3 }, { filename = "/bin/ps", tid = 4938, old_tid = 4938 } [12:56:16.047191671] (+2.378884907) x220i sched_process_exec: { cpu_id = 3 }, { filename = "/usr/bin/sudo", tid = 4939, old_tid = 4939 } [12:56:16.059363974] (+0.012172303) x220i sched_process_exec: { cpu_id = 3 }, { filename = "/usr/bin/lttng", tid = 4940, old_tid = 4940 } The LTTng wiki contains many useful worked examples and is well worth exploring.
As it stands, LTTng is relatively light weight. Research by Romik Guha Anjoy and Soumya Kanti Chakraborty shows that LTTng describes how the CPU overhead is ~1.6% on a Intel® CoreTM 2 Quad with four 64 bit Q9550 cores. With measurements I've made with oprofile on a Nexus 4 with 1.5 GHz quad-core Snapdragon S4 Pro processor shows a CPU overhead of < 1% for kernel tracing. In flight recorder mode, one can generate a lot of trace data. For example, with all tracing enabled running multiple stress tests I was able to generate ~850K second of trace data, so this will obviously impact disk I/O.