Code:
// TicTacToe.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <string>
#include <ctime>
using namespace std;
char bing[9] = {'1','2','3','4','5','6','7','8','9'};
char clear[9] = {'1','2','3','4','5','6','7','8','9'};
class nigg
{
public:
bool playing;
void Draw();
void Draw(char,string,bool);
void Draw(char cz,int i){if(bing[i]==clear[i]){bing[i] = cz; system("cls");}}
void Clear(){for(int i=0;i<9;i++) bing[i]=clear[i]; Draw();}
void PC();
void Win();
void Check();
char winner,player,pc;
};
void nigg::Draw()
{
cout << bing[0];
cout << " | ";
cout << bing[1];
cout << " | ";
cout << bing[2] << "\n";
cout << "_________\n";
cout << bing[3];
cout << " | ";
cout << bing[4];
cout << " | ";
cout << bing[5] << "\n";
cout << "_________\n";
cout << bing[6];
cout << " | ";
cout << bing[7];
cout << " | ";
cout << bing[8];
cout << endl;
}
void nigg::PC() //Unfinished unbeatable AI not working after 2 steps(try it and you'll see why).
{
int block[25]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int count=0;
for(int i=0;i<9;i++)
{
if(bing[i]==player) count++;
}
if(count==1) {
if(bing[1]==player || bing[3]==player || bing[5]==player || bing[7]==player||bing[0] == player || bing[2] == player || bing[6] == player || bing[8] == player)
{
Draw(pc,4);
}
else if(bing[4] == player)
{
int trig = rand() %8 +1;
if(trig==0||trig==2||trig==4||trig==6||trig==8)
{
Draw(pc,trig);
}
else
{
trig++;
Draw(pc,trig);
}
}
}
else if(count==2) {
if(bing[0] == player && bing[1] == player)
{
Draw(pc,2);
block[0]=1;
}
if(bing[0]==player && bing[2]==player)
{
Draw(pc,1);
block[1]=1;
}
if(bing[1]==player && bing[2]==player)
{Draw(pc,0);
block[2]=1;}
if(bing[0]==player&&bing[3]==player)
{Draw(pc,6);
block[3]=1;}
if(bing[0]==player && bing[6]==player)
{Draw(pc,3);
block[4]=1;}
if(bing[6]==player && bing[3]==player)
{Draw(pc,0);
block[5]=1;}
if(bing[0]==player && bing[4] == player)
{Draw(pc,8);
block[6]=1;}
if(bing[0]==player && bing[8]==player)
{Draw(pc,4);
block[7]=1;}
if(bing[4]==player && bing[8]==player)
{Draw(pc,0);
block[8]=1;}
if(bing[1]==player && bing[4]==player)
{Draw(pc,7);
block[9]=1;}
if(bing[1]==player && bing[7]==player)
{Draw(pc,4);
block[10]=1;}
if(bing[4]==player && bing[7]==player)
{Draw(pc,1);
block[11]=1;}
if(bing[2]==player && bing[5]==player)
{Draw(pc,8);
block[12]=1;}
if(bing[2]==player && bing[8]==player)
{Draw(pc,5);
block[13]=1;}
if(bing[5]==player && bing[8]==player)
{Draw(pc,2);
block[14]=1;}
if(bing[2]==player && bing[4]==player)
{Draw(pc,6);
block[15]=1;}
if(bing[2]==player && bing[6]==player)
{Draw(pc,4);
block[16]=2;}
if(bing[4]==player && bing[6]==player)
{Draw(pc,2);
block[17]=1;}
if(bing[3]==player && bing[4]==player)
{Draw(pc,5);
block[18]=1;}
if(bing[3]==player && bing[5]==player)
{Draw(pc,4);
block[19]=1;}
if(bing[4]==player&& bing[5]==player)
{Draw(pc,3);
block[20]=1;}
if(bing[6]==player && bing[7]==player)
{Draw(pc,8);
block[21]=1;}
if(bing[6]==player && bing[8]==player)
{Draw(pc,7);
block[22]=1;}
if(bing[7]==player && bing[8]==player)
{Draw(pc,6);
block[23]=1;}
else
{
for(int j=0;j<9;j++)
{
if(bing[j]==pc)
{
j++;
Draw(pc,j);
}
}
}
} //Works up until here, then it bugs because instead of trying to win it just prevents you from winning
//And sometimes even put both(block you AND win)
else if(count==3) {
if(block[0]==1)
{
if(bing[3]==player)
{
Draw(pc,6);
winner= pc;
Win();
}
else if(bing[6]==player)
Draw(pc,3);
else
{
Draw(pc,6);
winner = pc;
Win();
}
}
else if(block[1]==1)
{
if(bing[7]==player)
Draw(pc,5);
else
{
Draw(pc,7);
winner=pc;
Win();
}
}
else if(block[2]==1)
{
if(bing[0]==player)
Draw(pc,3);
else
{
Draw(pc,8);
winner=pc;
Win();
}
}
else if(block[3]==1)
{
if(bing[2]==player)
Draw(pc,7);
else
{
Draw(pc,2);
winner=pc;
Win();
}
}
else if(block[4]==1)
{
if(bing[5]==player)
Draw(pc,7);
else
{
Draw(pc,5);
winner=pc;
Win();
}
}
else if(block[5]==1)
{
if(bing[8]==player)
Draw(pc,2);
else
{
Draw(pc,8);
winner=pc;
Win();
}
}
else if(block[8]==1) {}
else if(block[12]==1)
{
if(bing[0]==player)
Draw(pc,1);
else
{
Draw(pc,0);
winner=pc;
Win();
}
}
else if(block[13]==1)
{
if(bing[3]==player)
Draw(pc,7);
else
{
Draw(pc,3);
winner=pc;
Win();
}
}
else if(block[14]==1)
{
if(bing[6]==1)
Draw(pc,1);
else
{
Draw(pc,6);
winner=pc;
Win();
}
}
else if(block[19]==1)
{
if(bing[0]==player)
Draw(pc,6);
else
{
Draw(pc,0);
winner=pc;
Win();
}
}
else if(block[11]==1)
{
if(bing[0]==pc)
{
if(bing[2]==player)
Draw(pc,6);
else
{
Draw(pc,2);
winner=pc;
Win();
}
}
else if(bing[2]==pc)
{
if(bing[0]==player)
Draw(pc,8);
else
{
Draw(pc,0);
winner=pc;
Win();
}
}
else if(bing[6]==pc)
{
if(bing[0]==player)
Draw(pc,8);
if(bing[3]==player)
Draw(pc,8);
else
Draw(pc,1);
}
else if(bing[8]==pc)
{
if(bing[0]==player || bing[2]==player)
Draw(pc,8);
}
}
}
} //End of AI
void nigg::Check() //Made this function after I coded the (unfinished)AI, when I'm not lazy I'll code this into the AI and hopefully finish it
{
if(bing[0]==pc && bing[1]==pc && bing[2]==pc)
{
winner=pc;
Win();
}
else if(bing[0]==player && bing[1]==player && bing[2]==player)
{
winner=player;
Win();
}
else if(bing[0]==pc && bing[3]==pc && bing[6]==pc)
{
winner=pc;
Win();
}
else if(bing[0]==player && bing[3]==player && bing[6]==player)
{
winner=player;
Win();
}
else if(bing[0]==pc && bing[4]==pc && bing[8]==pc)
{
winner=pc;
Win();
}
else if(bing[0]==player && bing[4]==player && bing[8]==player)
{
winner=player;
Win();
}
else if(bing[1]==pc && bing[4]==pc && bing[7]==pc)
{
winner=pc;
Win();
}
else if(bing[1]==player && bing[4]==player && bing[7]==player)
{
winner=player;
Win();
}
else if(bing[2]==pc && bing[5]==pc && bing[8]==pc)
{
winner=pc;
Win();
}
else if(bing[2]==player && bing[5]==player && bing[8]==player)
{
winner=player;
Win();
}
else if(bing[2]==pc && bing[4]==pc && bing[6]==pc)
{
winner=pc;
Win();
}
else if(bing[2]==player && bing[4]==player && bing[6]==player)
{
winner=player;
Win();
}
else if(bing[3]==pc && bing[4]==pc && bing[5]==pc)
{
winner=pc;
Win();
}
else if(bing[3]==player && bing[4]==player && bing[5]==player)
{
winner=player;
Win();
}
else if(bing[6]==pc && bing[7]==pc && bing[8] == pc)
{
winner=pc;
Win();
}
else if(bing[6]==player && bing[7]==player && bing[8] == player)
{
winner=player;
Win();
}
else //Draw
{
int zls=0;
for(int j=0;j<9;j++)
{
if(!(bing[j]==clear[j]))
{
zls++;
}
}
if(zls==9)
{
system("COLOR 06");
cout << "IT'S A DRAW!\n";
system("PAUSE");
system("COLOR 0F");
system("CLS");
playing=false;
}
}
}
void nigg::Win()
{
if(winner==pc)
{
system("COLOR 89");
cout << "YOU LOSE!\n";
system("PAUSE");
system("COLOR 0F");
system("CLS");
playing=false;
}
if(winner==player)
{
system("COLOR 0A");
cout << "YOU WIN!!\n";
system("PAUSE");
system("COLOR 0F");
system("CLS");
playing=false;
}
}
int rans()
{
srand(time(0));
int x=rand() %8+1;
return(x);
}
void nigg::Draw(char cz,string poz,bool trigg)
{
if(playing)
{
int nig=0;
nig = atoi(poz.c_str()); //get value of string
nig--; //decrease to match with bing[] array(arrays count from 0 not 1)
if(bing[nig]==clear[nig])
{
bing[nig] = cz;
system("cls");
if(trigg==false) //HERE.
PC();
else
{
while(1)
{
int is=rans();
if(bing[is]==clear[is])
{
Draw(pc,is);
break;
}
}
}
Draw();
Check();
}
else
{
cout << "Invalid move, ******.\n";
}
}
}
int _tmain(int argc, _TCHAR* argv[])
{
srand(time(0));
char pla;
string chilla;
nigg *xo = new nigg();
xo->playing=false;
xo->Draw();
system("COLOR 0F");
cout << "YO WHADDUP MY NIGGA YOU PLAYIN NUR5'S VERY OWN TICTACTOE GAME MADE IN C++ BITCHY ENTAR YO SELECTION SENIOR\n";
cout << "B4 D0ING DAT BIZZLE HERE ARE HOW T0 PL4YNSHIT\n";
cout << "new -N3W GAME\n";
cout << "quit -QUIT\n";
cout << "WHEN PLAYING WRITE DA NUMBAH FOR YO BIZZLE AND WE WILL SHIZZLE DRIZZLE, GET IT, SHIZZLE DRIZZLE\n";
cout << "ENTAR YO SELECTION SENIOR: ";
while(1)
{
cout << "Command or Move: ";
cin >> chilla;
if(chilla == "new")
{
xo->Clear();
cout << "X or O?(instead of O you can write another letter): ";
cin >> pla;
xo->player=pla;
if(xo->player=='X')
xo->pc = 'O';
else
xo->pc = 'X';
xo->playing=true;
}
else if (chilla=="quit")
{
break;
}
else if(chilla=="1" || chilla== "2" || chilla=="3" ||chilla== "4" ||chilla== "5" ||chilla== "6" ||chilla== "7" ||chilla== "8"||chilla=="9")
{
if(xo->playing==true)
{
bool yes=true; //SO AI DOESN'T LOAD IF YOU WANT TO LOAD AI MAKE yes FALSE
xo->Draw(pla,chilla,yes);
}
else if(xo->playing==false)
cout << "NOT PLAYING ANY GAME TO MOVE.\n";
}
else
{
cout << "No such thing hurr durr...\n";
}
}
system("PAUSE");
return 0;
}