Closed Thread
Page 2 of 2 FirstFirst 12
Results 11 to 14 of 14

Thread: need help with simple C++ TicTacToe game with AI

  1. #11
    saurav is offline Newbie
    Join Date
    Aug 2007
    Posts
    8
    Rep Power
    0
    Does the following help a bit?
    In any case, can anyone help me make it shorter?
    Can we impart artificial intelligence to player 2?
    Code:
    #include<iostream>
    using namespace std;
    char board[9] ={'1','2','3','4','5','6','7','8','9'};
    
    void drawboard()
    {cout << "\n " << board[0] << " | " << board[1] << " | " << board[2] << endl;
     cout<< " ---------" << endl;
     cout<< board[3] << " | " << board[4] << " | " << board[5] << endl;
    cout<< " ---------" << endl;
    cout<< board[6] << " | " << board[7] << " | " << board[8] << endl;}
    
    int winning(char f[])
    {
    if(board[0] == board[1] && board[2] == board[0] )
    	        	return 1;
    		    
    			else if(board[3] == board[4] && board[5] == board[3])
    	        	return 1;
    
    			else if(board[6] == board[7] && board[8] == board[6])
    	        	return 1;
    
    			else if(board[0] == board[3] && board[6] == board[0])
    	        	return 1;
    
    			else if(board[1] == board[4] && board[7] == board[1])
    	        	return 1;
    
    			else if(board[2] == board[5] && board[8] == board[2])
    	        	return 1;
    
    			else if(board[0] == board[4] && board[8] == board[0])
    	        	return 1;
    
    			else if(board[2] == board[4] && board[6] == board[2])
    	        	return 1;
    			else
    				return 0;
    }
    
    
    
    int main()
    {
    char c[2] ={'x','o'};
    int move;
    int player=2;
    for(int i=0;i<=9;i++){
    if(i<9&&winning(board)==0)
    {
    drawboard();
    if(player ==1) player =2;
    else player =1;
    cout<<"player"<<player<<" please move"<<endl;
    cin>>move;
    board[move-1]=c[player-1];
    {if(winning(board) == 1)
    {cout<<"player"<<player<<" wins."<<endl;
     return 0;
    }}}
    
    else if(i==9&&winning(board)==0)
    cout<<"Tie"<<endl;
    
    else
    cin.get();
    }
    }

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #12
    CygnetGames's Avatar
    CygnetGames is offline Programmer
    Join Date
    May 2007
    Location
    York, England
    Posts
    119
    Rep Power
    0
    Ok, first off: sorry, I can't help you translate the C# code into C++. I've only used C myself.

    Secondly: don't be downhearted that you haven't got minimax to work yet. It is one of those algorithms that's simple to understand, but really tough to implement - especially if you're not that familiar with trees.

    Your teacher should not have said you couldn't become a good programmer - and he should hever have said you were stupid! The fact that you got the game to work with a different algorithm proves you aren't stupid. If I were you, I would leave minimax and move on to something different. Minimax is a combersome algorithm that has had it's time and isn't really popular in the AI community any more. If you're making a simple game, then minimax is too bulky to implement and you're better off using a simpler algorithm like you did in your game. If you're making a complex game, then the minimax tree tends to grow really huge and you have to bodge on loads of modifications to the algorithm to try and get it running fast enough to play the game. And it's only any good for turn-based games - once you start wanting to do a real-time game, minimax doesn't really apply any more.

    Also, you said you wanted to make games. Games are complex programs with many different parts. Don't be downhearted that you've got stuck with the AI part. You could always move on to a different aspect of game design for the time being and come back to AI later.

    A game needs at least these parts in it:
    • A physics engine to control how objects can move about in the world.
    • A rendering engine to control how objects are displayed on screen.
    • A control engine to handle starting new games, pausing, saving/loading, etc.
    • An AI engine to control the computer's behaviour.
    • Some graphics to make the game look good.
    Obviously, different games have these things to different levels of complexity. And you can choose which of them you want to work on at any time.

    I would suggest reading this introduction to games development. Remember that games take a lot of work and a long time to develop. That's why commercial games studios emply hundreds of people!
    This site has examples of games that students have made. Lots of them have source code, and you may even be able to contact the people who made them.

    Well done for getting the grade in your course. Programming is one of those things that is hard to teach, because so much of it is practice. Learning something new in programming usually follows this pattern (in my experience):
    • Find out about a cool new programming thing.
    • Wonder how to implement it.
    • Try to code it, and fail.
    • Bang head against wall.
    • Try to code it a different way, fail.
    • Bang head against wall.
    • Read about it, look at other people's code.
    • Go back to your old code, look at it again.
    • Throw away your old code and start again from scratch, fail.
    • Get a little further than before, but still fail.
    • Bang head against wall.
    • Do something else.
    • Suddenly have a revelation about what you were doing wrong.
    • Go back to your code and fix some things - get further with it.
    • Get stuck at a different problem.
    • Bang head against wall.
    • ... Repeat the above steps a few times ...
    • Finally manage to bodge together something that works. - Yipee!
    • Do something else
    • Come back a long time later to your old program - look at it - think "why the heck did I do it like that??!?
    • Re-write your old program in a day
    • Think - "that was actually quite simple - why was I having trouble with it?"
    Anyone else agree with this pattern of programming?
    The problem is that a lot of programming is very obvious once you've 'got it', but until you have, it seems like the hardest thing ever. But if you keep working at it then you will get it. You won't get it all at once - you will get different bits at different times, and there will always be something that seems like the hardest thing ever, and there will always be some things that you will never get, but that's life.

    Good luck with your games programming and remember that games programming isn't minimax - there are a million different things out there that you can do that are games programming. And never be afraid to ask questions!

  4. #13
    Rex Trayn is offline Newbie
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Re: need help with simple C++ TicTacToe game with AI

    I saw code for a very simple tic tac toe playing program - it was less that 50 lines of code I think.

    they used the concept of the "Magic square" ( sort of like a sudoku puzel) in which each of the 9 squares were assigned a number. ( I dont remember the numbers, but they are not hard to figure out)

    each row , column and diagonal added up to the same number. The coumpter just did a simple subtraction to pick the square that would complete the equation.

  5. #14
    Rex Trayn is offline Newbie
    Join Date
    May 2010
    Posts
    3
    Rep Power
    0

    Re: need help with simple C++ TicTacToe game with AI

    4 3 8 Magic square
    9 5 1
    2 7 6

    Each row, column and diagonal add up to 15 - to be used by tic tac toe program
    Last edited by Rex Trayn; 05-30-2010 at 10:28 AM. Reason: formatting changed when posted

Closed Thread
Page 2 of 2 FirstFirst 12

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Need help : Very simple C# game
    By static21 in forum C# Programming
    Replies: 5
    Last Post: 10-29-2010, 09:54 AM
  2. C++ Simple Game.
    By united07 in forum C and C++
    Replies: 2
    Last Post: 05-04-2010, 11:49 AM
  3. Help with a simple game.
    By Ascension in forum C# Programming
    Replies: 3
    Last Post: 03-10-2009, 06:06 PM
  4. just a simple guessing game
    By deryni in forum C and C++
    Replies: 2
    Last Post: 12-13-2008, 03:57 PM
  5. A simple TicTacToe game
    By Zunone in forum C and C++
    Replies: 1
    Last Post: 08-16-2007, 09:01 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts