8 Replies - 1097 Views - Last Post: 18 November 2012 - 02:33 AM Rate Topic: -----

#1 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Java Tic Tac Toe Program

Posted 13 November 2012 - 11:34 PM

Hi everyone! I need to write a java program that randomly fills in 0's and 1's into a 3x3 TicTacToe board, prints the board, and also a message saying whether any rows, columns, or diagonals with all 0's or 1s. So far I am just trying to print the board but instead of a 3x3 my code is printing a 3x27 /: If somebody could help me out it would be much appreciated!

import java.util.Arrays;
public class TicTacToe {
	public static void main(String[] args) {
		int[][] board = new int[3][3];
		for (int row = 0; row < board.length; row++) {
			for (int column = 0; column < board[row].length; column++) {
				board[row][column] = (int)(Math.random() * 2);
				
			for (int[] row1 : board) {
				for (int elem : row1)
					System.out.print(elem + " ");
				System.out.println();
			}
				
			}
		}
	}

}




Is This A Good Question/Topic? 0
  • +

Replies To: Java Tic Tac Toe Program

#2 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: Java Tic Tac Toe Program

Posted 13 November 2012 - 11:45 PM

			for (int[] row1 : board) {  
				for (int elem : row1)   
					System.out.print(elem + " ");
				System.out.println();


This is nested within the columns loop, so you do this:

System.out.print(elem + " ");


nine times in total for - each column. There's your 27.
Was This Post Helpful? 0
  • +
  • -

#3 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Java Tic Tac Toe Program

Posted 13 November 2012 - 11:45 PM

It looks like you have the print loops inside your other for loop, that might be your problem if not where you had intended them to be.

This post has been edited by DoinWork: 13 November 2012 - 11:47 PM

Was This Post Helpful? 1
  • +
  • -

#4 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Tic Tac Toe Program

Posted 14 November 2012 - 02:49 PM

I have changed my code and now I only get a 3x9 but I need to get a 3x3. Can anybody help?

import java.util.Arrays;
public class TicTacToe {
	public static void main(String[] args) {
		int[][] board = new int[3][3];
		for (int row = 0; row < board.length; row++) {
			for (int column = 0; column < board[row].length; column++) {
				board[row][column] = (int)(Math.random() * 2);
			}
			for (int[] row1 : board) {
				for (int elem : row1)
					System.out.print(elem + " ");
				System.out.println();
			
				
			}
		}
	}

}


Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7294
  • View blog
  • Posts: 12,138
  • Joined: 19-March 11

Re: Java Tic Tac Toe Program

Posted 14 November 2012 - 03:02 PM

Same problem - nested loops. Read your code, line for line, and ask yourself what each line means.
Was This Post Helpful? 2
  • +
  • -

#6 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Tic Tac Toe Program

Posted 14 November 2012 - 03:13 PM

Thank you guys! I figured it out! Now to try the rest of the program!
Was This Post Helpful? 0
  • +
  • -

#7 Brovahkiin501  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 48
  • Joined: 01-October 12

Re: Java Tic Tac Toe Program

Posted 14 November 2012 - 06:32 PM

Is there any way you guys could help me out with the second part of my program? For the second part we have to print a message saying whether any rows, columns, or diagonals with all 0s or 1s. If somebody could help me get started, it would be much appreciated!
Was This Post Helpful? 0
  • +
  • -

#8 farrell2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 797
  • View blog
  • Posts: 2,423
  • Joined: 29-July 11

Re: Java Tic Tac Toe Program

Posted 14 November 2012 - 07:27 PM

Why not just follow one of the tic tac toe tutorials here?
Was This Post Helpful? 0
  • +
  • -

#9 DoinWork  Icon User is offline

  • D.I.C Head

Reputation: 15
  • View blog
  • Posts: 69
  • Joined: 26-October 11

Re: Java Tic Tac Toe Program

Posted 18 November 2012 - 02:33 AM

To check if a row has same value all the way across, could just do a simple 2D array, and check horizontally and vertically. In first and last row, check for diagonals.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1