🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: alt.comp.lang.learn.c-c++
Article #218

Re: TIC-TAC-TOE in C++ !

#218
From: Risto Lankinen
Date: Thu, 15 Jan 1998 00:00
66 lines
2922 bytes
Hi!

Jon Watson wrote:

> My question is this:
>
> I currentl have:
>
> One loop to search rows for X's
> One loop to search columns for X's
> One loop to search the diagonals for X's
> One loop to search rows for O's
> One loop to search columns for O's
> One loop to search diagonals for O's.
>
> This is quite a bit of code. I thought about simply searching the board for
> two successive squares
> that aren't blank, but that won't work because I don't want it to flag an X
> and an O in one row
> as a potential win or block, so it seems it has to search for each
> individually.
>
> Finally, the question! Any ideas on how to compact this code so that it
> doesn't take up
> 100 lines?

First, notice that you can label a 3x3 board with numbers 1..9 in the "magic
square"
fashion (ie. where every row, column and diagonal yields 15 when summed).
Second,
notice that there are no more than 8 ways to choose three numbers from
{1,2,...,9} so that
their sum is exactly 15.  These two facts are both necessary and sufficient
conditions for
the transformation of Tic-Tac-Toe to the equivalent game:  Two players
alternating
turns in choosing an unchosen integer {1,2,..,9} with the aim of getting three
integers
that add exactly to 15.

Then, take a 16-bit integer and label its bits 0..15 , starting from the least
significant.
Let bit N be 1 if the sum N can be reached using the currently chosen set of
integers,
and 0 otherwise.  Then, entering a move is as short as...

   bitVector != (bitVector << nextMove);

... and detecting a win is as short as detecting if the 15th bit is set...

   if( bitVector & 0x8000 ) ...

Note that the bit vector must be initialized to 1 (ie. the 0th bit must be
initially set,
but this is consistent with the definition; that 0 can actually be reached by
summing
zero different integers) before entering the first move.

terv: Risto L.

--
Risto Lankinen           |     THIS ARTICLE HAS BEEN MODIFIED FROM ITS     |
Sr Technical Staff       |     ORIGINAL FORMAT.  IT HAS BEEN FITTED TO     |
ORACLE Corporation       |       THIS WINDOW AND EDITED FOR CONTENT.       |
import std.disclaimer.*;
Technical Details
Message-ID: <34BEA2B6.578B90E@us.oracle.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <01bd1f31$0a72dbc0$db4522cf@jwatson>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10d4ba,750a5b3b86788347
X-Google-Attributes: gid10d4ba,public
X-Google-Thread: 114592,750a5b3b86788347
X-Google-Attributes: gid114592,public
X-Google-Thread: 109fba,750a5b3b86788347
X-Google-Attributes: gid109fba,public
From: Risto Lankinen <rlankine@us.oracle.com>
Subject: Re: TIC-TAC-TOE in C++ !
Date: 1998/01/15
Message-ID: <34BEA2B6.578B90E@us.oracle.com>
X-Deja-AN: 316369748
Content-Transfer-Encoding: 7bit
References: <01bd1f31$0a72dbc0$db4522cf@jwatson>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Organization: Oracle Corporation. Redwood Shores, CA
Newsgroups: alt.comp.lang.learn.c-c++,att.lang.c++,comp.lang.c++