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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T11:35:23+00:00 2026-05-30T11:35:23+00:00

How can I check 3 elements of a single-dimensional array are equal to a

  • 0

How can I check 3 elements of a single-dimensional array are equal to a specific value?

Background: I am trying to check for the winner of a game of tic-tac-toe (noughts and crosses). In other words check for any lines in a 3×3 grid flattened into a single dimensional array.

Here is the code I have tried:

Attempt 1:

switch ()
{
    case board[(6&3&0) || (7&4&1)]: case board[7&4&1]: case board[8&5&2]: case board[0&1&2]: case board[3&4&5]: case board[6&7&8]: case board[6&4&2]: case board[8&4&0]:
    System.out.println("Equal to x")
    break;
}

Attempt 2:

if (board[7] , board[4] , board[1] == 'X')
system.out.println("Equal");

Attempt 3:

if ( board[6&3&0] == 'X'  || board[7&4&1] == 'X' || board[8&5&2] == 'X' || board[0&1&2] == 'X' || board[3&4&5] == 'X' || board[6&7&8] == 'X' || board[6&4&2] == 'X' || board[8&4&0] == 'X') {
    System.out.println("Equal to x");
}else if
( board[6&3&0] == 'O'  || board[7&4&1] == 'O' || board[8&5&2] == 'O' || board[0&1&2] == 'O' || board[3&4&5] == 'O' || board[6&7&8] == 'X' || board[6&4&2] == 'O' || board[8&4&0] == 'O'){
    System.out.println("Equal to o!");
}
  • 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-30T11:35:25+00:00Added an answer on May 30, 2026 at 11:35 am

    Your “playing-field” for tic-tac-toe looks like this:

    0 1 2
    3 4 5
    6 7 8
    

    With 0-8 being the indices in a char[] array:

    marks = new char[9];
    

    To check if we have three ‘x’ in a row:

    'x' == marks[0] == marks[3] == marks[6] // a vertical row
    'x' == marks[0] == marks[1] == marks[2] // a horizontal row
    'x' == marks[0] == marks[4] == marks[8] // a diagonal row
    

    This should get you started.

    EDIT: And this will compile:

    boolean win = marks[0] == 'x' && marks[3] == 'x' && marks[6] == 'x';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can I check out if one single list has two or more equal elements
HI how can i check if all elements in an array i created are
How can I check if all the elements inside a form textbox, checkbox, textarea,
Can Selenium or any other automated tool check for the proper positioning of elements
How can I check whether a particular element is inside an array? I don't
I am trying to parse through all elements in a single radio button (or
How can I easily check to see whether all the elements in one ArrayList
how can i make cool check-box and select-box elements in html form , that
I know you can check if all elements from one list exist in another
I'm trying to use $.trim() to trim whitespace in empty elements so I can

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.