Results 1 to 7 of 7
  1. #1
    orx's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Location
    Estonia
    Posts
    59
    Reputation
    11
    Thanks
    4

    OrX First TicTacToe

    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
    Phenom II X6 1090T @3.9ghz
    8GB DDRIII 1600mhz
    HD 6950(flashed to 6970)
    750W PSU
    AMD 870 chipset
    64GB SATAIII SSD
    2TB storage drive
    32" HD monitor
    Razer Lycosa mirror edition
    windows 7 ultimate 64bit
    Wicked sick machine

  2. #2
    FailHacker's Avatar
    Join Date
    Sep 2010
    Gender
    male
    Posts
    444
    Reputation
    8
    Thanks
    49
    I like your organization, I've seen Tic-Tac-Toe sources with minimal use of OOP. Just looks ugly.
    Legen...wait for it...dary







  3. #3
    orx's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Location
    Estonia
    Posts
    59
    Reputation
    11
    Thanks
    4
    Thanks currently adding some features, and will make the AI more advanced + idea for adding a two-player function, so you can play it with your buddy or what ever
    Phenom II X6 1090T @3.9ghz
    8GB DDRIII 1600mhz
    HD 6950(flashed to 6970)
    750W PSU
    AMD 870 chipset
    64GB SATAIII SSD
    2TB storage drive
    32" HD monitor
    Razer Lycosa mirror edition
    windows 7 ultimate 64bit
    Wicked sick machine

  4. #4
    Sixx93's Avatar
    Join Date
    Sep 2009
    Gender
    male
    Posts
    673
    Reputation
    21
    Thanks
    250
    My Mood
    Cool
    yes, i like it, cool the only problem is that the "pc" is very "stupid"... don't use a random position, just write an algorithm to block the user actions

  5. #5
    orx's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Location
    Estonia
    Posts
    59
    Reputation
    11
    Thanks
    4
    What did i say in previous post? ".. and will make the AI more advanced .."
    i am going to make a algorythm tho, so if tile 0 1 x | 3 x 5 | 6 7 8 . Then pc will always tick the 6, to prevent player from winning. and boom there you go, we got a algorythm for that
    Phenom II X6 1090T @3.9ghz
    8GB DDRIII 1600mhz
    HD 6950(flashed to 6970)
    750W PSU
    AMD 870 chipset
    64GB SATAIII SSD
    2TB storage drive
    32" HD monitor
    Razer Lycosa mirror edition
    windows 7 ultimate 64bit
    Wicked sick machine

  6. #6
    kibbles18's Avatar
    Join Date
    Oct 2008
    Gender
    male
    Location
    US
    Posts
    860
    Reputation
    5
    Thanks
    127
    use sockets and make it "online"

  7. #7
    orx's Avatar
    Join Date
    Jun 2008
    Gender
    male
    Location
    Estonia
    Posts
    59
    Reputation
    11
    Thanks
    4
    Quote Originally Posted by kibbles18 View Post
    use sockets and make it "online"
    Btw i was thinking about it earlier today. that i make a server.exe and client system, would be a rly nice thing to practice it on. + i have 150mbit/s internet connection to host my tic-tac-toe server xd
    Phenom II X6 1090T @3.9ghz
    8GB DDRIII 1600mhz
    HD 6950(flashed to 6970)
    750W PSU
    AMD 870 chipset
    64GB SATAIII SSD
    2TB storage drive
    32" HD monitor
    Razer Lycosa mirror edition
    windows 7 ultimate 64bit
    Wicked sick machine

Similar Threads

  1. War Rock - First Ban?
    By arunforce in forum General Gaming
    Replies: 26
    Last Post: 01-27-2006, 09:11 AM
  2. My First completed Siggy!
    By h0ang in forum Art & Graphic Design
    Replies: 1
    Last Post: 01-26-2006, 09:11 AM
  3. My first signature
    By h0ang in forum Art & Graphic Design
    Replies: 5
    Last Post: 01-24-2006, 03:35 PM
  4. First sig
    By SadisticGrin in forum Art & Graphic Design
    Replies: 20
    Last Post: 01-19-2006, 04:38 AM
  5. My First Sig
    By OutZida in forum Art & Graphic Design
    Replies: 17
    Last Post: 01-14-2006, 03:36 PM