Showing posts with label projects. Show all posts
Showing posts with label projects. Show all posts

Wednesday, 18 March 2015

An implementation of the Tic Tac Toe game in Java

Lately I’ve been really busy, and I’ve not had that much time to spend on programming. I’ve had this implementation of the Tic Tac Toe game in my “to do list” for a very long time but sadly for one reason or another it just kept being delayed. In some sense it is easier to code than the text editor I made here, however it has not been so in practice. In particular, I can’t shake the feeling that the code of this project is redundant and sloppy. I’d be happy to be proved wrong but I guess there’s no point in arguing over such a small project.

This project, aside from getting more familiar with Java syntax and its libraries, helped me in becoming more fluent in building a GUI with Swing. There are some technical details which I’d like to discuss perhaps with some professional Java coder in case I get the chance.to talk to one (indeed networking is important).

The main Idea was to develop a Tic Tac Toe implementation and a battleship game as well, however, due to the lack of spare time I put aside the battleship game project and perhaps I will work on it in the future, although the basics of the underlying code are pretty similar to those used here (so similar that it might actually be a repetition).

Enough talk, let’s cut to the chase! Here are some screenshots, and below you can find the download link for the Java application. As usual, I’ve put into the .zip folder the source code so that you can check it out, work on it, whatever you like

How to play:
Basically you click on one of the three columns (if you click on any button on the selected column that’s also fine) and your mark (“x” for the player) is dropped into the lowest box available. Then the computer makes its move (randomly). Whoever reaches first 3 in a row.wins. The idea of the lowest box is due to the fact that I had another game in mind while developing this, it turned out as a “new” kind of Tic Tac Toe and I thought it was nice to play with. Of course by working on the source code I uploaded along with the runnable jar one could easily remove this and get back to the classic game.

Dropbox download link.

Immagine 001

Immagine 002

Hope this was interesting.

Monday, 9 March 2015

My first serious Java project

Finally here I am with my first serious Java project: a text editor!

Yeah I know, perhaps the word “serious” is a bit of an overkill for a simple project such as a text editor. However for a Java newbie, as I consider myself to be, this was actually a middle level project. The hard part was using Swing to implement the user interface, although I find Swing to be easier to use than Tkinter (Python) once that you’ve got the basics.

