View Single Post
Old 02-28-2013, 05:42 PM   PM User | #2
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,452
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
i won't do your homework for you, but i can point you to http://danml.com/pub/connect4.html, where i have a working copy of the same game for you to inspect. it's not the best code ever, i wrote it in an afternoon, but it seems to work ok.


a row win is easy: cast the board into a string, each char meaning red or black or empty.
then you can simply String.match() that string to find a row winner.

a col winner is almost as easy; just transpose the board and again look for row winners.


the diag win was one that gave me some pause. one easy way is to hard-code all the diags, but that's cheap. I came up with a way to transpose the board in diagonals (both NW and NE), and once again used the string match() row-winner finder to examine the result for a winner.

all the win detection code spans from line 222-300 (ignore the comments).
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.8% IE9:11.4% IE10:6.5%
rnd me is offline   Reply With Quote