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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:49:38+00:00 2026-06-01T12:49:38+00:00

I am making a TicTacToe game in Java I am required to use a

  • 0

I am making a TicTacToe game in Java I am required to use a two-dimensional array and to use recursion to check if there is a winner.

I feel like I could easily do a non-recursive check for a winner, but I don’t know where to start if I were to use recursion to do it because I am very new at recursion. Could someone guide me on where to start the process of such an algorithm?

  • 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-01T12:49:39+00:00Added an answer on June 1, 2026 at 12:49 pm

    Assuming your board looks something like:

                |            |
     cell[0][0] | cell[1][0] | cell[2][0]
                |            |
    ------------+------------+------------
                |            |
     cell[0][1] | cell[1][1] | cell[2][1]
                |            |
    ------------+------------+------------
                |            |
     cell[0][2] | cell[1][2] | cell[2][2]
                |            |
    

    One way is to simply check adjacent cells recursively (in a single direction). For example (pseudo-code):

    def checkSame (val, cellX, cellY. deltaX, deltaY):
        # No winner if check value is empty.
    
        if val == empty: return false
    
        # Winner if we've gone off edge. No need to worry about < 0
        # since one direction is always ascending but I've left it
        # in anyway.
    
        if cellX > 2 or cellY > 2: return true
        if cellX < 0 or cellY < 0: return true
    
        # No winner if piece has changed.
    
        if cell[cellX][cellY] != val: return false
    
        # Otherwise use recursion to check next one.
    
        return checkSame (val, cellX + deltaX, cellY + deltaY, deltaX, deltaY)
    

    Then, we just have to check the eight possible start-point/direction values:

    # Check rows.
    
    if checkSame (cell[0][0], 0, 0, 1, 0): return true
    if checkSame (cell[0][1], 0, 1, 1, 0): return true
    if checkSame (cell[0][2], 0, 2, 1, 0): return true
    
    # Check columns.
    
    if checkSame (cell[0][0], 0, 0, 0, 1): return true
    if checkSame (cell[1][0], 1, 0, 0, 1): return true
    if checkSame (cell[2][0], 2, 0, 0, 1): return true
    
    # Check diagonals.
    
    if checkSame (cell[0][0], 0, 0, 1, 1): return true
    return checkSame (cell[0][2], 0, 2, 1, -1)
    

    Now, granted that’s a fairly limited (and contrived) use of recursion but, as you say, it’s not really a situation suited to recursion anyway. Far better is just to use eight if statements, provided you’re not planning of expanding this to more than standard 3×3 tic-tac-toe.

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

Sidebar

Related Questions

Im making a game in Java with a few other people but we are
I built a TicTacToe game for android. And i want that there should be
Making an adobe flex ui in which data that is calculated must use proprietary
Making UML sequence diagram in VS 2010RC I've observed that there is no activation
Making websites that appear correctly in IE is a big problem. Is there any
Making a new shooter game here in the vein of Galaga (my fav shooter
Making a search result list (like in Google) is not very hard, if you
Im making slide show with jquery How i can set interval between two function
making a multi-language site with codeginiter. I have created two folders. One for french
Making the transition from Vim to gVim, I would like to disable all toolbars

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.