My first program.

Posts 115 of 18 · Page 1 of 2
My first program.
I've watched tutorials and read on some basic functions of C++

So I am attempting to make a console tic-tac-toe game.

Here is the code:
Code:
#include <iostream>
void main()
{
	char SquareA ('a');
	char SquareB ('b');
	char SquareC ('c');
	char SquareD ('d');
	char SquareE ('e');
	char SquareF ('f');
	char SquareG ('g');
	char SquareH ('h');
	char SquareI ('i');
	int playerturn(1);
	bool gameover(true);

	//game
	do
	{
		//prints the game board that we shall be playing our game.

			std::cout << SquareA << "|" << SquareB << "|" << SquareC << std::endl;
			std::cout << "-+-+-" << std::endl;
			std::cout << SquareD << "|" << SquareE << "|" << SquareF << std::endl;
			std::cout << "-+-+-" << std::endl;
			std::cout << SquareG << "|" << SquareH << "|" << SquareI << std::endl;
			// create teh marks
			char playermark;
			if (playerturn == 1) {
				playermark = 'X';
			} else {
				playermark = 'O';
			}
			// turn prompt
			std::cout << "Player" << playerturn << "'s turn:" << std::endl;
			bool ValidMove;
			// loops 'til valid
			do {
				char nextmove;
				std::cin >> nextmove;
				ValidMove = true;
				// make sure its valid
				if (nextmove == 'a' && SquareA == 'a') {
					SquareA = playermark;
				} else if (nextmove == 'b' && SquareB == 'b') {
					SquareB = playermark;
				} else if (nextmove == 'c' && SquareC == 'c') {
					SquareC = playermark;
				} else if (nextmove == 'd' && SquareD == 'd') {
					SquareD = playermark;
				} else if (nextmove == 'e' && SquareE == 'e') {
					SquareE = playermark;
				} else if (nextmove == 'f' && SquareF == 'f') {
					SquareF = playermark;
				} else if (nextmove == 'g' && SquareG == 'g') {
					SquareG = playermark;
				} else if (nextmove == 'h' && SquareH == 'h') {
					SquareH = playermark;
				} else if (nextmove == 'i' && SquareI == 'i') {
					SquareI = playermark;
				} else {
					std::cout << "What are you? Dumb? Try again." << std::endl;
					ValidMove = false;
				}
			} while (!ValidMove);
			gameover = false;
			bool WinGame = true;
			// Game complete?
		if (SquareA != 'a') {
			if (SquareB == SquareA && SquareC == SquareA) {
				gameover = true;
			}
			if (SquareD == SquareA && SquareG == SquareA) {
				gameover = true;
			}
		}
		if (SquareE != 'e') {
			if (SquareA == SquareE && SquareI == SquareE) {
				gameover = true;
			}
			if (SquareB == SquareE && SquareH == SquareE) {
				gameover = true;
			}
			if (SquareD == SquareE && SquareF == SquareE) {
				gameover = true;
			}
			if (SquareC == SquareE && SquareG == SquareE) {
				gameover = true;
			}
		}
		if (SquareI != 'i') {
			if (SquareC == SquareI && SquareF == SquareI) {
				gameover = true;
			}
			if (SquareG == SquareI && SquareH == SquareI) {
				gameover = true;
			}
		}
		// Tie
		if (SquareA != 'a' && SquareB != 'b' && SquareC != 'c' &&
			SquareD != 'd' && SquareE != 'e' && SquareF != 'f' &&
			SquareG != 'g' && SquareH != 'h' && SquareI != 'i' && !gameover)
		{
			gameover = true;
			WinGame = false;
		}

		if (gameover) {
			if (WinGame) {
				std::cout << "Player" << playerturn << " wins!" << std::endl;
			}
			// end board printed
			std::cout << SquareA << "|" << SquareB << "|" << SquareC << std::endl;
			std::cout << "-+-+-"<< std::endl;
			std::cout << SquareD << "|" << SquareE << "|" << SquareF << std::endl;
			std::cout << "-+-+-"<< std::endl;
			std::cout << SquareG << "|" << SquareH << "|" << SquareI << std::endl;

			std::cout << "Game Over!" << std::endl;
			std::cout << "Want to get owned again? (y/n)?" << std::endl;
			char PlayAgain;
			std::cin >> PlayAgain;

			if (PlayAgain == 'y') {
				gameover = false;
				// Clears all
				SquareA = 'a';
				SquareB = 'b';
				SquareC = 'c';
				SquareD = 'd';
				SquareE = 'e';
				SquareF = 'f';
				SquareG = 'g';
				SquareH = 'h';
				SquareI = 'i';
			}
			playerturn = 1;
		} else {
			// alt player
			if (playerturn == 1) {
				playerturn = 2;
			} else {
				playerturn = 1;
			}
		}
	} while (!gameover);
}
It opens up but when I hit a key and enter it closes. Any suggestions?
-----------------
EDIT: Since the problem is solve Imma upload the file D:

EDIT#2: could a moderator change this from Help to Release?


Virus scan:
http://www.virustotal.com/file-scan/...fb2-1308210799 0/ 42
http://www.virscan.org/report/38b779...f2dfd3a3d.html "Scanners did not find malware!"
TicTacToe.zipattachment deleted · 61 downloads before removal
The loop only runs until gameover = false. Change the while(gameover) to while(!gameover).
Quote Originally Posted by Nico View Post
The loop only runs until gameover = false. Change the while(gameover) to while(!gameover).
Damn it was that last line >.>
It's getting late so I hurried on the last section. I checked all but that part lol.
Thanks a lot.
Quote Originally Posted by Pie View Post
Damn it was that last line >.>
It's getting late so I hurried on the last section. I checked all but that part lol.
Thanks a lot.
Good job! I have so far neglected to finish learning C++, but I will try to learn it soon =P. Good luck with your next program!
See my sign and learn in there
Quote Originally Posted by Lyoto Machida View Post
See my sign and learn in there
Code is too ugly to learn from.
Quote Originally Posted by Lyoto Machida View Post
See my sign and learn in there
I prefer to use book and watch well done and thorough video tutorials.

Quote Originally Posted by Auxilium View Post


Code is too ugly to learn from.
It's a bit "ugly" but that's kinda harsh D:
Quote Originally Posted by Pie View Post
I prefer to use book and watch well done and thorough video tutorials.



It's a bit "ugly" but that's kinda harsh D:
I was talking about my tic tac toe game xD
Quote Originally Posted by Lyoto Machida View Post


I was talking about my tic tac toe game xD
Oh what I'm doing is learning from tutorials (information on what each does) and then taking only what I learned to make that tic-tac-toe game.
---
Yes the only thing I know currently is what I did for that game D:
I faied my programming fundamentals 2 class because I couldn't write the code for a tic tac toe game ;x good job on yours
Great to see you get into C++
Good Job *Gives a Thumbs up*
D3d is where it gets kinda fun.
I don't believe this is your first Program.
Quote Originally Posted by NoShame View Post
I don't believe this is your first Program.
Actually it is. I never did the hello world program. Just watched some C++ videos that taught me those things O_O

EDIT: now i got a bit lazy to continue learning. and now am busy with other shit.
Posts 115 of 18 · Page 1 of 2

Post a Reply

Tags for this Thread

None

Need help?