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.
//
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
//Game Outline
//Players Enter 1-9 for each square
//Player 1 always moves first
//Player 1 uses X, Player 2 uses O
//Run Variables
//Loop
//-Display the current board
//-Make a Movie
//-Check Who Wins
//-Want to Play again?
//Menu
//Want to play Tic Tac Toe
//Player Chooses Yes or No
//Game Starts and Displays Board
//Menu Choice
//Set Player Marker, Player 1 uses X and Player 2 uses O
char PlayerMarker;
if (PlayerTurn = 1){
PlayerMarker = 'X';
}else{
PlayerMarker = 'O';
}
//Start the game, asking for each Player for a move
//Bool Variable in order to check if the move is valid
bool ValidTurn;
//Run a do while loop to check if their hasn't been a square filled in or
do{
//Char Variable to get next move
char CurrentMove;
cout << "Player" << PlayerTurn << "'s turn, set move on what square: " << endl;
cin >> CurrentMove;
ValidTurn = true;
//If Statment Checking if the move is invalid
//If the move equals 1-9 and a number, and if no one has marked the space yet
//Checks Each Square
if (CurrentMove == '1' && Square1 == '1') {
Square1 = PlayerMarker;
} else if (CurrentMove == '2' && Square1 == '2') {
Square2 = PlayerMarker;
} else if (CurrentMove == '3' && Square1 == '3') {
Square3 = PlayerMarker;
} else if (CurrentMove == '4' && Square1 == '4') {
Square4 = PlayerMarker;
} else if (CurrentMove == '5' && Square1 == '5') {
Square5 = PlayerMarker;
} else if (CurrentMove == '6' && Square1 == '6') {
Square6 = PlayerMarker;
} else if (CurrentMove == '7' && Square1 == '7') {
Square7 = PlayerMarker;
} else if (CurrentMove == '8' && Square1 == '8') {
Square8 = PlayerMarker;
} else if (CurrentMove == '9' && Square1 == '9') {
Square9 = PlayerMarker;
} else {
cout << "Invalid Move, make another one:" << endl;
ValidTurn = false;
}
} while(!ValidTurn);
// GameOverDecider Bool Variable now set to false, showing the game is over
GameOverDecider = false;
// New Bool variable
bool WinGame = true;
//Win Condition that's through either square 1, 4, and 7 or 1, 2, and 3
if(Square1 != '1'){
//1, 2, and 3
if (Square2 == Square1 && Square3 == Square1) {
GameOverDecider = true;
}
//1, 4, and 7
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:
Code:
#include <ctype.h>
//...
//Board Variables
char Square[] = "123456789";
//...
int actualPos = CurrentMove - '0'; // convert character to integer
if (isdigit( CurrentMove ) && (CurrentMove == Square[actualPos - 1] ))
Square[actualPos - 1] = PlayerMarker;
else
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.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; November 27th, 2011 at 06:12 PM.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.