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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:20:06+00:00 2026-05-27T02:20:06+00:00

To explain what I’m doing, I’m basically taking a string of X’s and O’s

  • 0

To explain what I’m doing, I’m basically taking a string of X’s and O’s as an input String, converting it to an array, so XXXOXOXOO as the input would be the same array. Then I’m checking each possible scenario where one could win, coordinate wise, and if it matches it through if statements it returns the number corresponding to who won.

But it’s not quite working, haha. It seems to only output O as the winner, in clear cases where X should win.

Could anyone lend a hand? Much appreciated in advance, thanks.

import java.util.Scanner;
public class TicTacToe {
    public static void main(String[] args) {

        Scanner keyboard = new Scanner(System.in);

        System.out.println("Type in a board for analysis");
        String b = keyboard.nextLine();

        drawBoard(b);

        int result = decide(b);

        if (result == 1) {
            System.out.println("Win for X");
        }
        else if (result == 0) {
            System.out.println("Win for O");
        }
        else if (result == 3)  {
            System.out.println("Draw");
        }
        else {
            System.out.println("Game Unfinished");
        }
   }

    public static int decide(String scheme) {
        int decision = 0;

        String boardScheme[] = new String[9];

        for (int i = 0; i < 9; i++) {
            boardScheme[i] = scheme.substring(i,i+1);
        }

        if ((boardScheme[0] == boardScheme[1] && boardScheme[0] == boardScheme[2]) || (boardScheme[0] == boardScheme[3] && boardScheme[0] == boardScheme[6]) || (boardScheme[0] == boardScheme[4] && boardScheme[0] == boardScheme[8])) {
            if (boardScheme[0] == "X") {
                decision = 1;
            }
            else {
                decision = 0;
            }
        }
        else if (boardScheme[3] == boardScheme[4] && boardScheme[3] == boardScheme[5]) {
            if (boardScheme[3] == "X") {
                decision = 1;
            }
            else {
                decision = 0;
            }
        }
        else if (boardScheme[6] == boardScheme[7] && boardScheme[6] == boardScheme[8]) {
            if (boardScheme[6] == "X") {
                decision = 1;
            }
            else {
                decision = 0;
            }
        }
        else if (boardScheme[1] == boardScheme[4] && boardScheme[1] == boardScheme[7]) {
            if (boardScheme[1] == "X") {
                decision = 1;
            }
            else {
                decision = 0;
            }
        }
        else if ((boardScheme[2] == boardScheme[5] && boardScheme[2] == boardScheme[8]) || (boardScheme[2] == boardScheme[4] && boardScheme[2] == boardScheme[6])) {
            if (boardScheme[2] == "X") {
                decision = 1;
            }
            else {
                decision = 0;
            }
        }

        return decision;
    }

    public static void drawBoard(String scheme) {
        if (scheme.length() == 9) {
            String board[] = new String[9];

            for (int i = 0; i < 9; i++) {
                board[i] = scheme.substring(i,i+1);
            }

            System.out.println(board[0] + " | " + board[1] + " | " + board[2]);
            System.out.println(board[3] + " | " + board[4] + " | " + board[5]);
            System.out.println(board[6] + " | " + board[7] + " | " + board[8]);
        }
        else {
            System.out.println("Please input 9 characters, no more, no less.");
        }
    }
}
  • 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-27T02:20:06+00:00Added an answer on May 27, 2026 at 2:20 am

    You are not covering all the possible cases. What would happen if you had missed a case? sorry, I went through them and indeed you do cover each case correctly. However, as noted by Hovercraft Full Of Eels in the comment to the question, you are comparing strings incorrectly. Still I would suggest the below.

    You are setting decision = 0, so whenever a case occur which you are not covering, then you will end up returning 0. Which will output ‘O’ wins.

    So, what you need to do is to figure out each possible case that can happen and then check for it. I would also suggest that you use codereview.stackexchange.com and ask for advice on how to restructure your code a bit, it will make it easier for you in the end.

    For example what if you had a function horizontalWin(int column) and verticalWin(int row) and diagonalWin().

    Good luck!

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

Sidebar

Related Questions

Please explain working in each case. Why both cases have same output? Case I:
Please explain from Linux, Windows perspectives? I am programming in C#, would these two
Please explain impersonation for non-technical users.Then please explain it in the context of ASP.NET.
Can someone explain the mechanics of a jump table and why is would be
Explain me please what this line doing: <a href={% url video.media.views.channel_browse slug=slug%}>Archie Channel</a> Actually
explain the rangevalidator control,is it take string also? explain thank you
Explain SQL (in phpmyadmin) of a query that is taking more than 5 seconds
To explain the domain... I have a bunch (1,000,000) of items, each of a
Please explain what this is doing with regards to calling a function and using
Explain why a nullable int can't be assigned the value of null e.g int?

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.