Monday, May 3, 2021

Fun with Racket: Noughts and Crosses

TicTacToe Racket Style

This project was the first independent coding project (with GUI) undertaken by the author.   You can find the project by clicking the Github icon. ->GitHub

Author Review


Racket was the first language I learned to code with - unless you count HTML, but let's be honest, nobody ever does. With Information Technology being one of my favourite subjects at school, it was a bit disappointing coding and programming were never part of it. And with my luck, Computer Science was being introduced as a subject in my final year. But not to me.

Taking aid from the IT teachers, I got hold of the Python programming worksheets and in my spare time, tried to understand the absolute Greek in front of me. Not only did I leave it as a "future" problem, I didn't even get through the download/installation process.

And then I got to university. First year in Computer Science, we started with Racket. Suddenly everything started making sense. I was breezing through the material and so, let go of the fear and picked up Python again. Now that I understood various terms and jargon, it was a lot easier to go through. Racket was a great learning language, although I'm not quite sure if it's widely used in industry today.


Introducing Racket


Racket is a programming language - a dialect of Lisp and descendant of Scheme. It allows functional programming and other different models and ideas that programmers aren't even aware of. It's not the most infamous when it comes to languages, but it is a great starter and easy to build concepts and training.

The environment, Dr. Racket, is quite intuitive and once you get to know it a little better, can become incredibly easy to understand. It usually works in a read-eval-print loop, or REPL. Imagine your machine just sitting there and waiting for something to read. Once it gets it, it evaluates and then prints the result.  


 Dr. Racket environment


The Assignment



Using the skills and knowledge we have gained from Racket, the task involved building a game, complete with a user interface, in Racket. The game should allow two players to complete a game. 

The minimum your program should be able to do is to allow two players to go
together. This means that your program should:
• Display the state of the game;
• Allow players to make moves;
• Keep players from making illegal moves;
• Detect if one of the players has won and notify the players if this is the case.


Design and Development


To think about the game and design at first, I had to choose the data structure to represent the board. Vectors were easy to work with and were mutable which fit the requirements perfectly, although I did some experimentation with other data structures with code that was already available to us.

TicTacToe in DrRacket interactions window

Next came the rules we had to identify. Of course, to play tictactoe, any cell or position would have 3 distinct values at any time in the game - empty, X or O. I chose to represent this as 0, 1 and 2 respectively. Identifying all the possible wins and losses for each side, and mapping it out on the vector board took a while to do, but it worked out in the end.

The final interface was quite simple and easy to put together. A series of buttons along with a label to announce player turns and notify if a player has won, in a frame, all provided by racket/gui. The below figure shows the final result.

Two-player Racket Tictactoe interface

I even added an extra feature, the Refresh button, to the game as it got extremely annoying to close and re-run the program every time someone wanted to play. Any tictactoe game has three results - Player 1 wins, Player 2 wins or it's a draw. The below figure shows all three endings.
TicTacToe Results

Evaluation



Racket GUI: Noughts and Crosses

Playing around with the GUI elements in Racket and trying to get different versions of it to go along were fun. I tried to have the two players as Mickey Mouse and Donald Duck, and instead of having X's and O's, an image of them would be displayed. As much as I tried though, it wasn't working out.



I opened up to extend it to having a "computer" player as well, although being a beginner, I didn't get very far. This project, being a first-time coder, I learnt my way around data structures and in some way, working with requirement specifications. The bottom line is not everything works!



0 comments:

Post a Comment




 
Loading...