Hi folks....I have two separate copies of this program to show how my thought process worked in initial creation and how it has changed since I was helped by you folks in my airline reservation program. It does not work but I DID try to apply what I have learned from you all to get it to work.
Nothing special about this......all of the examples I have found out there were over complicated and involved things I can't use because I haven't learned it yet. I actually get it to let me put in input but that is it. For example when I choose player 1 it makes me enter row and column and x or o....and then repeats it for player 1 a few times before giving me my menu thing again. This obviously doesn't work out. Well tear apart my program and let me know where I suck and how to fix it and not suck. I actually started this before my airline reservation so the second iteration might have become somewhat mixed up in that I was trying to apply things I had learned from that program.
import java.util.*;
public class ticTacToe
{
public static void main(String[] args)
{
char[][] gameBoard = new char[3][3];
Scanner input = new Scanner(System.in);
System.out.println("Player 1 Enter X or O");
gameBoard[0][0] = input.next().charAt(0);
System.out.println("Player 2 Enter X or O");
gameBoard[0][1] = input.next().charAt(0);
System.out.println("Player 1 Enter X or O");
gameBoard[0][2] = input.next().charAt(0);
System.out.println("Player 2 Enter X or O");
gameBoard[1][0] = input.next().charAt(0);
System.out.println("Player 1 Enter X or O");
gameBoard[1][1] = input.next().charAt(0);
System.out.println("Player 2 Enter X or O");
gameBoard[1][2] = input.next().charAt(0);
System.out.println("Player 1 Enter X or O");
gameBoard[2][0] = input.next().charAt(0);
System.out.println("Player 2 Enter X or O");
gameBoard[2][1] = input.next().charAt(0);
System.out.println("Player 1 Enter X or O");
gameBoard[2][2] = input.next().charAt(0);
printBoard(gameBoard);
}
public static void printBoard(char[][] gameBoard)
{
for (int row = 0; row < gameBoard.length; row++)
{
for (int col = 0; col < gameBoard[0].length; col++)
{
System.out.print(gameBoard[row][col]);
}
System.out.println();
}
}
}
and the latest iteration.
import java.util.*;
public class ticTacToe2
{
public static void main(String[] args)
{
char[][] gameBoard = new char[3][3];
String player1 = "player1";
String player2 = "player2";
int turns = 9;
int playerTurn;
Scanner input = new Scanner(System.in);
for(int i = 0; i < gameBoard.length;i++)
{
for(int j=0;j < gameBoard[0].length;j++)
{
gameBoard[i][j] = '_';
}
}
while (turns > 0)
{
System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
switch(playerTurn)
{
case 1:
System.out.println("Player 1 Hit X or O");
playerChoice(gameBoard,input);
System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
break;
case 2:
System.out.println("Player 2 Hit X or O");
playerChoice(gameBoard,input);
System.out.println("Who's turn is it? For Player 1 Hit 1, For Player 2 Hit 2");
playerTurn = input.nextInt();
break;
default:
System.out.println("Invalid Choice");
}
turns--;
}
printBoard(gameBoard);
}
public static void printBoard(char[][] gameBoard)
{
for (int row = 0; row < gameBoard.length; row++)
{
for (int col = 0; col < gameBoard[0].length; col++)
{
System.out.print(gameBoard[row][col]);
}
System.out.println();
}
}
/*public static boolean isNextTurn(char[][]gameBoard, int turns)
{
boolean result;
for (int index = 0; index < gameBoard.length; index++)
{
if (turns < gameBoard.length)
result = true;
else
result = false;
return result;
}
}*/
public static char[][] playerChoice(char[][]gameBoard,Scanner input)
{
for (int index = 0; index < gameBoard.length; index++)
{
System.out.println("Which row do you want?");
int row = input.nextInt();
System.out.println("What column do you want?");
int col = input.nextInt();
System.out.println("Enter an X or O");
gameBoard[row][col] = input.next().charAt(0);
}
return gameBoard;
}
}
The commented out boolean was supposed to see whose turn it is but that never worked out for me lol.