tictactoe

S

Sonia

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
=================================================
 
M

Mike Wahler

Sonia said:
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
 
K

Karl Heinz Buchegger

Sonia said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,201
Messages
2,571,049
Members
47,653
Latest member
YvonneJif

Latest Threads

Top