Widget360 here, posting again. I am creating the tic tac toe game for a simple beginner tutorial. However I have a question about the actual gameplay scripting for it( like everyting about it ). Here is what I got so far. ( PS: i am using Bloddshed Dev-C++ )
Of course this is just me trying to make it beutifal. Im sure you can also tell I like to be organized ( hint: all my int's ). Im pretty sure it has to do with imputing variables or something. Any help will be apreciated!
- Make Gameboard return void as it is not beneficial to return 0
- Create a for loop within Gameboard to write the board
- Pass an array containing the game board to Gameboard so you can change it. You may need a pointer or you could make the array global (although others will dislike that method)
There's more you could do to make it more organized but that would be a big improvement.
I should have included hte fact i am lower than a beginer. I look at these tutorials however all i see are loops for numbers. Am i suposed to say that x = ( whatever my gameboard is ) and then say
int x = 10?
here is the for loop i am looking at
1 2
for (int n=10; n>0; n--) {
cout << n << ", "; }
From what i am getting, i would put my gameboard betweeng the " " 's
A i have always said. I am superb at looking at a code and defining it han paragraphs of explanations. So a example or link to one would be nice. Thanx
little note, i believe you're trying to declare a function/ void in lines 11, 23 and 31. These declarations should be done outside of main.
and also you want to have a } at the end of line 23 (to end the title function, which would now come outside of main ;)
And i don't see where this loop is at, but basically you'd show:
For the for loop, how would i use that.
As kaduuk said, " I dont see where this loop is" I dont have one. I know hoe to make numbers count up and then stop. But how do i change that from numbers to the game board, What do i do? All loop examples i see are using integers. I have seen some whisper of something called the "exit function". As I said, my second commet shows the for loop i am studying. Thanx
#include <iostream>
usingnamespace std;
int main (void)
{
int Title ();
{
cout<< " The Tic Tac Toe Game" << " " << " By: Widget360\n";
cout<< "\n";
cout<< "Let The Games Begin!!! \n";
cout<< "\n"; }
int Playeronename ();
{
char question[] = "Player 1, Enter your name: ";
char greeting[] = "Hello: ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!"; }
cout<<"\n";
cout<<"\n";
cout<<"\n";
cin.get();
int Playertwoname ();
{
char question[] = "Player 2, Enter your name: ";
char greeting[] = "Hello: ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!"; }
int Gameboard ();
{
for (int n=3; n>0; n--) {
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<"-----1-----1-----\n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<" 1 1 \n"; }
cin.get(); }
cin.get();
return 0;
}
As shown, I have 3 boards.
Now how do i let it let me inter a variable before looping. Is it the " go to " or "continue" statement?
*** Important- as of right now, i have no gameplay code, meaning i havent written the code to input my x's and o's. Some tips on that will now be apreciated !!*******
Okay, Maybe my question was a little confusing but i want to know is why i am forced to enter a numeric value for cin<<a
if i try to enter a letter such as "x or o" then it gives me some weird number and closes automatically. OR can i declare that maybe 1 = x? then i could have a number for each tic tac toe slot; when i enter that slot number in, it puts the desired letter there.
I hope you can answer my question.
#include <iostream>
usingnamespace std;
int main ()
{
int Title ();
{
cout<< " The Tic Tac Toe Game" << " " << " By: Widget360\n";
cout<< "\n";
cout<< "Let The Games Begin!!! \n";
cout<< "\n"; }
int Playeronename ();
{
char question[] = "Player 1, Enter your name: ";
char greeting[] = "Hello: ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!"; }
cout<<"\n";
cout<<"\n";
cout<<"\n";
cin.get();
int Playertwoname ();
{
char question[] = "Player 2, Enter your name: ";
char greeting[] = "Hello: ";
char yourname [80];
cout << question;
cin >> yourname;
cout << greeting << yourname << "!"; }
int Gameboard ();
{
for (int n=1; n>0; n--) {
cout<<"\n";
cout<<"\n";
cout<<"\n";
int x = 1;
cout<<"Player 1, Place Your X or O!\n";
cin >> x;
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<"\n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
if ( x == 1 ) {
cout<<" x 1 1 \n";}
if ( x == 2 ) {
cout<<" o 1 1 \n";}
if ( x == 3 ) {
cout<<" 1 x 1 \n";}
if ( x == 4 ) {
cout<<" 1 o 1 \n";}
if ( x == 5 ) {
cout<<" 1 1 x \n";}
if ( x == 6 ) {
cout<<" 1 1 o \n";}
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<"-----------------1-----------------1-----------------\n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<"-----------------1-----------------1-----------------\n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cout<<" 1 1 \n";
cin.get(); }
cout<<"Well, Done!\n";
cin.get();
}
cin.get();
return 0;
}
I need to loop my board after i enter the first letter. How do i make it loop and show whatever variable i have already entered. If anybody need's help explaining what I'm trying to do, TELL ME!!!
(PS: odd numbers are x's and evens are o's;
the second and third row of the board i have not entered yet. But i am working on it.)
EDIT to make more sense: The code you provide shows that you don't have a running environment working because this won't compile. What beginner tutorial are you looking at? I would suggest you start from a book or our tutorial here if you don't like the pricing of a book. I would suggest you start over.
#include <iostream.h>
#include <conio.h>
int main ()
{
usingnamespace std;
system ("title Tic Tac Toe Game by CPPProgrammer4L");
// Spaces for the play mat
char cSpaces [] = {'1', '2', '3', '4', '5', '6', '7', '8', '9'};
// Bool for game over, we set this to false
bool bGameOver = false;
// Int for whos turn it is, 1 for first player, 2 for second player
int iPlayerTurn = 1;
// Int for the current move
int iInput;
// Game loop
while (!bGameOver)
{
// Display the play mat
cout << cSpaces [0] << " | " << cSpaces [1] << " | " << cSpaces [2] << endl;
cout << "---------" << endl;
cout << cSpaces [3] << " | " << cSpaces [4] << " | " << cSpaces [5] << endl;
cout << "---------" << endl;
cout << cSpaces [6] << " | " << cSpaces [7] << " | " << cSpaces [8] << endl << endl;
// Get the move
cout << "Player " << iPlayerTurn << "'s Move: ";
cin >> iInput;
// Clear the screen
system ("cls");
// If the selection was already used then continue
if (cSpaces [iInput] == 'x' || cSpaces [iInput] == 'o')
{
continue;
}
// Go to the next player
iPlayerTurn++;
// If we go to player three go back to player one
if (iPlayerTurn == 3) iPlayerTurn = 1;
// Set the input to the play mat space
if (iPlayerTurn == 1)
cSpaces [--iInput] = 'x';
else
cSpaces [--iInput] = 'o';
// Check if the game is over
if (cSpaces [0] == cSpaces [1] && cSpaces [2] == cSpaces [0])
break;
elseif (cSpaces [3] == cSpaces [4] && cSpaces [5] == cSpaces [3])
break;
elseif (cSpaces [6] == cSpaces [7] && cSpaces [8] == cSpaces [6])
break;
elseif (cSpaces [0] == cSpaces [3] && cSpaces [6] == cSpaces [0])
break;
elseif (cSpaces [1] == cSpaces [4] && cSpaces [7] == cSpaces [1])
break;
elseif (cSpaces [2] == cSpaces [5] && cSpaces [8] == cSpaces [2])
break;
elseif (cSpaces [0] == cSpaces [4] && cSpaces [8] == cSpaces [0])
break;
}
// Set the correct player winner
if (iPlayerTurn == 2) iPlayerTurn = 1;
else iPlayerTurn = 2;
// Print a exit message and winner
cout << "Winner: " << iPlayerTurn << endl << "Thank you for playing CPPProgrammer4L's Tic Tac Toe Game!" << endl;
getch ();
// Return from the main function and exit the program
return 0;
}
As for computer quip: I am not using a tutorial and it run's and compiles fine on dev-c++.
As for CPPProgramer4L: Thanx, UR code does have some bugs but it has shown me some things i have never seen. For ex; bool and the fact u can define your char in the beggining and then use it later and not immediatley after stateing it.
2 questions:
on lines 14,42. Is that all you have to put down to intiate switching between turn's. And how did you state their are only 2 players.
Lastley; lines 12,19. Is that you'r entire loop. That looks so easy. Correct me if im worng. you stated that you continue playing until on of the lines between 54-67 are true? That's pretty sick.
You should read a tutorial to understand how things work, try going to http://Youtube.com and search "Xoax C++ 0" and watch his tutorials, there are only 49 of them and you will learn a lot.
Question One: Basically yeah, but there is a few more lines in there (Like 47 - 52) that we use for the player turn.
Question Two: My game loop goes from 19 to 68. And it will loop around for ever but I added a few checks at 53 - 67 to see if the game is over, if it is then we break from the loop.
Out of the loop the variable iPlayerTurn is set to the loosing player so on lines 70 - 72 I set the correct winner and then display a quit message and the winner on lines 74 - 76.
If you have any more questions let me know.
P.S: Looking over the code I saw that I didn't add a check for a draw game, but I am sure you will be able to do this if you want, let me know if you can't and I will help.
Please consider using the string class and getline() for this.
Your code structuring doesn't seem to make any sense to me, either. It seems like you enter the main function, define functions in them (but you still put semicolons behind them) and then hope it works. Or you use functions you did not show us, but then, what are the data specifying prefixes for? Mind telling us what you expect this code to do, per line?