By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

tictactoe

P: n/a
I have come across this solution to a tic tac toe game. It basically checks
who won the game:
Is there a better more efficient way of testing if the tic tac toe has been
won/lost or is over?
Thanks in advance,

==========================
function User_Won : boolean
if (takenx (1) = true and takenx (2) = true and takenx (3) = true) and
(takeny (1) = 1 and takeny (2) = 1 and takeny (3) = 1)
or (takenx (4) = true and takenx (5) = true and takenx (6) = true) and
(takeny (4) = 1 and takeny (5) = 1 and takeny (6) = 1)
or (takenx (7) = true and takenx (8) = true and takenx (9) = true) and
(takeny (7) = 1 and takeny (8) = 1 and takeny (9) = 1)
or (takenx (1) = true and takenx (5) = true and takenx (9) = true) and
(takeny (1) = 1 and takeny (5) = 1 and takeny (9) = 1)
or (takenx (7) = true and takenx (5) = true and takenx (3) = true) and
(takeny (7) = 1 and takeny (5) = 1 and takeny (3) = 1)
or (takenx (1) = true and takenx (4) = true and takenx (7) = true) and
(takeny (1) = 1 and takeny (4) = 1 and takeny (7) = 1)
or (takenx (2) = true and takenx (5) = true and takenx (8) = true) and
(takeny (2) = 1 and takeny (5) = 1 and takeny (8) = 1)
or (takenx (3) = true and takenx (6) = true and takenx (9) = true) and
(takeny (3) = 1 and takeny (6) = 1 and takeny (9) = 1) then
who_won_game := "player 1"
end if
end User_Won

function Computer_Won : boolean
if (takenx (1) = true and takenx (2) = true and takenx (3) = true) and
(takeny (1) = 2 and takeny (2) = 2 and takeny (3) = 2)
or (takenx (4) = true and takenx (5) = true and takenx (6) = true) and
(takeny (4) = 2 and takeny (5) = 2 and takeny (6) = 2)
or (takenx (7) = true and takenx (8) = true and takenx (9) = true) and
(takeny (7) = 2 and takeny (8) = 2 and takeny (9) = 2)
or (takenx (1) = true and takenx (5) = true and takenx (9) = true) and
(takeny (1) = 2 and takeny (5) = 2 and takeny (9) = 2)
or (takenx (7) = true and takenx (5) = true and takenx (3) = true) and
(takeny (7) = 2 and takeny (5) = 2 and takeny (3) = 2)
or (takenx (1) = true and takenx (4) = true and takenx (7) = true) and
(takeny (1) = 2 and takeny (4) = 2 and takeny (7) = 2)
or (takenx (2) = true and takenx (5) = true and takenx (8) = true) and
(takeny (2) = 2 and takeny (5) = 2 and takeny (8) = 2)
or (takenx (3) = true and takenx (6) = true and takenx (9) = true) and
(takeny (3) = 2 and takeny (6) = 2 and takeny (9) = 2) then
who_won_game := "computer"
end if
end Computer_Won
=================================================

Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"Sonia" <sm*****@hotmail.com> wrote in message
news:fx*****************@bignews1.bellsouth.net...
I have come across this solution to a tic tac toe game.
That's not C++ (Pascal?). The topic here is C++.

It basically checks
who won the game:
Is there a better more efficient way of testing if the tic tac toe has been won/lost or is over?


Of course, in C++, the better way is to use C++ code.
How to actually write the code for checking the winner
depends upon how the program represents the game board.

If you want to try to write such a program, and get
stuck, post your code here, ask specific questions,
and you'll get plenty of assistance.

-Mike
Jul 23 '05 #2

P: n/a
Sonia wrote:

I have come across this solution to a tic tac toe game. It basically checks
who won the game:
Is there a better more efficient way of testing if the tic tac toe has been
won/lost or is over?


There are various ways, this mess can be be written in a more readable way.
One possible way might eg be, to define an array of possible win situations
and use a loop to inspect, if one of them is present on the board.

BTW: Looking at that code snippet, it seems to me, that you complicated
your program a lot by introducing an overly complicated data structure.
It seems that there are at least 18 variables (2 arrays of 9) to represent
9 fields (where each field can have one of: free, taken_by_player, taken_by_computer)

I would suggest to step back, look at your code from a more global perspective
and decide if it wouldn't be better to change the whole data structure und thus
simplifying all of that mess.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.