1 00:00:00,000 --> 00:00:09,410 2 00:00:09,410 --> 00:00:11,002 >> ZAMYLA CHAN: Let's have fun with 15. 3 00:00:11,002 --> 00:00:13,960 15 is the first game that you get to implement. 4 00:00:13,960 --> 00:00:15,350 And it's interactive. 5 00:00:15,350 --> 00:00:16,079 Now, not to worry. 6 00:00:16,079 --> 00:00:18,490 You don't have to write the whole thing yourself. 7 00:00:18,490 --> 00:00:19,650 >> Look at the distribution code. 8 00:00:19,650 --> 00:00:23,560 Because a lot of the game structure is already set up for you. 9 00:00:23,560 --> 00:00:27,820 It accepts and parses a command line argument from the user, and creates a 10 00:00:27,820 --> 00:00:30,900 board based on that input. 11 00:00:30,900 --> 00:00:35,240 >> It checks if the game is won, and exits once the user's won the game. 12 00:00:35,240 --> 00:00:38,010 And to win the game, it gets input from the user and 13 00:00:38,010 --> 00:00:40,260 calls the move function. 14 00:00:40,260 --> 00:00:44,270 So we're going to be implementing four functions for the game of 15-- 15 00:00:44,270 --> 00:00:47,780 init, draw, move, and won. 16 00:00:47,780 --> 00:00:50,000 >> First, let's tackle init. 17 00:00:50,000 --> 00:00:54,820 In init, for initialize, we represent the board in a 2D integer array. 18 00:00:54,820 --> 00:01:00,840 And this is a global variable called board with dimensions max and max, the 19 00:01:00,840 --> 00:01:03,550 maximum dimensions of the board. 20 00:01:03,550 --> 00:01:06,970 Now, the actual dimension of the board is given by the user, represented in 21 00:01:06,970 --> 00:01:10,300 the integer d, which could be less than max. 22 00:01:10,300 --> 00:01:13,030 But in C, you can't resize arrays. 23 00:01:13,030 --> 00:01:16,400 So you're stuck with that maximum dimension. 24 00:01:16,400 --> 00:01:19,510 >> Your job in init is to populate the values of the board 25 00:01:19,510 --> 00:01:21,540 with the correct values. 26 00:01:21,540 --> 00:01:23,600 Now, we've seen 1D arrays. 27 00:01:23,600 --> 00:01:26,570 But how do 2D arrays work? 28 00:01:26,570 --> 00:01:29,820 There's an index of the row, 0 index, as always, and 29 00:01:29,820 --> 00:01:31,770 then also of the column. 30 00:01:31,770 --> 00:01:35,790 >> And you'll fill your grid in in descending values, just like this. 31 00:01:35,790 --> 00:01:41,330 Grid 0, 0, row 0, column 0, is 8. 32 00:01:41,330 --> 00:01:42,930 Grid 0, 1 is 7. 33 00:01:42,930 --> 00:01:48,160 This is for an example where d, little d, is 3. 34 00:01:48,160 --> 00:01:52,630 Now, the board in 15 must also contain a blank tile, if you've ever played 35 00:01:52,630 --> 00:01:53,940 with the physical game. 36 00:01:53,940 --> 00:01:56,350 >> But board is an integer array. 37 00:01:56,350 --> 00:01:58,880 So all values have to be integers. 38 00:01:58,880 --> 00:02:06,580 So it's up to you to decide an integer value to represent a blank tile. 39 00:02:06,580 --> 00:02:10,250 >> To initialize your board, you can use loop structures to contain the 40 00:02:10,250 --> 00:02:16,100 starting state of the board, where board i, j represents the element at 41 00:02:16,100 --> 00:02:19,050 row i and column j. 42 00:02:19,050 --> 00:02:20,710 They start in descending order. 43 00:02:20,710 --> 00:02:24,200 And remember that if the number of tiles is odd, then you're going to 44 00:02:24,200 --> 00:02:29,260 have to swap the location of 2 and of 1. 45 00:02:29,260 --> 00:02:31,420 So there we have our initialized board. 46 00:02:31,420 --> 00:02:32,683