Great Deal! Get Instant $10 FREE in Account on First Order + 10% Cashback on Every Order Order Now

03/08/2020 COMP1911 - Introduction to Programming - 20T2 1/6 COMP1911 20T2 (

1 answer below »
03/08/2020 COMP1911 - Introduction to Programming - 20T2
https: 1/6
COMP1911 20T2 (https:
Introduction to Programming (https:
COMP1911 - Introduction to
Programming - 20T2
Assignment 2 - 0x800
Change log
16/07: Co
ected due date to Sunday from Friday
28/07: Explanation of CRITICAL code change to make in your 0x800.c. Search "If you setup your file prior to 2am" to
find the relevant section
30/07: If you are having issues where the terminal becomes unusable after your game crashes, you can uncomment the
line in 0x800.c "setAntiCrashMode(1)"
In 2014 teenagers across Australia were captivated by a new game: 2048 (https:
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, o
ight 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 (hex) . (i.e.NULL) 0x2 0x4 0x8 0x10 0x20 0x40 0x80 0x100 0x200 0x400 0x800
Power (hex) (i.e. x in 2^x XXXXXXXXXX9 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 use
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 cu
ent 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:
03/08/2020 COMP1911 - Introduction to Programming - 20T2
https: 2/6
1911 ass2reload
Running and editing the code
This download will include a number of files:
Do not edit this file. Do not remove this file. Do not wo
y about this file.
Do not edit this file. Do not remove this file. Do not wo
y about this file.
This is pre-compiled completed game you can play with. See below.
This is pre-compiled completed game you can test with. See below.
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.
If you setup your file prior to 2am on the 28th of July, then please edit your 0x800.c file and replace the following code:
if (argc == 2) {
ret = atoi(argv[1]);
eturn ret;
with this:
if (argc == 3) {
ret = atoi(argv[2]);
eturn ret;
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 co
esponds 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),
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 othe
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.
03/08/2020 COMP1911 - Introduction to Programming - 20T2
https: 3/6
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
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 a
ay of "cells", where each cell is an a
ay of characters (which we more often refer to as a
Therefore our board is a 2D a
ay 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
[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
[3][2] = "0x40";
The tasks
Part 0 - Creating and freeing a board
complete the newBoard() function in 0x800.c
This function malloc's a 2D a
ay 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
ight length (SIZE*SIZE) it populates the cells. Note: All values of the populate string passed into your program will
e 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
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 e
or message:
03/08/2020 COMP1911 - Introduction to Programming - 20T2
https: 4/6
printf("Please enter a valid populate string\n");
Each of the (SIZE*SIZE) digits passed in co
espond to the "Power (hex)" values in the table at the top of this
page. You should insert a string of the co
esponding "Value (hex)" into your board.
E.G. String " XXXXXXXXXX" 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 a
ay of character a
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
eak 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 togethe
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:
03/08/2020 COMP1911 - Introduction to Programming - 20T2
https: 5/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.
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.
Answered Same Day Aug 03, 2021


Arun Shankar answered on Aug 10 2021
135 Votes

This is your standard includes for external modules
#include #include #include This includes a special file used for this assignment
that was made for COMP1911
#include "0x800.h"
int main(int argc, char *argv[]) {
char c;
int success;
int seed = getSeed(argc, argv);
int finishedGame = FALSE;
Board board = newBoard();

Do not move, replace, rename, or delete this function.

It ca
ies out some necessary magic - be nice to it!

ignoreThisFunction1(board, seed, SIZE, DEBUG, TEXT_ONLY);

The function to populate the board from the command line

argument is called. If there is no argument in command line

then two random
int boardWasPopulated = populateBoard(board, argc, argv);
if (!boardWasPopulated) {

Continually executing the game loop until the game is finished
while (!finishedGame) {
c = getcharX();
success = FALSE;

Check what kind of key press was made using getcha
if (c == KEYBOARD_LEFT) {
success = moveLeft(board);
} else if (c == KEYBOARD_RIGHT) {
success = moveRight(board);
} else if (c == KEYBOARD_UP) {
success = moveUp(board);
} else if (c == KEYBOARD_DOWN) {
success = moveDown(board);
} else if (c == 'q') {
QUIT? (y/n)");
c = getcharX();
if (c == 'y') {
finishedGame = TRUE;
} else {

If a move key was pressed and the board was actually moved,

then draw the board again, add a new random ell, draw the

board again, and check if the game is finished
if (success) {

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here