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 ?


#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.
Pretend that the game is won by filling entire rows only. What you could do is establish the balance for each row. For X, you increase the balance for the row. For O, you decrease the balance. If it is neither, you do neither. If the balance for some row ends up -3 or +3, you can establish the winner. If it ends up between -2 and +2, you continue by checking the next row. If all rows had been checked and a winner could not be established, the game continues with the next player's move.

Another option, for this simplified case, is to maintain the balance for each row as a separate variable that you update after each player's move. If an X is placed in some position, the balance of the respective row is increased during that move. If an O is written, the balance of the respective row is decreased during that move. If after modification the balance of the row becomes -3 or +3 you announce the winner and the game is over. If the balance is still between -2 or +2 for that row (the one that was changed), then you give the other player the right to move. The balances are kept in separate variables as I said. So you will need array of 3, one for each row.

In reality, you have to consider not only the rows, but also the columns. This should be easy. You just have to swap the indexes during iteration. You also have to handle the primary and secondary diagonals. The primary is formed from the elements with equal first and second indices ([i][i]), and the secondary is formed from the elements with the first index subtracted from the maximum of the second index ([i][2-i]).

EDIT: If you use the strategy that incrementally updates the balances after each player's turn, then you simply need 3 more variables for the three columns (array of 3) and 2 more variables for each diagonal. You must update the row and column balances for each move. The primary and/or secondary diagonal balances would be updated only for writing in diagonal entries.

EDIT II: There is also the possibility for pre-computing the state of the game (O wins, X wins, no winner) for each of the 19683 hypothetical configurations of the board. It would take some K space, and generator program to provide the values, but then you will perform very fast look-up. Well, not so much faster, either. I just mention it for completeness.

Regards
Last edited on
Hey chess....
do you want your function jto simply check and decide as to who has won ?
This function is NOT concerned about filling in the X or O ?
Topic archived. No new replies allowed.