Skip to main content
GameDev.net gamedev.net
🔒 Locked

I'm relatively new to C++, having trouble with remembering stuff.

Started by GraySnakeGenocide Nov 20, 2009 at 5:47 PM 8 replies 1.3k views
Original Post
GraySnakeGenocide
GraySnakeGenocide
I am trying to follow the tutorials at Xoax.net, I'm currently on lesson 9, which is Tic Tac Toe, using if/if else statements, do while, and the standard variables, to make a simple version. I get how the logic works, my problem is remembering what variables go where, and when to change them to true/false/etc. And when certain things are put up. Is it a bad thing if I basically have to lay out every single little thing out for myself, like a cheat sheet? Because I just can NOT remember everything. I just think it'd be bad because I doubt most professional programmers have to do the same thing. The Bolded parts are what I tend to forget most

#include <iostream>

void main() {
	char cSquare1('1');
	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 {
		// Print 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;

		// Set player marker: Player 1 uses X and Player 2 uses O
		char cPlayerMark;
		if (iPlayerTurn == 1) {
			cPlayerMark = 'X';
		} else {
			cPlayerMark = 'O';
		}
		
		// Prompt the player for a move
		std::cout << "Player" << iPlayerTurn << "'s move:" << std::endl;
		bool bValidMove;

		// Loop until we get a valid move
		do {
			char cNextMove;
			std::cin >> cNextMove;
			bValidMove = true;

			// Check for a valid move
			if (cNextMove == '1' && cSquare1 == '1') {
				cSquare1 = cPlayerMark;
			} else if (cNextMove == '2' && cSquare2 == '2') {
				cSquare2 = cPlayerMark;
			} else if (cNextMove == '3' && cSquare3 == '3') {
				cSquare3 = cPlayerMark;
			} else if (cNextMove == '4' && cSquare4 == '4') {
				cSquare4 = cPlayerMark;
			} else if (cNextMove == '5' && cSquare5 == '5') {
				cSquare5 = cPlayerMark;
			} else if (cNextMove == '6' && cSquare6 == '6') {
				cSquare6 = cPlayerMark;
			} else if (cNextMove == '7' && cSquare7 == '7') {
				cSquare7 = cPlayerMark;
			} else if (cNextMove == '8' && cSquare8 == '8') {
				cSquare8 = cPlayerMark;
			} else if (cNextMove == '9' && cSquare9 == '9') {
				cSquare9 = cPlayerMark;
			} else {
				std::cout << "Invalid Move. Try again." << std::endl;
				bValidMove = false;
			}
		} while (!bValidMove);

		bGameOver	= false;
		bool bWinGame	= true;

		// Check for end of game conditions
		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 (cSquare4 == cSquare5 && cSquare6 == cSquare5) {
				bGameOver = true;
			}
			if (cSquare3 == cSquare5 && cSquare7 == cSquare5) {
				bGameOver = true;
			}
		}
		if (cSquare9 != '9') {
			if (cSquare3 == cSquare9 && cSquare6 == cSquare9) {
				bGameOver = true;
			}
			if (cSquare7 == cSquare9 && cSquare8 == cSquare9) {
				bGameOver = true;
			}
		}
		// Need to check the board full (no-win condition)
		if (cSquare1 != '1' && cSquare2 != '2' && cSquare3 != '3' &&
			cSquare4 != '4' && cSquare5 != '5' && cSquare6 != '6' &&
			cSquare7 != '7' && cSquare8 != '8' && cSquare9 != '9' && !bGameOver)
		{
			bGameOver = true;
			bWinGame = false;
		}

		if (bGameOver) {
			if (bWinGame) {
				std::cout << "Player" << iPlayerTurn << " wins!" << std::endl;
			}
			// Print ending 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;

			if (cPlayAgain == 'y') {
				bGameOver = false;
				// Clear the board
				cSquare1 = '1';
				cSquare2 = '2';
				cSquare3 = '3';
				cSquare4 = '4';
				cSquare5 = '5';
				cSquare6 = '6';
				cSquare7 = '7';
				cSquare8 = '8';
				cSquare9 = '9';
			}
			iPlayerTurn = 1;
		} else {
			// Alternate player turns
			if (iPlayerTurn == 1) {
				iPlayerTurn = 2;
			} else {
				iPlayerTurn = 1;
			}
		}
	} while (!bGameOver);

