Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,175 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,582 people online right now. Registration is fast and FREE... Join Now!




Need help with some logic

 

Need help with some logic

GiveMeYourBagel

5 Nov, 2009 - 06:50 PM
Post #1

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 45

I'm trying to make a tic tac toe program, but i'm not sure where to start. I need some help on using 2D arrays to set the spot on the grid where x or o goes, any suggestions on how to start? Just logic not the code.

User is offlineProfile CardPM
+Quote Post


macosxnerd101

RE: Need Help With Some Logic

5 Nov, 2009 - 06:55 PM
Post #2

Self-Trained Economist
Group Icon

Joined: 27 Dec, 2008
Posts: 1,886



Thanked: 208 times
Dream Kudos: 325
Expert In: Java

My Contributions
Are you trying to design your own Artificial Intelligence program to play against a user or just place the Xs and Os? If you are doing the latter, just use a Random object calling the nextBoolean() method. And don't forget to check to make sure that there are either 4 Xs and 5 Os or 5 Xs and 4 Os. If you need more help, show us that you have put some more thought into your program. We will be glad to help you; we just need to see evidence that you are actually trying. Good luck! smile.gif
User is online!Profile CardPM
+Quote Post

pbl

RE: Need Help With Some Logic

5 Nov, 2009 - 08:53 PM
Post #3

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,966



Thanked: 1188 times
Dream Kudos: 450
My Contributions
There is not much logic (artificial inteligence) involved in a TicTacToe game... just brute force
for every free spot what hapen if computer plays there ? then for every free spots left what happens if user plays there ?
repeat he process until there is no more free spot
if one of the play (here a bit of AI) makes the computer sure to win take that one
User is offlineProfile CardPM
+Quote Post

GiveMeYourBagel

RE: Need Help With Some Logic

6 Nov, 2009 - 01:50 PM
Post #4

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 45

I'm not trying to make an AI program, i just gotta put the user input into the correct spot. I guess im just having trouble with that. It's the 2D arrays thats confusing me. So something like the user inputs the position and it assigns it to the 2D array. I guess i need help with 2D arrays, they confuse me and im not sure how to assign something to them. Another thing im not sure about is the grid, i have no idea how to do that

This post has been edited by GiveMeYourBagel: 6 Nov, 2009 - 01:55 PM
User is offlineProfile CardPM
+Quote Post

GiveMeYourBagel

RE: Need Help With Some Logic

7 Nov, 2009 - 09:11 AM
Post #5

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 45

Can someone explain to me how you can assign 2D arrays from a user input? or show an example?
User is offlineProfile CardPM
+Quote Post

GiveMeYourBagel

RE: Need Help With Some Logic

7 Nov, 2009 - 09:19 PM
Post #6

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 45

I still need help on how to put the X's and O's on the grid at the correct position

CODE
import java.util.Scanner;

public class TicTacToeMain {

  public static void main(String[] args) {
      int position;
    Scanner keyboard = new Scanner(System.in);
      System.out.println ("Welcome to Tic Tac Toe!");


    System.out.println ("Enter the position of your piece. Enter the Row first then the Column");
      int row = keyboard.nextInt();
      int col = keyboard.nextInt();

    int[][] grid = new int[3][3];
    
    for (int r = 0; r < 3; r ++)
        for (int c = 0; c < 3; c++)
            grid[row][col]= ' ';
          


      
        

  }
    
    
}

User is offlineProfile CardPM
+Quote Post

GiveMeYourBagel

RE: Need Help With Some Logic

8 Nov, 2009 - 12:14 PM
Post #7

New D.I.C Head
*

Joined: 24 Sep, 2009
Posts: 45

Here's some newer code that i came up with, but i still need help with outputting the x's and o's in the correct position

CODE
import java.util.Scanner;

public class TicTacToeMain {

  public static void main(String[] args) {
      int position;
      int turn = 0;
      boolean result = false;
    Scanner keyboard = new Scanner(System.in);
      System.out.println ("Welcome to Tic Tac Toe!");


    char[][] grid = new char[3][3];

    while (result == false) {
        
    if (turn % 2 == 0){
        System.out.println ("Player 1, it is your turn");
        System.out.println ("Please input the row and column you wish you play on");
          int row = keyboard.nextInt();
          int col = keyboard.nextInt();
          
          for (int r = 0; r < 3; r ++)
            for (int c = 0; c < 3; c++)
                grid[row][col]= ' ';
            
        grid[row][col] = 'X';
        System.out.println(grid[row][col]);
        }        
    
    else {
        System.out.println ("Player 2, it is your turn");
        System.out.println ("Please input the row and column you wish you play on");
          int row = keyboard.nextInt();
          int col = keyboard.nextInt();
          
          for (int r = 0; r < 3; r ++)
            for (int c = 0; c < 3; c++)
                grid[row][col]= ' ';
                
        grid[row][col] = 'O';
        System.out.println(grid[row][col]);
    }
    
    turn += turn;
    
    }
  }
}


And for some reason, the turns isn't working either any help?

This post has been edited by GiveMeYourBagel: 8 Nov, 2009 - 01:32 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 07:13PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month