Assignment - 2048

Due Date: Tuesday, August 5th, 23:59:59

Worth: 30 Marks

Introduction

In this assignment you will write a C program 2048.c to implement the recently developed game 2048

2048 has been most popular as Javascript web application.

You will write a command line program which display the game in ASCII.

Reference Implementation

This assignment is largely specified by a reference implementation available on CSE machines.

/home/rz9280/bin/2048
Enter 16 numbers making up initial board:
0 0 0 0
0 0 2 0
8 0 2 4
0 0 2 4
Repeat game by running: /home/rz9280/bin/2048 1399537570
Enter h for left, j for down, k for up, l for right, q to quit
+------------------------+
|    .     .     .     . |
|                        |
|    .     .     2     . |
|                        |
|    8     .     2     4 |
|                        |
|    .     .     2     4 |
+------------------------+
Your score is 0.
> j
+------------------------+
|    .     2     .     . |
|                        |
|    .     .     .     . |
|                        |
|    .     .     2     . |
|                        |
|    8     .     4     8 |
+------------------------+
Your score is 12.
>

You must use the reference implementation to understand what is required for this assignment.

An important part of the assignment is analysing the requirements by running the reference implementation and exploring its behaviour.

You must use the reference implementation to understand what is required for this assignment. It is important that you match the output from this reference implementation exactly (character-for-character). If you believe the reference implementation behaves inappropriately raise this with Angela via the course message board.

Starting Point Code

The file 2048.c contains starting point code which you must use for this assignment.

You cannot change some of the code in 2048.c.

Do not change the code in the functions main, insertNewNumber and printHelp

Note insertNewNumber depends on a vacant square containing the value 0 so you must use this representation.

There are 8 functions moveLeft, moveRight, moveDown, moveUp, gameOver, boardContains2048, printBoard, readBoard which you must implement.

Do not change the type of these functions - just change the function body. You can change the code provided for the function bodies - this code (return statements) is provided to allow the starting point code compiles.

You may (should) add your own functions to 2048.c.

Your code must work if the SIZE constant changed. So for example if it it is changed to 10. You code should work with a 10x10 board. You can assume b>SIZE >= 2.

Hints

Implement printBoard & readBoard functions first.

The boardContains2048 function can be implemented simply.

Get one of the move functions e.g. moveLeft working before attempting the other move functions..

You can use dryrun to test your program like this:

/home/rz9280/bin/dryrun 2048

Dryrun can produce a very large amount of output for this assignment So you may need to pipe it to more which will let you scroll through it:

/home/rz9280/bin/dryrun 2048 | more

Or you may need to use dryrun's optional second argument to specify that only a single test should be run:

/home/rz9280/bin/dryrun 2048 0

The first 2 dryrun tests check readBoard/printBoard so run them ASAP.

The code you've been supplied pseudo-randomly places new numbers on the board. By default this will occur differently for every or this execution of the program. This can confuse debugging & testing

If you specify an integer command-line argument - this will be used used to seed (initialize) the pseudo-random number generator. This allows executions of the program to be made repeatable

Given the same starting position and the same moves, if the same seed is specified on the command line then the new numbers will be placed in the same squares.

The dryrun tests specify an integer command-line argument for this reason.

Single Character I/O

It would be more enjoyable if a move could be made with a single key press. This doesn't occur by default because operating systems such a Linux by default accumate characters until they have an entire line before passing them to your program.

You can request the operating system send individual characters immediately to your program (unbuffered I/O). This has not been incorporated in the code you have been given because it would require including compiler and operating system dependant code which might intefere with students developing on their own computers.

On Linux it is also possible to request unbuffered I/O from the operating system by running a separate program stty, before you run 2048. For example:

stty -icanon
/home/rz9280/bin/2048
....
stty icanon
Note this produces more enjoyable game-play on Linux but has absolutely no effect on the code you must write for the assignment!

Diary

You must keep notes of each piece of you make work on this assignment. The notes should include date, starting&finishing time, and a brief description of the work carried out. This diary should be submitted as text (ASCII) file named diary.txt For example:

DateStartStopActivityComments
29/0416:0017:30codingimplemented exit
30/0420:0010:30debuggingfound bug in for loops

Assessment

This assignment will contribute 30% to your final mark.

The work you submit must be your own work. Submission of work partially or completely derived from any other person or jointly written with any other person is not permitted. The penalties for such an offence may include negative marks, automatic failure of the course and possibly other academic discipline. Assignment submissions will be examined both automatically and manually for such submissions.

Do not provide or show your assignment work to any other person, apart from the teaching staff of REGZ9280. If you knowingly provide or show your assignment work to another person for any reason, and work derived from it is submitted you may be penalized, even if the work was submitted without your knowledge or consent. This may apply even if your work is submitted by a third party unknown to you.

Note, you will not be penalized if your work is taken without your consent or knowledge.

Submission

Submit the assignment using this give command:

regz9280 classrun 14s2 give assn 2048.c diary.txt

If your assignment is submitted after this date, each day it is late reduces the maximum mark it can achieve by 1 mark per day for the first day and 2 marks per day after that. No submissions will be accepted after Friday 9th August 23:59:59.