473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

tictactoe

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
2 1165

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Randi | last post by:
Hi, I have this assignment to make a Tic Tac Toe game, I believe I got the whoe thing done. I set up 9 lbl through the use of an array, I can check to see if there is a winner. But I don't know...
6
by: yolkman | last post by:
How do you alternate between 2 human players?
0
by: chandniashar | last post by:
This is my perl program for tictactoe. Can anyone help me run the program as a cgi script? #!/usr/bin/perl # Description: This program implements the game of Tic Tac Toe # The grid...
9
by: wizardRahl | last post by:
Hello, I'm attempting to write a TicTacToe program for class and need some help with arrays. We have to write a program that will allow two users to play tic-tac-toe. The program needs to have...
7
by: Warren Hoskins | last post by:
Old title: Homework Due 2-20-07 can"t understand why this will not compile. I've been working on tis all week end. Need Help desperately
12
Nepomuk
by: Nepomuk | last post by:
Hi! I want to have my program delete some folders including all contents. For that, I wrote this method: private static void delete(String source) { File tmp = new File(source);...
1
by: racshah | last post by:
I have a tictactoe script with 2 users playing. I need a perl script in which one user plays with the computer. Can anyone help me with it???
0
by: Sean McIlroy | last post by:
""" AUTHOR: Sean McIlroy LANGUAGE: Python 2.5 OVERVIEW: instances of "tictactoeplayer" play optimal tictactoe SPECIALIZED TYPES: player={ex,oh}; empty={blank}; cell=player+empty; board= TYPE...
6
by: 1051109210 | last post by:
Im tryin to create a tic tac toe ( more then 3x3 but startin with 3x3) with ai. the prob is gettin the ai to work. many eg of 3x3 are out there but they seem to be done based on if this then do this....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.