COMP1911 21T2 COMP1911 - Introduction to Programming - 21T2 Introduction to Programming

Background

In 2014 teenagers across Australia were captivated by a new game: 2048.

The game consists of a 4 x 4 grid consisting of cells that have base 2 values up to 2048. Users can slide cells up, down, left, or right to move all cells in that direction. If adjacent cells (in the direction of travel) are equal then they merge and form a cell that has value twice as big as either cell (i.e. a base 2 step higher).

Every move a random cell appears in an empty cell.

If all of the cells are filled up in such a way that none can be merged, the game is complete

Introduction to 0x800

For assignment 2 students are required to write C code to create their own 2048 game.

Because this is computer science, and we want to flex a little, this implementation of the game will use strings of hexadecimal values instead of decimal values.

This means that the progression of cells will also be strings of hexadecimal values.

Value (decimal) N/A 2 4 8 16 32 64 128 256 512 1024 2048
Value (hex) . (i.e.NULL) 0x2 0x4 0x8 0x10 0x20 0x40 0x80 0x100 0x200 0x400 0x800
Power (hex) (i.e. x in 2^x) 0 1 2 3 4 5 6 7 8 9 A B

One key difference is that in our implementation, the game stops when a user creates a "0x800" cell (equivalent to 2048) and they cannot continue playing.

Part of the game is already implemented for you, including:

Your job is to implement more features of the game to complete it.

Getting Started

Create a new directory for this assignment by typing:

mkdir ass2

Change to this directory by typing:

cd ass2

Once in that directory run the following commands to get all of the starting code

1911 ass2setup

This will download ALL necessary files into your current directory (including the starting point for 0x800.c).

If you want to redownload all files except 0x800.c (e.g. reference solutions, scripts) but NOT download 0x800.c, you can use the command:

1911 ass2reload

Running and editing the code

This download will include a number of files:

gameSystem.o

Do not edit this file. Do not remove this file. Do not worry about this file.

LICENSE

Do not edit this file. Do not remove this file. Do not worry about this file.

0x800_reference

This is pre-compiled completed game you can play with. See below.

0x800_reference_text

This is pre-compiled completed game you can test with. See below.

0x800.c

This file is where the majority of the code you'll be writing will end up.

It's a normal C file where a lot of the code has been written, except there are some functions that need to be implemented by you for the program to function properly.

0x800.h

This is a file that contains some key typedefs and #defines that can be used in your .c file.

You can edit a few things in this file to alter your interaction with the game. These include:

Running the code

To run the code you must first compile it

$ dcc 0x800.c gameSystem.o -o 0x800

Then you can run the compiled program

$ ./0x800

Note: gameSystem.o is some special code that you compile with the code we give you that does all of the printing and other funky things behind the scenes. We put this in a separate file so that you're not overwhelmed by code in your primary .c file. If you try and compile it in a way that is not like we state above, you will have issues.

Reference solution

You can also play with the reference solution to see what intended behaviour is like.

Simply run:

./0x800_reference

Reference solution (text-only)

If you want a reference solution that you can generate test output from, you can use our text-based reference solution. This solution doens't print a coloured interface. The majority of students should just ignore this file - it's for the students who have the time and headspace to write their own tests.

Simply run:

./0x800_reference_text

Understanding the code

The "Board" type

The entire game centres around this idea of creating a "Board" and then passing that around between functions until it is free'd.

The type of a Board can be explained inside 0x800.h where there is the line of code

typedef char *cell;
typedef cell **Board;

This means that a board is a 2D array of "cells", where each cell is an array of characters (which we more often refer to as a string).

Therefore our board is a 2D array of "strings", where each string is a variation of "Value (hex)" found in the table at the top of the page.

If I wanted to set the cell at index [1][1] to be blank given a valid board "b" I would write

b[1][1] = NULL;

If I wanted to set the cell at index [3][2] to be 0x40 given a valid board "b" I would write

b[3][2] = "0x40";

The tasks

Part 0 - Creating and freeing a board

Part 1 - Populating a board

Hint: For populateBoard() look at getSeed() for inspiration.

Part 2 - Sliding and merging cells

Four key functions need to be implemented.

All 4 of these functions have similar implementations. You may be able to generalise out aspects of their implementation into other helper functions. If a successful move is made (i.e. cells are changed during a move attempt) then the function returns TRUE. If no cells can be moved in the direction the function wants, it returns FALSE.

You can break this step into two tasks.

  1. Make them slide: Cells slide in the direction the function implies, such that they are now stacked up in that direction
  2. Make them merge: Adjacent cells during sliding are merged together

