They had the game pretty much coded except I had to come in and write 4 functions that would make the game playable.
Initialize the board
Draw the game board
Write a move function
Write a won function.
Initialize
void init(void)Comments
{
//total numbers needed
int total = d * d;
//draws the board top left to right and reduce by 1
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
board[i][j] = --total;
}
}
// If board has even columns swap 1 % 2
if ( d % 2 == 0)
{
board[d-1][d-3] = 1;
board[d-1][d-2] = 2;
}
}
This function creates an array of numbers, and swaps the last 2 numbers if their are even columns.
Draw
void draw(void)
{
for (int i = 0; i < d; i++)
{
for (int j = 0; j < d; j++)
{
//initalizes _ to value of 0. if value is 0 draw an underscore
if (board[i][j] == 0 )
{
printf(" _");
}
else
{
printf("%3i",board[i][j]);
}
}
printf("\n");
}
}
Comments
This function "draws" the board. i designates rows, j designates columns.
When the array reaches 0 it instead draws an underscore.
Move
bool move(int tile)
{
// TODO
for( int i = 0; i < d ; i++)
{
for (int j = 0; j < d; j++)
{
//searches for where user input is
if( board[i][j] == tile)
{
//checks north tile and swaps4
if((i - 1 >= 0) && (board[i - 1][j] == 0 )) //if value above =input
{
board[i-1][j] = board[i][j];
board[i][j] = 0;
return true;
}
//checks right of tile
else if((j + 1 < d) && (board[i][j + 1] == 0))
{
board[i][j+1] = board[i][j];
board[i][j] = 0;
return true;
}
//checks south of tile
else if ((i + 1 < d) && (board[i + 1][j] == 0 ))
{
board[i+1][j] = board[i][j];
board[i][j] = 0;
return true;
}
//checks left of tile
else if ((j - 1 >= 0) && (board[i][j - 1] == 0))
{
board[i][j-1] = board[i][j];
board[i][j] = 0;
return true;
}
}
}
}
return false;
}
Comments
This one took me some time to do. For the longest time I couldn't understand why my board was drawing an extra 0 on only some moves. Then I realized I wasn't error checking on if the move was in the confines of the virtual board.
Won Function
bool won(void)
{
int counter = 0;
//checks each tile to see if it's in order
for( int i = 0; i < d ; i++)
{
for (int j = 0 ; j < d ; j++)
{
//checks last spot and if not correct value
if (++counter != (d*d) && board[i][j] != counter)
{
return false;
}
}
}
return true;
}
Won Function Comments
This function checks to see if the board is in the correct order. If the board is in correct order it returns true, and stops the program.
PSET 3 Comments
It was overwhelming at first seeing all the lines of code already written. However this problem gave me a great view on how everything fits together. Try not to think of everything all at once and break the problem down...easier said than done.
Coding
I't is still not clicking for me on breaking everything down. I wouldn't be able to write this program from scratch. I feel like it should take me way less time to break everything down. All the advice I've gotten is to keep plugging away, eventually it will just snap in.
Apparently the difficulty for the next PSET is pretty tough. It starts introducing concepts about changing variables located in memory. PSET4 looks very exciting, being able to code my own file recovery software, and learn how data is stored in a jpeg, or bitmap file. However that may change in the new year when the course changes.
I've started with the videos learning the next bit of material because I'm sure most of it will still be relevant, however I am going to take a small break and wait until the course material is updated.
Progress
Their are 9 problem sets and a final project. I've completed 4 of the PSETS(0,1,2,3)
I started the last week in November so It's taken me 5 weeks to complete 4 psets with working 40 hours a week. I'm pretty proud of that so far. I am fairly certain that as more concepts are introduced It will take me more time to go through. More than anything though I'm hoping for the point where coding just clicks..
I am starting to loose a little bit of motivation. That is normal for me at this point. I always tend to get bored quick. This break will hopefully me a good refresh.