>> ZAMYLA CHAN: Let's have fun with Fifteen.
Fifteen is the first game that you get to implement and it's interactive.
Now, not to worry.
You don't have to write the whole thing yourself.
Look at the distribution code because a lot of the game structure is already
set up for you.
It accepts and parses a command line argument from the user and creates a
board based on that input.
It checks if the game is won and exits once the user's won the game.
And to win the game, it gets input from the user and
calls the Move function.
>> So we're going to be implementing four functions for the game of Fifteen,
init, draw, move, and won.
First, let's tackle init.
In init, for initialize, we represent the board in a 2D integer array.
And this is a global variable called board with dimensions MAX, and MAX,
the maximum dimensions of the board.
Now, the actual dimension of the board is given by the user, represented in
the integer d, which could be less than MAX.
But, in C, you can't resize arrays, so you're stuck with
that maximum dimension.
>> Your job in init is to populate the values of the board
with the correct value.
Now, we've seen 1D arrays, but how do 2D arrays work?
There's an index of the row, zero indexed as always, and then also of
the column.
And you'll fill your grid in in descending values, just like this.
Grid, 0, 0, row 0, column 0, is 8, grid 0, 1 is 7.
This is for an example where d, little d, is 3.
>> Now, the board in Fifteen must also contain a blank tile, if you've ever
played with the physical game.
But, board is an integer array, so all values have to be integers.
So it's up to you to decide an integer value to represent a blank tile.
To initialize your board, you can use loop structures to contain the
starting state of the board, where board i j represents the element at
row i and column j.
They start in descending order and, remember, that if the number of tiles
is odd, then you're going to have to swap the location of 2 and of 1.
So there, we have our initialized board.
>> Now, that we've initialized our board, it's time to draw it.
Draw will print the current state of the board, but you need to make sure
to print tiles in the same order that you've initialized them.
And you also need to format your numbers correctly.
Because we might have single digits and double digits, then you want to
print a blank space before any single digit numbers.
You use that by using the placeholder -.
>> But remember our blank space.
We don't want to print the actual number that we've chosen to represent
that blank space in board, but we also don't want to print nothing at all.
So what you can do is to define a symbol or character to represent the
blank tile.
In previous examples I've chosen an underscore, and then you just print
that whenever you reach the blank space in your draw function.
>> So draw will have nested for loops.
Something like this.
For each row, and then for each value in the row, you're going to print the
value in this space.
Once you've printed all the values in the row, then you
can print a new line.
Remember that the order for your draw function must echo or mirror the order
in your initialized function.
>> Now that you initialized the board and that you've drawn it, it's time to let
the user edit it and make their moves.
So in the Fifteen.c function, the program takes input from the user and
then calls the move function, passing in the number of the tile that the
user wants to move.
Now, be careful.
This is the actual number of the tile and not its actual position.
So, you'll need to search for the tile's position to know where it is.
>> Now, you should only allow the user to make a move if it's legal.
A legal move is any tile that is adjacent to the blank tile.
That means, above and below, to the left and to the right.
So you'll need to know where the blank tile is as well.
Now, for every move you're searching for the users tile, but it's probably
not best to search for the blank tile every time because you're doing it
every single time the user wants to move.
So, instead, it's best to remember where the blank tile is using some
well named variables.
So once you allow the user to make their moves, they are well on their
way to winning the game of Fifteen.
>> To win the game of Fifteen, the tiles have to be in a specific order, and
the won function checks whether the game is won.
It returns True if the game is won and the tiles are in the correct order,
and False otherwise.
So to win the game of Fifteen, tiles have to be increasing order, with the
blank tile at the bottom right corner.
So how do you check whether the user has moved the board into the right
orientation?
>> Well, you'll iterate over the board and check the values to make sure that
they're in the right place.
To do this, you can use nested for loops just like you did
in draw and in init.
There are a couple of ways to check and validate whether the board is
correct and a winning formation, though.
If you go from left to right, starting from the top row down, then every
number must be greater than the previous one.
Be careful about what value you've chosen for your blank tile though.
>> Or you could use a counter variable to ensure that each value is in place, if
you come up with some sort of formula to represent this.
So have fun experimenting with the math.
Once you've come up with a way, return True once the user has won the game.
But if any value is incorrect, return False, the user has to continue moving
because they haven't won the game.
Once you implement this check and, along with initialize, draw, and move,
you've finished the game of Fifteen.
Congratulations and have fun playing.
My name is Zamyla and this is CS50.