Join Anton Delsink for an in-depth discussion in this video Chess game model and controller, part of Object Oriented Programming with C#.
- [Instructor] To take a board-centric and a game-centric view of chess, let's create a new unit test project and a class library project, in which we will place our code. So open Visual Studio, and select file, new project. Now we'll use dot net core, MS test project, and we'll call it tests, and I'll call my solution chess.
You can place your solution in any folder. Once your solution opens, let's rename the first class. We'll rename from the solution explorer window. I'll call this tests underscore chess. So yes, let's rename the class as well. And then I'll go and rename the method, test zero zero one. Now, first thing to note, is we can design on a whiteboard, and you saw me start doing that in the slides. So we thought model, view, controller.
This is a nice way to just think in terms of, what is the class that I'm creating actually for? Is it the model, the representation of the game state? Is it a view, how're we going to show things to a user? And is it the controller, perhaps, where the logic of the game goes? And so, we will get to those distinctions in a minute, but let's just first think of the most trivial way that we could write a method, that represents something about chess. And so, the obvious thing to do, would be to say, you know what, I need a chessboard. And I'm not going to implement this.
So you'll see the compiler complain at me, and that's okay. I'm just going to keep writing as if this exists. And so here, I say, for each move in a collection of moves, make the board do the move. So what is moves? It's, perhaps, just a collection of strings. New list of strings. Not particularly difficult to do. Now for list from the genetic type. Control dot. So we can say, using systems dot collection dot genetic.
But of course, chessboard doesn't exist. Now the moves collection exists. And I'm saying, imagine there is a move method on a class chessboard, we could say board dot move, here's the move we want you to make. And so we just think of the board-centric, or the chessboard-centric view of the world, everything goes on the chessboard, and we make it do the move as the user provides, one move after another. Now, couple of things here. First, this is maybe the obvious way to start, but imagine for a moment, again, in the context of model view controller, the chessboard is doing two things here.
It is representing the chessboard, and it's representing the rules of what constitutes a valid move. And so when we modify the state of the game, it's the chessboard object here, that's doing a move. But that implies that it's applying the rules, as it's also making changes to the state. So let's make a slight variation on this example. And here on line 22, let's just change the board dot move, to an apply method. I'm going to say apply, the move, to the board.
Now this is subtle, but it's important, because the move is one thing, it's text that describes what piece goes where. And there's the board, as a parameter, to say, "this move applies to this particular chessboard." But the method apply, we haven't written yet, the method apply can exist somewhere else, somewhere other than the chessboard itself. And so, is that perhaps, another object? And perhaps yes. So we can add var, game.
And if the game is a game of chess. Then perhaps it is the game which understands the rules of the game, that applies a move to a board, and so then the board doesn't need to know much about the game at all. In fact, a chessboard and a checkers board, look very much the same. It's just a grid. And so before I even implement this class, I'll just delete this first part. So we have chess, as a game. And we have a board, which is an eight by eight grid.
And we're saying the game understands the rules. It's in charge, it's the controller. And the game can apply a move to a board. And the board is generic in this case, it's just eight by eight squares. Now, note in this first example, we have not even considered the various pieces that might be placed on a board, or that might have different rules, based on which game we're playing. It's just an outsider's view of, there is a sequence of moves, that need to be applied, to a board, subject to the rules of the game.
And so this is probably the simplest way we can look at chess from the outside, from a model view controller point of view. And, using the word view, I'm noting, we haven't addressed the view yet. We haven't actually visualized any of this. And so there's maybe a third thing, and in my case, I'm just going to use the console. And so as we have game dot apply. We can have printer dot show, and so now we have three objects representing the three core capabilities we need.
We need a model, so this is the board, eight by eight squares, we need a view, a way to show it, that's our board printer. And we need a controller, that's in charge of the game rules, and will advance the game by playing one move after another. And so now we have, essentially, a first version, a draft, of a model view controller, for the game of chess.
- Abstract classes
- Processing text with StreamReader
- IEnumerable and yield return
- Windows Forms controls
- Windows Forms with and without the designer
- Adding, organizing, and testing forms
- Creating classes