Hi, i made my first TicTacToe via c++, took me about a hour or so,
Has a AI but it is realy easy to beat. detects game ending, and created a class for whole game
source code
Main.cpp
Code:
//Tic-Tac-Toe
//This class is written by Lauri Orgla. a.k.a OrX
//18.07.2011
//
#include "stdafx.h"
#include "GameFrame.h"
int main(){
system("title OrX - TicTacToe 2011");
MFrame NewGame;
NewGame.SetUp();
while(NewGame.GAME = true){
NewGame.Draw();
NewGame.GameState();
NewGame.GetMove();
NewGame.AIMove();
}
system("Pause");
return 0;
}
GameFrame.h
Code:
//Tic-Tac-Toe
//This class is written by Lauri Orgla. a.k.a OrX
//18.07.2011
//
#include <iostream>
class MFrame {
private:
char T[9];
int f[9]; // fields
int a[9]; // allocated
public:
bool GAME;
void SetUp();
void Draw();
int GetMove();
bool IsSlotAvailible(int slotID);
int MakeMove(int Move);
void AIMove();
int CheckForWin();
int GameState();
void ExitGame();
};
void MFrame::Draw (){
int temp = 0;
while(temp <= 9){
if(a[temp] == 1){T[temp] = 'X';}
if(a[temp] == 2){T[temp] = 'O';}
if(a[temp] == 0){T[temp] = (char)(((int)'0')+temp);}
temp++;
}
system("CLS");
std::cout << T[0] << " | " << T[1] << " | " << T[2] << std::endl;
std::cout <<"- + - + -" << std::endl;
std::cout << T[3] << " | " << T[4] << " | " << T[5] << std::endl;
std::cout <<"- + - + -" << std::endl;
std::cout << T[6] << " | " << T[7] << " | " << T[8] << std::endl;
}
void MFrame::SetUp (){
//note allocation status 1 = X and 2 = O
f[0] = 1;
f[1] = 2;
f[2] = 3;
f[3] = 4;
f[4] = 5;
f[5] = 6;
f[6] = 7;
f[7] = 8;
f[8] = 9;
//fields
a[0] = 0;
a[1] = 0;
a[2] = 0;
a[3] = 0;
a[4] = 0;
a[5] = 0;
a[6] = 0;
a[7] = 0;
a[8] = 0;
GAME = true;
}
bool MFrame::IsSlotAvailible(int slotID){
if(a[slotID] == 0){
return true;
}
return false;
}
int MFrame::GetMove(){
int move = 0;
std::cout << std::endl << "Pick a number: ";
std::cin >> move;
if(move >= 0){
if(move <= 9){
if(IsSlotAvailible(move) == true){
return MakeMove(move);
}
}
}
std::cout << std::endl << "You cant choose this number ! try another one " ;
GetMove();
}
int MFrame::MakeMove(int move){
// 1 is player move, 2 is AI
a[move] = 1;
return 1;
}
void MFrame::AIMove(){
int random_integer = 0;
while(1){
random_integer = ((9 * rand()) / RAND_MAX);
if(IsSlotAvailible(random_integer) == true){
a[random_integer] = 2;
break;
}
if(a[0] != 0 && a[1] != 0 && a[2] != 0 && a[3] != 0 && a[4] != 0 && a[5] != 0 && a[6] != 0 && a[7] != 0 && a[8] != 0){
break;
}
}
}
int MFrame::CheckForWin(){
//lets check for player 1
int pl1 = 0, pl2 = 0;
if(a[0] == 1 && a[1] == 1 && a[2] == 1){pl1 = 1;}
if(a[3] == 1 && a[4] == 1 && a[5] == 1){pl1 = 1;}
if(a[6] == 1 && a[7] == 1 && a[8] == 1){pl1 = 1;}
if(a[0] == 1 && a[3] == 1 && a[6] == 1){pl1 = 1;}
if(a[1] == 1 && a[4] == 1 && a[7] == 1){pl1 = 1;}
if(a[2] == 1 && a[5] == 1 && a[8] == 1){pl1 = 1;}
if(a[0] == 1 && a[4] == 1 && a[8] == 1){pl1 = 1;}
if(a[2] == 1 && a[4] == 1 && a[6] == 1){pl1 = 1;}
if(a[0] == 2 && a[1] == 2 && a[2] == 2){pl2 = 1;}
if(a[3] == 2 && a[4] == 2 && a[5] == 2){pl2 = 1;}
if(a[6] == 2 && a[7] == 2 && a[8] == 2){pl2 = 1;}
if(a[0] == 2 && a[3] == 2 && a[6] == 2){pl2 = 1;}
if(a[1] == 2 && a[4] == 2 && a[7] == 2){pl2 = 1;}
if(a[2] == 2 && a[5] == 2 && a[8] == 2){pl2 = 1;}
if(a[0] == 2 && a[4] == 2 && a[8] == 2){pl2 = 1;}
if(a[2] == 2 && a[4] == 2 && a[6] == 2){pl2 = 1;}
if(pl2 == 1 && pl1 == 1){return 3;}
if(pl1 == 1){return 1;}
if(pl2 == 1){return 2;}
if(a[0] != 0 && a[1] != 0 && a[2] != 0 && a[3] != 0 && a[4] != 0 && a[5] != 0 && a[6] != 0 && a[7] != 0 && a[8] != 0){return 3;}
return 0;
}
int MFrame::GameState(){
switch(CheckForWin()){
case 1:
std::cout << "Player has won the game! " << std::endl;
ExitGame();
break;
case 2:
std::cout << "PC has won the game! " << std::endl;
ExitGame();
break;
case 3:
std::cout << "Seems that we have a TIE! " << std::endl;
ExitGame();
break;
}
return 0;
}
void MFrame::ExitGame(){
char tmp;
std::cout << "Would you like to play more? y/n :";
std::cin >> tmp;
if(tmp == 'n'){
GAME = false;
exit(1);
}
else
{
SetUp();
Draw();
}
}
Cheers