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

  • SEARCH
  • Home
  • 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 623847
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:08:14+00:00 2026-05-13T19:08:14+00:00

Post your shortest code, by character count, to check if a player has won,

  • 0

Post your shortest code, by character count, to check if a player has won, and if so, which.

Assume you have an integer array in a variable b (board), which holds the Tic Tac Toe board, and the moves of the players where:

  • 0 = nothing set
  • 1 = player 1 (X)
  • 2 = player 2 (O)

So, given the array b = [ 1, 2, 1, 0, 1, 2, 1, 0, 2 ] would represent the board

X|O|X
-+-+-
 |X|O
-+-+-
X| |O

For that situation, your code should output 1 to indicate player 1 has won. If no-one has won you can output 0 or false.

My own (Ruby) solution will be up soon.

Edit: Sorry, forgot to mark it as community wiki. You can assume the input is well formed and does not have to be error checked.


Update: Please post your solution in the form of a function. Most people have done this already, but some haven’t, which isn’t entirely fair. The board is supplied to your function as the parameter. The result should be returned by the function. The function can have a name of your choosing.

  • 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-13T19:08:14+00:00Added an answer on May 13, 2026 at 7:08 pm

    C, 77 (83) characters

    This is a variant of dmckee‘s solution, except that each pair of digits in the Compact Coding is now the base-9 digits of the ASCII characters.

    The 77-char version, does not work on MSVC:

    // "J)9\t8\r=,\0" == 82,45,63,10,62,14,67,48,00 in base 9.
    char*k="J)9 8\r=,",c;f(int*b){return(c=*k++)?b[c/9]&b[c%9]&b[*k--%9]|f(b):0;}
    

    This 83-char version, should work on every C compiler:

    f(int*b){char*k="J)9    8\r=,",s=0,c;while(c=*k++)s|=b[c%9]&b[c/9]&b[*k%9];return s;}
    

    (Note that the spaces between the 9 and 8 should be a tab. StackOverflow converts all tabs into spaces.)


    Test case:

    #include <stdio.h>  
    void check(int* b) {
        int h0 = b[0]&b[1]&b[2];
        int h1 = b[3]&b[4]&b[5];
        int h2 = b[6]&b[7]&b[8];
        int h3 = b[0]&b[3]&b[6];
        int h4 = b[1]&b[4]&b[7];
        int h5 = b[2]&b[5]&b[8];
        int h6 = b[0]&b[4]&b[8];
        int h7 = b[2]&b[4]&b[6];
        int res = h0|h1|h2|h3|h4|h5|h6|h7;
        int value = f(b);
        if (value != res)
            printf("Assuming f({%d,%d,%d, %d,%d,%d, %d,%d,%d}) == %d; got %d instead.\n", 
                b[0],b[1],b[2], b[3],b[4],b[5], b[6],b[7],b[8], res, value);
    }
    #define MAKEFOR(i) for(b[(i)]=0;b[(i)]<=2;++b[(i)])
    
    int main() {
        int b[9];
    
        MAKEFOR(0)
        MAKEFOR(1)
        MAKEFOR(2)
        MAKEFOR(3)
        MAKEFOR(4)
        MAKEFOR(5)
        MAKEFOR(6)
        MAKEFOR(7)
        MAKEFOR(8)
            check(b);
    
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

1st post on stackoverflow, hope to have great feedback :) I'm currently trying to
I have to POST some parameters to a URL outside my network, and the
In handling a form post I have something like public ActionResult Insert() { Order
Post your best solutions! You can find the full problem description and examples here:
Can you post your most tricky and useful commands while you run a debugger
PLEASE READ THE FULL QUESTION BEFORE POST YOUR ANSWER Hello guys! I'm currently developing
From Flickr's community guidelines: Do link back to Flickr when you post your photos
For example, I ask this question and click Post your question and stay in
first post don't hurt me :) I am using a BorderLayout with the usual
JavaScript post request like a form submit shows you how to submit a form

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.