So, what’s this text editor about? Basically it is a notepad (like the one that comes natively with Windows but with less options, and of course it is written in Java. Here is a screenshot:

im1

Now, let’s talk about how to use this.

The file menu:

The file menu has 4 options: open, show current working directory, set current working directory and exit.
The open command lets you search for a .txt file to display in the user-interface.
The show and change current working directory commands are used to show and change, respectively, the directory you are working in.

You can open a file with the commands “open” and “load .txt file” or work on a new one simply by typing into the white box. When you have edited a file in the text editor, you can save it by typing a name into the bottom text field and then clicking the bottom right button. If you do not enter a name, the file will be saved with a default one. Furthermore, once you click on “save .txt data” the program asks you if you want to save the file in the current working directory. If you need to choose another location click “no” and then choose the directory to save the file in. Enter the same name of the file you are working on, to overwrite it.

The edit menu:

The edit menu basically lets you change font size (by 3 pt a click).

You can download the program (the runnable .jar file and the source code) at this address below:

https://www.dropbox.com/s/e4jhp1w9n1w6pp8/java%20text%20editor.zip?dl=0

Hope this was interesting.

Monday, 13 October 2014

A simple program to calculate LED lights savings

 

EDIT: For the most updated application, please check the following page:
New version and bug fixes for LED lights savings calculator application


A week ago, a friend asked for a simple program which could help him calculate savings consumptions fast and give a glance at the overall picture.

If you did not know, by replacing your old bulbs and lamps with LED lights you can save up to 90% depending on the replaced lamp. For instance, if you replace neon with LED, savings are at least 45%, however they might be higher due to neon maintenance and hidden consumptions. If you replace incandescent bulbs with LED you save at least 80%.

Although initial price may be higher, LED lamps will surely pay off in the long run, since they are expected to last 50.000 hours!

This simple program lets you enter the data on your actual lights and the LED, then it calculates:
-Savings
-Savings at each year, for 10 years
-Amortization time if the lamps are paid in one shot
-Financing of the LED lamps with a basic constant pay-out mortgage assuming a 5% interest rate
-Immediate savings if the LED lamps are financed

Click here for the python source code. Again, the program is pretty simple since only a sketch is needed. It surely can be improved. If you have any suggestion please let me know.

Here are some screenshots and a calculation example

Immagine 2

The result of a simple replacement: 200 neon tubes with 200 LED tubes (assuming a cost of 26 euro/pc for LED tube.


Looks really like LEDs are the lights of the future.


Note: some data may be incorrect in your country (for example energy cost, tube cost, etc..), if you should ever use this program please check each data you input.

Wednesday, 13 August 2014

Controlling your Arduino Uno board with Python

Today I am going to talk about a particular topic which overlaps Arduino and computer programming.

Last month I was watching video about electronics and thinking about the solar controller which many solar stand alone kits use. For some reason they do not behave as they should (i.e. cut the power to the lights when the sun is rising and give full power in the evening). However, a far more important point is that for this kind of controller a centralized control system is not available. Therefore I had a great idea: let’s try to program a board such as Arduino, to do the job.

I bought an Arduino Uno, the basic model, which I guess is suitable for beginners as myself. You can check more details here.

Arduino is an open source project and can be programmed in a language which is similar to C. This point is fine, since I have some knowledge of C and can get by quite easily with beginner projects. Once the main code has been loaded on Arduino, you can interact with the board through the USB and the “shell”, and, for instance, give instructions to trigger some control flow structure such as if-if else- else. However, in some cases it does not work (still do not know why) or it is impractical, since you can enter only one value at time. It would be nice to control the board with some external tool which could let you write some script to execute. It turns out that there is a module which enables you to do such operations. This module is pySerial. It can be downloaded here and is available for Python 3 as well!

The module is great! It works really smooth and in a linear manner, as it should. However, Arduino accepts only raw bytes and binary code as input therefore some little amendments must be made to be able to communicate with it through Python 3. Note that there are some differences with Python 2 which I will not cover.

First of all, Python 3 wants naturally work with ASCII characters, therefore when sending an integer to Arduino Uno, our sweet board will understand anything but what we have sent. For characters such as ‘a’, the matter is somewhat at ease, since you just need to send the character with a b in front of it: b’a’. On the opposite side, when reading data, you need to convert it into a readable format. To solve all these “problems” I decided to build a simple class which essentially is a wrapper of some functions of Serial and can be used directly to send characters, lists and integers to Python. Perhaps I will add also the possibility to send floats and strings although I am not sure the latter can be send and understood from the board.

Here is a basic example of communication with Arduino Uno through Python 3.

First of all we need to load the following code to Arduino Uno. This simple code lights a LED light according to the value of  readData which is read from the USB port. Serial.print prints out the value of readData to the USB port.

For instance, to control the board in this case, you can use this Python code:

However, writing this code again and again is boring and can easily lead to mistakes. Therefore I decided to built a class whose name is Arduino


By creating an Arduino object using the ArduinoClass, you can call the following methods:

I am also working on a GUI with PyQt4 for this script. I will keep you posted.


The source code of the ArduinoClass is available here.


Hope this is useful!

Monday, 28 July 2014

A BlackJack game simulator with Python

Hi everyone! Here is another one of the first projects I have developed. The project is quite simple as the name tells: A blackJack game simulator.

The modules used to build this game are the following:
-The Random module, mainly the sample function to sample from a deck of 52 cards.
-EasyGui. Again, another one such as tkinter would have probably been better however I did not know even the name of tkinter at the time I built this.

Some notes on the game:
-Double down is allowed
-The scores are the same as most real BlackJack games
-You can set your initial amount available and the maximum bet (you could even set a negative bet I guess it should work even in that case Occhiolino ah ah).
-There still no insurance available and splitting is not allowed (perhaps I will implement this when I’ll improve the GUI)

IMPORTANT NOTE: Please, before running the script with Python make sure to set the path to the folder where the images are located so that they can appear on the EasyGui window.

You can download the source code here.

Here are some screenshots

im1

im2

Hope you enjoy.

Sunday, 27 July 2014

A simple roulette game simulator created with Python

Another project! This is one of the first game simulator I have tried to develop with Python. It’s basically a roulette simulator and has all the features a roulette should has, I guess!

Some notes:
-The roulette has only one zero (no double zero)
-Payoff are the same as a real roulette (no real money is involved, of course!)
-Outcomes should be uniformly distributed
-The graphics is not the best due to the fact that I used Easygui. Perhaps in the future I will implement the game with tkinter.

Here are the modules used:
-Random. The random module is necessary to simulate the random spin of the roulette.
-Easygui for the graphics. Easygui is a very simple module for user-interaction. I believe it is built on tkinter but has way less features. If you are new to GUI programming in Python I suggest starting with either tkinter or wxPython but not Easygui. Easygui is easier but not that useful nor customizable. On the contrary, the other two modules are harder to use for a new programmer but way more customizable and “complete”. Also the playability is somewhat ruined by the appearing and disappearing windows by Easygui.

You can download the source code here. Only to be run with Python.

Here are some screenshots.

im

im2

First project: a (very) simple database management software.

Hi to everyone! Today I am going to upload a project I have recently developed to enhance my coding skills. The project is a simple database management software.

It is build using Python and the following modules:
-sqlite3 module for the database. I wanted to use the mysql module however there is still no version of this module available for Python 3. Anyway sqlite3 is a very light and fast module. It is pretty simple too.
-The GUI has been coded using tkinter. I do not know if tkinter is the best option but it is the first module for GUIs that I started learning and so far so good. It works good!

As of now, the software lets you do the following:
-Create a database
-Create and delete as many tables as you want (though only 3 fields are allowed as for now). You can create tables with up to 3 fields.
-Add and delete records
-Display records, all records or records according to defined criteria.
-Display available tables and table structure

Check the help section for allowed type of data. You can download the program from here:
https://www.dropbox.com/s/llycwz0fnlkhw11/software%20database_.zip

The source code sure has some redundancies which can be deleted. Please if you have some suggestion that would make the code work and or look better or be more manageable please leave a comment or let me now.

Here are some screenshots:

screenshot2

screenshot1