nhold
nhold
Do you mean what variable types go where? Or what variables go where?

I don't really get your question.

also, that naming convention...cSqaure1, cSqaure2 etc. Is pretty bad, you want your variable names to mean something. For example:

char TicTacGrid[9]; // Yay 9 elements of a char.int CurPlayersTurn; // This could just be a bool (there isn't more then 2 players in Tic Tac Toe.)bool IsGameOver; // false means it isn't over!bool IsGameWon; // false means it isn't won!


If you still wanted to you could chuck the variables type in front.
Engineering Manager at Deloitte Australia
Acef
Acef
I think this is common with most beginners because because they don't yet 'see' the effect their code has on the program. Our brains suck at remembering things, but they are the best at recognizing patterns. I think after some practices you'll get the hang of it. Either way, no one memorizes everything their code does. I can tell you that everyone has to read the comments they left for themselves at one point or another.
GraySnakeGenocide
GraySnakeGenocide
That may be part of my problem, I am just going by the tutorial.

My problem is, I tend to forget when to put certain variables down, also when to change certain variables, if that makes any more sense.
rip-off
rip-off
That comes with practise. Try to write lots of programs, small ones. So instead of maybe copying code from tutorials, try to write the program from scratch on your own (as much as possible). Use the tutorial as a reference when things don't go to plan.

What would help your program is breaking it into lots of small functions. Each function will only be a couple of lines long, and have a small set of variables to work with. You may not have come across writing your own functions yet.
GraySnakeGenocide
GraySnakeGenocide
I haven't reached functions yet.

I've tried a million tutorials.

I was mainly looking for tutorials that would teach me how to create games, as it would catch my interest easier.

These games on xoax.net are simple DOS Window games, but i've gotta start somewhere.

I tried an absolute noon spoonfeed tutorial on YT, by the user Anti-RTFM. But his tutorials were 10 minutes per video, 70 + videos....He allllllllways tallllllllllked like thiiiiiiiiiis and it was so dragged on and boring I couldn't bother watching anymore.
Washu
Washu
I would highly recommend you get a book on the subject, tutorials will not teach you C++.

You can also use our helpful C++ Workshop that was hosted a while ago.

I should note that C++ is a hard language to learn, and certainly not a good one to learn as your first language. If you're looking to get up and going with writing games, you would be better off starting with Python/PyGame or C# and XNA.

THere's also plenty of free frameworks, like Unity, that will let you get up and running and creating games while you learn to program on the side. This lets you get the ideas down and then implement them as you become more advanced in programming.
In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.
The Communist Duck
The Communist Duck
Quote:
Original post by GraySnakeGenocide
I haven't reached functions yet.

I've tried a million tutorials.

I was mainly looking for tutorials that would teach me how to create games, as it would catch my interest easier.

These games on xoax.net are simple DOS Window games, but i've gotta start somewhere.

I tried an absolute noon spoonfeed tutorial on YT, by the user Anti-RTFM. But his tutorials were 10 minutes per video, 70 + videos....He allllllllways tallllllllllked like thiiiiiiiiiis and it was so dragged on and boring I couldn't bother watching anymore.

If you're wanting to learn through making games, this book is very good.
Beginning C ++ Through Game Programming
-TCD.
Perost
Perost
Another good book is "Thinking in C++" by Bruce Eckel, which has been made available as a free download here. But as Washu said, perhaps you should look at other alternatives than C++ if you only want to create games.
Slateboard
Slateboard
I would recommend C# from my personal experience.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.