Free'ing a cell that was added by addRandom: If you're interested in free'ing all memory during merges, then here is the implementation of the addRandom function below. You can see that it mallocs a string of size 6:

int cell = rand() % 2;
char* c = malloc(sizeof(char) * 6);
c[0] = '0';
c[1] = 'x';
c[3] = '\0';
if (cell == 0) c[2] = '2';
else c[2] = '4';

Part 3 - Restarting a game

Modify the program such that typing "r" will prompt the user, then if they type "y" after it it restarts the game.

Hint: Just because this is part 3, it does not mean it's harder than part 2. It's actually quite easy.

Part 4 - Creating an "Undo" move

Modify the program such that typing "u" will undo the previous move and display the game state (i.e. board) that was immediately prior to the last keypress.

Hint: This is best implemented using a Stack of game states (defined as a struct of 2D arrays).

Hint: Once you have created the stack, there is actually fairly minimal code to write, however the code that is written requires some thought.

Hints

Important notes for 0x800.c

Assessment

This assignment will contribute 20% to your final mark.

The assessment for the assignment recognizes the difficulty of the task, the importance of style, and the importance of appropriate use of programming methods (e.g. using while loops instead of a dozen if statements).

10%Correctness - Part 0

Automarking completed for part 0

20%Correctness - Part 1

Automarking completed for part 1

30%Correctness - Part 2

Automarking completed for part 2

5%Correctness - Part 3

Automarking completed for part 3

10%Correctness - Part 4

Automarking completed for part 4

25%Style

Strict adherence to the style guide. Tutors will hand mark the readability of the C you have written. These marks will be awarded on the basis of clarity, commenting, elegance and style. This will include using suitable variable names, using consistent layout and indentation.

If you implement any functions in ways that show poor design (e.g. hard coding values instead of using loops), then this will greatly impact your style marks.

The lecturer may vary the assessment scheme after inspecting the assignment submissions but it will remain broadly similar to the description above.

Assignment Conditions

Joint work is not permitted on this assignment.

This is an individual assignment. The work you submit must be entirely your own work: submission of work even partly written by any other person is not permitted.

Do not request help from anyone other than the teaching staff of COMP1911 - for example, in the course forum, or in help sessions.

Do not post your assignment code to the course forum. The teaching staff can view code you have recently submitted with give, or recently autotested.

Assignment submissions are routinely examined both automatically and manually for work written by others.

Rationale: this assignment is designed to develop the individual skills needed to produce an entire working program. Using code written by, or taken from, other people will stop you learning these skills. Other CSE courses focus on skills needed for working in a team.

The use of code-synthesis tools, such as GitHub Copilot, is not permitted on this assignment.

Rationale: this assignment is designed to develop your understanding of basic concepts. Using synthesis tools will stop you learning these fundamental concepts, which will significantly impact your ability to complete future courses.

Sharing, publishing, or distributing your assignment work is not permitted.

Do not provide or show your assignment work to any other person, other than the teaching staff of COMP1911. For example, do not message your work to friends.

Do not publish your assignment code via the Internet. For example, do not place your assignment in a public GitHub repository.

Rationale: by publishing or sharing your work, you are facilitating other students using your work. If other students find your assignment work and submit part or all of it as their own work, you may become involved in an academic integrity investigation.

Sharing, publishing, or distributing your assignment work after the completion of COMP1911 is not permitted.

For example, do not place your assignment in a public GitHub repository after this offering of COMP1911 is over.

Rationale: COMP1911 may reuse assignment themes covering similar concepts and content. If students in future terms find your assignment work and submit part or all of it as their own work, you may become involved in an academic integrity investigation.

Violation of any of the above conditions may result in an academic integrity investigation, with possible penalties up to and including a mark of 0 in COMP1911, and exclusion from future studies at UNSW. For more information, read the UNSW Student Code, or contact the course account.

Originality of Work

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.

Relevant scholarship authorities will be informed if students holding scholarships are involved in an incident of plagiarism or other misconduct.

Do not provide or show your assignment work to any other person - apart from the teaching staff of COMP1911. 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 has the potential to be taken without your consent or knowledge.

Submission

This assignment is due Sunday 8th August 19:59:59 Submit the assignment using this give command:

give cs1911 ass2 0x800.c 0x800.h
To just run the automarking tests without submission run
1911 autotest ass2

Late Submission Policy

If your assignment is submitted after this date, each hour it is late reduces the maximum mark it can achieve by 1%. For example if an assignment worth 76% was submitted 5 hours late, the late submission would have no effect. If the same assignment was submitted 30 hours late it would be awarded 70%, the maximum mark it can achieve at that time.