Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9005859
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T01:15:24+00:00 2026-06-16T01:15:24+00:00

I’m building a program that plays Connect 4, and one of the things to

  • 0

I’m building a program that plays Connect 4, and one of the things to check for is cases where your opponent has the following board positions:

[0,1,1,0,0] or [0,1,0,1,0] or [0,0,1,1,0]

where your opponent is one move away from having three pieces in a row with a blank on either side. If you don’t fill one of the middle elements on your next move, your opponent can go there and force a checkmate.

What I have is a board of 42 squares, numbered 1:42. And I created a matrix called FiveCheck, where each row maps to five consecutive board positions. For example:

    FiveCheck(34,:) = [board(7),board(14),board(21),board(28),board(35)];
    FiveCheck(35,:) = [board(14),board(21),board(28),board(35),board(42)];

are two of the diagonals of the board.

I can test for the possible checkmate with

    (sum(FiveCheck(:,2:4),2) == 2 + sum(FiveCheck,2) == 2) == 2

And that gives me a vector with 1’s indicating that the corresponding FiveCheck row has a possible checkmate. Let’s say the 34th element of that vector has a 1, and the pattern for that diagonal (from the example given above) is [0,0,1,1,0]. How do I return 14, the board position I should move to?

Another separate example, if the 35th element of that vector has a 1, and the pattern for that diagonal is [0,1,0,1,0], how do I return 28?

EDIT: I just realized this is impossible without some sort of a map. So I created FiveMap, a matrix the same size of FiveCheck, with the same formulas except the word “board” is removed. For example:

    FiveMap(34,:) = [(7),(14),(21),(28),(35)];
    FiveMap(35,:) = [(14),(21),(28),(35),(42)];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T01:15:26+00:00Added an answer on June 16, 2026 at 1:15 am

    Since you are dealing with binary vectors of size 5, a very efficient solution might be the use of a look up table.

    Consider board to be a binary matrix. you can filter it with 4 filters (of length 5), representing horizontal, vertical and two diagonals to identify possible positions you are looking for. Then, for specious locations, you can extract the 5 binary bits and use a look up table of size 32 to get the offset to the position where you should place your piece.

    a small example:

    % construct LUT
    LUT = zeros(32,2); % dx and dy offsets for each entry
    LUT(12,:) = [ 1 0 ]; % covering the case [0 1 1 0 0] - put piece 1 to the right of center
    % continue constructing LUT here...
    
    horFilt = ones(1, 5);
    resp = imfilter( board, horFilt ); % need to think about board''s boundaries here...
    [yy xx] = find( resp == 2 ); all locations where filter caught 2 out of 5
    pat = board( yy, xx + [ -2 1 0 1 2] ); % I assume here only one location was found
    pat = bin2dec( '0'+char( pat ) ); % convert binary pattern to decimal entry
    board( yy + LUT(pat,2) , xx + LUT(pat, 1) ) = ... ; % your move here...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
Basically, what I'm trying to create is a page of div tags, each has
I have a small JavaScript validation script that validates inputs based on Regex. I
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
In my XML file chapters tag has more chapter tag.i need to display chapters

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.