CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2011
    Posts
    1

    Smile simple tic tac toe

    Hi
    I would like to write simple code for Tic-Tac toe that just checks the rows and columns and diagonals for X or O and for X if it matches , it returns 1 and for O returns 0 and otherwise return -1.(I have to define function called hasWon to do this)
    How Can I do that ?

    Code:
    #include<iostream>
    using namespace std;
    int hasWon(char board[3][3]);
    int main()
    {
        int i,j;
    	
    	char board[3][3]={{'X','X','O'},
    					  {' ','X','O'},
    					  {' ','O','O'}};
    	for(i=0; i<3; i++)
    	{
    		for(j=0; j<3; j++)
    			cout<<board[i][j]<<"\t";
    	cout<<endl;
    	}
    }
    I could like just up to initialization of board and then what should I do to check rows and columns and diagonals.?
    Any response will be highly appreciated.

  2. #2
    Join Date
    Aug 2009
    Posts
    78

    Re: simple tic tac toe

    I dont want to give you the solution but you can do that:
    choose column - lets say 0 ,
    For each index in column 0 (3 items) you check the row and column in the matrix.

    You can check the diagonals at the end in a separate loop .

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured