Results 1 to 5 of 5
  1. #1
    Nur5's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    8
    Reputation
    10
    Thanks
    1
    My Mood
    Bored

    Tic tac toe with unfinished expert AI

    This is tic tac toe with an extremely easy opponent
    It works by randomly throwing in numbers(shit I know)
    I was supposed to make an unbeatable AI but I got lazy and tired so I just left it out(the AI is not halfway done and it has 275 lines of code you can try it if you know how to make it work just read throughout the code and you'll see)

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

    Enjoy learning

  2. #2
    FailHacker's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    444
    Reputation
    8
    Thanks
    49
    so u just made an if statement for each posibility (i skimmed code maybe overlooked?)

    btw love the ghetto vocabulary
    Legen...wait for it...dary







  3. #3
    Nur5's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    8
    Reputation
    10
    Thanks
    1
    My Mood
    Bored
    Yeah but since there are so many possibilities I got bored of doing it, and thanks <3

  4. #4
    -xGhost-'s Avatar
    Join Date
    Oct 2010
    Gender
    male
    Location
    C:\Sytem32
    Posts
    344
    Reputation
    9
    Thanks
    57
    My Mood
    Relaxed
    Well done, did you create it on your on or did you use a Tutorial?
    [IMG]https://i714.photobucke*****m/albums/ww144/sandro911/ProXtremeSignature.png[/IMG]

    [IMG]https://i714.photobucke*****m/albums/ww144/sandro911/Signature.png[/IMG]

  5. #5
    Nur5's Avatar
    Join Date
    May 2008
    Gender
    male
    Posts
    8
    Reputation
    10
    Thanks
    1
    My Mood
    Bored
    Thanks, and I did it on my own(I doubt there are entire-game tutorials ), because I saw a thread here where a member also made a tic-tac-toe game but he didn't use OOP so I made one with OOP and a halfway-done AI bot :P

Similar Threads

  1. How to make Tic Tac Toe with AI setting-VB
    By PWPSky in forum Programming Tutorials
    Replies: 6
    Last Post: 05-03-2010, 11:14 AM
  2. [Tutorial]Tic Tac Toe Game[Leeched]
    By jimmy541 in forum Visual Basic Programming
    Replies: 7
    Last Post: 04-20-2010, 03:43 PM
  3. [Help] Tic-Tac-Toe
    By aLcohoL_95 in forum Visual Basic Programming
    Replies: 11
    Last Post: 04-08-2010, 10:04 PM
  4. Just owned some Tic Tac Toe :)
    By skip in forum C++/C Programming
    Replies: 1
    Last Post: 08-30-2008, 05:03 AM
  5. ARUN BEAT ME ON TIC TAC TOE
    By CAPTAIN OBVIOUS in forum Flaming & Rage
    Replies: 22
    Last Post: 04-30-2008, 05:33 PM