| molecularman (107) | |||
|
I have to make a tic-tac-toe game with a 1D array. I was told to make the array type char. This is my code so far
I'm not sure how to manipulate the gameboard so that I can display the gameboard after each time a player makes a choice. I wanted to use the array elements to print out the gameboard, but I'm not sure how to do that or if that is even the best way to do it. Also I'm not sure how to check to see if a player won or if it was a draw. Would I use a switch with a condition such as (1==2==3==true)? So would I make a condition for all 8 possible winning combinations? Would I have to use 16 conditions to check for both player X and player O? | |||
|
|
|||
| LowestOne (895) | |||
Show board is generally: cout << a[0] << "|" << a[1] << "|" << a[2] << endl; //... You don't need to pass anything else to that function, the contents in the array are blank (or a number to show what input means what), and the 'x' or 'o' that might exist there.
Heavens no. I used a switch for the "game state" (continue, draw, winner), but that was about it as far as switches went.
Yes. | |||
|
Last edited on
|
|||
| molecularman (107) | |
|
when I used cout to display the specific array elements I got weird characters when in the output. Do anybody know that is occurring? How do I make it so the showboard function displays an updated version of the gameboard after each selection by a player? | |
|
Last edited on
|
|
| Jacky Billards (5) | |
|
That's the link to another person having a problem with their code. They are making an array that has tictactoe as well. His problem however is that he can't display "winner" You guys each have a problem with a similar project, maybe you can help each other. Lol, maybe you guys are even working on the same project for the same class or something of that nature | |
|
|
|
| molecularman (107) | |
|
Thanks. That person is using a bool function for some of his or her functions. I'm not sure f that is the best option. I think my only problem right now is that I don't know how to manipulate and show the gameboard after each selection. I would really appreciate if someone could help so I don't end up wasting a ton of time I don't have. | |
|
|
|
| molecularman (107) | ||||
I edited my code. My output is beneath the code. The problem seems to be that if I enter one number that array element is not changed until the next player (playerO) types a number. No matter what number playerO types the array element of the first number entered is changed to X. Does anybody know why this is occuring and how it can be fixed?
| ||||
|
Last edited on
|
||||
| drognisep (42) | |||
Try using a globally declared gameboard, that way you can assign values to the individual element you want, I don't think you can assign values like "a={1,2,3...etc};" after declaration. Try
Also, you have to declare variables before you use them (in 'get_input' 'i' isn't declared) | |||
|
Last edited on
|
|||
| molecularman (107) | |
|
For my class I don't think I'm allowed to globally declare variables. I tried that anyway though, I still have the same issue. I'm thinking it might be because I'm calling the function showboard twice inside one for loop. I declared "i" within the for loop. Is that a bad technique? | |
|
Last edited on
|
|
| molecularman (107) | |||
I changed the get_input function and I'm still getting the same issue. I changed the for loop to a while loop because I didn't want to the game to keep going if someone won. I don't think what i did eliminates that issue though.
| |||
|
Last edited on
|
|||
| molecularman (107) | |
| Can anybody help. I'm losing so much time to this. I think it's something simple I can't see since I don't have a lot of experience. | |
|
Last edited on
|
|
| LowestOne (895) | ||||
Tic-Tac-Toe seems to be the "beginner" game of choice (I know it was for me). I think you have all of the code that you need, the problem is that it is in the wrong places. The simplest way seems to be to declare a bool to determine whos turn it is:
showboard() does nothing to the array, it only shows it. | ||||
|
Last edited on
|
||||
| molecularman (107) | |
|
What would I use for the while loop to execute? Edit: nevermind this. I thought the first while loop was necessary and the code you was just a function that would be part of a program. For what I have to do the showboard function has to show the gameboard at the beginning of the game, after each player choice, and at the end of the game. I need something to manipulate the array. The code is due at 11 pm. | |
|
Last edited on
|
|
| drognisep (42) | |
|
Yeah, that makes sense, you just need to separate your if else chain, so if it's playerX's turn it only checks playerX's winning conditions. The way you have it it's checking both at the same time. Since PlayerX is checked first, PlayerO is ignored. Just pass an extra parameter to winning to ensure your function knows which player is being checked. Also, put the code for displaying the board after the array is set. EDIT: Seperate the if else statements in showboard. :) | |
|
Last edited on
|
|
| molecularman (107) | |
|
I'm a little lost between what you two guys are saying. I need showboard to be able to show the board at the beginning, and end, and after each choice. So showboard has to meet conditions/qualifications. I don't know how to separate the winning conditions. I need to get input from player X, then manipulate the array, then show the board, then get input from player O, then manipulate the array, then show the board. I need to continue that until there is a winner than I show the board one last time (or I guess just leave the board showing) and then tell who won or if it was a draw. Are you guys saying to create another function that manipulates the gameboard and just use showboard to display the changed board? | |
|
Last edited on
|
|
| drognisep (42) | |
| Basically, instead of passing both PlayerX and PlayerO to showboard, just pass a parameter that says which player is setting a mark (bool?). You are displaying the board before setting what changes the player made. | |
|
|
|
| molecularman (107) | |
|
I tried so many combos of the if else statements in showboard. I can't think of that extra parameter, either because the deadline is 11 pm and I'm rushed or I just can't code it. I feel like I'm close though and what to get this. I told see how in LowestOne's code player O will ever be prompted for input. | |
|
Last edited on
|
|
| drognisep (42) | |||
You don't have to change winning... that was a typo.
Or something like that | |||
|
Last edited on
|
|||
| molecularman (107) | |||
Alright, I have this now. The compiler is telling me choice is being used without being initialized. What would I initialize it too?
| |||
|
|
|||
| molecularman (107) | |
|
drognisep, can you help me finish this up. I have a little over 10 minutes left. I can't even compile now. I don't think there is anyway I can get this done in 10 minutes. Amazing how the hours just pass by and there is so much work still to do. I tried just initializing choice to 1 just to get something going and now after hitting any key I get an error that says whichplayer is being used without being initialized. | |
|
Last edited on
|
|
| molecularman (107) | |
| . | |
|
Last edited on
|
|