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 4046522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T13:34:26+00:00 2026-05-20T13:34:26+00:00

For those who don’t know what Pentago is, it’s not really that important to

  • 0

For those who don’t know what Pentago is, it’s not really that important to the problem, but suffice to say that you have a 6×6 board with four quadrants. Each player takes turns placing a piece and then rotating a quadrant. The game is won when one player gets five in a row (either before or after the player’s rotate phase).

I’m writing an algorithm to play many different random Pentago games. However, since it’s completely random, I see no good way to get around checking to see if someone wins in between the place and rotate phase of the turn (otherwise, you might accidentally rotate the winning move). Eventually, I plan on rewriting this to where it has a little bit more strategy involved instead of completely random, but this is for statistical purposes, so randomness does just fine (and in fact is quite useful in some ways).

Anyways, currently I am programming in Matlab, and an empty board looks like this

eeeeee
eeeeee
eeeeee
eeeeee
eeeeee
eeeeee

As the game progresses, the board fills with w‘s and b‘s. The way that I check for a winning board is quite literally iterating through every column and every row (and every diagonal) to see if there is a winner by performing a regular expression check on the “string” that is returned.

In short, my question is this:

Is there a more efficient method for determining the winners of a Pentago board?

  • 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-05-20T13:34:27+00:00Added an answer on May 20, 2026 at 1:34 pm

    EDIT:

    There are two solutions I’ve come up with: one based on convolution (using the function CONV2) and one based on brute-force indexing for all possible strings of 5 elements (similar to b3’s newer answer). Here they are:

    Convolution:

    function winner = pentago_winner_conv(board)
    %# Input should be a 6-by-6 matrix with:
    %#   - 0 for an empty space
    %#   - 1 for pieces from one player
    %#   - -1 for pieces from the other player
    %# The return value is 0 for no winner, -1 or 1 for the winning player,
    %#   or 2 if there is a draw.
    
      metric = [conv2(board,ones(1,5),'same') ...     %# Check horizontal strings
                conv2(board,ones(5,1),'same') ...     %# Check vertical strings
                conv2(board,eye(5),'same') ...        %# Check diagonal strings
                conv2(board,fliplr(eye(5)),'same')];  %# Check anti-diagonal strings
      limits = [min(metric(:)) max(metric(:))];  %# Find the min and max values
      limits = fix(limits/5);                    %# Convert them to -1, 0, or 1
      if all(limits)
        winner = 2;            %# A draw condition
      else
        winner = sum(limits);  %# Find the winner, if any
      end
    
    end
    

    Indexing:

    function winner = pentago_winner_brute(board)
    %# Input should be a 6-by-6 matrix with:
    %#   - 0 for an empty space
    %#   - 1 for pieces from one player
    %#   - -1 for pieces from the other player
    %# The return value is 0 for no winner, -1 or 1 for the winning player,
    %#   or 2 if there is a draw.
    
      index = reshape(1:36,6,6);
      index = [index(1:5,:).'; index(2:6,:).'; ...  %# Vertical string indices
               index(:,1:5); index(:,2:6); ...      %# Horizontal string indices
               1:7:29; 2:7:30; 7:7:35; 8:7:36; ...  %# Diagonal string indices
               5:5:25; 6:5:26; 11:5:31; 12:5:32];   %# Anti-diagonal string indices
      metric = sum(board(index),2);
      limits = [min(metric) max(metric)];  %# Find the min and max values
      limits = fix(limits/5);              %# Convert them to -1, 0, or 1
      if all(limits)
        winner = 2;            %# A draw condition
      else
        winner = sum(limits);  %# Find the winner, if any
      end
    
    end
    

    Out of curiosity, I thought I’d measure the speed and accuracy of these solutions versus b3’s newer answer. I created 4 test boards: -1 wins, no winner, 1 wins, both win (draw). Then I ran each solution 10,000 times for each board and timed them. Here are the results:

                   |   Calculated winner
    ---------------+-----------------------
    convolution    |  -1     0     1     2
    indexing       |  -1     0     1     2
    b3 solution    |  -1     0     1    -1
    
                   |   Running time for 10,000x (seconds)
    ---------------+---------------------------------------
    convolution    |  0.4863    0.5305    0.5248    0.4787
    indexing       |  0.1706    0.1770    0.1755    0.1889
    b3 solution    |  0.6607    1.3958    1.4223    0.7507
    

    Note that b3’s solution fails to detect a draw. Even though the code for the convolution-based solution was the shortest and easiest to implement (I didn’t have to create a list of indices by hand), the indexing solution I give above ends up being the fastest.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For those who don't know, FizzBuzz is the following problem: Write a program that
I have two Resources files under App_GlobalResources MyApp.resx MyApp.sv.resx for those who don't know:
I have a website that I host with PHPFog. PHPFog, for those who don't
In the game of Rummikub, for those who don't know it, you have tiles
Does python have an equivalent to Tcl's uplevel command? For those who don't know,
For those who don't know the model. You can read this pdf . I
For those who don't know GNASH is an open source flash movie player. It
I'm learning processing (for those who don't know its a java-based language geared towards
Short Version For those who don't have the time to read my reasoning for
While poking around zeroMQ (A very useful socket replacement for those who don't know)

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.