Hi
This is my first post here, and ill give some little info about me.
Recently after long term thinking i decided to follow the programmers step since i love to solve problems and mostly the math ones. As well i like create.. i have to say so fur i have enjoyed learning c++.
2 weeks ago i started to learn c++ from a book "Michael Dawson - Beginning C++ Through Game Programming", but as in every book there is no answer to every question.
Now i gave my self a task to recreate the game Tic Tac Toe from the book.
The first time i went really far but somewhere in the middle of the project i got myself stuck and i decided to begin again.
With cleaner pseudo planning and more sophisticated code.
However now i am stuck with this (not enough experience with the c++ syntax so far..) syntax problem.
ok .. so this is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
|
#include <iostream>
#include <vector>
#include <string>
using namespace std;
bool doYouWantToPlayFirst(string question);
int main ()
{
char humanPiece;
char computerPiece;
doYouWantToPlayFirst("Do you want to take the first move? <y/n>: ");
// now here the problems lies! ill explain what i tried after the code finishes....
if (doYouWantToPlayFirst("Do you want to take the first move? <y/n>: ") == true)
{
humanPiece = 'X';
computerPiece = 'O';
}
else
{
humanPiece = 'O';
computerPiece = 'X';
return 0;
}
bool doYouWantToPlayFirst(string question)
{
char first;
cout << question;
do
{
cin >> first;
first = toupper(first);
if ((first != 'Y') && (first != 'N'))
{
cout << "\nEnter 'y' for Yes or 'n' for NO! <y/n>: ";
}
}while ((first != 'Y') && (first != 'N'));
if (first == 'Y')
{
cout << "\nYou got the first move!\n\n";
return true;
}
if (first == 'N')
{
cout << "\nOk, you shall feel the wrath of my AI brain!!\n\n";
return false;
}
}
|
so when i call the function like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ");
if ((doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ")) == true)
{
humanPiece = 'X';
computerPiece = 'O';
}
else
humanPiece = 'O';
computerPiece = 'X';
cout << humanPiece ;
return 0;
}
|
it actually works! But it calls the function twice really (asks the users twice for <y/n> from the first time i call the function and the second time in the if (statment where i just want to compare it).
If i try to call the function like this:
|
if (doYouWantToPlayFirst() == true)
|
it says that the function doesn't take 0 arguments.. so for the doYouWantToPlayFirst(string question) i must pass a value.. ok i understand that.. but how (is it actually possible to only use the return value of the function..
as simple as this: if doYouWantToPlayFirst (returned TRUE) to make something else...
Later on i have found a solution to this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
int main ()
{
char humanPiece;
char computerPiece;
if (doYouWantToPlayFirst("Dali sakas da si prv na poteg? <y/n>: ") == true)
{
humanPiece = 'X';
computerPiece = 'O';
}
else
humanPiece = 'O';
computerPiece = 'X';
cout << humanPiece ;
return 0;
}
|
as you can see in this code i actually only called the function directly in the if statement. But i would still like to know if there is some other way.
Thanks again.
p.s. so far the use of functions are the most confusing in bigger codes (cause i am still not used to them) its actually a bit overwhelming but i DO NOT INTEND TO GIVE UP... like ever ;)
Thanks.