Thread: How to code in c++ programming for tic tac toe(player vs computer)AI using arrays

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    How to code in c++ programming for tic tac toe(player vs computer)AI using arrays

    Eh, anyone can help me how to code for the c++ programming AI tic tac toe . I completely stuck like .........

  2. #2
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Code:
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
       
       char square[3][3] = {{' ',' ',' '},{' ',' ',' '},{' ',' ',' '}};
       
       bool validrow, validcol, validmove;
       int row, col;
       char winner = ' ';
       char turn = 'X';
    
    		cout << "Welcome to tic tac toe game" << endl;
       
    	  while (winner == ' ')
    	  {
    		
    		cout << "    1   2   3" << endl;
    		cout << " ---------------" << endl;
    		for(int i = 1; i < 4; i++)
    		{
    			cout << " " << i << " | " << square[i-1][0] << " | " << square[i-1][1] << " | " << square[i-1][2] << " |" << endl << " ---------------" << endl;
    		}
    			 validrow = false;
    			 validmove = false;
    			 validcol = false;
    			while(!validmove)
    			{
    				validrow = false;
    				//Loop until the player selects a valid row
    				while(!validrow)
    				{
    					cout << "Row: ";
    					cin >> row;
    					if (row == 1 || row == 2 || row == 3)
    					{
    						validrow = true;
    					}
    					else
    					{
    						cout << endl << "Invalid row!" << endl;
    					}
    				}
               
             //Loop until the player selects a valid column
    				while(!validcol)
    				{
    					cout << "Column: ";
    					cin >> col;
    					if(col == 1 || col == 2 || col == 3)
    					{
    						 validcol = true;
    					}
    					else
    					{
    						cout << endl << "Invalid column!" << endl;
    					}
    				}
    				validmove = true;
    			
                    bool validturn;
    				validturn = false;
    
    			while (!validturn)
    			{
    			if (square[row-1][col-1] == ' ')
    			{
    				square[row-1][col-1] = turn;
    				if (turn == 'X')
    				{
    					turn = 'O';
    				}
    				else
    				{
    					turn = 'X';
    				}
    			
    			
    			
    			
                
    			
    
    			if (row == 1  || row == 3 || col == 1 || col == 3 )
    			{
    				
    				square[1][1] = turn;
    				cout << "Your next move" << endl;
    				if (row == 1  || col == 1)
    				{
    					square[2][2] = turn;
    					cout << "Your next move" << endl;
    					validturn = true;
    				}
    				validturn = true;
    				
    			}
    			else if (row == 2 || col == 2)
    			{
    				square[2][1] = turn;
    				cout << "Your next move" << endl;
    				validturn = true;
    				
    			} 
    			
                else
    		    {
                    cout << "The selected square is occupied!" << endl;
    			    cout << "Select again:" << endl;
    			}
    			}
    			}
    			}
    			  	
    
    			
    			
    	  }
    
    			
       
       system ("pause");
       return 0;
    }
    This is my code. I completely stuck

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    First, indent your code properly.
    Then, I figure you are stuck on a specific part, so ask specific questions.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    any1 give reply on how to start on the tic tac game with first? Any steps before i trying coding. need help badly. Thank you.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    IF you need help, ask a specific question about what you need help with. What are you stuck on?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    I am not sure what you are asking, but as I see it, there is no AI implemented at all yet?

    • Have you tried meta-planning?

    • How does the computer win the game? 3 in-a-row, of course, so check if it can do that first ,and make the computer do it.

    • What is the first step to getting to 3 in-a-row? 2 in-a-row. check to see if the computer can get two in a row, with the possibility to make it three in the next turn (No sense in going for two in a row, where the third one is already occupied by the enemy!).

    • I'd also say, if the center wasn't occupied, go for that one first.

    A little backwards, but I'm sure you can make sense of it

    As my tutor said, there are no big problems in programming... only a lot of smaller ones! Break the big problems into small chunks and it will be soo much easier.

    PS. I'm sorry I can't give you any examples, I'm new at this too

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    You need a checkWin function or piece of code before you start making any AI.
    This topic has been covered several times before on this forum if you care to do a search.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    How to start of with? Any method on how should I start first.
    Player against computer:
    The current state of the board should be displayed at all times. The program should be able to detect a winning combination and stop the game at
    that point. (This is my school question)

    I just need a simple tic tac toe AI using only like arrays, control selection(if/else) and loops and not graphics

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Tic Tac Toe Player vs Computer
    By chakolate in forum C++ Programming
    Replies: 6
    Last Post: 04-18-2011, 11:13 PM
  2. HowTo code a C++ Internet Radio Player?
    By martinuk in forum C++ Programming
    Replies: 7
    Last Post: 05-08-2009, 12:33 AM
  3. Is it a watch? Is it an MP3 player? Is it a video player?
    By twomers in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 08-15-2006, 10:54 PM
  4. 'now playing' code for windows media player?
    By X PaYnE X in forum Windows Programming
    Replies: 0
    Last Post: 05-25-2006, 02:23 AM
  5. What inspired you to become a computer programming?
    By ZooTrigger1191 in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 02-10-2003, 07:03 AM