Thread: Tic-Tac-Toe-HELP!!!!

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    24

    Tic-Tac-Toe-HELP!!!!

    Does anyone have a Tic-Tac-Toe program? I am currently working on a game in C++ where I have to code using both the MINIMAX and the alpha-beta algorithms, and am having problems figuring it out.

    HELP

    Tesita

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    I am also in the middle of a Tic Tac Toe game. I don't know what you mean by MINIMAX and alpha beta algorithms. I am currently using a 2 dimensional array. I recently learned how to pass a 2d array to a function so I will probably start using functions instead of just loops and straight up main function.

    Ryan

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    24

    Tic-Tac-Toe-HELP

    I am working on an artificial intelligence course where I have to write a program that simulates a tic-tac-toe game. The program has to devise an evaluation function that can be used to evaluate the state of the game board each time a move has to be made. I have already implemented the game board as a 3x3 array but its the actual game play itself that I am having problems coding.

    Remember, at the end of the day I should be able to play a game against the program itself.

    Hope this clarifies

    Tesita

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    66
    here is my version just finished tonight have not worked out all bugs cause haven't had any but anyways if I was to do AI I would just have it do a random number between 1 and 9 until it finds a place it can go. If it finds 2 0's in a row (if that is what the AI is) then it will try to place in the next spot. If it finds 2 X's then it will block.


    #include <iostream.h>
    #include <windows.h>
    #include "checktack.h"

    int getgrid(int gridnum);
    char winner(char grid[][12], int MAXCOL);

    void main()
    {
    const int MAXCOL = 12;
    const int MAXROW = 12;
    const char filled[] = "You can't go here it is already filled";
    char grid[MAXCOL][MAXROW];
    int fplayer;
    int gridnum = 0;



    for(int i = 0; i < MAXROW; ++i)
    {
    for(int j = 0; j < MAXCOL; ++j)
    {
    grid[j][i] = ' ';
    }
    }

    for(int k = 0; k < MAXROW; ++k)
    {
    grid[11][k] = '\n';
    }

    MessageBox(0, "Tic Tac Toe V5.130ash", "Tic Tac Toe", MB_OK);

    do
    {
    cout << "Who goes first 1 for X and 2 for 0: \n";
    cin >> fplayer;
    if(fplayer > 2 || fplayer < 1)
    {
    MessageBox(0, "Pretty Smart Using A Window For Errors Oh And It Wasn't Between 1 and 2 ", "Your Stupid", MB_OK || MB_ICONWARNING);
    }
    }
    while(fplayer > 2 || fplayer < 1);



    do
    {

    gridnum = getgrid(gridnum);
    if(fplayer == 1)
    {
    if(gridnum == 1)
    {
    CHECKTACKX(1,1);
    }
    if(gridnum == 2)
    {
    CHECKTACKX(5, 1);
    }

    if(gridnum == 3)
    {
    CHECKTACKX(9, 1);
    }
    if(gridnum == 4)
    {
    CHECKTACKX(1, 5);
    }
    if(gridnum == 5)
    {
    CHECKTACKX(5, 5);
    }
    if(gridnum == 6)
    {
    CHECKTACKX(9, 5);
    }
    if(gridnum == 7)
    {
    CHECKTACKX(1, 9);
    }
    if(gridnum == 8)
    {
    CHECKTACKX(5, 9);
    }
    if(gridnum == 9)
    {
    CHECKTACKX(9, 9);
    }
    }

    else if(fplayer == 2)
    {
    if(gridnum == 1)
    {
    CHECKTACKO(1, 1);
    }
    if(gridnum == 2)
    {
    CHECKTACKO(5, 1);
    }

    if(gridnum == 3)
    {
    CHECKTACKO(9, 1);
    }
    if(gridnum == 4)
    {
    CHECKTACKO(1, 5);
    }
    if(gridnum == 5)
    {
    CHECKTACKO(5, 5);
    }
    if(gridnum == 6)
    {
    CHECKTACKO(9, 5);
    }
    if(gridnum == 7)
    {

    CHECKTACKO(1, 9);
    }
    if(gridnum == 8)
    {
    CHECKTACKO(5, 9);
    }
    if(gridnum == 9)
    {
    CHECKTACKO(9, 9);
    }
    }

    for( i = 0; i < MAXROW; ++i)
    {
    for(int j = 0; j < MAXCOL; ++j)
    {
    cout << grid[j][i];
    }
    }

    }while(winner(grid, MAXCOL));




    }



    int getgrid(int gridnum)
    {
    do
    {
    cout << "What grid number would you like to go in 1 - 9\n";
    cin >> gridnum;
    if(gridnum > 9 || gridnum < 1)
    {
    MessageBox(0, "Pretty Smart Using A Window For Errors Oh And It Wasn't Between 1 and 9 ", "Your Stupid", MB_OK || MB_ICONWARNING);
    }
    }
    while(gridnum > 9 || gridnum < 1);
    return gridnum;
    }


    char winner(char grid[][12], int MAXCOL)
    {
    grid[MAXCOL][12];

    if((grid[1][1] == 'X' && grid[5][1] == 'X' && grid[9][1] == 'X') ||
    (grid[1][5] == 'X' && grid[5][5] == 'X' && grid[9][5] == 'X') ||
    (grid[1][9] == 'X' && grid[5][9] == 'X' && grid[9][9] == 'X') ||
    (grid[1][1] == 'X' && grid[1][5] == 'X' && grid[1][9] == 'X') ||
    (grid[5][1] == 'X' && grid[5][5] == 'X' && grid[5][9] == 'X') ||
    (grid[9][1] == 'X' && grid[9][5] == 'X' && grid[9][9] == 'X') ||
    (grid[1][1] == 'X' && grid[5][5] == 'X' && grid[9][9] == 'X') ||
    (grid[9][1] == 'X' && grid[5][5] == 'X' && grid[1][9] == 'X'))
    {
    cout << "X wins ";
    cin >> grid[0][0];
    return 0;
    }
    if((grid[1][1] == 'O' && grid[5][1] == 'O' && grid[9][1] == 'O') ||
    (grid[1][5] == 'O' && grid[5][5] == 'O' && grid[9][5] == 'O') ||
    (grid[1][9] == 'O' && grid[5][9] == 'O' && grid[9][9] == 'O') ||
    (grid[1][1] == 'O' && grid[1][5] == 'O' && grid[1][9] == 'O') ||
    (grid[5][1] == 'O' && grid[5][5] == 'O' && grid[5][9] == 'O') ||
    (grid[9][1] == 'O' && grid[9][5] == 'O' && grid[9][9] == 'O') ||
    (grid[1][1] == 'O' && grid[5][5] == 'O' && grid[9][9] == 'O') ||
    (grid[9][1] == 'O' && grid[5][5] == 'O' && grid[1][9] == 'O'))
    {
    cout << "O wins ";
    cin >> grid[0][0];
    return 0;
    }
    }


    here is the header file checktack.h

    #include <iostream.h>

    #ifndef __CHECKTACK_H__
    #define __CHECKTACK_H__


    #define CHECKTACKX(a, b)\
    if(grid[a][b] == ' ') \
    { \
    grid[a][b] = 'X'; \
    fplayer += 1;\
    }\
    else \
    {\
    cout << filled << '\n'; \
    continue;\
    }\

    #define CHECKTACKO(a, b)\
    if(grid[a][b] == ' ') \
    { \
    grid[a][b] = 'O'; \
    fplayer -= 1;\
    }\
    else \
    {\
    cout << filled << '\n'; \
    continue;\
    }\


    #endif

  5. #5
    Registered User Dual-Catfish's Avatar
    Join Date
    Sep 2001
    Posts
    802
    No offense, but your Tic tac toe seems overly complicated.. here's mine.

    Code:
    #include <iostream.h>
    #include <windows.h>
    #include <conio.h>
    
    typedef unsigned short int USHORT;
    char board[3][3];
    char playername1[25];
    char playername2[25];
    const char player1 = 'x';
    const char player2 = 'o';
    char playerup = 'x';
    const char blankchar = '-';
    USHORT i, ii, x, y, moves, move;
    
    void table(void);
    int init(void);
    int getInput(char player);
    int setInput(char player);
    USHORT checkWin(char playerup);
    
    int main ()
    {
    
    	init();
    	system("CLS");
    	table();
    	do
    	{
    		if (moves < 9)
    		{
    				getInput(playerup);
    				while (setInput(playerup))
    				{	
    					cout << "\tThat move has already been made!\n\n";
    					getInput(playerup);
    				}
    				system("CLS");
    				table();
    				if (playerup == 'o')
    				{
    					playerup = 'x';
    				} else {
    					playerup = 'o';
    				}
    		} else {
    			cout << "\n\tNo winner this time, thanks for playing\n\n\n\t";
    			//system("PAUSE");
    			_getch();
    			return 0;
    		}
    	} while (checkWin(playerup) == 2);
    	if (playerup == 'x')
    	{ 
    		cout << "\n\tCongratulations " << playername2 << ", you've won!\n\n\n\t";
    		//system("PAUSE");
    		_getch();
    		return 0;
    	} else {
    		cout << "\n\tCongratulations " << playername1 << ", you've won!\n\n\n\t";
    		//system("PAUSE");
    		_getch();
    		return 0;
    	}
    }
    
    
    USHORT checkWin(char playerup)
    {	
    	if (playerup == 'x')
    	{
    
    		if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] != blankchar)
    		{
    			return 0;
    		}
    		if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] != blankchar)
    		{
    			return 0;
    		}
    		if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] != blankchar)
    		{
    			return 0;
    		}
    		//CHECK COLUMNS
    		if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] != blankchar)
    		{
    			return 0;
    		}
    		if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] != blankchar)
    		{
    			return 0;
    		}
    		if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] != blankchar)
    		{
    			return 0;
    		}
    		//CHECK DIAGONALS
    		if (board[0][2] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != blankchar)
    		{	
    			return 0;
    		}
    		if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[0][2] != blankchar)
    		{	
    			return 0;
    		}
    
    
    	} else {
    
    
    		if (board[0][0] == board[0][1] && board[0][1] == board[0][2] && board[0][2] != blankchar)
    		{
    			return 1;
    		}
    		if (board[1][0] == board[1][1] && board[1][1] == board[1][2] && board[1][2] != blankchar)
    		{
    			return 1;
    		}
    		if (board[2][0] == board[2][1] && board[2][1] == board[2][2] && board[2][2] != blankchar)
    		{
    			return 1;
    		}
    		//CHECK COLUMNS
    		if (board[0][0] == board[1][0] && board[1][0] == board[2][0] && board[2][0] != blankchar)
    		{
    			return 1;
    		}
    		if (board[0][1] == board[1][1] && board[1][1] == board[2][1] && board[2][1] != blankchar)
    		{
    			return 1;
    		}
    		if (board[0][2] == board[1][2] && board[1][2] == board[2][2] && board[2][2] != blankchar)
    		{
    			return 1;
    		}
    		//CHECK DIAGONALS
    		if (board[0][2] == board[1][1] && board[1][1] == board[2][2] && board[2][2] != blankchar)
    		{	
    			return 1;
    		}
    		if (board[2][0] == board[1][1] && board[1][1] == board[0][2] && board[0][2] != blankchar)
    		{	
    			return 1;
    		}
    
    	}
    	return 2;
    	
    
    }
    
    //UGLY CODE; THIS PRINTS OUT A NICE LOOKING TABLE.
    void table(void)
    {
    	cout << "\n\n\tCurrent Table\n";
    	cout << "\t" << (char) 201 << char (205) << char (205) << char (205) << char (205) << char (205) << char (203) << (char) 205 << char (205) << char (205) << char (205) << (char) 205 << (char) 203 << (char) 205 << char (205) << (char) 205 << char (205) << char (205) << char (187) << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "  " <<board[0][0]<<"  " << (char) 186 << "  " << board[0][1] << "  " << (char) 186 << "  " <<board[0][2]<<"  " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;
    	cout << "\t" << (char) 204 << char (205) << char (205) << char (205) << char (205) << char (205) << (char) 206 << char (205) << char (205) << char (205) << char (205) << char (205) << (char) 206 << char (205) << char (205) << char (205) << char (205) << char (205) << char (185) << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "  " <<board[1][0]<<"  " << (char) 186 << "  " << board[1][1]<<"  " << (char) 186 << "  " <<board[1][2]<<"  " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;	
    	cout << "\t" << (char) 204 << char (205) << char (205) << char (205) << char (205) << char (205) << (char) 206 << char (205) << char (205) << char (205) << char (205) << char (205) << (char) 206 << char (205) << char (205) << char (205) << char (205) << char (205) << char (185) << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "  " <<board[2][0]<<"  " << (char) 186 << "  " << board[2][1] << "  " << (char) 186 << "  " << board[2][2] << "  " << (char) 186 << endl;
    	cout << "\t" << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << "     " << (char) 186 << endl;
    	cout << "\t" << (char) 200 << char (205) << char (205) << char (205) << char (205) << char (205) << char (202) << (char) 205 << char (205) << char (205) << char (205) << (char) 205 << (char) 202 << (char) 205 << char (205) << (char) 205 << char (205) << char (205) << char (188) << endl;
    	cout << "\n";
    }
    
    
    //UGLY CODE; THIS JUST PRINTS OUT A PRETTY TITLE
    int init(void)
    {
    	cout << "\t" << (char) 201 << (char) 205 << (char) 203 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 203 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 205 << (char) 187 << "  " << (char) 201 << (char) 205 << (char) 203 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 205 << (char) 187 << "  " << (char) 201 << (char) 205 << (char) 203 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 205 << (char) 187 << " " << (char) 201 << (char) 205 << (char) 205 << (char) 187 << "\n";
    	cout << "\t  " << (char) 186 << "     " << (char) 186 << "   " << (char) 186 << "       " << (char) 186 << "   " <<  (char) 204  <<  (char) 205 << (char) 205 <<  (char) 185 << " " << (char) 186 << "       " << (char) 186 << "   " << (char) 186 << "  " << (char) 186 << " " << (char) 204 << (char) 205 << "\n";
    	cout << "\t  " << (char) 186 << "     " << (char) 186 << "   " << (char) 186 << "       " << (char) 186 << "   " <<  (char) 186 << "  " <<  (char) 186 << " " << (char) 186 << "       " << (char) 186 << "   " << (char) 186 << "  " << (char) 186 << " " << (char) 186 << " \n";
    	cout << "\t  " << (char) 202 << "   " << (char) 200 << (char) 205 << (char) 202 << (char) 205 << (char) 188 << " " << (char) 200 << (char) 205 << (char) 205 << (char) 188 << "    " << (char) 202 << "   " << (char) 202 << "  " << (char) 202 << " " << (char) 200 << (char) 205 << (char) 205 << (char) 188 << "    " << (char) 202 << "   " << (char) 200 << (char) 205 << (char) 205 << (char) 188 << " " << (char) 200 << (char) 205 << (char) 205 << (char) 188 << "\n";
    	cout << "\tInstructions: use number keys to determine move.\n\n";
    	cout << "\t[1][2][3]\n";
    	cout << "\t[4][5][6]\n";
    	cout << "\t[7][8][9]\n\n";
    	cout << "\tContact: [email protected]\n";
    	cout << "\t[x] Player 1, please enter your name now->";
    	cin.getline(playername1, 25, '\n');
    	cout << "\t[o] Player 2; enter yours->";
    	cin.getline(playername2, 25, '\n');
    
    	for (i = 0; i < 3; i++)
    	{
    		for (ii = 0; ii < 3; ii++)
    		{
    			board[i][ii] = blankchar;
    		}
    	}
    	return 0;
    }
    
    int getInput(char player)
    {
    	if (player == 'x')
    	{
    		cout << "\t" << playername1 << ", make your move->";
    		cin>>move;
    		while (move != 1 && move != 2 && move != 3 && move != 4 && move != 5 && move != 6 && move != 7 && move != 8 && move != 9)
    		{
    			cout << "\n\t Invalid input, please restate->";
    			cin>>move;
    		}
    		switch (move)
    		{
    			case 1: { x = 0; y = 0; break; }
    			case 2: { x = 0; y = 1; break; }
    			case 3: { x = 0; y = 2; break; }
    			case 4: { x = 1; y = 0; break; }
    			case 5: { x = 1; y = 1; break; }
    			case 6: { x = 1; y = 2; break; }
    			case 7: { x = 2; y = 0; break; }
    			case 8: { x = 2; y = 1; break; }
    			case 9: { x = 2; y = 2; break; }
    			{
    				cout << "\n\tInvalid input, please restate->";
    				getInput(playerup);
    			}
    
    		}
    		return 1;
    	} else {
    		cout << "\t" << playername2 << ", make your move->";
    		cin>>move;
    		switch (move)
    		{
    			case 1: { x = 0; y = 0; break; }
    			case 2: { x = 0; y = 1; break; }
    			case 3: { x = 0; y = 2; break; }
    			case 4: { x = 1; y = 0; break; }
    			case 5: { x = 1; y = 1; break; }
    			case 6: { x = 1; y = 2; break; }
    			case 7: { x = 2; y = 0; break; }
    			case 8: { x = 2; y = 1; break; }
    			case 9: { x = 2; y = 2; break; }
    			{
    				cout << "\n\tInvalid input, please restate->";
    				getInput(playerup);
    			}
    
    		}
    		return 2;
    	}
    }
    
    int setInput(char player)
    {
    	if (board[x][y] == blankchar)
    	{
    		board[x][y] = player;
    		moves++;
    		return 0;
    	} 
    		return 1;
    }

  6. #6
    Unregistered
    Guest
    Try to make your code less complicated by using functions.

    I made tictactoe game back in 1998 in grade 12.

    Freature

    2 players(no Challenge)
    against the computer (not very hard)

    I got 120% as a mark.


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, 06:25 PM
  2. tic tac toe
    By holden in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 05-09-2004, 09:59 AM
  3. Help with Tic Tac Toe game
    By snef73 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2003, 08:33 AM
  4. tic tac toe game
    By Leeman_s in forum Game Programming
    Replies: 9
    Last Post: 04-24-2002, 03:24 AM
  5. my tic tac toe game, please try it
    By Leeman_s in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2002, 05:16 PM