1 00:00:05,960 --> 00:00:09,710 All right, so let's begin creating our game. 2 00:00:09,710 --> 00:00:13,940 So I just want to start by cleaning up a little bit here. 3 00:00:15,210 --> 00:00:25,200 And now we can start off by creating our cell and we're going to want our cell to be exposed to 4 00:00:27,600 --> 00:00:28,950 the Java script. 5 00:00:29,520 --> 00:00:36,990 So let's go ahead and use ASM, Bind Gen to expose it. 6 00:00:39,060 --> 00:00:42,600 And we want it to be represented, represented by an unsigned. 7 00:00:44,130 --> 00:00:47,610 Eight bit integer and. 8 00:00:48,920 --> 00:00:54,410 And we're going to need to derive a couple of traits. 9 00:00:54,410 --> 00:00:54,830 And I. 10 00:00:58,090 --> 00:01:02,940 So the couple of traits that we're going to want as a couple, we're going to want many traits. 11 00:01:02,950 --> 00:01:05,590 We're going to want clone copy. 12 00:01:06,190 --> 00:01:13,540 Debug partial IQ for equality and IQ as well. 13 00:01:15,370 --> 00:01:18,460 And now we're going to create an enum called Cell. 14 00:01:19,420 --> 00:01:26,680 And then inside of here, we're going to have dead equals zero and then a live equals one. 15 00:01:28,900 --> 00:01:38,050 And so to expand on the repr unit, that's important because we want each cell to be represented as 16 00:01:38,050 --> 00:01:40,390 a single byte. 17 00:01:41,680 --> 00:01:49,330 And so the next thing we can define is our universe, which is also something that we're going to want 18 00:01:51,850 --> 00:01:57,130 accessible inside of the Java script. 19 00:01:57,130 --> 00:02:06,430 So we're going to say pub struct universe and then inside of here we're going to have a width of an 20 00:02:06,430 --> 00:02:13,090 unsigned 30 to a height, which is also unsigned 32. 21 00:02:13,090 --> 00:02:21,400 And then we want our cells to be in the universe as well, which we will represent as a vector of cells. 22 00:02:22,790 --> 00:02:27,840 And so now that we have our universe defined, we can start implementing some methods on it. 23 00:02:27,860 --> 00:02:35,390 The first method we want to implement is accessing the cell at a given row and column into an index 24 00:02:35,390 --> 00:02:37,970 into the cells vector. 25 00:02:38,780 --> 00:02:42,650 So we will implement some methods on our universe. 26 00:02:44,090 --> 00:02:51,380 And again, we want to be able to get an index into the vector. 27 00:02:51,380 --> 00:02:53,900 So we'll create a get index function. 28 00:02:54,530 --> 00:02:56,870 We're going to pass in self. 29 00:02:56,870 --> 00:03:02,090 And then we have our row and our. 30 00:03:03,360 --> 00:03:04,050 Call them. 31 00:03:07,080 --> 00:03:10,830 And we're going to return a few size. 32 00:03:12,580 --> 00:03:18,790 So the logic in here is going to be pretty straightforward. 33 00:03:18,880 --> 00:03:29,740 We're just going to return a the row times our self dot width plus the column and we want to return 34 00:03:29,770 --> 00:03:31,570 it as you size. 35 00:03:31,960 --> 00:03:37,450 And that's all we need to do in order to get our index. 36 00:03:39,440 --> 00:03:45,200 And now that we have the method implemented, we can calculate the next state of a cell. 37 00:03:45,440 --> 00:03:52,940 So we need to get a count of how many of its neighbors are alive and the method is going to use deltas 38 00:03:52,940 --> 00:03:58,820 and a modulo which is the. 39 00:03:59,950 --> 00:04:07,870 Percent sign, and we're using that to avoid a special casing the edges of the universe with if statements. 40 00:04:07,870 --> 00:04:10,030 So we're just trying to avoid having. 41 00:04:11,790 --> 00:04:16,830 Extra if statements when we can, we don't need to do them by using this logic. 42 00:04:18,180 --> 00:04:23,790 And so by applying a delta of minus one, we are actually adding. 43 00:04:25,070 --> 00:04:29,170 Minus one to the height and then we mod it. 44 00:04:29,180 --> 00:04:37,250 So this allows us to avoid unsigned integer under flow, meaning we went below zero and since its unsigned 45 00:04:37,250 --> 00:04:41,120 are unsigned our results would be end incorrect. 46 00:04:41,240 --> 00:04:42,590 So let's begin. 47 00:04:42,590 --> 00:04:45,230 Let's start implementing this and this will make a little bit more sense. 48 00:04:45,230 --> 00:04:51,830 So we're going to say our live neighbor count again, we're passing in our self, our row 49 00:04:54,200 --> 00:05:02,990 and our column and we're going to return back au8. 50 00:05:04,700 --> 00:05:08,810 So we'll say let mutable count equals zero. 51 00:05:08,810 --> 00:05:19,160 And then for delta rho in self dot height. 52 00:05:20,340 --> 00:05:21,570 Minus one 53 00:05:24,000 --> 00:05:25,530 zero for the row. 54 00:05:27,190 --> 00:05:33,190 One for the column and then we are going to iterate. 55 00:05:35,090 --> 00:05:35,720 Clone. 56 00:05:38,520 --> 00:05:41,520 And now we're going do the same thing for the row. 57 00:05:41,520 --> 00:05:50,970 So we're going to have Delta or excuse me, the column, Delta column in self dot width. 58 00:05:52,210 --> 00:06:01,570 Minus one 01. eter dot cloned. 59 00:06:03,220 --> 00:06:16,780 And then if Delta Rho is equal to zero and Delta column is also equal to zero. 60 00:06:17,800 --> 00:06:19,390 Then continue. 61 00:06:21,030 --> 00:06:28,740 So now down here we can say let neighbor row is equal to the row plus. 62 00:06:30,200 --> 00:06:31,340 Delta Rho. 63 00:06:33,200 --> 00:06:36,260 Mod self dot height. 64 00:06:37,960 --> 00:06:52,450 And let neighbor call equals column plus delta call mod self dot width. 65 00:06:56,330 --> 00:07:11,180 And then ah we'll just go for index self dot get index of our neighbor row and then for our column, 66 00:07:13,370 --> 00:07:14,330 neighbor column. 67 00:07:17,150 --> 00:07:24,170 And now we can add to count self dott sells. 68 00:07:24,750 --> 00:07:25,290 At. 69 00:07:26,370 --> 00:07:30,450 The index as a unsigned. 70 00:07:32,330 --> 00:07:37,520 And then all we want to do when this loop is done is return our count. 71 00:07:38,090 --> 00:07:48,020 So all we're doing is getting the count of all the cells located of all the neighbor cells located at 72 00:07:48,020 --> 00:07:53,810 that row and that column inside of our universe. 73 00:07:55,370 --> 00:08:00,980 And so now that we have that done, we have everything needed to compute the next generation. 74 00:08:01,160 --> 00:08:02,330 And we. 75 00:08:03,270 --> 00:08:10,110 We can have a straightforward implementation of the game's rules by using a match expression. 76 00:08:10,110 --> 00:08:15,200 And since we want JavaScript to control this portion, we are also going to play. 77 00:08:15,230 --> 00:08:18,510 We are going to place this inside a ASM. 78 00:08:20,290 --> 00:08:21,310 Engine block. 79 00:08:22,590 --> 00:08:26,730 So let's go ahead and add it. 80 00:08:26,730 --> 00:08:34,350 So we have Ys and Bingen and now we're going to pull Universe again because we want this to be exposed 81 00:08:34,350 --> 00:08:36,750 to the the JavaScript interface. 82 00:08:36,870 --> 00:08:41,760 And now inside of here we're going to create a function called tick. 83 00:08:42,090 --> 00:08:45,480 And basically a tick is a new generation. 84 00:08:47,340 --> 00:08:49,860 And so we're going to pass in our self. 85 00:08:50,460 --> 00:08:52,410 And now inside here let me. 86 00:08:52,800 --> 00:08:57,000 Next equals self cells. 87 00:08:58,410 --> 00:09:00,180 That clone. 88 00:09:02,720 --> 00:09:17,140 And now we can say for ro in zero to self dot height and for call and zero to self dot with. 89 00:09:19,550 --> 00:09:33,800 You can say let it x equals self dot get index of our row and r column and then let cell equals to self 90 00:09:33,800 --> 00:09:35,210 dot cells. 91 00:09:36,500 --> 00:09:37,760 I oop. 92 00:09:40,200 --> 00:09:41,760 Of our index. 93 00:09:43,760 --> 00:09:46,010 And then let live. 94 00:09:48,640 --> 00:09:54,100 Neighbors equals to self live neighbor count of our row. 95 00:09:57,140 --> 00:09:58,160 In our column. 96 00:10:00,340 --> 00:10:04,870 And now we can start implementing in the rules. 97 00:10:09,160 --> 00:10:17,590 So let next sell equals match on the cell and the live neighbors. 98 00:10:18,160 --> 00:10:35,830 So we have rule one, which is any live cell with fewer than two live neighbors dies because of under 99 00:10:37,000 --> 00:10:38,320 population. 100 00:10:39,220 --> 00:10:43,720 And so we can say sell alive. 101 00:10:46,790 --> 00:10:51,800 X if x is. 102 00:10:53,380 --> 00:10:58,510 Less than to then sell is. 103 00:11:02,300 --> 00:11:14,090 And now we have put another rule to any live cell. 104 00:11:15,350 --> 00:11:21,620 With two or three live neighbors lives on. 105 00:11:21,740 --> 00:11:23,880 And we'll just go ahead and write out the rest. 106 00:11:23,900 --> 00:11:37,940 Rule three Any live cell with more than three live name brs dies because of over population and then 107 00:11:37,940 --> 00:11:39,050 rule for. 108 00:11:40,970 --> 00:11:48,950 Any dead cell with exactly three live neighbors. 109 00:11:51,060 --> 00:12:00,390 Becomes a live sell due to re pro duction. 110 00:12:00,390 --> 00:12:03,210 So now let's implement rule two. 111 00:12:03,570 --> 00:12:07,560 So we have cell is alive. 112 00:12:10,150 --> 00:12:10,750 To. 113 00:12:11,560 --> 00:12:12,220 Or. 114 00:12:13,650 --> 00:12:16,740 Cell is alive. 115 00:12:19,830 --> 00:12:20,370 Three. 116 00:12:24,330 --> 00:12:26,670 So alive. 117 00:12:30,920 --> 00:12:33,200 So now we can implement rule three. 118 00:12:33,380 --> 00:12:37,100 So we will have a cell alive. 119 00:12:39,370 --> 00:12:41,410 If X. 120 00:12:43,090 --> 00:12:44,440 Greater than three. 121 00:12:45,250 --> 00:12:48,640 Then our cell is now dead. 122 00:12:52,580 --> 00:12:53,330 Excuse me. 123 00:12:53,960 --> 00:12:54,400 All right. 124 00:12:54,410 --> 00:12:55,850 We have a little air here. 125 00:12:55,880 --> 00:12:56,450 What do we. 126 00:13:01,420 --> 00:13:05,680 And now we can implement on rule four. 127 00:13:06,370 --> 00:13:09,190 So our dead cell. 128 00:13:11,880 --> 00:13:18,090 If has three neighbors now becomes in a life cell. 129 00:13:20,550 --> 00:13:24,990 And then otherwise, because remember, you have to handle all situations. 130 00:13:25,890 --> 00:13:33,030 Otherwise, all cells maintain themselves. 131 00:13:37,560 --> 00:13:44,850 Otherwise sell remains in the same state as before. 132 00:13:46,240 --> 00:13:57,040 So now we can end that block and then we can have our next iteration on the next index equals to our 133 00:13:57,040 --> 00:13:58,600 next cell. 134 00:13:59,800 --> 00:14:09,550 And we can say self dot cells equals to next. 135 00:14:12,070 --> 00:14:18,340 So currently we don't have any way to display the game currently. 136 00:14:18,340 --> 00:14:25,390 So we need to implement a method that will make this easy for us to see what is going on when we execute 137 00:14:25,390 --> 00:14:26,080 our program. 138 00:14:27,360 --> 00:14:33,750 We will use empty and full squares to signify if a cell is dead or alive. 139 00:14:34,020 --> 00:14:41,190 So just copy and paste the symbols from the code that I provide you. 140 00:14:44,190 --> 00:14:47,370 So we need to use standard format. 141 00:14:48,300 --> 00:14:56,910 And down here we're going to implement format display for our universe. 142 00:14:58,050 --> 00:15:07,290 And in here we're going to have a formatting function going to refer to self and then it's going to 143 00:15:07,560 --> 00:15:16,500 have a mutable formatter which is going to return a result for us. 144 00:15:18,910 --> 00:15:33,850 So in here we have for each line and the self dot cells as a slice dot chunks self dot width as you 145 00:15:33,850 --> 00:15:34,660 size. 146 00:15:37,440 --> 00:15:47,880 And then for a reference to our sell in line, we're going to say let symbol equals to if cell equals 147 00:15:49,170 --> 00:15:53,490 is equivalent to a dead cell. 148 00:15:55,200 --> 00:15:58,200 Then we want a. 149 00:16:00,770 --> 00:16:07,370 T and I'm just going to copy and paste this in for myself else. 150 00:16:11,310 --> 00:16:11,880 Here we go. 151 00:16:12,180 --> 00:16:12,930 Else? 152 00:16:13,200 --> 00:16:15,870 We want that guy. 153 00:16:16,290 --> 00:16:19,710 So let me format this real quick to make it pretty. 154 00:16:21,030 --> 00:16:22,500 All right, so let's symbol. 155 00:16:23,690 --> 00:16:24,470 Cool. 156 00:16:24,470 --> 00:16:29,060 If cell is equal to dead cell, then we want. 157 00:16:34,230 --> 00:16:36,790 If the cell is dead, we want an empty square. 158 00:16:36,810 --> 00:16:38,550 Else of the cell is alive. 159 00:16:38,610 --> 00:16:40,980 Then we want a full square. 160 00:16:41,010 --> 00:16:41,820 That is correct. 161 00:16:43,280 --> 00:16:47,480 So now we want to write F 162 00:16:49,970 --> 00:16:54,230 and then symbol propagate if there is an air. 163 00:16:55,790 --> 00:16:57,590 And now again. 164 00:16:57,800 --> 00:16:58,520 Right. 165 00:16:58,520 --> 00:17:03,980 But this time, we just want to write out a new line character. 166 00:17:07,930 --> 00:17:10,370 And return. 167 00:17:10,400 --> 00:17:13,570 Oh, okay. 168 00:17:14,140 --> 00:17:14,530 All right. 169 00:17:14,530 --> 00:17:18,430 So it doesn't look like we have any air, so everything seems very happy with that. 170 00:17:18,430 --> 00:17:23,200 So now we have a way to display out our universe. 171 00:17:23,200 --> 00:17:25,240 So I'm going to take a quick pause right here. 172 00:17:25,240 --> 00:17:32,620 That way, this lecture doesn't get too terribly long, and we will pick up in the next lecture.