Checkers/Tic-Tac-Toe

This is a discussion on Checkers/Tic-Tac-Toe within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, is there anyone out there who would like to write a checkers or tic tac toe game, or ...

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    Checkers/Tic-Tac-Toe

    Hi everyone, is there anyone out there who would like to write a checkers or tic tac toe game, or maybe both? I'm looking for someone on the upper side of begginer who would like to write these games with me. Like I said, I'm still a begginer but would like to learn more, and not that the tutorials on this site aren't great, but I figure the best way to learn is hands on experiance, or start something like this. I know checkers or tic tac toe doesn't sound to hard, but most of the games I've seen people use random numbers to place were the peices go, but I'd like to try to use some AI. If anyone wants to help, reply here, or email me if you like, Dummies102@aol.com

  2. #2
    Unregistered
    Guest

    A brief note

    If you want to try your hand at AI, look towards the checkers idea. Tictactoe really is, in my opinion, too simple for AI and a reasonable level of intelligence could be reached in very few lines of code.

    Of course I may be wrong, and if thats the case I have an MFC TicTacToe game you can modify if you so wish...

  3. #3
    Unregistered
    Guest
    well here is my pitiful attempt at tictactoe no ai yet its 100% random

    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <ctime>
    
    using namespace std;
    
    char board[3][3];
    
    void FillBoard();
    void DisplayBoard();
    void FirstPlayer();
    bool NoWinner();
    void GetPlayerMove();
    int GetComputerMove();
    
    int main()
    {
    	srand(time(NULL));
    	FillBoard();
    	FirstPlayer();
    	
    	while(NoWinner())
    	{
    		GetPlayerMove();
    
    		if(NoWinner()==false)
    		{
    			break;
    		}
    
    		while(GetComputerMove()==0)
    		{
    		}
    	}
    
    	
    	return (0);
    }
    //---------------------------------------------------------------
    void FillBoard()
    {
    	int i, j;
    	
    	for(i=0;i<3;i++)
    		for(j=0;j<3;j++)
    			board[i][j]=' ';
    }
    //---------------------------------------------------------------
    void DisplayBoard()
    {
    	int i;
    	
    	for(i=0;i<3;i++)
    		cout<<"["<<board[i][0]<<"]"<<"["<<board[i][1]<<"]"<<"["<<board[i][2]<<"]"<<endl;
    }
    //---------------------------------------------------------------
    void GetPlayerMove()
    {
    	int movetobox;
    
    	do
    	{
    		cout<<"Enter the box you want to move to (1-9): ";
    		cin>>movetobox;
    	}while((movetobox<1)||(movetobox>9));
    
    	if((movetobox==1)&&(board[0][0]==' '))
    	{
    		board[0][0]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==2)&&(board[0][1]==' '))
    	{
    		board[0][1]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==3)&&(board[0][2]==' '))
    	{
    		board[0][2]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==4)&&(board[1][0]==' '))
    	{
    		board[1][0]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==5)&&(board[1][1]==' '))
    	{
    		board[1][1]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==6)&&(board[1][2]==' '))
    	{
    		board[1][2]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==7)&&(board[2][0]==' '))
    	{
    		board[2][0]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==8)&&(board[2][1]==' '))
    	{
    		board[2][1]='X';
    		DisplayBoard();
    	}
    	else if((movetobox==9)&&(board[2][2]==' '))
    	{
    		board[2][2]='X';
    		DisplayBoard();
    	}
    	else
    	{
    		cout<<"Illegal Move!"<<endl;
    		GetPlayerMove();
    	}	
    }
    //---------------------------------------------------------------
    void FirstPlayer()
    {
    	int random;
    	srand(time(NULL));
    	random=(rand () % 2);
    
    	if(random==0)
    	{
    		cout<<"Computer goes first!"<<endl;
    		
    		while(GetComputerMove()==0)
    		{
    		}
    	}
    
    	else if(random==1)
    	{
    		cout<<"Player goes first!"<<endl;
    		DisplayBoard();
    		GetPlayerMove();
    
    		while(GetComputerMove()==0)
    		{
    		}
    	}
    }
    //---------------------------------------------------------------
    int GetComputerMove()
    {
    	int movetobox;
    	movetobox=(rand () % 9);
    	
    		if((movetobox==1)&&(board[0][0]==' '))
    		{
    			board[0][0]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==2)&&(board[0][1]==' '))
    		{
    			board[0][1]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==3)&&(board[0][2]==' '))
    		{
    			board[0][2]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==4)&&(board[1][0]==' '))
    		{
    			board[1][0]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==5)&&(board[1][1]==' '))
    		{
    			board[1][1]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==6)&&(board[1][2]==' '))
    		{
    			board[1][2]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==7)&&(board[2][0]==' '))
    		{
    			board[2][0]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==8)&&(board[2][1]==' '))
    		{
    			board[2][1]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    		else if((movetobox==9)&&(board[2][2]==' '))
    		{
    			board[2][2]='O';
    			cout<<"Computer moves to box "<<movetobox<<endl;
    			DisplayBoard();
    			return (1);
    		}
    	
    	return (0);
    }
    //---------------------------------------------------------------
    bool NoWinner()
    {
    	if((board[0][0]=='O')&&(board[0][1]=='O')&&(board[0][2]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[1][0]=='O')&&(board[1][1]=='O')&&(board[1][2]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[2][0]=='O')&&(board[2][1]=='O')&&(board[2][2]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][0]=='O')&&(board[1][0]=='O')&&(board[2][0]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][1]=='O')&&(board[1][1]=='O')&&(board[2][1]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][2]=='O')&&(board[1][2]=='O')&&(board[2][2]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][0]=='O')&&(board[1][1]=='O')&&(board[2][2]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][2]=='O')&&(board[1][1]=='O')&&(board[2][0]=='O'))
    	{
    		cout<<"Computer Wins!"<<endl;
    		return (false);
    	}
    
    	if((board[0][0]=='X')&&(board[0][1]=='X')&&(board[0][2]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[1][0]=='X')&&(board[1][1]=='X')&&(board[1][2]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[2][0]=='X')&&(board[2][1]=='X')&&(board[2][2]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][0]=='X')&&(board[1][0]=='X')&&(board[2][0]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][1]=='X')&&(board[1][1]=='X')&&(board[2][1]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][2]=='X')&&(board[1][2]=='X')&&(board[2][2]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][0]=='X')&&(board[1][1]=='X')&&(board[2][2]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	else if((board[0][2]=='X')&&(board[1][1]=='X')&&(board[2][0]=='X'))
    	{
    		cout<<"Player Wins!"<<endl;
    		return (false);
    	}
    	
    	if((board[0][0]!=' ')&&(board[0][1]!=' ')&&(board[0][2]!=' ')&&(board[1][0]!=' ')&&(board[1][1]!=' ')&&(board[1][2]!=' ')&&(board[2][0]!=' ')&&(board[2][1]!=' ')&&(board[2][2]!=' '))
    	{
    		cout<<"Game ends in a draw!"<<endl;
    		return (false);
    	}
    
    	return (true);
    }

  4. #4
    Registered User
    Join Date
    Feb 2002
    Posts
    33

    Thumbs up

    nice game =)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help me with my simple Tic tac toe prog
    By maybnxtseasn in forum C Programming
    Replies: 2
    Last Post: 04-04-2009, 07:25 PM
  2. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 09:33 AM
  3. Need some help with a basic tic tac toe game
    By darkshadow in forum C Programming
    Replies: 1
    Last Post: 05-12-2002, 05:21 PM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 04:24 AM
  5. Tic Tac Toe Help
    By aresashura in forum C++ Programming
    Replies: 1
    Last Post: 11-21-2001, 12:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21