-
February 18th, 2011, 11:46 PM
#1
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.
-
February 19th, 2011, 02:55 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|