COMP1511 17s2
— Lecture 18 —
Shuffle!
Andrew Bennett
<andrew.bennett@unsw.edu.au>
assignment 2
lists of items
Andrew Bennett
<andrew.bennett@unsw.edu.au>
assignment 2
lists of items
hopefully you’ve had a good break
(and done lots of programming!)
we’re nearly there…
assignment 2 in progress
arrays/strings prac
marking in progress
testGame.c … testing the Game ADT
Game.c … implementing the Game ADT
player.c … using the Game ADT
testing each of the Game ADT interface functions
to make sure they’re doing what they should be doing.
how? “simulate” a game:
make moves, check game state is correct.
how?
you specify the deck,
you decide the moves,
so you know the resulting game state
first: design
what do you need to keep track of game state?
what will you put in your struct _game?
how will you store hands, deck, discard pile?
tip: sit down with your group and discuss this before coding
then: implement
function at a time…
add tests as you go!
write an AI to play the game…
more details coming soon
open for testGame.c and Game.c!
player.c coming later in week 12
submit as usual
give, through WebCMS3, etc.
submit early … submit often
submit a compiling stub version now
(more on this later)
Submissions Play Other Tests and Submissions
submit partial (but compiling) code
submit often
Sundays, Wednesdays, Fridays
at 8:30pm
figuring out what’s wrong with your code
disputing incorrect tests
refuting claims that your test was wrong
Previously on COMP1511…
printf(“Hello, world!\n”);
conditionals (if/else), functions, loops
memory, arrays, strings, pointers
structs, typedef, dynamic memory allocation
testing, problem solving & abstraction, ADTs
separating the interface from the implementation
Last In, First Out (LIFO)
we can only
add, see, or remove
(“push”, “peek”, or “pop”)
the top-most element
arrays: a fixed size, contiguous block of memory
(a continuous series of boxes in memory)
each item has a known location –
it’s right after the previous item
int array[7] = { 3, 1, 4, 1, 5, 9, 2 };
gives us a sequence of elements in memory:
┄─┬────┬────┬────┬────┬────┬────┬────┬─┄ │ 3 │ 1 │ 4 │ 1 │ 5 │ 9 │ 2 │ ┄─┴────┴────┴────┴────┴────┴────┴────┴─┄
what if stacks aren’t of known-maximum length?
what if we need arrays of dynamic length?
in C, we have dynamic allocation…
but that’s still (effectively) fixed in length
we need a new way to represent
collections of things
… what if we had a way to store data
that increased by exactly the right amount
every time we add a value?
we could allocate memory for a new value as needed
int *a = calloc (1, sizeof (int));
*a = 3;
int *b = calloc (1, sizeof (int));
*b = 1;
int *c = calloc (1, sizeof (int));
*c = 4;
// ... and so on
we’d have to hold on to all those pointers…
and we don’t have a nice way to do that.
┄┬────┬┄ ┄┬────┬┄ ┄┬────┬┄ ┄┬────┬┄ │ 3 │ │ 9 │ │ 5 │ │ 2 │ ┄┴────┴┄ ┄┴────┴┄ ┄┴────┴┄ ┄┴────┴┄ ┄┬────┬┄ ┄┬────┬┄ ┄┬────┬┄ │ 1 │ │ 4 │ │ 1 │ ┄┴────┴┄ ┄┴────┴┄ ┄┴────┴┄
… what if each item knows
where the next one is?
╭───╮ ╭───╮ ╭───╮ ╭───╮ │ 3 ├─╮ │ 5 ├───│ 9 ├───│ 2 ├╳ ╰───╯ │ ╰───╯ ╰───╯ ╰───╯ │ ╰───────────────╮ ╭───╮ ╭───╮ ╭───╮ │ │ 1 ├───│ 4 ├───│ 1 ├─╯ ╰───╯ ╰───╯ ╰───╯
a sequence of nodes,
each with a value and a next
╭───╮ │ 3 ├─* ╰───╯
typedef struct _node *Node;
typedef struct _list {
Node head;
} list;
typedef struct _node {
int value;
Node next;
} node;
╭───╮ ╭───╮ ╭───╮ │ 3 ├────│ 1 ├────│ 4 ├─╳ ╰───╯ ╰───╯ ╰───╯ a b c
node a = { 3 };
node b = { 1 };
node c = { 4 };
a.next = &b;
a.next->next = &c;
a.next->next->next = &d;
a player has a hand of cards
possible actions:
draw card (add card to hand)
play card (remove from middle?)
how could we implement this?
… an array of Cards?
remove from middle?
shuffle all array elements down…
add card to hand?
add onto the end?
… what if we run out of space?
Card hand[SOME_BIG_NUMBER];
int upto = 0;
Card first = getCard(???);
hand[upto] = first;
upto++;
Card second = getCard(???);
hand[upto] = second;
upto++;
… a list of Cards?
remove from middle?
easy! just remove a Card + adjust pointers
add card to hand?
easy! just add to the start/end of list
… no size limit!
typedef struct _node *Node;
typedef struct _node {
// int value;
Card card;
Node next;
} node;
…
Node hand;
Card first = getCard(???);
hand->card = first;
Card second = getCard(???);
hand->next = newNode();
hand->next->card = second;
void fillArray (int array[ARRAY_SIZE], int value) {
int i = 0;
while (i < ARRAY_SIZE) {
array[i] = value; // set the value
i++; // move to next element
}
}
void fillList (List list, int value) {
Node curr = list->head;
while (curr != NULL) {
curr->value = value; // set the value
curr = curr->next; // move to next node
}
}
insert into list
remove from list
create a new node
print out list
insert into list
remove from list
create a new node
print out list
insert into list
remove from list
create a new node
print out list
insert into list
remove from list
create a new node
print out list