1 00:00:00,000 --> 00:00:09,386 2 00:00:09,386 --> 00:00:11,332 >> ZAMYLA CHAN: Let's have fun with Fifteen. 3 00:00:11,332 --> 00:00:15,680 Fifteen is the first game that you get to implement and it's interactive. 4 00:00:15,680 --> 00:00:16,410 Now, not to worry. 5 00:00:16,410 --> 00:00:18,830 You don't have to write the whole thing yourself. 6 00:00:18,830 --> 00:00:22,320 Look at the distribution code because a lot of the game structure is already 7 00:00:22,320 --> 00:00:23,880 set up for you. 8 00:00:23,880 --> 00:00:28,160 It accepts and parses a command line argument from the user and creates a 9 00:00:28,160 --> 00:00:31,230 board based on that input. 10 00:00:31,230 --> 00:00:35,570 It checks if the game is won and exits once the user's won the game. 11 00:00:35,570 --> 00:00:38,340 And to win the game, it gets input from the user and 12 00:00:38,340 --> 00:00:40,610 calls the Move function. 13 00:00:40,610 --> 00:00:44,600 >> So we're going to be implementing four functions for the game of Fifteen, 14 00:00:44,600 --> 00:00:48,110 init, draw, move, and won. 15 00:00:48,110 --> 00:00:50,340 First, let's tackle init. 16 00:00:50,340 --> 00:00:55,150 In init, for initialize, we represent the board in a 2D integer array. 17 00:00:55,150 --> 00:01:01,070 And this is a global variable called board with dimensions MAX, and MAX, 18 00:01:01,070 --> 00:01:03,880 the maximum dimensions of the board. 19 00:01:03,880 --> 00:01:07,310 Now, the actual dimension of the board is given by the user, represented in 20 00:01:07,310 --> 00:01:10,620 the integer d, which could be less than MAX. 21 00:01:10,620 --> 00:01:14,660 But, in C, you can't resize arrays, so you're stuck with 22 00:01:14,660 --> 00:01:16,730 that maximum dimension. 23 00:01:16,730 --> 00:01:19,870 >> Your job in init is to populate the values of the board 24 00:01:19,870 --> 00:01:21,860 with the correct value. 25 00:01:21,860 --> 00:01:26,910 Now, we've seen 1D arrays, but how do 2D arrays work? 26 00:01:26,910 --> 00:01:30,985 There's an index of the row, zero indexed as always, and then also of 27 00:01:30,985 --> 00:01:32,100 the column. 28 00:01:32,100 --> 00:01:36,120 And you'll fill your grid in in descending values, just like this. 29 00:01:36,120 --> 00:01:43,260 Grid, 0, 0, row 0, column 0, is 8, grid 0, 1 is 7. 30 00:01:43,260 --> 00:01:48,500 This is for an example where d, little d, is 3. 31 00:01:48,500 --> 00:01:52,690 >> Now, the board in Fifteen must also contain a blank tile, if you've ever 32 00:01:52,690 --> 00:01:54,280 played with the physical game. 33 00:01:54,280 --> 00:01:59,210 But, board is an integer array, so all values have to be integers. 34 00:01:59,210 --> 00:02:06,950 So it's up to you to decide an integer value to represent a blank tile. 35 00:02:06,950 --> 00:02:10,460 To initialize your board, you can use loop structures to contain the 36 00:02:10,460 --> 00:02:16,440 starting state of the board, where board i j represents the element at 37 00:02:16,440 --> 00:02:19,380 row i and column j. 38 00:02:19,380 --> 00:02:23,035 They start in descending order and, remember, that if the number of tiles 39 00:02:23,035 --> 00:02:29,590 is odd, then you're going to have to swap the location of 2 and of 1. 40 00:02:29,590 --> 00:02:33,790 So there, we have our initialized board. 41 00:02:33,790 --> 00:02:37,440 >> Now, that we've initialized our board, it's time to draw it. 42 00:02:37,440 --> 00:02:41,260 Draw will print the current state of the board, but you need to make sure 43 00:02:41,260 --> 00:02:44,260 to print tiles in the same order that you've initialized them. 44 00:02:44,260 --> 00:02:47,300 And you also need to format your numbers correctly. 45 00:02:47,300 --> 00:02:51,700 Because we might have single digits and double digits, then you want to 46 00:02:51,700 --> 00:02:54,540 print a blank space before any single digit numbers. 47 00:02:54,540 --> 00:03:00,150 You use that by using the placeholder -. 48 00:03:00,150 --> 00:03:02,550 >> But remember our blank space. 49 00:03:02,550 --> 00:03:05,970 We don't want to print the actual number that we've chosen to represent 50 00:03:05,970 --> 00:03:10,410 that blank space in board, but we also don't want to print nothing at all. 51 00:03:10,410 --> 00:03:15,310 So what you can do is to define a symbol or character to represent the 52 00:03:15,310 --> 00:03:17,050 blank tile. 53 00:03:17,050 --> 00:03:21,030 In previous examples I've chosen an underscore, and then you just print 54 00:03:21,030 --> 00:03:26,970 that whenever you reach the blank space in your draw function. 55 00:03:26,970 --> 00:03:29,850 >> So draw will have nested for loops. 56 00:03:29,850 --> 00:03:31,150 Something like this. 57 00:03:31,150 --> 00:03:35,660 For each row, and then for each value in the row, you're going to print the 58 00:03:35,660 --> 00:03:36,940 value in this space. 59 00:03:36,940 --> 00:03:39,470 Once you've printed all the values in the row, then you 60 00:03:39,470 --> 00:03:41,180 can print a new line. 61 00:03:41,180 --> 00:03:47,730 Remember that the order for your draw function must echo or mirror the order 62 00:03:47,730 --> 00:03:48,980 in your initialized function. 63 00:03:48,980 --> 00:03:51,290 64 00:03:51,290 --> 00:03:55,160 >> Now that you initialized the board and that you've drawn it, it's time to let 65 00:03:55,160 --> 00:03:58,500 the user edit it and make their moves. 66 00:03:58,500 --> 00:04:03,840 So in the Fifteen.c function, the program takes input from the user and 67 00:04:03,840 --> 00:04:07,690 then calls the move function, passing in the number of the tile that the 68 00:04:07,690 --> 00:04:09,270 user wants to move. 69 00:04:09,270 --> 00:04:10,380 Now, be careful. 70 00:04:10,380 --> 00:04:14,200 This is the actual number of the tile and not its actual position. 71 00:04:14,200 --> 00:04:19,010 So, you'll need to search for the tile's position to know where it is. 72 00:04:19,010 --> 00:04:23,440 >> Now, you should only allow the user to make a move if it's legal. 73 00:04:23,440 --> 00:04:27,910 A legal move is any tile that is adjacent to the blank tile. 74 00:04:27,910 --> 00:04:32,020 That means, above and below, to the left and to the right. 75 00:04:32,020 --> 00:04:34,680 So you'll need to know where the blank tile is as well. 76 00:04:34,680 --> 00:04:39,720 Now, for every move you're searching for the users tile, but it's probably 77 00:04:39,720 --> 00:04:43,030 not best to search for the blank tile every time because you're doing it 78 00:04:43,030 --> 00:04:45,270 every single time the user wants to move. 79 00:04:45,270 --> 00:04:50,300 So, instead, it's best to remember where the blank tile is using some 80 00:04:50,300 --> 00:04:52,650 well named variables. 81 00:04:52,650 --> 00:04:55,970 So once you allow the user to make their moves, they are well on their 82 00:04:55,970 --> 00:04:59,700 way to winning the game of Fifteen. 83 00:04:59,700 --> 00:05:03,940 >> To win the game of Fifteen, the tiles have to be in a specific order, and 84 00:05:03,940 --> 00:05:06,970 the won function checks whether the game is won. 85 00:05:06,970 --> 00:05:10,290 It returns True if the game is won and the tiles are in the correct order, 86 00:05:10,290 --> 00:05:12,210 and False otherwise. 87 00:05:12,210 --> 00:05:15,830 So to win the game of Fifteen, tiles have to be increasing order, with the 88 00:05:15,830 --> 00:05:19,230 blank tile at the bottom right corner. 89 00:05:19,230 --> 00:05:23,630 So how do you check whether the user has moved the board into the right 90 00:05:23,630 --> 00:05:25,010 orientation? 91 00:05:25,010 --> 00:05:29,200 >> Well, you'll iterate over the board and check the values to make sure that 92 00:05:29,200 --> 00:05:30,550 they're in the right place. 93 00:05:30,550 --> 00:05:33,910 To do this, you can use nested for loops just like you did 94 00:05:33,910 --> 00:05:36,520 in draw and in init. 95 00:05:36,520 --> 00:05:40,430 There are a couple of ways to check and validate whether the board is 96 00:05:40,430 --> 00:05:42,860 correct and a winning formation, though. 97 00:05:42,860 --> 00:05:47,330 If you go from left to right, starting from the top row down, then every 98 00:05:47,330 --> 00:05:50,590 number must be greater than the previous one. 99 00:05:50,590 --> 00:05:54,530 Be careful about what value you've chosen for your blank tile though. 100 00:05:54,530 --> 00:05:59,250 >> Or you could use a counter variable to ensure that each value is in place, if 101 00:05:59,250 --> 00:06:03,660 you come up with some sort of formula to represent this. 102 00:06:03,660 --> 00:06:06,250 So have fun experimenting with the math. 103 00:06:06,250 --> 00:06:10,930 Once you've come up with a way, return True once the user has won the game. 104 00:06:10,930 --> 00:06:15,950 But if any value is incorrect, return False, the user has to continue moving 105 00:06:15,950 --> 00:06:18,440 because they haven't won the game. 106 00:06:18,440 --> 00:06:23,030 Once you implement this check and, along with initialize, draw, and move, 107 00:06:23,030 --> 00:06:25,110 you've finished the game of Fifteen. 108 00:06:25,110 --> 00:06:27,620 Congratulations and have fun playing. 109 00:06:27,620 --> 00:06:30,600 My name is Zamyla and this is CS50. 110 00:06:30,600 --> 00:06:37,632