Hi! Im a beginner in programming, and I have been given an assignment to program a TicTacToe game.
I could not find any programs as such for reference over the Interwebs thus would want the expertise of daniweb users to help.
The problem im having:
1. How do i transfer the input from the main method to the getBoard constructor method.
Just how do i transfer the input from the main method to userInput in the if statements in the constructor class.
2. when the user enters an input it can replace an input already on the board(That is ofcourse when i put everything in everything in the main method)
thx for helping
import java.util.Scanner;
public class TicTacToe
{
public static void main(String[] args)
{
String userInput = "";
System.out.println("Looking at the Key above enter X or O in the correct quadrant\nseperated with a hyphen; ie. X-NW" +"\n");
Scanner in = new Scanner(System.in);
System.out.println("Enter Your first input ie. X and its Quadrant:"+"\n");
System.out.println("Command Key:"+"\n"+"\t[R] = Play Again" +"\n"+ "\t[Q] = Quit");
userInput = in.next();
TicTacToe tac = new TicTacToe();
String game = "";
game = tac.getBoard;
}
public void getBoard(String[] args)
{
/*Tic-Tac-Toe Board (KEY)*/
System.out.println("Tic Tac Toe Key:" +"\n");
String topRow = "NW | N | NE";
System.out.println(" "+topRow);
System.out.println("----|---|----");
String middleRow = " W | C | E";
System.out.println(" "+middleRow);
System.out.println("----|---|---- ");
String bottomRow = "SW | S | SE";
System.out.println(" "+bottomRow +"\n");
/*Initialize the values of the Quadrants*/
String NW = " ";
String N= " ";
String NE = " ";
String E= " ";
String C= " ";
String W= " ";
String SW= " ";
String S= " ";
String SE = " ";
/*Possible Inputs*/
boolean checkWin = false;
boolean checkPlay = false;
while(!checkPlay)
{
while(!checkWin)
{
/*Quit Command*/
if(userInput.equalsIgnoreCase("Q")){
System.out.println("Thanks you for playing Tic-Tac-Toe by Ritesh Pakala.");
System.exit(0);
}
/*Input Locations*/
if(userInput.equalsIgnoreCase("X-NW"))
{
NW = "[X]";
}else if(userInput.equalsIgnoreCase("O-NW")){
NW = "[O]";
}
if(userInput.equalsIgnoreCase("X-N"))
{
N = "[X]";
}else if(userInput.equalsIgnoreCase("O-N")){
N = "[O]";
}
if(userInput.equalsIgnoreCase("X-NE"))
{
NE = "[X]";
}else if(userInput.equalsIgnoreCase("O-NE")){
NE = "[O]";
}
if(userInput.equalsIgnoreCase("X-W"))
{
W = "[X]";
}else if(userInput.equalsIgnoreCase("O-W")){
W = "[O]";
}
if(userInput.equalsIgnoreCase("X-C"))
{
C = "[X]";
}else if(userInput.equalsIgnoreCase("O-C")){
C = "[O]";
}
if(userInput.equalsIgnoreCase("X-E"))
{
E = "[X]";
}else if(userInput.equalsIgnoreCase("O-E")){
E = "[O]";
}
if(userInput.equalsIgnoreCase("X-SW"))
{
SW = "[X]";
}else if(userInput.equalsIgnoreCase("O-SW")){
SW = "[O]";
}
if(userInput.equalsIgnoreCase("X-S"))
{
S = "[X]";
}else if(userInput.equalsIgnoreCase("O-S")){
S = "[O]";
}
if(userInput.equalsIgnoreCase("X-SE"))
{
SE = "[X]";
}else if(userInput.equalsIgnoreCase("O-SE")){
SE = "[O]";
}
/*Win Scenarios*/
if (NW.equalsIgnoreCase("[X]") && N.equalsIgnoreCase("[X]") && NE.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (NW.equalsIgnoreCase("[O]") && N.equalsIgnoreCase("[O]") && NE.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (W.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && E.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (W.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && E.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (SW.equalsIgnoreCase("[X]") && S.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (SW.equalsIgnoreCase("[O]") && S.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (NW.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (NW.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (NE.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && SW.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (NE.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && SW.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (NW.equalsIgnoreCase("[X]") && W.equalsIgnoreCase("[X]") && SW.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (NW.equalsIgnoreCase("[O]") && W.equalsIgnoreCase("[O]") && SW.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (N.equalsIgnoreCase("[X]") && C.equalsIgnoreCase("[X]") && S.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (N.equalsIgnoreCase("[O]") && C.equalsIgnoreCase("[O]") && S.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player O Wins!\n"+thumb);
checkWin = true;
}
else if (NE.equalsIgnoreCase("[X]") && E.equalsIgnoreCase("[X]") && SE.equalsIgnoreCase("[X]"))
{
System.out.println("Congrats! Player X Wins!\n"+thumb);
checkWin = true;
}else if (NE.equalsIgnoreCase("[O]") && E.equalsIgnoreCase("[O]") && SE.equalsIgnoreCase("[O]")){
System.out.println("Congrats! Player 0 Wins!\n"+thumb);
checkWin = true;
System.exit(0);
}else
System.out.println("You tied the game.");
/*Board*/
String newGame = " | | " +"\n"+
" " +NW+ " "+"|"+" " +N+" "+"|"+" " +NE +"\n"+
"-----|-----|-----" +"\n"+
" " +W+ " "+"|"+" " +C+" "+ "|"+" " +E +"\n"+
"-----|-----|-----" +"\n"+
" " +SW+ " "+"|"+" " +S+" "+ "|"+" " +SE +"\n"+
" | | " +"\n";
System.out.print(newGame);
}
}
}
}