Just Play With it !!


AI(Artificial intelligence)Artificial intelligence that is What I type before but when I type it I don’t Know anything about AI and What we Can do With it so I Try to be more professional tonight and break it with XO game or like they say tic-tac-toe in the beginning thinking in games it’s efficient way to learn some thing and to be more comfortable with it to be creative in this thing so let’s start with XO I’ll make 1 to 9
square to make user select between it to the player 1 or player 2

That is the code for that:-


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);

and thinking in XO 3 Condition to Win the first When square1 not equal 1 equal X or O in that if his row or his column is equal and the second one When square5 not equal 5 equal X or O in that 4 paths path to 5 well make the end of the game with win someone the last Condition in Wining the game When square9 not equal 9 equal X or O in that if his row or his column is equal the player is win in the 3 condition

Fail Condition thinking When all the square is not equal to it’s number and the game is still running so that is fail condition when no one is when

that is it in the all of the game you must of course initializing the game board and do the loop for all condition Until the game is still running and to the code the all code is here take a look :-


#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:" <> 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)?" <> 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);
}

Any question I’m ready to answer it in any time.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s