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
array tic tac toe
Page 1 of 14 Replies - 2561 Views - Last Post: 05 March 2010 - 08:52 AM
Replies To: array tic tac toe
#2
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:
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();
}
}
#3
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.
#5
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.
Page 1 of 1

New Topic/Question
Reply



MultiQuote


|