Why not just use a for loop to fill the grid with 0s?
18 Replies - 6469 Views - Last Post: 27 February 2011 - 02:36 AM
#17
Re: Tic Tac Toe using 2D array
Posted 26 February 2011 - 07:51 PM
Hi, 21q,
I didn't realize you had a post with a topic 'tic tac toe' when I posted mine last night.
It is sad, but I'm so slow and I don't know enough to ask a right question.
I know my text book covered loop, function, array and a few more things thus far, and my last assignment was discounted beause I didn't use functions and I was determined to use functions--not being so successful.
My code compiles but it does not finish the job I wanted it to do
I didn't realize you had a post with a topic 'tic tac toe' when I posted mine last night.
It is sad, but I'm so slow and I don't know enough to ask a right question.
I know my text book covered loop, function, array and a few more things thus far, and my last assignment was discounted beause I didn't use functions and I was determined to use functions--not being so successful.
My code compiles but it does not finish the job I wanted it to do
#18
Re: Tic Tac Toe using 2D array
Posted 26 February 2011 - 08:11 PM
I haven't read the entire thread here but I'm hideous logic to determine the winner (I hope you don't mind me saying). Here is how you can determine the winner efficiently in code:
1. What is unique about the winner? They occupy one of five positions guaranteed: middle top, any position in the middle row, or middle bottom.
2. If, for example, middle top is occupied you check for the same symbol either side in the row, and failing that you check directly below in succession. Similar algorithm can be applied to other possible positions.
3. You only check for a winner after a minimum of three plays have been made.
That is an initial approach. There are further optimizations that can be made, but I'll leave you to work that one out as well.
1. What is unique about the winner? They occupy one of five positions guaranteed: middle top, any position in the middle row, or middle bottom.
2. If, for example, middle top is occupied you check for the same symbol either side in the row, and failing that you check directly below in succession. Similar algorithm can be applied to other possible positions.
3. You only check for a winner after a minimum of three plays have been made.
That is an initial approach. There are further optimizations that can be made, but I'll leave you to work that one out as well.
#19
Re: Tic Tac Toe using 2D array
Posted 27 February 2011 - 02:36 AM
First thing.
As ButchDean has pointed out we are using a very inefficient algorithm for solving this.
At this point I think it is better to just get something that works and ignore efficiency for the moment. You have a very powerful computer so let's just waste some cycles, we have plenty of them available so wasting some makes no great difference. Later you can try out different ways of achieving the same outcomes in better ways. For now let's just get something that gets the right answer, even if by a slow and clumsy path.
I am trying to give you something that is focused on being easy for you, the human, to write. If the robot, the computer, has to work a bit harder then that is tough luck for the robot. We are the humans and the robot's job is to make our life easy, not the other way around.
So, knowing we are doing this in a very simplistic way, and one that may be completely discarded and replaced before you even finish this program, let's proceed in a steady path to just getting a simple program that correctly checks for winners finished off and then you can use what you have learned to do better in your next version of this program.
That's right.
Because it returns an integer we can know who won.
If it returns zero, no one has won.
If it returns 1 then player number one has won.
If it returns 2 then player two has won.
Make sense?
You certainly could.
I suggest you use my little test program for now. That will keep it simple and focused on what we want to work on right now. Let's see you add to my base by adding the test for columns next. We will leave diagonals for last.
Give that a try.
Either way will work fine.
Do it however you like best.
I would probably imagine calling this from main() but anything that works is fine.
But let's leave that for later. Let's get a working function that checks for winners first.
Once we have that working we can slot it into the program
We all know how scary this stuff is when you are starting out. We have all been there in one way or another at one point or another. Glad to have you in the community and glad you are finding it useful.
So what next?
1 - Take the excellent advice of 21q by adding a nested for() loop structure to my little test program that fills your 2D array with zeros. Put that in main() before my bit about manually adding test values to the grid.
2 - Next build on the function by adding the test for columns.
3 - When you have those done share your code with us and explain any problems you might be having.
As ButchDean has pointed out we are using a very inefficient algorithm for solving this.
At this point I think it is better to just get something that works and ignore efficiency for the moment. You have a very powerful computer so let's just waste some cycles, we have plenty of them available so wasting some makes no great difference. Later you can try out different ways of achieving the same outcomes in better ways. For now let's just get something that gets the right answer, even if by a slow and clumsy path.
I am trying to give you something that is focused on being easy for you, the human, to write. If the robot, the computer, has to work a bit harder then that is tough luck for the robot. We are the humans and the robot's job is to make our life easy, not the other way around.
So, knowing we are doing this in a very simplistic way, and one that may be completely discarded and replaced before you even finish this program, let's proceed in a steady path to just getting a simple program that correctly checks for winners finished off and then you can use what you have learned to do better in your next version of this program.
goldfish, on 27 February 2011 - 11:25 AM, said:
Your checkForWinner()function will determine IF there's a winner and WHO is the winner, right?
That's right.
Because it returns an integer we can know who won.
If it returns zero, no one has won.
If it returns 1 then player number one has won.
If it returns 2 then player two has won.
Make sense?
goldfish, on 27 February 2011 - 11:25 AM, said:
In order to make this fully work, I need to expand it to check columns and diagonals also? Could I use a nested if for columns and diagonals?
You certainly could.
I suggest you use my little test program for now. That will keep it simple and focused on what we want to work on right now. Let's see you add to my base by adding the test for columns next. We will leave diagonals for last.
Give that a try.
goldfish, on 27 February 2011 - 11:25 AM, said:
Ah... why is it int numberOfRows instead of int 3? I remember reading passing 2D array to function and it showed like
void showArray(int array[][COLS], int rows) that I did not fully understand.
can numberOfRows be omitted and just use int there or is it needed?
void showArray(int array[][COLS], int rows) that I did not fully understand.
can numberOfRows be omitted and just use int there or is it needed?
Either way will work fine.
Do it however you like best.
goldfish, on 27 February 2011 - 11:25 AM, said:
I understand this function returns value winningPlayer. Is it best to call this function from main() or another function like playerInput?
I would probably imagine calling this from main() but anything that works is fine.
But let's leave that for later. Let's get a working function that checks for winners first.
Once we have that working we can slot it into the program
goldfish, on 27 February 2011 - 11:25 AM, said:
I have been working on this Thursday and Friday by myself with almost no progress and my desperation got me registered on this site. I was really intimidated and afraid to let people know how little I know but you guys have been so kind and helpful. Thank you so much.
We all know how scary this stuff is when you are starting out. We have all been there in one way or another at one point or another. Glad to have you in the community and glad you are finding it useful.
So what next?
1 - Take the excellent advice of 21q by adding a nested for() loop structure to my little test program that fills your 2D array with zeros. Put that in main() before my bit about manually adding test values to the grid.
2 - Next build on the function by adding the test for columns.
3 - When you have those done share your code with us and explain any problems you might be having.
|
|

New Topic/Question
Reply





MultiQuote






|