I'm in the process of making a Tic Tac Toe game, and decided to do a huge loop, or basically to make as simple as possible to make it. But I want to make it simpler, and alot more organized, then the loop mess that I made. So I thought, I would make the whole build into a class, and use the main to run the whole thing. Problem is, I think that the code would look alot more complicated then it should be. Then again, how would I incorporate a class into such a simple game.
Here's the code I have right now, it works by the way
// tictactoeMain.cpp : Defines the entry point for the console application.
using namespace std;
//Players Enter 1-9 for each square
//Player 1 always moves first
//Player 1 uses X, Player 2 uses O
//-Display the current board
//-Make a Movie
//-Check Who Wins
//-Want to Play again?
//Want to play Tic Tac Toe
//Player Chooses Yes or No
//Game Starts and Displays Board
I'm in the process of making a Tic Tac Toe game, and decided to do a huge loop, or basically to make as simple as possible to make it. But I want to make it simpler, and alot more organized, then the loop mess that I made. So I thought, I would make the whole build into a class,
First, use code tags when posting code. The code you posted is almost unreadable
Before even thinking about classes, learn how to use arrays.
I'll ask you -- what if there were 1000 squares instead of 9? Would you create 1000 variables and if() statements? How long would you keep your sanity if you really had to write 1,000 variables, if() statements, and assignments in various places?
Arrays simplify this:
char Square = "123456789";
int actualPos = CurrentMove - '0'; // convert character to integer
if (isdigit( CurrentMove ) && (CurrentMove == Square[actualPos - 1] ))
Square[actualPos - 1] = PlayerMarker;
cout << "Invalid Move, make another one:" <<
Those 5 lines of code replaces everything you did above. Since arrays start at 0, I subtracted 1 from the integer version of CurrentMove to get the correct entry in the array.
Anytime you're repeating code, where the only difference is a couple of values, then you can simplify that code, just as I did above.
Last edited by Paul McKenzie; November 27th, 2011 at 05:12 PM.