4 Replies - 2561 Views - Last Post: 05 March 2010 - 08:52 AM Rate Topic: -----

#1 bailey23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-March 10

array tic tac toe

Posted 05 March 2010 - 08:33 AM

I am a beginner in Java. I am supposed to create a tic tac toe game in Java using arrays. I am having a few issues. The main one is the show board method is not working properly and this is the method that is supposed to show the array. Any help would be very appreciate.

This is the code so far.

import java.util.Scanner; //Needed for scanner class

public class TicTacToe
{
public static void main(String[] args)
{
// create an array to hold 3 rows and 3 colums

char[][] board = { {' ', ' ', ' '},{' ', ' ', ' '},{' ', ' ', ' '}};

// create a Scanner object for keyboard input.
Scanner keyboard = new Scanner(System.in);

// display introduction
System.out.println ("Welcome to Tic Tac Toe! \n");

showBoard(board);

int row;
int col;
char placePiece = ' ';
int count = 0;
int player = 1;

do
{
do
{
count = count++;
System.out.println("Player " + player + " enter the row you want to placeyour piece in:");
Scanner scan = new Scanner(System.in);
row=scan.nextInt();
System.out.println("Player " + player + " enter the column you want to place your piece in:");
col=scan.nextInt();

//Code to place pieces goes here
//Need help placing the piece

}
while(col < 0 || col > 2 || row < 0 || row > 2 || board[row][col] != ' ');
{
if(player == 1)
{
placePiece = 'x';
}
else
{
placePiece = 'o';
}

board[row][col] = placePiece;
showBoard(board);


// switch to other player
if (player == 1)
{
player = 2;
}
else
{
player = 1;
}
}

if(board[0][0]==board[0][1]&&board[0][1]==board[0][2]&&board[0][1]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[1][0]==board[1][1]&&board[1][1]==board[1][2]&&board[1][0]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[2][0]==board[2][1]&&board[2][1]==board[2][2]&&board[2][0]!=' ')
{
System.out.println("Player " + player + "you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[0][0]==board[1][1]&&board[1][1]==board[2][2]&&board[0][0]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[0][2]==board[1][1]&&board[1][1]==board[2][0]&&board[0][2]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[0][0]==board[1][0]&&board[1][0]==board[2][0]&&board[2][0]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
if(board[0][1]==board[1][1]&&board[1][1]==board[2][1]&&board[2][1]!=' ')
{
System.out.println("Player " + player + " you have won");
System.out.println("Thank you for playing Tic Tac Toe");
System.exit(1);
}
}
while (count <9);
System.out.println("You tied!");

}//End of main

private static void showBoard(char [][] array)
{
for ( int row =' '; row <array.length; row++)
{
for ( int col = ' '; col<array[row].length; col++)
System.out.print( array[row][col] + "\t");
System.out.println();
System.out.println();
}
}
}//end

Is This A Good Question/Topic? 0
  • +

Replies To: array tic tac toe

#2 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: array tic tac toe

Posted 05 March 2010 - 08:39 AM

why are you setting the first index in the loops to ' '?
the array's first indexes are 0. as:
private static void showBoard(char [][] array)
{
for ( int row =0; row <array.length; row++)
{
for ( int col = 0; col<array[row].length; col++)
System.out.print( array[row][col] + "\t");
System.out.println();
System.out.println();
}
}

Was This Post Helpful? 0
  • +
  • -

#3 bailey23   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-March 10

Re: array tic tac toe

Posted 05 March 2010 - 08:44 AM

I'm not sure why I initialized it ''..I thought it was just the right way to do it when the array type was char. I appreciate the help.:)
Was This Post Helpful? 0
  • +
  • -

#4 japanir   User is offline

  • jaVanir
  • member icon

Reputation: 1014
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: array tic tac toe

Posted 05 March 2010 - 08:48 AM

NP, Glad i could help :)
Was This Post Helpful? 0
  • +
  • -

#5 williamgeorgegardner   User is offline

  • CEO of GeekTelligence
  • member icon

Reputation: 19
  • View blog
  • Posts: 584
  • Joined: 27-December 09

Re: array tic tac toe

Posted 05 March 2010 - 08:52 AM

Please can you put your code inside the [ code ] code [ / code ] (without the spaces) tags please.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1