COMP1911 - Introduction to Programming - 21T2


Assignment 2 - 0x800

Background

In 2014 teenagers across Australia were captivated by a new game: 2048 (https://play2048.co/).

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:

● The printing of the game board

Determining when the game is complete

Reading keyboard keys from the user

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


Getting Started

Create a new directory for this assignment by typing:

Change to this directory by typing:

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

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:


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:

● DEBUG: If DEBUG is 0, the game appears normally (each print overwrites the previous, making it seem like an interactive game). If DEBUG is 1 then each time the game prints it will print out on a new line. This will help you debug the program when you want to see more information about what steps the program is making.

TEXT_ONLY: If TEXT_ONLY is 0, then the game appears normally (as a coloured board). However, if TEXT_ONLY is 1 then the board is printed as a SIZE * SIZE string where each digit corresponds to the base 2 exponent of a particular cell, where the cells match [ (0,0), (0,1), (0,2), (0,3), (1,0), (1,1), (1,2), (1,3), (2,0), (2,1), (2,2), (2,3), (3,0), (3,1), (3,2), (3,3)]


Running the code

To run the code you must first compile it

Then you can run the compiled program

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:


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:


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

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

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


The tasks

Part 0 - Creating and freeing a board

complete the newBoard() function in 0x800.c

○ This function malloc's a 2D array for the board and initialises all cells to NULL

complete the freeBoard() function in 0x800.c

This function free's all the memory associated with a board that was created in newBoard.

Part 1 - Populating a board

complete the populateBoard() function in 0x800.c

This function takes in a board, as well as argc/argv values, and if a 1st command line argument is present of the right length (SIZE*SIZE) it populates the cells. Note: All values of the populate string passed into your program will be in the set {0,1,2,3,4,5,6,7,8,9,A,B}

There are three cases of what can occur:

■ If the user enters a 1st command line argument and it's of valid length, the board is populated and the function returns TRUE.

If the user does not enter a 1st command line argument, the function returns false and no board needs to be populated

If the user enters this 1st command line argument, but it is not valid (i.e. not SIZE*SIZE in length) then the program shall quit with an error message:

Each of the (SIZE*SIZE) digits passed in correspond to the "Power (hex)" values in the table at the top of this page. You should insert a string of the corresponding "Value (hex)" into your board.

E.G. String "2040000000000004" would insert [ "0x4", NULL, "0x10", NULL ] - into your board's first row, NULL in all of the cells in row 2 and 3, and [ NULL, NULL, NULL, "0x10" ] into the fourth row.

Which would produce the following output when ran:

This is how it would look inside your "Board" if you were to represent it visually as a 2D array of character arrays

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

Part 2 - Sliding and merging cells

Four key functions need to be implemented.

int moveUp()

int moveLeft()

int moveRight()

int moveDown()

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:

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.

If the game was given a populate string (i.e. argv[1]) then it should reset the game to the original populate board state.

If there was no populate string (i.e. argv[1]) given (i.e. Game run simply with "./0x800") then you can simply call newBoard again to create a newBoard and call addRandom twice to add two new cells to the board in whatever locations addRandom puts them.

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

Don't be afraid if you can't attempt Part 4. It's meant as a bit of a "bonus" part. Focus on getting the other parts right before attempting part 4.


Important notes for 0x800.c

While you may add code to the main function, be careful. Ensure that the "ignoreThisFunction" function's remain in their same relative location in the file.

You can use printf in your program, however if you want to print something that will only print in the GUI but not the text-only compilation, use the provided printfGame function.

In populate board, or during the merge, when you create cells (i.e. add strings), you can either create them with malloc or you can just use string literals. String literals will be VERY easy to use, and get you started very quickly, but you will be unable to free any memory in the assignment because of this. So if you aren't feeling confident, start with string literals, but then once you want to get more marks move to a malloc'd solution


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 (mailto:[email protected]).


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:

To just run the automarking tests without submission run


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.