Hello, and welcome back.
In this lecture, I want to give you another challenge.
It's related to functions, but this challenge is going to be more
about divide and conquer. It's going to be more about organizing your
program around modules, it's going to be more about problem solving.
What I want you to do is i want you to write a tic-tac-toe game.
This is going to help you just become a better problem solver,
and it's going to help you organize your program around functions.
You're also going to utilize an array,
you're going to have to figure out how to format certain data,
but it's going to be a simulation of a tic-tac-toe game.
We're not going to write a computer.
It's going to be player 1 versus player 2.
So I want you to write a program that plays tic-tac-toe.
Hopefully, everybody is familiar with this program
or this game, I should say.
It's just a game that's played on a 3 x 3 grid with two players.
You take turns.
X's a nose.
Hopefully, that rings a bell.
I didn't want to have to go and explain this game, but hopefully
you're familiar with tic-tac-toe.
I'm going to provide a little demonstration of how it works and how
I'd expect the output to play out.
But it's a basic tic tac toe game.
What I want to do is you're going to probably need to
create an array to represent the actual board, the 3 x 3 grid.
I think you can get away with just creating a single dimension array.
You could actually create a 2D array as well to represent
the rows and the columns.
2D would probably be more intuitive, but it might be a bit
harder to visualize in your code.
But either way works.
The good thing about programming is there's many solutions to a problem.
I'm going to tell you one of my solutions and my design approach,
but feel free to change the design.
As long as you're using the c constructs correctly,
that's all i care about.
I want you to learn how to use c.
As long as your program is modulized and it's organized and it's easy
to maintain, and it's quality code, that's what I'm worried about.
So one way to implement only one way is representing the board as a single
dimensional array with ten elements.
The reason I have ten elements here is I don't want you to use zero.
These ten elements are going to represent the board, but
they're also going to represent the coordinates on the board.
So I'm going to demo this and show you how I'm thinking of working.
But the way that you're going to actually pick the box that
you want to draw in is going to be based on the element number.
In addition to creating an array for the board, you have to try to think of
how you're going to break this problem down, how you're going to organize it.
I'm thinking at the very minimum that you're going to need at least
three functions, probably more.
But you need at least have one function that checks for winners.
This will basically just see if a person has won the game or
they may also even check for a draw.
You should have a function that draws the board.
Because you're going to be drawing the board after every player's turn.
And you're going to -- so you're going to be reusing this.
Another function for marking the board, this happens when
the user actually wants to put x or an o in a certain box.
You need a function to set that box as an x or an o.
What will happen is you'll probably be populating the array with that x or o.
And that way you can look at the array to see if you won or lost.
And in the mark board, it would also allow you to check
for an invalid selection.
So if valid selections were one through nine and the user
entered ten, you would say, hey, you can't do that, try again.
The game should be user friendly, should be intuitive to use as
far as a user perspective and it should be organized as far
as a programming perspective.
Really think about this one.
Get some planning out, do some planning, get some paper out, start writing some design about how you're
going to attack this problem and what functions you're going to need to use.
The way that I'm thinking of this is going to run, let
me show you a little demo.
I'm not going to show you any source code, but this is what i envision the
game will look like when we run it.
Maybe a little title that says, hey, the game.
And then also a little information about the player.
Player 1 is going to be x, player 2 is going to be o.
Remember, this does not have to be playing the computer.
You can just put in each turn.
So you'd have to play this game with a friend who is sitting next
to you, and they would have to tell you where to put the number.
If you're getting really ambitious, you can write the
game to play against a computer.
But you'll notice that the 3 x 3 grid has numbers in it initially.
You can use these numbers to enter.
So if you player one wants to take their turn and they
represent x, depending on what number they enter, that's where
the x is going to be placed.
So we print the grid, we display the board and then we also tell
the user whose turn it is and we tell them to enter a number.
So in this case, I'm going to go ahead and enter number two and hit return.
And what will happen after I hit two and return, it's going to
actually see if somebody won, it's going to clear the screen and
it's going to redraw the board.
So after I hit 2 and enter, you'll notice that I still have the title,
I still have player 1 and player 2.
But now in box 2, I have an x.
So what else I should have done there is I should have checked
before I displayed the board whether or not we had a winner or draw.
And obviously that's the first entry so we have neither.
Since we don't have a winner or a draw, I'm now going to ask
player to enter a number and then they can pick any number here.
Now if I entered an invalid number like 10, it should
tell me it's invalid.
And it should allow me to maybe make another move.
If I would enter 2, 2 should also be invalid.
So if I enter 2, it also says invalid because 2 is already taken by x.
So if the program is allowing me to try again, you could just get rid of
their turn but that's not really fair.
So now I'm just going to say where do I want to place the o? I'm
going to say 5, I'm going to put invalid move, it's going to redraw
the board and there's an o and 5.
And now it says player 1.
And you keep doing this until you have a winner or a draw.
I'm just going to say 9.
Now I have an x there, valid entries are 1 3 4 6 7 and 8.
I'm going to say 3.
And let's actually try to get x to win so now x is going to go into eight
and I'll put o and 6 just to show you that the game works with a winner.
And then I put 7 for x, the game should now be over. I hit return.
It says player one wins, and it even played a little sound for me,
I did a little fancy stuff there.
So the game is over, player one win.
And it should also happen if player 2 wins or if there's a draw.
So think about how you want to attack this problem.
Nice little program.
I'll go through a solution and provide the source code in the next lecture. Thank you.