You have to check few conditions for each time, need not to consider the sequence. Possible cases of win for any player would be:
I am assuming that numbering is row wise and started from 0.
Now you have to tell computer based on the input of user.
User has selected 2, so you have
posStatus = 1 Now the only cells you have to check are 0,1,4,5 and 6 as these are the only possible cells for win.
To handle the beginning of game situation, you have two options:
- Let the user start always.
- Start anyone from 0,2,6 and 8 as these are part of the maximum solutions.
There are 3 possibilities of success:
A. Row wise : Add or substitute number by 1 and check status ,
B. Column wise : Add or substitute number by 3 and check status ,
C. Diagonally : Add or substitute number by 2 AND 4 then check status
For example if user has selected
i cell. Your check should be:
A. See the status of
(i-2),(i-1),(i+1) AND (i+2) - Obviously this should be in range of 0-8
B. See the status of
(i-6),(i-3),(i+3) AND (i+6)
C. See the status of
(i-8),(i-4),(i-2),(i+2),(i+4) AND (i+8)