Can anyone help me construct a Java program that plays TicTacToe without using Arrays?
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
Can anyone help me construct a Java program that plays TicTacToe without using Arrays?
That sounds like a step towards using assembly language instead of java. WHY?
If you don't understand my answer, don't ignore it, ask a question.
It's an assignment. The point is to see if we can construct a TicTacToe game without using arrays. Here's what I have so far, but I keep getting errors:
import java.util.*;
public class TicTacToe
{
public enum Marker
{
EMPTY,
X,
O
};
private static Marker position1 = Marker.EMPTY;
private static Marker position2 = Marker.EMPTY;
private static Marker position3 = Marker.EMPTY;
private static Marker position4 = Marker.EMPTY;
private static Marker position5 = Marker.EMPTY;
private static Marker position6 = Marker.EMPTY;
private static Marker position7 = Marker.EMPTY;
private static Marker position8 = Marker.EMPTY;
private static Marker position9 = Marker.EMPTY;
private static Marker turn = Marker.X;
public static void main(String[]args)
{
Scanner play = new Scanner(System.in);
do
{
isTie = 0;
while(hasWon()==false)
{
exampleBoard();
printBoard();
hasWon();
turn();
markTheBoard(Marker m, int pos);
if(isTie = true)
{
break;
}
}
exampleBoard();
printBoard();
if(hasWon()=='X')
System.out.println("X wins!");
else if(hasWon()=='O')
System.out.println("O wins!");
else
isTie();
System.out.println("Would you like to play again? (Y/N)");
}
while(play.next().toLowerCase().charAt(0)=='y');
}
}
/**
* This function returns true if the spot is empty, false if not.
*/
public static boolean isValidMove(int pos)
{
String intPos = "position" + pos;
if (pos>0 && pos <10)
{
Marker test = Marker.EMPTY;
switch(pos)
{
case 1: test = position1; break;
case 2: test = position2; break;
case 3: test = position3; break;
case 4: test = position4; break;
case 5: test = position5; break;
case 6: test = position6; break;
case 7: test = position7; break;
case 8: test = position8; break;
case 9: test = position9; break;
}
if (test == Marker.EMPTY)
return true;
else return false;
}
else
{
System.out.println("This is an invalid move, must be an integer from 1 to 9, please try again");
return false;
}
}
/**
* This method will print the board as shown in the above example.
*/
public static void printBoard()
{
System.out.println("| " + position1 + " | " + position2 + " | " + position3 + " |");
System.out.println("-------------");
System.out.println("| " + position4 + " | " + position5 + " | " + position6 + " |");
System.out.println("-------------");
System.out.println("| " + position7 + " | " + position8 + " | " + position9 + " |");
}
public static void exampleBoard()
{
System.out.println("| 1 | 2 | 3 |");
System.out.println("-------------");
System.out.println("| 4 | 5 | 6 |");
System.out.println("-------------");
System.out.println("| 7 | 8 | 9 |");
}
/**
* Checks if a particular player has won.
*
* @param m The player to check
* @return true if the player won, false if not
*/
public static boolean hasWon(Marker m)
{
if(position1 == m && position2 == m && position3 == m) ||
(position1 == m && position4 == m && position7 == m) ||
(position1 == m && position5 == m && position9 == m) ||
(position2 == m && position5 == m && position8 == m) ||
(position3 == m && position5 == m && position7 == m) ||
(position3 == m && position6 == m && position9 == m) ||
(position4 == m && position5 == m && position6 == m) ||
(position7 == m && position8 == m && position9 == m)
return true;
else return false;
}
/**
* Checks if the board is full with no winner
* @return true if the board is full with no winner, false otherwise
*/
public static boolean isTie()
{
if(hasWon = false)
System.out.print("This game is tied.");
}
/**
* Mark the given position with the given marker
* @param m The marker of the player given
* @param pos The position that we are marking
*/
public static void markTheBoard(Marker m, int pos)
{
Scanner play = new Scanner(System.in);
System.out.print("What is your position? ");
int position = play.nextInt();
char piece;
if(m == turn)
piece = 'X';
else
piece = 'O';
if(position == 1)
if(position1 == Marker.Empty)
position1 = piece;
else markTheBoard(m);
if(position == 2)
if(position2 == Marker.Empty)
position2 = piece;
else markTheBoard(m);
if(position == 3)
if(position3 == Marker.Empty)
position3 = piece;
else markTheBoard(m);
if(position == 4)
if(position4 == Marker.Empty)
position4 = piece;
else markTheBoard(m);
if(position == 5)
if(position5 == Marker.Empty)
position5 = piece;
else markTheBoard(m);
if(position == 6)
if(position6 == Marker.Empty)
position6 = piece;
else markTheBoard(m);
if(position == 7)
if(position7 == Marker.Empty)
position7 = piece;
else markTheBoard(m);
if(position == 8)
if(position8 == Marker.Empty)
position8 = piece;
else markTheBoard(m);
if(position == 9)
if(position9 == Marker.Empty)
position9 = piece;
else markTheBoard(m);
}
}
}
Makes for ugly, repetitive code. Arrays and loops make for shorter less error prone code. What is your instructor trying to teach with this assignment?
Please copy the full text of the error message and paste it here. It has important info about the error.I keep getting errors:
One possible problem is the missing {}s following if statements. You should ALWAYS surround the code in if statements and loops with {}s
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
I added brackets in and still received the same errors.
some of the errors are:
error: illegal start of expression
markTheBoard(Marker m, int pos);
error: not a statement
markTheBoard(Marker m, int pos);
error: class, interface, or enum expected
public static boolean isValidMove(int pos)
That's a nice summary of the errors. If you want any help fixing them you will need to post the full text of the error messages.some of the errors are:
Please copy full text of the compiler's error message and paste it here.
The message should show the source with a ^ under the location of the error.
Here is a sample from the javac compiler:
TestSorts.java:138: cannot find symbol symbol : variable var location: class TestSorts var = 2; ^
If you don't understand my answer, don't ignore it, ask a question.
I don't know how to upload the error screen. So I just used Paint to take screen pics and attaching it to this thread:
javaerror.jpgjavaerror2.jpgjavaerror3.jpgjavaerror4.jpg
Here's what I have again:
import java.util.*;
public class TicTacToe
{
public enum Marker
{
EMPTY,
X,
O
};
private static Marker position1 = Marker.EMPTY;
private static Marker position2 = Marker.EMPTY;
private static Marker position3 = Marker.EMPTY;
private static Marker position4 = Marker.EMPTY;
private static Marker position5 = Marker.EMPTY;
private static Marker position6 = Marker.EMPTY;
private static Marker position7 = Marker.EMPTY;
private static Marker position8 = Marker.EMPTY;
private static Marker position9 = Marker.EMPTY;
private static Marker turn = Marker.X;
public static void main(String[]args)
{
Scanner play = new Scanner(System.in);
do
{
isTie = 0;
while(hasWon()==false)
{
exampleBoard();
printBoard();
hasWon();
turn();
markTheBoard(Marker m, int pos);
if(isTie = true)
{
break;
}
}
exampleBoard();
printBoard();
if(hasWon()=='X')
{
System.out.println("X wins!");
}
else if(hasWon()=='O')
{
System.out.println("O wins!");
}
else
{
isTie();
}
System.out.println("Would you like to play again? (Y/N)");
}
while(play.next().toLowerCase().charAt(0)=='y');
}
}
/**
* This function returns true if the spot is empty, false if not.
*/
public static boolean isValidMove(int pos)
{
String intPos = "position" + pos;
if (pos>0 && pos <10)
{
Marker test = Marker.EMPTY;
switch(pos)
{
case 1: test = position1; break;
case 2: test = position2; break;
case 3: test = position3; break;
case 4: test = position4; break;
case 5: test = position5; break;
case 6: test = position6; break;
case 7: test = position7; break;
case 8: test = position8; break;
case 9: test = position9; break;
}
if (test == Marker.EMPTY)
{
return true;
}
else
{
return false;
}
}
else
{
System.out.println("This is an invalid move, must be an integer from 1 to 9, please try again");
return false;
}
}
/**
* This method will print the board as shown in the above example.
*/
public static void printBoard()
{
System.out.println("| " + position1 + " | " + position2 + " | " + position3 + " |");
System.out.println("-------------");
System.out.println("| " + position4 + " | " + position5 + " | " + position6 + " |");
System.out.println("-------------");
System.out.println("| " + position7 + " | " + position8 + " | " + position9 + " |");
}
public static void exampleBoard()
{
System.out.println("| 1 | 2 | 3 |");
System.out.println("-------------");
System.out.println("| 4 | 5 | 6 |");
System.out.println("-------------");
System.out.println("| 7 | 8 | 9 |");
}
/**
* Checks if a particular player has won.
*
* @param m The player to check
* @return true if the player won, false if not
*/
public static boolean hasWon(Marker m)
{
if(position1 == m && position2 == m && position3 == m) ||
(position1 == m && position4 == m && position7 == m) ||
(position1 == m && position5 == m && position9 == m) ||
(position2 == m && position5 == m && position8 == m) ||
(position3 == m && position5 == m && position7 == m) ||
(position3 == m && position6 == m && position9 == m) ||
(position4 == m && position5 == m && position6 == m) ||
(position7 == m && position8 == m && position9 == m)
{
return true;
}
else
{
return false;
}
}
/**
* Checks if the board is full with no winner
* @return true if the board is full with no winner, false otherwise
*/
public static boolean isTie()
{
if(hasWon = false)
{
System.out.print("This game is tied.");
}
}
/**
* Mark the given position with the given marker
* @param m The marker of the player given
* @param pos The position that we are marking
*/
public static void markTheBoard(Marker m, int pos)
{
Scanner play = new Scanner(System.in);
System.out.print("What is your position? ");
int position = play.nextInt();
char piece;
if(m == turn)
{
piece = 'X';
}
else
{
piece = 'O';
}
if(position == 1)
{
if(position1 == Marker.Empty)
{
position1 = piece;
}
else markTheBoard(m);
}
if(position == 2)
{
if(position2 == Marker.Empty)
{
position2 = piece;
}
else markTheBoard(m);
}
if(position == 3)
{
if(position3 == Marker.Empty)
{
position3 = piece;
}
else markTheBoard(m);
}
if(position == 4)
{
if(position4 == Marker.Empty)
{
position4 = piece;
}
else markTheBoard(m);
}
if(position == 5)
{
if(position5 == Marker.Empty)
{
position5 = piece;
}
else markTheBoard(m);
}
if(position == 6)
{
if(position6 == Marker.Empty)
{
position6 = piece;
}
else markTheBoard(m);
}
if(position == 7)
{
if(position7 == Marker.Empty)
{
position7 = piece;
}
else markTheBoard(m);
}
if(position == 8)
{
if(position8 == Marker.Empty)
{
position8 = piece;
}
else markTheBoard(m);
}
if(position == 9)
{
if(position9 == Marker.Empty)
{
position9 = piece;
}
else markTheBoard(m);
}
}
}
Where are the error messages displayed? If in an IDE there is usually a "COPY" option for the window.
Please edit your post and wrap your code with code tags:
[code=java]
YOUR CODE HERE
[/code]
to get highlighting and preserve formatting.
If you don't understand my answer, don't ignore it, ask a question.
I don't use an IDE. The instructor said to not use an IDE, just use Notepad++ and "cmd". I'll attach the error images again here. I had to use Paint to take the screen pics:
javaerror.jpg
javaerror2.jpg
javaerror3.jpg
javaerror4.jpg
If that is the command prompt console on windows:
To copy the contents of the command prompt window:
Click on Icon in upper left corner
Select Edit
Select 'Select All' - The selection will show
Click in upper left again
Select Edit and click 'Copy'
Paste here.
If you don't understand my answer, don't ignore it, ask a question.
Oh cool! Thank you. Here's the paste:
C:\Users\Thao\Java\jdk1.7.0_40\bin>javac TicTacToe.java
TicTacToe.java:57: error: ')' expected
markTheBoard(Marker m, int pos);
^
TicTacToe.java:57: error: illegal start of expression
markTheBoard(Marker m, int pos);
^
TicTacToe.java:57: error: ';' expected
markTheBoard(Marker m, int pos);
^
TicTacToe.java:57: error: not a statement
markTheBoard(Marker m, int pos);
^
TicTacToe.java:57: error: ';' expected
markTheBoard(Marker m, int pos);
^
TicTacToe.java:87: error: class, interface, or enum expected
public static boolean isValidMove(int pos)
^
TicTacToe.java:90: error: class, interface, or enum expected
if (pos>0 && pos <10)
^
TicTacToe.java:93: error: class, interface, or enum expected
switch(pos)
^
TicTacToe.java:95: error: class, interface, or enum expected
case 1: test = position1; break;
^
TicTacToe.java:96: error: class, interface, or enum expected
case 2: test = position2; break;
^
TicTacToe.java:96: error: class, interface, or enum expected
case 2: test = position2; break;
^
TicTacToe.java:97: error: class, interface, or enum expected
case 3: test = position3; break;
^
TicTacToe.java:97: error: class, interface, or enum expected
case 3: test = position3; break;
^
TicTacToe.java:98: error: class, interface, or enum expected
case 4: test = position4; break;
^
TicTacToe.java:98: error: class, interface, or enum expected
case 4: test = position4; break;
^
TicTacToe.java:99: error: class, interface, or enum expected
case 5: test = position5; break;
^
TicTacToe.java:99: error: class, interface, or enum expected
case 5: test = position5; break;
^
TicTacToe.java:100: error: class, interface, or enum expected
case 6: test = position6; break;
^
TicTacToe.java:100: error: class, interface, or enum expected
case 6: test = position6; break;
^
TicTacToe.java:101: error: class, interface, or enum expected
case 7: test = position7; break;
^
TicTacToe.java:101: error: class, interface, or enum expected
case 7: test = position7; break;
^
TicTacToe.java:102: error: class, interface, or enum expected
case 8: test = position8; break;
^
TicTacToe.java:102: error: class, interface, or enum expected
case 8: test = position8; break;
^
TicTacToe.java:103: error: class, interface, or enum expected
case 9: test = position9; break;
^
TicTacToe.java:103: error: class, interface, or enum expected
case 9: test = position9; break;
^
TicTacToe.java:104: error: class, interface, or enum expected
}
^
TicTacToe.java:108: error: class, interface, or enum expected
}
^
TicTacToe.java:112: error: class, interface, or enum expected
}
^
TicTacToe.java:117: error: class, interface, or enum expected
return false;
^
TicTacToe.java:118: error: class, interface, or enum expected
}
^
TicTacToe.java:124: error: class, interface, or enum expected
public static void printBoard()
^
TicTacToe.java:127: error: class, interface, or enum expected
System.out.println("-------------");
^
TicTacToe.java:128: error: class, interface, or enum expected
System.out.println("| " + position4 + " | " + position5 + " | "
+ position6 + " |");
^
TicTacToe.java:129: error: class, interface, or enum expected
System.out.println("-------------");
^
TicTacToe.java:130: error: class, interface, or enum expected
System.out.println("| " + position7 + " | " + position8 + " | "
+ position9 + " |");
^
TicTacToe.java:131: error: class, interface, or enum expected
}
^
TicTacToe.java:133: error: class, interface, or enum expected
public static void exampleBoard()
^
TicTacToe.java:136: error: class, interface, or enum expected
System.out.println("-------------");
^
TicTacToe.java:137: error: class, interface, or enum expected
System.out.println("| 4 | 5 | 6 |");
^
TicTacToe.java:138: error: class, interface, or enum expected
System.out.println("-------------");
^
TicTacToe.java:139: error: class, interface, or enum expected
System.out.println("| 7 | 8 | 9 |");
^
TicTacToe.java:140: error: class, interface, or enum expected
}
^
TicTacToe.java:148: error: class, interface, or enum expected
public static boolean hasWon(Marker m)
^
TicTacToe.java:160: error: class, interface, or enum expected
}
^
TicTacToe.java:164: error: class, interface, or enum expected
}
^
TicTacToe.java:171: error: class, interface, or enum expected
public static boolean isTie()
^
TicTacToe.java:176: error: class, interface, or enum expected
}
^
TicTacToe.java:184: error: class, interface, or enum expected
public static void markTheBoard(Marker m, int pos)
^
TicTacToe.java:187: error: class, interface, or enum expected
System.out.print("What is your position? ");
^
TicTacToe.java:188: error: class, interface, or enum expected
int position = play.nextInt();
^
TicTacToe.java:189: error: class, interface, or enum expected
char piece;
^
TicTacToe.java:190: error: class, interface, or enum expected
if(m == turn)
^
TicTacToe.java:193: error: class, interface, or enum expected
}
^
TicTacToe.java:197: error: class, interface, or enum expected
}
^
TicTacToe.java:204: error: class, interface, or enum expected
}
^
TicTacToe.java:206: error: class, interface, or enum expected
}
^
TicTacToe.java:212: error: class, interface, or enum expected
}
^
TicTacToe.java:214: error: class, interface, or enum expected
}
^
TicTacToe.java:220: error: class, interface, or enum expected
}
^
TicTacToe.java:222: error: class, interface, or enum expected
}
^
TicTacToe.java:228: error: class, interface, or enum expected
}
^
TicTacToe.java:230: error: class, interface, or enum expected
}
^
TicTacToe.java:236: error: class, interface, or enum expected
}
^
TicTacToe.java:238: error: class, interface, or enum expected
}
^
TicTacToe.java:244: error: class, interface, or enum expected
}
^
TicTacToe.java:246: error: class, interface, or enum expected
}
^
TicTacToe.java:252: error: class, interface, or enum expected
}
^
TicTacToe.java:254: error: class, interface, or enum expected
}
^
TicTacToe.java:260: error: class, interface, or enum expected
}
^
TicTacToe.java:262: error: class, interface, or enum expected
}
^
TicTacToe.java:268: error: class, interface, or enum expected
}
^
TicTacToe.java:270: error: class, interface, or enum expected
}
^
72 errors
Is that a method definition or a call to a method?markTheBoard(Marker m, int pos);
You don't code the variable type in a method call:
markTheBoard(m, pos);
That error usually happens when the definition for the previous class has ended with a } and there is misplaced code after the }.class, interface, or enum expected
The compiler wants the next thing after a class's definition to be one of these: class, interface, or enum expected
Check where the } ending the class's is located. Fixing that will get rid of many errors.
If you don't understand my answer, don't ignore it, ask a question.
I'm not exactly sure why the instructor had us use "markTheBoard(Marker m, int pos)". I don't know what to do with 2 parameters, but he specifically said we cannot change it. I don't know what the "m" is for really....
Look at the tutorial on how to define and use methods:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
If you don't understand my answer, don't ignore it, ask a question.