---
title: "Finding a Winner"
url: "https://courses.osherove.com/courses/tdd-pairing-sessions-building-a-go-game/finding-a-winner.md"
canonical: "https://courses.osherove.com/courses/tdd-pairing-sessions-building-a-go-game/finding-a-winner/"
tags: []
author: "Roy Osherove"
description: "Lesson 5 of 11 in TDD Pairing Sessions - Building a Go Game (The Sessions), 1:01:59. Free video by Roy Osherove."
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# Finding a Winner

1. [Courses](https://courses.osherove.com/#courses)
2. [TDD Pairing Sessions - Building a Go Game](https://courses.osherove.com/courses/tdd-pairing-sessions-building-a-go-game/)
3. The Sessions

Lesson 5 of 11

Duration: 1:01:59 [Watch on YouTube](https://www.youtube.com/watch?v=05IPTAYRrTQ)

## In this lesson

In session three the pair remove the hard-coded corner cell from the winner check, duplicating passing tests instead of changing them and changing one assumption per test. Debugging shows the board starts all black, so they first write a failing test where white should win by komi, then fix the enum and an off-by-one edge check. They loop over the board to find surrounded cells, add an edge constant and remove the assumption that black always wins.

## Transcript

7,316 words

[\[00:05\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=5s) Welcome back to session three. Raymond, how are you doing? >> I'm doing great. >> So last in the last session, we finished it up with a refactoring and that we we kind of liked but then we had to finish. So my question to you is, do you want to continue to do some more refactoring because I also got some comments that the code is still not readable enough? Or do you want to continue and do some more some more?

[\[00:37\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=37s) >> what did they what did they say was not readable enough? >> specifically, I think someone mentioned this flag right here. Uhhuh. >> What do you think? >> I think it's fine for now. >> Okay. >> I think we should continue. >> okay. And let's get rid of this toolbar right here again.

[\[01:09\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=69s) Is it the text editor? Yeah. >> Yes. >> I didn't know that [NUnit](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-2#ch02lev1sec3 "First steps with NUnit (The Art of Unit Testing, 2nd ed., ch. 2)") had a toolbar. No, I I made it myself. >> Oh, what happens when I click it? I just clicked. It >> launches this this one cuz I wasn't using Testr runner. >> Oh, how hacky of you. That's pretty cool. Let's see what happens. Nice. I like the green feeling there. >> Yeah. >> Okay. Okay. So, let's open up the Excel file and see where we're at and where we're trying to to go.

[\[01:43\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=103s) >> Okay. So, Oh, I did not open it. So, we were trying to we have not we were working on the territories and then we decided to do a refactoring and and I got a comment about the territories that it's actually called chains. >> No, they're actually talking about what I called groups. So, when I said surround group, >> yeah, >> they're actually talking about surround chain. >> Mhm. Okay. So, >> so

[\[02:13\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=133s) >> we were in the middle of something or were we just finishing? I think we're just finishing the winner or we just beginning the terminal winner with some features. >> So, we we were still determining winner. We were trying to get the black as a winner. So, the last test we had was this one. Determine winner for black while plays last. Yeah, determine the winner. At least one cell one for black and white plays last and black wins.

[\[02:44\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=164s) We made it pass because So, let's just make sure we we we said board make board and we added a stone right to to the right of the black territory and another one just below >> and then the white plays a stone that has nothing to do with that. >> Right. And the way we made it pass was we just checked that this position here was surrounded by black. So as you can tell from right here >> Yeah. is fully surrounded by.

[\[03:16\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=196s) >> Okay. So obviously this is only partially working, right? >> Yeah. >> Okay. So what scenario do we want to cover to make sure it's working better? >> how about surround a different territory? That way it's not only one one like maybe another corner. >> Okay. >> Or either that or we could make it something more in the middle not on a corner on the edge.

[\[03:48\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=228s) >> Sure. Okay. Why did you write that test? So I can I can exactly understand what you mean. >> Okay. Okay. So maybe we'll just I mean this would basically force us to >> remember you have the test >> so this would just force us to change our code to be more general. Right. >> So what assumption are we making right now that you're you're trying to remove >> this right here?

[\[04:18\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=258s) The winner is the one that surrounds one one right. So we just >> Oh yeah, the 11 one is hardcoded. Sure. >> Okay, look. >> So now just use tab. >> Oh, yeah. I'm not sure how to share. >> Well, we can say that it's a different it's it's basically the same one as before.

[\[04:50\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=290s) >> Yeah. >> But it's a different cell. Right. >> Right. All right. >> So we can say a cell not corner cell. >> Okay. >> Is one not corner cell one for black or you know what? One for white. Now let's see that the white can win. >> Okay. >> and same behavior just tab to white wins.

[\[05:26\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=326s) Okay, good. So, so we can either just copy the this part and make a territory for white somewhere else or

[\[06:01\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=361s) We just basically just have white by itself so that it won't it won't win by komi. >> whatever you whatever is the simplest and most [readable test](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-9 "Readability (The Art of Unit Testing, 3rd ed., ch. 9)"). I'm not sure if that's the simplest. >> It's the [simplest way](https://osherove.com/blog/2010/1/6/tdd-4-questions-that-will-help-you-create-the-simplest-thing.html "TDD: 4 questions that will help you create the simplest thing that could possibly work (osherove.com)") to prove our point. That's that's what I'm trying to say. Okay. >> Sure.

[\[06:50\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=410s) >> So why do you want a black territory at all? >> So that we can force because if there's no black territories then it basically win. Well, actually, no, you're right because the komi is only if there is if it's played, right? By the way, a way to delete the whole line is control shift L for line control. >> Yeah. >> Okay, that's cool.

[\[07:21\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=441s) >> so why don't you Yeah, let's see. Where are you getting the result from? You started from the assert and then you started again from the beginning of the test. I'm wondering why >> because every time we type board before it would always just give me the board object when I didn't define the board. >> Sure. >> But you're right. I should probably

[\[07:53\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=473s) And then I can define the winner. >> Okay. So, white territory this time should be surrounded in all parts, right? >> Right. Or at the edge. >> Well, let's do something a bit different. We don't want this. The test specifically says not corner cell. >> Okay. All right.

[\[08:40\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=520s) So line three, line four. We also need line four. Yeah, exactly. Two white. The white territory is in three. And then we need a four and four. All right. >> Oh, >> I think we we the way we wrote this is a little bit different from the way we were wrote in the other tests

[\[09:15\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=555s) Because this this was X. So we had X going this way and this was Y. Y was going this way. >> When you're saying this way, I can't see anything being selected. So I don't know what you mean this way. What does this >> So >> can you make a selection so I can see? >> When we did right here. >> Yeah. >> For X, we were going left to right. >> Sure. >> Was increasing left to right. And for Y, we were increasing top to bottom,

[\[09:46\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=586s) Which is different from the way we've been we wrote it yesterday. So why why why are you writing it differently? >> well I think yesterday you started it that way but I didn't correct you. So you started writing it 2 one and then one two. So it should have been one. >> Yeah because X and Y are the opposites. Yeah. >> Wait, but hold on. Are you are you changing an existing passing test right now?

[\[10:19\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=619s) >> Yeah. So, I should probably not do that, right? >> Well, yeah. If you think that this is this test is wrong, >> we should comment this one out first. >> Sure. >> It's an easy way to comment >> just control slash >> control which slash >> right slash forward slash. Yeah. >> All right.

[\[10:49\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=649s) >> so you think that test is wrong. So create the the the good version of it. >> Oh, first I need to make it run. How come I'm getting >> alt R? I'm pressing alt R. >> interesting. >> Okay, so let's make a good >> I think because there was nothing to repeat. So now if you press alt R there is will be a repetition. Alt R repeats a did you did you duplicate the test now?

[\[11:22\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=682s) >> No I did not. I just fixed it. >> You fixed it back to the way it was. Okay. >> but make sure that you run all the tests not just one. You I think you just clicked on a single test to run. >> No it ran 11. >> Oh okay. >> okay. So duplicate the test. Write it the way you think it should should be written. Hold on. Are you fixing the second test now? >> No, these are two different tests.

[\[11:53\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=713s) This one and this one. We copied it over here. >> Did you copy them? I didn't see that you were copying them. That's what >> Oh, I was not copying it. I was just switching the X and the Y. >> But they're passing to me. That feels wrong. >> Well, I just make it so it's more consistent with the other tests. or or we can just we can fix it. Such as that it makes sense. >> I'm trying to understand why it feels wrong to me that you're you're changing an existing test.

[\[12:25\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=745s) First of all, >> because well, I'll tell you what my guiding principle is. If something is passing, we want it we want to know it continues to pass later. So if you're changing it, you're basically removing your tracks. That's why I like to duplicate. The reason why I was saying is inconsistent is if you look at these above tests, this is X. The one is X, the three is Y. >> Mhm. >> And then the the X when we put it right next to it, the X increases,

[\[12:56\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=776s) Right? So originally originally it was 21. So basically, if we want to keep it the same as the previous test, the x on this side should be less than the x on this side. >> Sure. >> So I'm just worried that the person who picks up this test afterwards will be a little bit confused like why why is it different? Why is it position different?

[\[13:29\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=809s) >> Okay, I understand that. Run all the test. Let's see what happens after you fixed it. Okay. So, it's basic. It's the same as I guess I I could to I could have copied and paste it here and copy this one and paste it up there. >> Sure. >> I could have done that. But, I think it said it all passed.

[\[14:01\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=841s) Yeah. So, 11 passed. It's your fa. We're good to go. >> Okay. >> How do I Is it slash >> again? Just the same the way you commented it. Control forward slash. >> Okay. >> Okay. So now that we have it consistent, we can >> So we have yeah x and y

[\[14:31\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=871s) >> two and this one would be 3 1. This one would be 32. This one would be 3 three and this one would be 42. Right? >> I think it really helps now that it's it's visually arranged that way. Right. >> Okay. >> Okay. So, we can run this. It's failed. >> Okay.

[\[15:01\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=901s) So, how do we want to change update this? >> So, the our current problem is that we have a hardcoded one and one, right? We're always looking for the >> for the let's see where we Oh, yeah. We extracted it to to the rules, right? >> Rules, right? >> Okay. So, one in one is no longer okay. >> So, what's the simplest thing we can do to make everything pass?

[\[15:34\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=934s) So, you remember yesterday I told you imagine you're the laziest developer in the world. You have to go in 5 minutes. >> Yeah. >> so, here's another way to think about it. Imagine you're you're like your 13-year-old cousin who just started programming in C. How would he solve it? Right? Cuz he also wants to go but he doesn't have the knowledge of making something complicated.

[\[16:06\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=966s) >> Okay. >> So just a way to think about these things. So where are we getting the one load right now? We have no idea where to look for, right? >> I mean, we know that we can look at 32. Basically, do the same thing we did with the one one, but I'm not sure if you want to do that. That would be the simplest way right now to the quickest way to see to look at 32. >> Yeah. >> As well. >> Mhm. Actually, you know what I'm thinking?

[\[16:38\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=998s) I'm looking at this code right now is that we're always checking for black. We could have done the same exact test from before and removed the assumption about the color. >> Yep, we could we could have done that too. >> Do we want to go back? >> Because right now, here's what I think. In this current test, we changed two assumptions, both the color and the position. >> So, let's keep one of them. >> Do we want to keep the color or do we want to keep the position? >> So, let's comment this one out first. >> I I don't think we need to comment it.

[\[17:08\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1028s) I think we got the test wrong. I think we we need to to to either reverse it so that black is still supposed to win but in a different position >> or that white wins in the original position. >> Okay. All right. So, let's just make it make it black then. >> Yeah, I think that would be easiest. So, I mean first of all change the name of the test. >> nonor cell one for black. Yeah. >> And black wins. >> Yeah. >> Okay.

[\[17:49\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1069s) Will that test fail actually? >> Yeah. Yeah, it will fail >> cuz it's not at the corner. >> Yeah, but it it's the la the last player is white. Well, we have to make the last player white. >> You're right. >> Right. Same reason why the other test passed. >> Okay. >> Which this actually cannot be the same. It's called four.

[\[18:20\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1100s) >> Yep. >> What happens now? >> Well, why is it fast? >> Interesting. So, we got the test wrong. Interesting. We're expecting black and we actually get black >> cuz current winner is last note added surrounding return color.

[\[18:51\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1131s) >> Ah, so this is interesting. So we're not sure how to get it to fail. >> Oh, okay. I think we should I think the easiest would be >> actually, you know what? Here's why it's working. Because we we that that location is actually surrounded by black, >> isn't it? >> One and one. Isn't it surrounded now? >> One one would be like right here,

[\[19:22\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1162s) Right? >> Uhhuh. >> And then one, two, and then two, one. >> Yeah. But when we check is fully surrounded by I think we're also checking the the ones right next to it as well. Is that possible? >> No, >> you're right. Maybe. Actually, I don't think so. This >> you can So here's a here's a trick by the way if you want to go back.

[\[19:59\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1199s) All right. this. How do you go back? >> Control minus. >> Okay, that's what I did, but it didn't go back for me. >> interesting. Another way is to control Alt F7. >> Never mind. I lied. It went back for me. >> Bastard. >> All right. >> control turns it into a link and then you can click it. Oh, control. >> Yeah, control. And then you just hover the mouse over anything, it becomes a link.

[\[20:31\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1231s) >> Oh, wow. Okay. >> Yeah. >> Interesting. >> Yeah. I'm used to pressing F12, but >> F12 or controlB will work as well. >> Control V and ReSharper. Okay. So, we're getting something wrong here, right? We cannot this test is wrong in in one way or the test is right and our code is doing too much. I'm not sure. >> I think the test I think the test is right and there's something wrong with our code.

[\[21:04\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1264s) >> So we have to write a test that fails. It proves that there's a bug. >> Okay. >> You agree? I mean, I was thinking it might be easier to just walk through the code and find >> Yeah, I I I definitely think we can we can debug the code. What I'm saying is that we cannot change it because there's if you think it's obvious that there is a bug in the production code, we need to be able to later reproduce it with a test before we fix it. >> Okay, >> agreed.

[\[21:34\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1294s) >> All right, I agree. But I'm not sure how >> well first of all we have to to realize what the problem is >> right. >> Okay. So what we can do is we can basically a break point here. >> Oh I see. So we we do break into it find out the problem and then >> then write a test that recreates a problem. >> I see. Okay. >> Basic forensics. >> All right. So you go all the way up here.

[\[22:06\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1326s) I can't see it. You say test with debugger. >> Okay. Okay. So, let's step into it. Let's see what happens should be false, right? >> Sure. surrounded >> and is it just let's see what it returns.

[\[22:36\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1356s) >> Oh, I forgot was the command to skip back up this one. >> Never mind. >> All right, >> just F10. Yeah. >> So, it says surrounded is true. So, you are right. >> Okay. So, but we can't just create a test that proves that is surrounded by returns true or false because that doesn't is not a [unit of work](https://osherove.com/blog/2020/3/12/unit-testing-entry-points-and-exit-points "Unit Testing Entry and Exit Points (osherove.com)"). It's a it's an internal thing. So the only thing we need to say is that something else doesn't work because of that.

[\[23:07\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1387s) Something that should have we should have gotten a different result when we're getting a a real one. So is fully surrounded by returns true. Let's see why it checks it returns true. Now we can actually jump into it with F11. >> Okay. >> Okay. Let's just see which one returns true at some point. >> Surround on left equals true. >> Yeah. Well, that's okay.

[\[23:38\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1418s) >> Well, it's one it's one one. Oh, yeah. Zero 01, right? So, that's okay. >> Okay. Surround on right should be false. Let's see what happens. I can't see it. Surrounded on right is true. >> Interesting. X + one is so this is two two one I'm interested what the board is. So this is looking at the surrounded

[\[24:11\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1451s) Get stone color. So it should be so everything is black. It says I thought we made it so everything is empty at the beginning. >> Everything was supposed to be empty, >> right? >> But we Yeah. >> So I guess our our test could be we we initialize the board and we verify that it's empty. >> that would be that that would be one way, but I would rather verify that there is a bug where for example we have something that's not surrounded

[\[24:47\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1487s) And so the winner will should be the other color and we get the the the the one that's wrong. >> Okay. >> For example, we can create a test almost like we have right now. >> Mhm. >> but we have a stone that's not to the right. So there is no winner. So, we should be getting a white by komi, right? >> I see. I see. >> But because it thinks it's surrounded, it's going to be false.

[\[25:18\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1518s) >> I see. >> Does it make sense? >> Okay. Yeah, that makes sense. >> And just to make sure, let's see what surround on bottom and top says. Okay. >> It's F10 and F10. >> True. Sorry. >> It's going to say true because >> Yeah, because everything is black, >> right?

[\[25:55\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1555s) You want to write this test? >> Okay. Let's see. So what's what's going to happen is we're going to write a failing test. When we make that test pass, this test should start failing. >> And then we can fix it. >> Okay. >> So, you're going to just you're going to just add the test. You're not going to comment this out. >> No, this test passes. We're going to use it later when it stops passing.

[\[26:25\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1585s) Determine winner. Own any cell. White should win. But wait, >> actually, I realized this one. Oh, okay. Oh, I see. I see what you mean. >> Okay.

[\[26:57\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1617s) Okay. >> It's different from a test we already have because the black has already played, >> right? Black played but doesn't own any territories, right? Okay. >> and in that case, we can say make board add stone black at 3.1 and that's it. Right? >> I'm going to leave these lines. I should know that. But

[\[27:29\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1649s) >> what? Oh, you we missed you think that the white should have played already, right? >> Right. But then I think you're going to get white because the current winner. >> Yeah, I we're supposed to get white, but but I think we're going to get black. That's my point. >> Okay. >> You because everything by default is black. >> I see. You're right. You're right. I agree. I agree. >> so we're supposed to be getting white here because by komi we should be getting white.

[\[28:02\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1682s) So red once white should win by komi, right? Make sense. Or does komi not play into anything? >> No, it it should be owned by komi because neither of them own any territories now. >> Okay. So, komi only stops applying when someone owns a territory. >> Yeah, none of them own territories. So, white should

[\[28:40\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1720s) Ah, yeah, it's debugging because that what we did before. That's the last thing we did. So, just right click here, run tests. Cool. >> White, but was black. That recreates our problem. >> So now we can go ahead and say, "Oh, there is an initial board that's supposed to be everything's supposed to be empty because our enum here. >> Let's move our enum here for a second. >> Good job. >> Sorry. >> You need to make it int.

[\[29:11\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1751s) Declare intum." I forgot how you do that. >> And in enum is int by default if I remember correctly. So why is it not setting it as zero? >> Because we have to set the other ones. >> The solution would be just to move empty on the top. That will >> Sorry. >> Or do that. >> Let's see what happens. >> I was going to put empty on the top. >> Oh, two fail now. >> black wins. Black wins.

[\[29:41\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1781s) But you notice that our test currently passes. The new one. >> yeah, the new one passes. Okay. Right. Cuz in our test, white should have won. >> Right. Right. Okay. >> Okay. So, we have one test we expected to fail, which is a not corner cell. Right. >> Right. That one we expect to fail. The first one though we expect to pass. >> At least one cell one for black and white.

[\[30:11\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1811s) Well, at least one cell one for black and white plays last. Black wins. okay. So I would suggest let's fix the let's comment out the test we have that we that we expected to fail so we can focus on the one we don't we didn't expect to fail >> which is this is the one we expected to fail. Right. >> Right.

[\[30:48\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1848s) Oops. That's not what I meant. Control Z. Control slash. >> you do it. Control forward slash. I think our keyboards are just different. >> Okay. >> Maybe. >> Alt R. Okay. Now, let's see what the hell happened here. Double click here. And where are we?

[\[31:19\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1879s) >> We're at the top. That was pretty cool. >> Oh, yeah. Okay. So, now we can actually debug that and see what happens. Okay. Oops. >> So, you press F11. For me, it doesn't work. >> Oh, really?

[\[31:49\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1909s) Okay. >> All right. So, >> okay. F10. Let's see. Is fully surrounded by what does it say? False. That's okay. That's good for us, right? >> No, it's supposed to be true. >> But it's not. It's How is it supposed to be true? Oh, at least one cell, one for black. That's true. So we have >> So are did we get the test wrong?

[\[32:19\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1939s) >> The thing just is not surrounded. >> I remember remember we were talking about how we do not I I was talking about how we did not have test verifying that surrounded on the corner or on the edge. And then he you thought maybe it might it might it might pass or our implementation or took care of it. So we decided to go >> other stuff first. >> Okay. >> So I think this might be relating to that where we did not have tests originally to verify that checking in the corner that it's surrounded actually passes.

[\[32:59\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=1979s) >> Okay. So why don't we step into is fully surrounded by and see exactly what the problem is. F11 that sucker. Okay, >> so start on left is going to be false. Oh, what just happened? What did I just do? Why? Why you look funny? Oh, get out. F10 sur is false. So I think the issue is that we're treating the boundary as zero here.

[\[33:36\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2016s) >> Yeah. >> Where >> it should have been. >> Well, where we wanted it to be one through 19 instead of zero through 18. Like we didn't want the for the term. >> We should just say x is bigger than one. No, x is smaller than two maybe >> or x. Yeah, >> because x is one right now. >> Right. So if x is smaller than two then yeah we're surrounded on the left or actually just if x is equals one. No.

[\[34:08\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2048s) >> Yeah. Yeah. >> So it's a wrong zero. Yeah. So x equals 1 would be the change here. >> Okay. >> And for surrounded on top it's y equals one. But let's see what happens. Let's just fix this one and see what happens. >> Okay. So should we keep on stepping through or just run through the rest? Whatever you think. >> Turn on to the rest since we already know what we want. Okay. So, we don't want to fix this equals one.

[\[34:40\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2080s) >> Mhm. Let's see. Yeah. Now, don't repeat. Just run all the tests with a menu for now >> because it will repeat the debug action. >> Yeah. >> Wow. Awesome. What the hell? I think maybe a lot of our tests we we treat X as Y. Maybe that's the problem. No, I don't think that's it. I think Oh, so what's that filling acid above and surround color index out of range?

[\[35:17\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2117s) Next one's out of range. because well, how about x is smaller than two. I'm not even going to try to fix everything. I'm just going to say if x is smaller than two, maybe that's a simpler fix Okay. All right. So, at least one cell one for black and white plays last. Black wins. So, I think we should also fix the Y for smaller than two.

[\[35:59\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2159s) >> Good pass. >> Wow. >> All right. >> Okay. Okay. So, let's let's uncomment the the other test and that should fail. Let's see. Right. >> Okay. So, determine winner. Not corner cell one for black. Black wins. But it was white,

[\[36:35\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2195s) >> right? Because we're we're just >> because now that now we're back to fixing the one and one, >> right? To fixing that hard-coded X and Y for always at the corner. So, now we can actually go and fix that. Right. >> yeah. >> Now that's the actual thing we wanted to try. We were yak shaving up until now. So So that's our problem is surrounded by we have a black that's surrounded but it's not one and one. Okay. So

[\[37:06\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2226s) What's the easiest way to find black is the winner? Remember we assume that black is the winner. So we just need to find that black has surrounded one. >> Well, the first easiest one would be just check this territory. But would we want to do that? Is that what you mean by simplest way? >> Well, simple does not necessarily mean well remember simple is first of all as close to the top of the message as possible and less indented.

[\[37:41\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2261s) If we just check two places, it just leads us down a rabbit hole of continuing to check two places, three or four places. So, assuming that it will be actually easier to and simpler to just write a loop that goes through everything maybe. >> Yeah, that's what I was think that was I think was the next simplest solution. So, >> actually, here's something simpler. What if we didn't use a loop? What if every time we added a stone, we would check if it's fully surrounded by?

[\[38:14\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2294s) >> Every time we added a stone, >> we would check for if for around it if something is fully surrounded. >> Right. We already have a remove stone is surrounded. Right. >> Right. Right. So that means every time we add a stone for each of its neighbors, we check if it's surrounded. >> Yeah. We already have the infrastructure in place. We just in the place where we would have removed the stone, we would also just declare the winner as well for now. >> Okay.

[\[38:44\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2324s) >> what do you think? >> I want I I want to see you do it. I'm not sure if I fully understand. >> Okay. So remove stone is surrounded. Already knows if something is surrounded, right? >> Yes. So if it's fully surrounded right now, we always assume that black is the winner. Right. >> Right. >> So we just say >> right now we doing it's fully surrounded by white only in that code. >> Sorry. >> Right now we're only doing fully surrounded by white.

[\[39:19\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2359s) >> Ah interesting. So that's another thing to take care of. But let's see it's always surround. So that's something we will need to take care of as well. So I would say also so that we won't forget to do it later. We would say to-do remove assumption

[\[39:50\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2390s) That checks that removes move stones only if they are surrounded in white cuz that's a bug. Right. >> Right. So one way to fix this is to actually to take that as a parameter and then >> right >> move surrounded if

[\[40:23\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2423s) Cuz I'm pretty sure we have two ways to go about this at least. We can either do this and then when we know that it's black then just declare the winner or does if someone has something fully surrounded just declare that as the winner in the rules. >> Okay. >> I mean rules is fully surrounded by is already sending stone color white. Agreed. >> Mhm. >> Actually yeah we don't have to go far. We go to rules is fully surrounded by

[\[40:55\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2455s) And we'll say here if fully surrounded and last what was it? Current winner equals surrounding.

[\[41:33\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2493s) >> Interesting. All right. Now the get winner won't have to check if something is fully surrounded. current winner, >> right? So we would want to do if fully surrounded by in the ad stone,

[\[42:04\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2524s) >> We're we're doing that, >> right? Okay. >> Okay. So we ran the test. Two failed at least one cell one for black and white plays last. Black wings failed and not corner cell one for black. Black wins failed. So either that didn't work or I missed something. >> No, I think you're only checking if fully surrounded for that current

[\[42:36\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2556s) Current stone >> whenever we gather position. >> I think you need to check for the neighbors. >> Okay, you know what? Let's do it simpler. Hold on. I'm going to undo. Okay.

[\[43:06\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2586s) Just this and here simplest thing I can think of is just to do a loop over everything and just count the points. >> Yep. >> Basically, so for int i in what was it? Board dot or actually >> sorry oh yeah equals 0 to or is it one >> one? Well, we're treating one as the edge, right?

[\[43:38\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2618s) >> One one is smaller than board dot. What was the name of that thing of that const size? >> Yeah, I think it's public. >> We'll make it And I think it's supposed to be while I is less than equal to.

[\[44:18\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2658s) Cool. Is that supposed to be public or private? Let's see. Nope, that doesn't. So, I'm missing the name of that. Let's see. board size was without an underscore. That's it. Okay. Make it public. And now we need an inner loop, >> right? >> Yep. >> Four

[\[44:49\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2689s) And J. Is it the same? Right. >> Right. So, we want to equal one also. Yeah, >> our board size right now is 20, right? So it should be >> should be 19.

[\[45:21\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2721s) >> Is it 19? >> Yeah. >> 0 to 9, >> I think. So we stopped. We did we tried changing it but we backed out on it if I remember correctly. >> Right. >> We can go back there. >> The thing is we want to treat the edge as 19 the array is zero between 18. Right. >> Hold on. Yeah. >> So when it does check the edge for 19 you may get an out of index exception.

[\[45:53\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2753s) We would say here basically we would we would just back out on the first surrounding. So we would say just come on if is fully surrounded by we'll just use I injure

[\[46:40\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2800s) Stone color black. How's that? >> Yeah. >> And now we don't need to do anything here. >> Mhm. >> That makes sense. >> Yeah. >> Well, we still have those two failed, but it's still index. >> So, I guess we should be Yeah. >> Okay.

[\[47:18\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2838s) >> Stone color. Where is that gotten from? >> It's fl Oh, it's because I I know why. >> Okay. >> It's because we did not we're not checking the edges for right here. So, right and bottom. >> Mhm. We're not taking >> should be board size minus one. >> All right. >> Small smaller than board size minus two I guess.

[\[47:53\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2873s) >> Well, it's going to be greater than because we wanted to return true, right? for upward size minus one >> one. Why do we have >> Well, you still have to put it in brackets in parenthesis. >> Do we have to put it in brackets? >> I think it's more readable. Okay.

[\[48:42\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2922s) Okay. >> It's bigger than board size minus one. Actually, I would say it is. It's equal to board size or it's bigger than board size minus two. I think it's the same same deal. Cuz if it's bigger than both sides minus one. Sure. Okay. You know what? Sure. >> So, let's debug this thing. >> All right. >> But we can see exactly where we got the message here.

[\[49:12\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2952s) Stone color get winner. Then we don't have a line number. Yeah, we do. some color matrix here and get it from this line. >> Okay, >> let's run it on bottom just just this. You see? And there we have this one.

[\[49:43\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=2983s) It's just minus two. So, this does sounds feel like we're kind of like just hacking our way through like >> Oh, yeah. Definitely. >> Together. >> Well, I It's more like a trial and error because this is I mean, it's known to be like an error and an off by one error. So instead of just thinking it through, I'm just going to let the compiler and the tests let me know when I'm done

[\[50:16\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3016s) Because I know that the range is one here or one to the other way. So I'm not going to bother thinking too much about it. If I realize that that's not the real problem, then I can start delving into it. But I'd rather just focus on like really difficult stuff here. This is simple. It either works or it doesn't. And we know that this is where the line this is what we changed. So it's kind of simple. Okay, that makes sense. >> Okay. So, so now that we have everything passing, let's refactor a bit because I don't like these number twos there.

[\[50:48\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3048s) >> Yeah. >> So, we can make them into const, right? >> say again. >> So, we can make them into const numbers. >> Okay. Right. >> With a good name. What would a good name be though? >> if it's smaller than >> like the edge,

[\[51:21\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3081s) >> let's just call it edge. >> Yeah, >> That makes sense. >> Sure. >> Okay. So we can go ahead and say control shift R. Introduce variable or field replace for call it edge. Make it a static. It's not an edge.

[\[51:51\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3111s) It's an edge. What's that? So he's fully surrounded by surrounded on the left. We don't need to do this check

[\[52:24\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3144s) So, what did we just add? What feature did we just add? >> We added in the >> Oh, remove Oh, we removed the assumption of one and one. Actually, right. >> you you broke up a little bit there for me. >> We we removed the assumption of one and one. >> Right. Right. >> Hold on. Okay. So, we we removed the assumption of that black is always at the same location. So we said if we had a black that's not in the corner, it would still be found as the winner.

[\[52:59\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3179s) >> So now we can remove the assumption that black is always the winner. >> Yep. Yep. We can do that. >> So we can say control Determine winner. I think it might be easier if we copy the one of the other tests. >> Oh, did I copy the wrong test? >> Yeah, the test above that one. >> Sure.

[\[53:30\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3210s) >> And then replace it with colors. >> Not corner cell one for black. Black wins. >> But now we're going to remove a different assumption. Determine winner. Not not corner cell one for white. >> Mhm. and white wings. Yep. >> White. White wings. So, we're just going to switch up the colors.

[\[54:00\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3240s) >> Yep. >> white. Let's do there. And actually, this means black. >> Yep. Black, white, blah blah blah. White. Okay. And the last play is by black.

[\[54:34\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3274s) Sure. >> Yep. And then you assert are equal. >> Oh, that's true. Okay, determining not corner. So one for white, white wins or yeah, white wins. But it was black. So now you can just say just return

[\[55:08\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3308s) Get or is it board get stone color at INJ, right? No, because it's fully surrounded by it's going to return false because it's only checking if surrounded by black, not >> inj plus one. >> Huh? >> No, I don't think it's going to even enter in here because this is going to be false because it's looking is surrounded by black instead of white.

[\[55:40\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3340s) So, I think the easiest one would be if we go back. Oops. If we do this. >> Yeah. Well, that will pass. It's pretty horrible. And then we'll make it nicer. >> Yeah. >> Yeah, I agree. >> This should pass. Yep. Okay. >> Now, kind of make it pretty. >> Now, I think we just need we need the fully surrounded by not to take a color.

[\[56:10\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3370s) That's basically it. and to have a version of that >> where we don't care about the color anymore, >> right? >> So imagine we did this surrounded by and then we would just say is not equal to empty, >> right?

[\[56:47\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3407s) Stone color empty, etc. >> Mhm. So, we have a version of this. And now we would go back to >> would we would it be would it be okay to change the return type or like I guess like if it's fully surrounded by it returns. >> It's just a boolean, isn't it? >> Right. But I mean it might be simple. >> Yeah. But imagine that it's partly surrounded in white and then partly in black.

[\[57:20\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3440s) What would you return if you return a color? >> Right. Well, I don't think that >> method right now actually solves that problem, but I think we might need to introduce a test to show that that fails. That should fix it, I think. Did I forget something? >> What did I forget? Oh, yeah. First you need to do this >> just

[\[57:57\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3477s) >> have something to do with the fact right now. >> Sorry. >> Right now you're only returning stone color black. >> Oh yeah, right. Good catch. The fact that it's 3:00 a.m. here might delimit my pro abilities for a bit. Board get stone color at I and well it's surrounded by but we can't assume that we get a surrounded by but I'm going to assume it's J plus one. >> Okay. >> let's see what happens.

[\[58:41\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3521s) Now I'm just toying with myself. So what do we do? >> we still need to get the other version. >> Yeah. >> What do you think? >> I was thinking that the fully surrounded by can return the color if it's surrounded. But maybe have out parameter. I think we're No, I think I think you had it right the first time we just say is surrounded by

[\[59:16\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3556s) Stone color. I think that would be the most readable and simplest. I think that's just >> Yeah, >> I had I had to I had to >> Yeah. And then you had two, of course, >> right? But it's still more readable and than anything more generic than that. I think I mean I I kind of like it. >> I kind of like it.

[\[59:47\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3587s) >> I mean I would have been nice to just do it in one. >> Yeah. >> But we can't assume that we know which color and from which side, >> right? >> Agreed. >> I agreed. You also might need to change back the >> just remove that. I'm going to remove the other is fully surrounded by. >> Yeah. >> Come on. There it is.

[\[1:00:24\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3624s) Okay. So, but we still have an assumption here, right? The assumption is that whoever has won any kind of territory is the winner. >> Yep. >> So we need to start counting territories, right? >> Yep. So what we can do is we can have but first of all let's commit. >> Okay.

[\[1:01:03\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3663s) Our shopper. >> you you'll do a git ignore for that later, right? >> I Yeah. Should I ignore those stuff? >> But not not for now because I think we're going to break for for now. It's been an hour. I'm going to go to sleep and we're going to continue at a different time. >> All right. So, what should I name this? Winner by color and position.

[\[1:01:38\]](https://www.youtube.com/watch?v=05IPTAYRrTQ&t=3698s) Territory position. Yeah. Okay. But but not not with count. >> Sure. >> All right. Cool. >> Cool. Okay. Thanks Raymond. I'll see you later. >> All right. >> Bye, everyone.

## Related

- [When should you remove or change a unit test?](https://osherove.com/blog/2005/4/3/when-should-you-remove-or-change-a-unit-test.html) · osherove.com

    Argues passing tests should not be removed or changed, the reason the pair duplicate tests rather than edit them.

- [A Unit test should test only one thing](https://osherove.com/blog/2005/4/3/a-unit-test-should-test-only-one-thing.html) · osherove.com

    Guideline that each test checks one thing, matching changing one assumption per new test.

- [TDD in .NET course - Day 2 Learnings](https://osherove.com/blog/2011/8/11/tdd-in-net-course-day-2-learnings.html) · osherove.com

    Notes that TDD is about removing assumptions one by one, as the pair drop the hard-coded corner and black-wins rule.

- [Changes forced by failing tests](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-8#sigil_toc_id_155) · The Art of Unit Testing, 3rd ed., ch. 8

    Book section on changes forced by failing tests, relevant when the all-black board breaks the komi expectation.

### Also linked in this lesson

#### The Art of Unit Testing, 3rd ed.

- [Readability](https://livebook.manning.com/book/the-art-of-unit-testing-third-edition/chapter-9), ch. 9

#### The Art of Unit Testing, 2nd ed.

- [First steps with NUnit](https://livebook.manning.com/book/the-art-of-unit-testing-second-edition/chapter-2#ch02lev1sec3), ch. 2

#### osherove.com

- [TDD: 4 questions that will help you create the simplest thing that could possibly work](https://osherove.com/blog/2010/1/6/tdd-4-questions-that-will-help-you-create-the-simplest-thing.html)
- [Unit Testing Entry and Exit Points](https://osherove.com/blog/2020/3/12/unit-testing-entry-points-and-exit-points)

[Previous lesson Colors and Winners](https://courses.osherove.com/courses/tdd-pairing-sessions-building-a-go-game/colors-and-winners/) [Next lesson Total Noob Fail](https://courses.osherove.com/courses/tdd-pairing-sessions-building-a-go-game/total-noob-fail/)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
