Sign in to follow this  
RussDawgUnlimited

Am I doing something wrong?

Recommended Posts

So I'm starting to learn me some programming, and I decided to start with C++ as its the most prominent, Ive been watching some tutorials and reading up on some books i found, did a program from a tutorial from Xoax.net ([url="http://www.youtube.com/playlist?list=PLA68C1F33757B4A38&feature=plcp"]http://www.youtube.c...38&feature=plcp[/url]) and I attempted to copy his game (Lesson 9 Tic Tac Toe) to see if I have an understanding of what Im doing (lol) I understand the program, but when i generate this code it comes up w/ an error msg: "The program cannot find the file specified." I've tried looking into it but it seems to be an error w/ Visual C++ Basic (2010) not being able to create a .exe file for the program, I've tried to reinstall multiple times but to no avail, any ideas? Or is it because of an error in my code? I've compared it to the Xoax code and I didnt see anything different...

Heres my code for a tic tac toe game in the console:

[source]#include <iostream>
int main() {
char cSquare1('1'); // Why not int? Char for letters/int for #'s?
char cSquare2('2');
char cSquare3('3');
char cSquare4('4');
char cSquare5('5');
char cSquare6('6');
char cSquare7('7');
char cSquare8('8');
char cSquare9('9');
int iPlayerTurn(1);
bool bGameOver(true);


//Main Game Loop
do {
// Board
std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;
//Player Mark 1 or 2
char cPlayerMark;
if (iPlayerTurn == 1) {
cPlayerMark = 'X';
} else {
cPlayerMark = 'O';
}
//Player Move
std::cout << "Player " << iPlayerTurn << "'s Turn" << std::endl;
bool bValidMove;
//needs valid move, if no valid move then loop until valid
do {
char cNextMove;
std::cin >> cNextMove;
bValidMove = true;
if (cNextMove == '1' && cSquare1 == '1') {
cSquare1 = cPlayerMark;
} else if (cNextMove == '2' && cSquare2 == '2') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '3' && cSquare3 == '3') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '4' && cSquare4 == '4') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '5' && cSquare5 == '5') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '6' && cSquare6 == '6') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '7' && cSquare7 == '7') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '8' && cSquare8 == '8') {
cSquare2 = cPlayerMark;
} else if (cNextMove == '9' && cSquare9 == '9') {
cSquare2 = cPlayerMark;
} else {
std::cout << "INVALID GO AGAIN" << std::endl;
bValidMove = false;
}
} while (!bValidMove);
bGameOver = false;
bool bWinGame = true;
// check if game over
if(cSquare1 != '1') {
if (cSquare2 == cSquare1 && cSquare3 == cSquare1) {
bGameOver = true;
}
if (cSquare4 == cSquare1 && cSquare7 == cSquare1) {
bGameOver == true;
}
}
if(cSquare5 != '5') {
if (cSquare1 == cSquare5 && cSquare9 == cSquare5) {
bGameOver == true;
}
if (cSquare2 == cSquare5 && cSquare8 == cSquare5){
bGameOver == true;
}
if (cSquare3 == cSquare5 && cSquare7 == cSquare5){
bGameOver == true;
}
if (cSquare4 == cSquare5 && cSquare6 == cSquare5){
bGameOver == true;
}
}
if(cSquare9 != '1'){
if(cSquare3 == cSquare9 && cSquare6 == cSquare9){
bGameOver == true;
}
if(cSquare7 == cSquare9 && cSquare8 == cSquare9){
bGameOver == true;
}
}
//Check for No Win Condition
if(cSquare1 != '1' && cSquare2 != '2' && cSquare != '3' &&
cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver)
{
bGameOver = true;
bWinGame = false;
}
//Declare Winner
if(bGameOver)
{
if(bWinGame){
std::cout << "Player" << iPlayerMove << " is the victor!!" << std::endl;
}
//Print Endgame Board
std::cout << cSquare1 << "|" << cSquare2 << "|" << cSquare3 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare4 << "|" << cSquare5 << "|" << cSquare6 << std::endl;
std::cout << "-+-+-" << std::endl;
std::cout << cSquare7 << "|" << cSquare8 << "|" << cSquare9 << std::endl;
std::cout << "Game Over" << std::endl;
std::cout << "Play Again? (y/n)" << std::endl;
char cPlayAgain;
std::cin >> cPlayAgain;
//Play Again? Yes
if(cPlayAgain == 'y'){
//Clear Board
bGameOver = false;
cSquare1 = '1';
cSquare2 = '2';
cSquare3 = '3';
cSquare4 = '4';
cSquare5 = '5';
cSquare6 = '6';
cSquare7 = '7';
cSquare8 = '8';
cSquare9 = '9';
}
iPlayer1 = 1;
} else {
//Alternate Player Turns
if(iPlayerTurn == 1) {
iPlayerTurn == 2;
} else {
iPlayerTurn == 1;
}
}
} while (!bGameOver);[/source] Edited by swiftcoder
Added source tags.

Share this post


Link to post
Share on other sites
It probably can't find the file because your code is incorrect, and hence it can't compile it into anything to run.

Some problems:[list]
[*]It looks like you're missing a closing brace ('}') for the main() function (could just be a copy 'n' paste error)
[*]Trying to compile the code, I get the errors (after adding a closing curly brace at the end):
[list]
[*]t.cpp:96: error: ‘cSquare’ was not declared in this scope
[list]
[*]Solution: you probably forgot a '3' at the end of 'cSquare' on line 96
[/list][*]t.cpp:107: error: ‘iPlayerMove’ was not declared in this scope
[list]
[*]Solution: there is no iPlayerMove variable declared (you probably meant to say iPlayerTurn?)
[/list][*]t.cpp:133: error: ‘iPlayer1’ was not declared in this scope
[list]
[*]Solution: there is no iPlayer1 variable declared (you probably meant to say iPlayerTurn?)
[/list]
[/list]
[/list]

You should look into using arrays. They'll simplify your code greatly.

[edit]

Removed re-formatted code. Edited by Cornstalks

Share this post


Link to post
Share on other sites
[quote name='Cornstalks' timestamp='1347586978' post='4979906']
You should look into using arrays. They'll simplify your code greatly.

[edit]

[/quote]

Please elaborate briefly, arrays? (still learning so forgive me, I've only just started a few weeks ago, got a few books i read in my spare time)

SwiftCoder, apologies, i'll keep that in mind next time. Edited by RussDawgUnlimited

Share this post


Link to post
Share on other sites
[quote name='RussDawgUnlimited' timestamp='1347588266' post='4979915']
[quote name='Cornstalks' timestamp='1347586978' post='4979906']
You should look into using arrays. They'll simplify your code greatly.

[edit]

[/quote]

Please elaborate briefly, arrays? (still learning so forgive me, I've only just started a few weeks ago, got a few books i read in my spare time)
[/quote]
In all honesty, I think [url="https://www.google.com/search?q=c%2B%2B+arrays&sugexp=chrome,mod=18&sourceid=chrome&ie=UTF-8"]google can elaborate better than I can[/url].

[quote name='RussDawgUnlimited' timestamp='1347588266' post='4979915']
SwiftCoder, apologies, i'll keep the [source] in mind next time.
[/quote]
Consider using [code ][/code ] (without the space) tags. You'll actually get syntax highlighting with code tags. Source tags on this site are broken.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this